Commit
·
4e5cacd
1
Parent(s):
0a8f03f
Guardar mis cambios locales
Browse files
app.py
CHANGED
@@ -4,16 +4,16 @@ import joblib
|
|
4 |
import gradio as gr
|
5 |
from dateutil.relativedelta import relativedelta
|
6 |
import calendar
|
7 |
-
import emoji # Importa la librería de emoji
|
8 |
|
9 |
def load_model():
|
10 |
try:
|
11 |
model = joblib.load('arima_sales_model.pkl')
|
12 |
return model, None
|
13 |
except Exception as e:
|
14 |
-
return None,
|
15 |
|
16 |
def parse_date(date_str):
|
|
|
17 |
try:
|
18 |
date = pd.to_datetime(date_str, format="%B-%Y")
|
19 |
_, last_day = calendar.monthrange(date.year, date.month)
|
@@ -21,18 +21,18 @@ def parse_date(date_str):
|
|
21 |
end_date = date.replace(day=last_day)
|
22 |
return start_date, end_date, None
|
23 |
except ValueError:
|
24 |
-
return None, None,
|
25 |
|
26 |
def forecast_sales(uploaded_file, start_date_str, end_date_str):
|
27 |
if uploaded_file is None:
|
28 |
-
return
|
29 |
|
30 |
try:
|
31 |
df = pd.read_csv(uploaded_file)
|
32 |
if 'Date' not in df.columns or 'Sale' not in df.columns:
|
33 |
-
return None,
|
34 |
except Exception as e:
|
35 |
-
return None,
|
36 |
|
37 |
start_date, _, error = parse_date(start_date_str)
|
38 |
_, end_date, error_end = parse_date(end_date_str)
|
@@ -60,20 +60,22 @@ def forecast_sales(uploaded_file, start_date_str, end_date_str):
|
|
60 |
ax.set_ylabel('Sales')
|
61 |
ax.set_title('Sales Forecasting with ARIMA')
|
62 |
ax.legend()
|
63 |
-
return fig,
|
64 |
except Exception as e:
|
65 |
-
return None,
|
66 |
|
67 |
def setup_interface():
|
68 |
with gr.Blocks() as demo:
|
69 |
gr.Markdown("## MLCast v1.1 - Intelligent Sales Forecasting System")
|
70 |
with gr.Row():
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
forecast_button.click(
|
78 |
forecast_sales,
|
79 |
inputs=[file_input, start_date_input, end_date_input],
|
|
|
4 |
import gradio as gr
|
5 |
from dateutil.relativedelta import relativedelta
|
6 |
import calendar
|
|
|
7 |
|
8 |
def load_model():
|
9 |
try:
|
10 |
model = joblib.load('arima_sales_model.pkl')
|
11 |
return model, None
|
12 |
except Exception as e:
|
13 |
+
return None, f"Failed to load model: {str(e)}"
|
14 |
|
15 |
def parse_date(date_str):
|
16 |
+
"""Parse the custom date format 'Month-Year'."""
|
17 |
try:
|
18 |
date = pd.to_datetime(date_str, format="%B-%Y")
|
19 |
_, last_day = calendar.monthrange(date.year, date.month)
|
|
|
21 |
end_date = date.replace(day=last_day)
|
22 |
return start_date, end_date, None
|
23 |
except ValueError:
|
24 |
+
return None, None, "Date format should be 'Month-Year', e.g., 'January-2024'."
|
25 |
|
26 |
def forecast_sales(uploaded_file, start_date_str, end_date_str):
|
27 |
if uploaded_file is None:
|
28 |
+
return "No file uploaded.", None, "Please upload a file."
|
29 |
|
30 |
try:
|
31 |
df = pd.read_csv(uploaded_file)
|
32 |
if 'Date' not in df.columns or 'Sale' not in df.columns:
|
33 |
+
return None, "The uploaded file must contain 'Date' and 'Sale' columns.", "File does not have required columns."
|
34 |
except Exception as e:
|
35 |
+
return None, f"Failed to read the uploaded CSV file: {str(e)}", "Error reading file."
|
36 |
|
37 |
start_date, _, error = parse_date(start_date_str)
|
38 |
_, end_date, error_end = parse_date(end_date_str)
|
|
|
60 |
ax.set_ylabel('Sales')
|
61 |
ax.set_title('Sales Forecasting with ARIMA')
|
62 |
ax.legend()
|
63 |
+
return fig, "File loaded and processed successfully."
|
64 |
except Exception as e:
|
65 |
+
return None, f"Failed to generate plot: {str(e)}", "Plotting failed."
|
66 |
|
67 |
def setup_interface():
|
68 |
with gr.Blocks() as demo:
|
69 |
gr.Markdown("## MLCast v1.1 - Intelligent Sales Forecasting System")
|
70 |
with gr.Row():
|
71 |
+
with gr.Column(scale=1):
|
72 |
+
file_input = gr.File(label="Upload your store data")
|
73 |
+
start_date_input = gr.Textbox(label="Start Date", placeholder="January-2024")
|
74 |
+
end_date_input = gr.Textbox(label="End Date", placeholder="December-2024")
|
75 |
+
forecast_button = gr.Button("Forecast Sales")
|
76 |
+
with gr.Column(scale=2):
|
77 |
+
output_plot = gr.Plot()
|
78 |
+
output_message = gr.Textbox(label="Notifications", visible=True, lines=2)
|
79 |
forecast_button.click(
|
80 |
forecast_sales,
|
81 |
inputs=[file_input, start_date_input, end_date_input],
|