Spaces:
Runtime error
Runtime error
Commit
•
a126029
1
Parent(s):
87d6a85
make-call-api (#1)
Browse files- Provided another parameter (ce54bdc0aeec42efb31f733f6c4f581d17afa74b)
Co-authored-by: Bright Eshun <[email protected]>
app.py
CHANGED
@@ -9,141 +9,106 @@ from pathlib import Path
|
|
9 |
from PIL import Image
|
10 |
import matplotlib.pyplot as plt
|
11 |
import seaborn as sns
|
|
|
12 |
|
13 |
|
14 |
|
15 |
-
# Setting the page configurations
|
16 |
-
st.set_page_config(page_title= "Prediction Forecasting", layout= "wide", initial_sidebar_state= "auto")
|
17 |
-
|
18 |
-
# Setting the page title
|
19 |
-
st.title("Grocery Store Forecasting Prediction")
|
20 |
-
|
21 |
-
# Load the saved data
|
22 |
-
df = pd.read_csv('Grocery.csv')
|
23 |
-
|
24 |
-
|
25 |
-
toolkit = "toolkit_folder"
|
26 |
-
@st.cache_resource
|
27 |
-
def load_toolkit(filepath = toolkit):
|
28 |
-
with open(toolkit, "rb") as file:
|
29 |
-
loaded_toolkit = pickle.load(file)
|
30 |
-
return loaded_toolkit
|
31 |
-
|
32 |
-
|
33 |
-
toolkit = load_toolkit()
|
34 |
-
Encoder = toolkit["OneHotEncoder"]
|
35 |
-
model = toolkit["model"]
|
36 |
-
|
37 |
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
st.write('Grocery Store Time Series Forecasting')
|
44 |
-
st.image('images1.jpg',width = 450)
|
45 |
-
st.write('Graphical representation and Data Overview')
|
46 |
-
if st.checkbox('Data Set '):
|
47 |
-
st.table(df.head(15))
|
48 |
-
st.title('Charts')
|
49 |
-
graph = st.selectbox('Varieties of graphs',['scatter plot','Bar chat','Histogram'])
|
50 |
-
if graph == 'scatter plot':
|
51 |
-
fig,ax = plt.subplots(figsize=(10,5))
|
52 |
-
sns.scatterplot(y = 'target',x = 'onpromotion',data = df.iloc[:1000],palette = 'bright',hue = 'city');
|
53 |
-
st.pyplot(fig)
|
54 |
-
|
55 |
-
if graph == 'Bar chat':
|
56 |
-
fig,ax = plt.subplots(figsize=(10,5))
|
57 |
-
t = df.groupby("city")["target"].sum().reset_index().sort_values(by="target",ascending=False).iloc[:10]
|
58 |
-
sns.barplot(data=t[:20] , y="target", x="city", palette='Blues_d')
|
59 |
-
st.pyplot(fig)
|
60 |
-
|
61 |
-
if graph == 'Histogram':
|
62 |
-
fig,ax = plt.subplots(figsize=(10,5))
|
63 |
-
st.write('Target Categories')
|
64 |
-
sns.distplot(df.target.iloc[:20], kde=True)
|
65 |
-
st.pyplot(fig)
|
66 |
-
|
67 |
|
|
|
|
|
|
|
68 |
|
|
|
|
|
69 |
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
st.sidebar.markdown('User Input Details and Information')
|
75 |
-
|
76 |
-
store_id= st.sidebar.selectbox('store_id', options = sorted(list(df['store_id'].unique())))
|
77 |
-
category_id= st.sidebar.selectbox('categegory_id',options = sorted(list(df['category_id'].unique())))
|
78 |
-
onpromotion= st.sidebar.number_input('onpromotion', min_value= df["onpromotion"].min(), value= df["onpromotion"].min())
|
79 |
-
year = st.sidebar.selectbox('year', options = sorted(list(df['year'].unique())))
|
80 |
-
month = st.sidebar.selectbox('month', options = sorted(list(df['month'].unique())))
|
81 |
-
dayofmonth= st.sidebar.number_input('dayofmonth', min_value= df["dayofmonth"].min(), value= df["dayofmonth"].min())
|
82 |
-
dayofweek = st.sidebar.number_input('dayofweek', min_value= df["dayofweek"].min(), value= df["dayofweek"].min())
|
83 |
-
dayofyear = st.sidebar.number_input('dayofyear', min_value= df["dayofyear"].min(), value= df["dayofyear"].min())
|
84 |
-
weekofyear = st.sidebar.number_input('weekofyear', min_value= df["weekofyear"].min(), value= df["weekofyear"].min())
|
85 |
-
quarter = st.sidebar.number_input('quarter', min_value= df["quarter"].min(), value= df["quarter"].min())
|
86 |
-
is_month_start = st.sidebar.number_input('is_month_start', min_value= df["is_month_start"].min(), value= df["is_month_start"].min())
|
87 |
-
is_month_end = st.sidebar.number_input('is_month_end', min_value= df["is_month_end"].min(), value= df["is_month_end"].min())
|
88 |
-
is_quarter_start = st.sidebar.number_input('is_quarter_start', min_value= df["is_quarter_start"].min(), value= df["is_quarter_start"].min())
|
89 |
-
is_quarter_end = st.sidebar.number_input('is_quarter_end', min_value= df["is_quarter_end"].min(), value= df["is_quarter_end"].min())
|
90 |
-
is_year_start = st.sidebar.number_input('is_year_start', min_value= df["is_year_start"].min(), value= df["is_year_start"].min())
|
91 |
-
is_year_end = st.sidebar.number_input('is_year_end', min_value= df["is_year_end"].min(), value= df["is_year_end"].min())
|
92 |
-
year_weekofyear = st.sidebar.number_input('year_weekofyear', min_value= df["year_weekofyear"].min(), value= df["year_weekofyear"].min())
|
93 |
-
city = st.sidebar.selectbox("city:", options= sorted(set(df["city"])))
|
94 |
-
store_type= st.sidebar.number_input('type', min_value= df["type"].min(), value= df["type"].min())
|
95 |
-
cluster = st.sidebar.selectbox('cluster', options = sorted(list(df['cluster'].unique())))
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
input_df = {
|
100 |
-
'store_id':[store_id],
|
101 |
-
'category_id':[category_id],
|
102 |
-
'onpromotion' :[onpromotion],
|
103 |
-
'year' : [year],
|
104 |
-
'month' :[month],
|
105 |
-
'dayofmonth' :[dayofmonth],
|
106 |
-
'dayofweek' : [dayofweek],
|
107 |
-
'dayofyear' : [dayofyear],
|
108 |
-
'weekofyear' : weekofyear,
|
109 |
-
'quarter' : [quarter],
|
110 |
-
'is_month_start' : [is_month_start],
|
111 |
-
'is_month_end' : [is_month_start],
|
112 |
-
'is_quarter_start' : [is_quarter_start],
|
113 |
-
'is_quarter_end' : [is_quarter_end],
|
114 |
-
'is_year_start' : [is_year_start],
|
115 |
-
'is_year_end' : [is_year_end],
|
116 |
-
'year_weekofyear' : [year_weekofyear],
|
117 |
-
'city' : [city],
|
118 |
-
'type' : [store_type],
|
119 |
-
'cluster': [cluster]
|
120 |
-
}
|
121 |
-
|
122 |
-
# Put the input dictionary in a dataset
|
123 |
-
input_data = pd.DataFrame(input_df)
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
# defining categories and numeric columns
|
128 |
-
|
129 |
-
col = ['city']
|
130 |
-
columns = list(input_data.columns)
|
131 |
-
encoded_cat = Encoder.transform(input_data[col])
|
132 |
-
encoded_cols = Encoder.get_feature_names()
|
133 |
-
encoded_cat_ = pd.DataFrame(encoded_cat, columns=encoded_cols)
|
134 |
|
|
|
|
|
|
|
135 |
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
# we dropped the categorical encoder column before we concat
|
138 |
-
train_enc = input_data.drop(['city'],axis = 1)
|
139 |
-
input_d = pd.concat([train_enc, encoded_cat_], axis=1)
|
140 |
-
|
141 |
-
# convert input_data to a numpy array before flattening to convert it back to a 2D array
|
142 |
-
input_df= input_d.to_numpy()
|
143 |
-
prediction = model.predict(input_df.flatten().reshape(1, -1))
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
|
|
|
9 |
from PIL import Image
|
10 |
import matplotlib.pyplot as plt
|
11 |
import seaborn as sns
|
12 |
+
import requests
|
13 |
|
14 |
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
+
# get absolute path and goo two levels up
|
19 |
+
DIRPATH = DIRPATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
|
20 |
+
# get path for app data
|
21 |
+
app_data_path =os.path.join(DIRPATH,'dev', 'datasets', 'app_data', 'Grocery.csv.crdownload')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# set api endpoint
|
24 |
+
URL = 'https://bright1-sales-forecasting-api.hf.space'
|
25 |
+
API_ENDPOINT = '/predict'
|
26 |
|
27 |
+
# Setting the page configurations
|
28 |
+
st.set_page_config(page_title = "Prediction Forecasting", layout= "wide", initial_sidebar_state= "auto")
|
29 |
|
30 |
+
# Setting the page title
|
31 |
+
st.title("Grocery Store Forecasting Prediction")
|
32 |
|
33 |
+
# Load the saved data
|
34 |
+
df = pd.read_csv(app_data_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# src\app\images1.jpg
|
37 |
+
image1 = Image.open('src/app/images1.jpg')
|
38 |
+
image2 = Image.open('src/app/image 2.jpg')
|
39 |
|
40 |
+
def make_prediction(store_id, category_id, onpromotion, year,month, dayofmonth,
|
41 |
+
dayofweek, dayofyear,weekofyear, quarter, is_month_start, is_month_end,
|
42 |
+
is_quarter_start, is_quarter_end, is_year_start, is_year_end,
|
43 |
+
year_weekofyear,city, store_type, cluster):
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
parameters = {
|
47 |
+
'store_id':int(store_id),
|
48 |
+
'category_id':int(category_id),
|
49 |
+
'onpromotion' :int(onpromotion),
|
50 |
+
'year' : int(year),
|
51 |
+
'month' : int(month),
|
52 |
+
'dayofmonth' :int(dayofmonth),
|
53 |
+
'dayofweek' : int(dayofweek),
|
54 |
+
'dayofyear' : int(dayofyear),
|
55 |
+
'weekofyear' : int(weekofyear),
|
56 |
+
'quarter' : int(quarter),
|
57 |
+
'is_month_start' : int(is_month_start),
|
58 |
+
'is_month_end' : int(is_month_end),
|
59 |
+
'is_quarter_start' : int(is_quarter_start),
|
60 |
+
'is_quarter_end' : int(is_quarter_end),
|
61 |
+
'is_year_start' : int(is_year_start),
|
62 |
+
'is_year_end' : (is_year_end),
|
63 |
+
'year_weekofyear' : int(year_weekofyear),
|
64 |
+
'city' : city,
|
65 |
+
'store_type' : int(store_type),
|
66 |
+
'cluster': int(cluster),
|
67 |
+
|
68 |
+
}
|
69 |
+
|
70 |
+
|
71 |
+
response = requests.post(url=f'{URL}{API_ENDPOINT}', params=parameters)
|
72 |
+
sales_value = response.json()['sales']
|
73 |
+
sales_value = round(sales_value, 4)
|
74 |
+
return sales_value
|
75 |
+
|
76 |
+
|
77 |
+
st.image(image1, width = 700)
|
78 |
+
|
79 |
+
st.sidebar.markdown('User Input Details and Information')
|
80 |
+
|
81 |
+
store_id= st.sidebar.selectbox('store_id', options = sorted(list(df['store_id'].unique())))
|
82 |
+
category_id= st.sidebar.selectbox('categegory_id',options = sorted(list(df['category_id'].unique())))
|
83 |
+
onpromotion= st.sidebar.number_input('onpromotion', min_value= df["onpromotion"].min(), value= df["onpromotion"].min())
|
84 |
+
year = st.sidebar.selectbox('year', options = sorted(list(df['year'].unique())))
|
85 |
+
month = st.sidebar.selectbox('month', options = sorted(list(df['month'].unique())))
|
86 |
+
dayofmonth= st.sidebar.number_input('dayofmonth', min_value= df["dayofmonth"].min(), value= df["dayofmonth"].min())
|
87 |
+
dayofweek = st.sidebar.number_input('dayofweek', min_value= df["dayofweek"].min(), value= df["dayofweek"].min())
|
88 |
+
dayofyear = st.sidebar.number_input('dayofyear', min_value= df["dayofyear"].min(), value= df["dayofyear"].min())
|
89 |
+
weekofyear = st.sidebar.number_input('weekofyear', min_value= df["weekofyear"].min(), value= df["weekofyear"].min())
|
90 |
+
quarter = st.sidebar.number_input('quarter', min_value= df["quarter"].min(), value= df["quarter"].min())
|
91 |
+
is_month_start = st.sidebar.number_input('is_month_start', min_value= df["is_month_start"].min(), value= df["is_month_start"].min())
|
92 |
+
is_month_end = st.sidebar.number_input('is_month_end', min_value= df["is_month_end"].min(), value= df["is_month_end"].min())
|
93 |
+
is_quarter_start = st.sidebar.number_input('is_quarter_start', min_value= df["is_quarter_start"].min(), value= df["is_quarter_start"].min())
|
94 |
+
is_quarter_end = st.sidebar.number_input('is_quarter_end', min_value= df["is_quarter_end"].min(), value= df["is_quarter_end"].min())
|
95 |
+
is_year_start = st.sidebar.number_input('is_year_start', min_value= df["is_year_start"].min(), value= df["is_year_start"].min())
|
96 |
+
is_year_end = st.sidebar.number_input('is_year_end', min_value= df["is_year_end"].min(), value= df["is_year_end"].min())
|
97 |
+
year_weekofyear = st.sidebar.number_input('year_weekofyear', min_value= df["year_weekofyear"].min(), value= df["year_weekofyear"].min())
|
98 |
+
city = st.sidebar.selectbox("city:", options= sorted(set(df["city"])))
|
99 |
+
store_type= st.sidebar.number_input('type', min_value= df["type"].min(), value= df["type"].min())
|
100 |
+
cluster = st.sidebar.selectbox('cluster', options = sorted(list(df['cluster'].unique())))
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# make prediction
|
105 |
+
sales_value = make_prediction(store_id, category_id, onpromotion, year,month, dayofmonth,
|
106 |
+
dayofweek, dayofyear,weekofyear, quarter, is_month_start, is_month_end,
|
107 |
+
is_quarter_start, is_quarter_end, is_year_start, is_year_end,
|
108 |
+
year_weekofyear,city, store_type, cluster)
|
109 |
+
|
110 |
+
# get predicted value
|
111 |
+
if st.button('Predict'):
|
112 |
+
st.success('The predicted target is ' + str(sales_value))
|
113 |
|
114 |
|