Spaces:
Running
Running
Commit
·
c49aa0f
1
Parent(s):
0d0d62b
Fix app
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ st.set_page_config(
|
|
| 18 |
)
|
| 19 |
|
| 20 |
# Preprocessing
|
| 21 |
-
@st.
|
| 22 |
def merge(B, C, A):
|
| 23 |
i = j = k = 0
|
| 24 |
|
|
@@ -53,6 +53,7 @@ def merge(B, C, A):
|
|
| 53 |
|
| 54 |
return A
|
| 55 |
|
|
|
|
| 56 |
def merge_sort(dataframe):
|
| 57 |
if len(dataframe) > 1:
|
| 58 |
center = len(dataframe) // 2
|
|
@@ -66,6 +67,7 @@ def merge_sort(dataframe):
|
|
| 66 |
else:
|
| 67 |
return dataframe
|
| 68 |
|
|
|
|
| 69 |
def drop (dataframe):
|
| 70 |
def get_columns_containing(dataframe, substrings):
|
| 71 |
return [col for col in dataframe.columns if any(substring.lower() in col.lower() for substring in substrings)]
|
|
@@ -76,6 +78,7 @@ def drop (dataframe):
|
|
| 76 |
|
| 77 |
return dataframe
|
| 78 |
|
|
|
|
| 79 |
def date_format(dataframe):
|
| 80 |
for i, d, s in dataframe.itertuples():
|
| 81 |
dataframe['Date'][i] = dataframe['Date'][i].strip()
|
|
@@ -86,6 +89,7 @@ def date_format(dataframe):
|
|
| 86 |
|
| 87 |
return dataframe
|
| 88 |
|
|
|
|
| 89 |
def group_to_three(dataframe):
|
| 90 |
dataframe['Date'] = pd.to_datetime(dataframe['Date'])
|
| 91 |
dataframe = dataframe.groupby([pd.Grouper(key='Date', freq='3D')])['Sales'].mean().round(2)
|
|
@@ -94,7 +98,7 @@ def group_to_three(dataframe):
|
|
| 94 |
return dataframe
|
| 95 |
|
| 96 |
# SARIMAX Model
|
| 97 |
-
@st.
|
| 98 |
def train_test(dataframe):
|
| 99 |
n = round(len(dataframe) * 0.2)
|
| 100 |
training_y = dataframe.iloc[:-n,0]
|
|
@@ -105,7 +109,7 @@ def train_test(dataframe):
|
|
| 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.
|
| 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,7 +123,7 @@ def model_fitting(dataframe, Exo):
|
|
| 119 |
model = futureModel
|
| 120 |
return model
|
| 121 |
|
| 122 |
-
@st.
|
| 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,7 +137,7 @@ def test_fitting(dataframe, Exo, trainY):
|
|
| 133 |
model = trainTestModel
|
| 134 |
return model
|
| 135 |
|
| 136 |
-
@st.
|
| 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,7 +149,7 @@ def forecast_accuracy(forecast, actual):
|
|
| 145 |
minmax = 1 - np.mean(mins/maxs) # minmax
|
| 146 |
return({'mape':mape, 'rmse':rmse, 'corr':corr, 'min-max':minmax})
|
| 147 |
|
| 148 |
-
@st.
|
| 149 |
def sales_growth(dataframe, fittedValues):
|
| 150 |
sales_growth = fittedValues.to_frame()
|
| 151 |
sales_growth = sales_growth.reset_index()
|
|
@@ -170,6 +174,7 @@ model_name = "google/tapas-large-finetuned-wtq"
|
|
| 170 |
tokenizer = TapasTokenizer.from_pretrained(model_name)
|
| 171 |
model = TapasForQuestionAnswering.from_pretrained(model_name, local_files_only=False)
|
| 172 |
|
|
|
|
| 173 |
def load_tapas_model(model, tokenizer):
|
| 174 |
pipe = pipeline("table-question-answering", model=model, tokenizer=tokenizer)
|
| 175 |
return pipe
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
# Preprocessing
|
| 21 |
+
@st.cache_data
|
| 22 |
def merge(B, C, A):
|
| 23 |
i = j = k = 0
|
| 24 |
|
|
|
|
| 53 |
|
| 54 |
return A
|
| 55 |
|
| 56 |
+
@st.cache_data
|
| 57 |
def merge_sort(dataframe):
|
| 58 |
if len(dataframe) > 1:
|
| 59 |
center = len(dataframe) // 2
|
|
|
|
| 67 |
else:
|
| 68 |
return dataframe
|
| 69 |
|
| 70 |
+
@st.cache_data
|
| 71 |
def drop (dataframe):
|
| 72 |
def get_columns_containing(dataframe, substrings):
|
| 73 |
return [col for col in dataframe.columns if any(substring.lower() in col.lower() for substring in substrings)]
|
|
|
|
| 78 |
|
| 79 |
return dataframe
|
| 80 |
|
| 81 |
+
@st.cache_data
|
| 82 |
def date_format(dataframe):
|
| 83 |
for i, d, s in dataframe.itertuples():
|
| 84 |
dataframe['Date'][i] = dataframe['Date'][i].strip()
|
|
|
|
| 89 |
|
| 90 |
return dataframe
|
| 91 |
|
| 92 |
+
@st.cache_data
|
| 93 |
def group_to_three(dataframe):
|
| 94 |
dataframe['Date'] = pd.to_datetime(dataframe['Date'])
|
| 95 |
dataframe = dataframe.groupby([pd.Grouper(key='Date', freq='3D')])['Sales'].mean().round(2)
|
|
|
|
| 98 |
return dataframe
|
| 99 |
|
| 100 |
# SARIMAX Model
|
| 101 |
+
@st.cache_data
|
| 102 |
def train_test(dataframe):
|
| 103 |
n = round(len(dataframe) * 0.2)
|
| 104 |
training_y = dataframe.iloc[:-n,0]
|
|
|
|
| 109 |
future_X = dataframe.iloc[0:,1:]
|
| 110 |
return (training_y, test_y, test_y_series, training_X, test_X, future_X)
|
| 111 |
|
| 112 |
+
@st.cache_data
|
| 113 |
def model_fitting(dataframe, Exo):
|
| 114 |
futureModel = pm.auto_arima(dataframe['Sales'], X=Exo, start_p=1, start_q=1,
|
| 115 |
test='adf',min_p=1,min_q=1,
|
|
|
|
| 123 |
model = futureModel
|
| 124 |
return model
|
| 125 |
|
| 126 |
+
@st.cache_data
|
| 127 |
def test_fitting(dataframe, Exo, trainY):
|
| 128 |
trainTestModel = auto_arima(X = Exo, y = trainY, start_p=1, start_q=1,
|
| 129 |
test='adf',min_p=1,min_q=1,
|
|
|
|
| 137 |
model = trainTestModel
|
| 138 |
return model
|
| 139 |
|
| 140 |
+
@st.cache_data
|
| 141 |
def forecast_accuracy(forecast, actual):
|
| 142 |
mape = np.mean(np.abs(forecast - actual)/np.abs(actual)).round(4) # MAPE
|
| 143 |
rmse = (np.mean((forecast - actual)**2)**.5).round(2) # RMSE
|
|
|
|
| 149 |
minmax = 1 - np.mean(mins/maxs) # minmax
|
| 150 |
return({'mape':mape, 'rmse':rmse, 'corr':corr, 'min-max':minmax})
|
| 151 |
|
| 152 |
+
@st.cache_data
|
| 153 |
def sales_growth(dataframe, fittedValues):
|
| 154 |
sales_growth = fittedValues.to_frame()
|
| 155 |
sales_growth = sales_growth.reset_index()
|
|
|
|
| 174 |
tokenizer = TapasTokenizer.from_pretrained(model_name)
|
| 175 |
model = TapasForQuestionAnswering.from_pretrained(model_name, local_files_only=False)
|
| 176 |
|
| 177 |
+
@st.cache_resource
|
| 178 |
def load_tapas_model(model, tokenizer):
|
| 179 |
pipe = pipeline("table-question-answering", model=model, tokenizer=tokenizer)
|
| 180 |
return pipe
|