Commit
·
fd022ae
1
Parent(s):
ddde3e7
Guardar mis cambios locales
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
-
from statsmodels.tsa.arima.model import ARIMA
|
| 4 |
import pickle
|
| 5 |
import gradio as gr
|
| 6 |
|
|
@@ -8,7 +7,7 @@ def load_model():
|
|
| 8 |
try:
|
| 9 |
with open('arima_sales_model.pkl', 'rb') as f:
|
| 10 |
arima_model = pickle.load(f)
|
| 11 |
-
return arima_model
|
| 12 |
except Exception as e:
|
| 13 |
return None, f"Failed to load model: {str(e)}"
|
| 14 |
|
|
@@ -18,20 +17,19 @@ def forecast_sales(uploaded_file, forecast_period=30):
|
|
| 18 |
|
| 19 |
try:
|
| 20 |
df = pd.read_csv(uploaded_file)
|
|
|
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
return f"Failed to read the uploaded CSV file: {str(e)}", None
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
arima_model, error = load_model()
|
| 32 |
-
if arima_model is None:
|
| 33 |
-
return error, None
|
| 34 |
|
|
|
|
| 35 |
forecast = arima_model.get_forecast(steps=forecast_period)
|
| 36 |
forecast_index = pd.date_range(df['ds'].max(), periods=forecast_period + 1, freq='D')[1:]
|
| 37 |
forecast_df = pd.DataFrame({'Date': forecast_index, 'Sales Forecast': forecast.predicted_mean})
|
|
@@ -56,9 +54,9 @@ def setup_interface():
|
|
| 56 |
gr.Markdown("## MLCast v1.1 - Intelligent Sales Forecasting System")
|
| 57 |
file_input = gr.File(label="Upload your store data here (must contain Date and Sales)")
|
| 58 |
forecast_button = gr.Button("Forecast Sales")
|
|
|
|
| 59 |
output_plot = gr.Plot()
|
| 60 |
-
|
| 61 |
-
forecast_button.click(forecast_sales, inputs=[file_input], outputs=[output_text, output_plot])
|
| 62 |
|
| 63 |
return demo
|
| 64 |
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import matplotlib.pyplot as plt
|
|
|
|
| 3 |
import pickle
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
| 7 |
try:
|
| 8 |
with open('arima_sales_model.pkl', 'rb') as f:
|
| 9 |
arima_model = pickle.load(f)
|
| 10 |
+
return arima_model, None
|
| 11 |
except Exception as e:
|
| 12 |
return None, f"Failed to load model: {str(e)}"
|
| 13 |
|
|
|
|
| 17 |
|
| 18 |
try:
|
| 19 |
df = pd.read_csv(uploaded_file)
|
| 20 |
+
if 'Date' not in df.columns or 'Sale' not in df.columns:
|
| 21 |
+
return "The uploaded file must contain 'Date' and 'Sale' columns.", None
|
| 22 |
except Exception as e:
|
| 23 |
return f"Failed to read the uploaded CSV file: {str(e)}", None
|
| 24 |
|
| 25 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 26 |
+
df = df.rename(columns={'Date': 'ds', 'Sale': 'y'})
|
| 27 |
|
| 28 |
+
arima_model, error = load_model()
|
| 29 |
+
if arima_model is None:
|
| 30 |
+
return error, None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
try:
|
| 33 |
forecast = arima_model.get_forecast(steps=forecast_period)
|
| 34 |
forecast_index = pd.date_range(df['ds'].max(), periods=forecast_period + 1, freq='D')[1:]
|
| 35 |
forecast_df = pd.DataFrame({'Date': forecast_index, 'Sales Forecast': forecast.predicted_mean})
|
|
|
|
| 54 |
gr.Markdown("## MLCast v1.1 - Intelligent Sales Forecasting System")
|
| 55 |
file_input = gr.File(label="Upload your store data here (must contain Date and Sales)")
|
| 56 |
forecast_button = gr.Button("Forecast Sales")
|
| 57 |
+
output_text = gr.Textbox(visible=True)
|
| 58 |
output_plot = gr.Plot()
|
| 59 |
+
forecast_button.click(forecast_sales, inputs=[file_input, gr.Slider(1, 60, 1, label="Forecast Period (days)", default=30)], outputs=[output_text, output_plot])
|
|
|
|
| 60 |
|
| 61 |
return demo
|
| 62 |
|