Spaces:
Running
Running
Circhastic
commited on
Commit
•
4de408f
1
Parent(s):
d7ac35b
Update app
Browse files
app.py
CHANGED
@@ -18,6 +18,7 @@ st.set_page_config(
|
|
18 |
)
|
19 |
|
20 |
# Preprocessing
|
|
|
21 |
def merge(B, C, A):
|
22 |
i = j = k = 0
|
23 |
|
@@ -93,6 +94,7 @@ def group_to_three(dataframe):
|
|
93 |
return dataframe
|
94 |
|
95 |
# SARIMAX Model
|
|
|
96 |
def train_test(dataframe):
|
97 |
n = round(len(dataframe) * 0.2)
|
98 |
training_y = dataframe.iloc[:-n,0]
|
@@ -103,7 +105,7 @@ def train_test(dataframe):
|
|
103 |
future_X = dataframe.iloc[0:,1:]
|
104 |
return (training_y, test_y, test_y_series, training_X, test_X, future_X)
|
105 |
|
106 |
-
|
107 |
def model_fitting(dataframe, Exo):
|
108 |
futureModel = pm.auto_arima(dataframe['Sales'], X=Exo, start_p=1, start_q=1,
|
109 |
test='adf',min_p=1,min_q=1,
|
@@ -117,6 +119,7 @@ def model_fitting(dataframe, Exo):
|
|
117 |
model = futureModel
|
118 |
return model
|
119 |
|
|
|
120 |
def test_fitting(dataframe, Exo, trainY):
|
121 |
trainTestModel = auto_arima(X = Exo, y = trainY, start_p=1, start_q=1,
|
122 |
test='adf',min_p=1,min_q=1,
|
@@ -130,6 +133,7 @@ def test_fitting(dataframe, Exo, trainY):
|
|
130 |
model = trainTestModel
|
131 |
return model
|
132 |
|
|
|
133 |
def forecast_accuracy(forecast, actual):
|
134 |
mape = np.mean(np.abs(forecast - actual)/np.abs(actual)).round(4) # MAPE
|
135 |
rmse = (np.mean((forecast - actual)**2)**.5).round(2) # RMSE
|
@@ -141,6 +145,7 @@ def forecast_accuracy(forecast, actual):
|
|
141 |
minmax = 1 - np.mean(mins/maxs) # minmax
|
142 |
return({'mape':mape, 'rmse':rmse, 'corr':corr, 'min-max':minmax})
|
143 |
|
|
|
144 |
def sales_growth(dataframe, fittedValues):
|
145 |
sales_growth = fittedValues.to_frame()
|
146 |
sales_growth = sales_growth.reset_index()
|
@@ -149,11 +154,11 @@ def sales_growth(dataframe, fittedValues):
|
|
149 |
|
150 |
sales_growth['Sales'] = (sales_growth['Sales']).round(2)
|
151 |
|
152 |
-
#Calculate and create the column for sales difference and growth
|
153 |
sales_growth['Forecasted Sales First Difference']=(sales_growth['Sales']-sales_growth['Sales'].shift(1)).round(2)
|
154 |
sales_growth['Forecasted Sales Growth']=(((sales_growth['Sales']-sales_growth['Sales'].shift(1))/sales_growth['Sales'].shift(1))*100).round(2)
|
155 |
|
156 |
-
#Calculate and create the first row for sales difference and growth
|
157 |
sales_growth['Forecasted Sales First Difference'].iloc[0] = (dataframe['Sales'].iloc[-1]-dataframe['Sales'].iloc[-2]).round(2)
|
158 |
sales_growth['Forecasted Sales Growth'].iloc[0]=(((dataframe['Sales'].iloc[-1]-dataframe['Sales'].iloc[-2])/dataframe['Sales'].iloc[-1])*100).round(2)
|
159 |
|
@@ -165,6 +170,7 @@ model_name = "google/tapas-large-finetuned-wtq"
|
|
165 |
tokenizer = TapasTokenizer.from_pretrained(model_name)
|
166 |
model = TapasForQuestionAnswering.from_pretrained(model_name, local_files_only=False)
|
167 |
|
|
|
168 |
def load_tapas_model(model, tokenizer):
|
169 |
pipe = pipeline("table-question-answering", model=model, tokenizer=tokenizer)
|
170 |
return pipe
|
@@ -207,7 +213,13 @@ st.subheader("Welcome User, start using the application by uploading your file i
|
|
207 |
|
208 |
# Session States
|
209 |
if 'uploaded' not in st.session_state:
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
# Sidebar Menu
|
213 |
with st.sidebar:
|
@@ -236,6 +248,7 @@ with st.sidebar:
|
|
236 |
if (st.session_state.uploaded):
|
237 |
st.line_chart(df)
|
238 |
|
|
|
239 |
period = st.slider('How many days would you like to forecast?', min_value=30, max_value=90)
|
240 |
forecast_period = round(period / 3)
|
241 |
|
|
|
18 |
)
|
19 |
|
20 |
# Preprocessing
|
21 |
+
@st.cache(show_spinner=False)
|
22 |
def merge(B, C, A):
|
23 |
i = j = k = 0
|
24 |
|
|
|
94 |
return dataframe
|
95 |
|
96 |
# SARIMAX Model
|
97 |
+
@st.cache(show_spinner=False)
|
98 |
def train_test(dataframe):
|
99 |
n = round(len(dataframe) * 0.2)
|
100 |
training_y = dataframe.iloc[:-n,0]
|
|
|
105 |
future_X = dataframe.iloc[0:,1:]
|
106 |
return (training_y, test_y, test_y_series, training_X, test_X, future_X)
|
107 |
|
108 |
+
@st.cache(show_spinner=False)
|
109 |
def model_fitting(dataframe, Exo):
|
110 |
futureModel = pm.auto_arima(dataframe['Sales'], X=Exo, start_p=1, start_q=1,
|
111 |
test='adf',min_p=1,min_q=1,
|
|
|
119 |
model = futureModel
|
120 |
return model
|
121 |
|
122 |
+
@st.cache(show_spinner=False)
|
123 |
def test_fitting(dataframe, Exo, trainY):
|
124 |
trainTestModel = auto_arima(X = Exo, y = trainY, start_p=1, start_q=1,
|
125 |
test='adf',min_p=1,min_q=1,
|
|
|
133 |
model = trainTestModel
|
134 |
return model
|
135 |
|
136 |
+
@st.cache(show_spinner=False)
|
137 |
def forecast_accuracy(forecast, actual):
|
138 |
mape = np.mean(np.abs(forecast - actual)/np.abs(actual)).round(4) # MAPE
|
139 |
rmse = (np.mean((forecast - actual)**2)**.5).round(2) # RMSE
|
|
|
145 |
minmax = 1 - np.mean(mins/maxs) # minmax
|
146 |
return({'mape':mape, 'rmse':rmse, 'corr':corr, 'min-max':minmax})
|
147 |
|
148 |
+
@st.cache(show_spinner=False)
|
149 |
def sales_growth(dataframe, fittedValues):
|
150 |
sales_growth = fittedValues.to_frame()
|
151 |
sales_growth = sales_growth.reset_index()
|
|
|
154 |
|
155 |
sales_growth['Sales'] = (sales_growth['Sales']).round(2)
|
156 |
|
157 |
+
# Calculate and create the column for sales difference and growth
|
158 |
sales_growth['Forecasted Sales First Difference']=(sales_growth['Sales']-sales_growth['Sales'].shift(1)).round(2)
|
159 |
sales_growth['Forecasted Sales Growth']=(((sales_growth['Sales']-sales_growth['Sales'].shift(1))/sales_growth['Sales'].shift(1))*100).round(2)
|
160 |
|
161 |
+
# Calculate and create the first row for sales difference and growth
|
162 |
sales_growth['Forecasted Sales First Difference'].iloc[0] = (dataframe['Sales'].iloc[-1]-dataframe['Sales'].iloc[-2]).round(2)
|
163 |
sales_growth['Forecasted Sales Growth'].iloc[0]=(((dataframe['Sales'].iloc[-1]-dataframe['Sales'].iloc[-2])/dataframe['Sales'].iloc[-1])*100).round(2)
|
164 |
|
|
|
170 |
tokenizer = TapasTokenizer.from_pretrained(model_name)
|
171 |
model = TapasForQuestionAnswering.from_pretrained(model_name, local_files_only=False)
|
172 |
|
173 |
+
@st.cache(show_spinner=False)
|
174 |
def load_tapas_model(model, tokenizer):
|
175 |
pipe = pipeline("table-question-answering", model=model, tokenizer=tokenizer)
|
176 |
return pipe
|
|
|
213 |
|
214 |
# Session States
|
215 |
if 'uploaded' not in st.session_state:
|
216 |
+
st.session_state.uploaded = False
|
217 |
+
|
218 |
+
if 'preprocessed_data' not in st.session_state:
|
219 |
+
st.session_state.preprocessed_data = None
|
220 |
+
|
221 |
+
if 'fitted_models' not in st.session_state:
|
222 |
+
st.session_state.fitted_models = {}
|
223 |
|
224 |
# Sidebar Menu
|
225 |
with st.sidebar:
|
|
|
248 |
if (st.session_state.uploaded):
|
249 |
st.line_chart(df)
|
250 |
|
251 |
+
|
252 |
period = st.slider('How many days would you like to forecast?', min_value=30, max_value=90)
|
253 |
forecast_period = round(period / 3)
|
254 |
|