Commit
·
376f47b
1
Parent(s):
7e178ba
Guardar mis cambios locales
Browse files
app.py
CHANGED
@@ -1,114 +1,66 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
from datetime import datetime
|
5 |
import plotly.graph_objects as go
|
6 |
-
import
|
7 |
-
from
|
8 |
-
import pmdarima as pm
|
9 |
-
from pmdarima import auto_arima
|
10 |
|
11 |
-
#
|
12 |
-
def
|
13 |
-
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
|
|
|
18 |
def drop(dataframe):
|
19 |
-
pass #
|
20 |
|
21 |
def date_format(dataframe):
|
22 |
-
pass #
|
23 |
|
24 |
def group_to_three(dataframe):
|
25 |
-
pass #
|
26 |
-
|
27 |
-
def series_to_df_exogenous(series):
|
28 |
-
pass # Implement series_to_df_exogenous function here...
|
29 |
-
|
30 |
-
def dates_df(dataframe):
|
31 |
-
pass # Implement dates_df function here...
|
32 |
|
33 |
def get_forecast_period(period):
|
34 |
-
|
35 |
|
36 |
-
def
|
37 |
-
|
38 |
-
|
39 |
-
def test_fitting(dataframe, Exo, trainY):
|
40 |
-
pass # Implement test_fitting function here...
|
41 |
-
|
42 |
-
def forecast_accuracy(forecast, actual):
|
43 |
-
pass # Implement forecast_accuracy function here...
|
44 |
-
|
45 |
-
def sales_growth(dataframe, fittedValues):
|
46 |
-
pass # Implement sales_growth function here...
|
47 |
|
48 |
def merge_forecast_data(actual, predicted, future):
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
|
54 |
-
|
55 |
-
model_name = "google/tapas-large-finetuned-wtq"
|
56 |
-
tokenizer = TapasTokenizer.from_pretrained(model_name)
|
57 |
-
model = TapasForQuestionAnswering.from_pretrained(model_name, local_files_only=False)
|
58 |
-
pipe = pipeline("table-question-answering", model=model, tokenizer=tokenizer)
|
59 |
-
return pipe
|
60 |
-
|
61 |
-
pipe = load_tapas_model()
|
62 |
-
|
63 |
-
def get_answer(table, query):
|
64 |
-
answers = pipe(table=table, query=query)
|
65 |
-
return answers
|
66 |
-
|
67 |
-
def convert_answer(answer):
|
68 |
-
pass # Implement convert_answer function here...
|
69 |
-
|
70 |
-
def get_converted_answer(table, query):
|
71 |
-
pass # Implement get_converted_answer function here...
|
72 |
-
|
73 |
-
# Gradio Interface with emojis and colors
|
74 |
def upload_and_forecast(uploaded_file, period):
|
75 |
if uploaded_file is None:
|
76 |
return "⚠️ Please upload a file to proceed."
|
77 |
|
78 |
-
# Load the data
|
79 |
df = pd.read_csv(uploaded_file)
|
80 |
df = drop(df)
|
81 |
df = date_format(df)
|
82 |
-
merge_sort(df)
|
83 |
series = group_to_three(df)
|
84 |
-
|
|
|
85 |
forecast_period = get_forecast_period(period)
|
86 |
-
|
87 |
|
88 |
-
#
|
89 |
-
|
90 |
-
|
91 |
-
training_y, test_y, test_y_series, training_X, test_X, future_X = train
|
92 |
-
train_test_model = test_fitting(df, training_X, training_y)
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
fitted_series = pd.Series(fitted)
|
97 |
-
fitted_series.index = index_of_fc
|
98 |
-
|
99 |
-
future_n_periods = forecast_period
|
100 |
-
future_fitted, confint = train_test_model.predict(X=df.iloc[-future_n_periods:, 1:], n_periods=future_n_periods, return_conf_int=True, freq='3D')
|
101 |
-
future_index_of_fc = pd.date_range(df['Sales'].index[-1], periods=future_n_periods, freq='3D')
|
102 |
-
future_fitted_series = pd.Series(future_fitted)
|
103 |
-
future_fitted_series.index = future_index_of_fc
|
104 |
-
|
105 |
-
# Calculate sales growth
|
106 |
-
future_sales_growth = sales_growth(df, future_fitted_series)
|
107 |
|
108 |
-
#
|
109 |
-
merged_data = merge_forecast_data(df['Sales'],
|
110 |
|
111 |
-
#
|
112 |
fig_compare = go.Figure()
|
113 |
fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
|
114 |
fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Predicted Sales'], mode='lines', name='Predicted Sales', line=dict(color='#006400')))
|
@@ -116,13 +68,12 @@ def upload_and_forecast(uploaded_file, period):
|
|
116 |
|
117 |
fig_forecast = go.Figure()
|
118 |
fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
|
119 |
-
fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=
|
120 |
fig_forecast.update_layout(title='🔮 Forecasted Sales Data', xaxis_title='Date', yaxis_title='Sales')
|
121 |
|
122 |
-
# Return the figures and growth data
|
123 |
return fig_compare, fig_forecast, future_sales_growth
|
124 |
|
125 |
-
#
|
126 |
def create_sidebar():
|
127 |
with gr.Column():
|
128 |
gr.Markdown("### 📂 Upload your sales data (CSV)")
|
@@ -131,17 +82,15 @@ def create_sidebar():
|
|
131 |
period = gr.Slider(minimum=30, maximum=90, step=1, label="Forecast period (in days)")
|
132 |
return uploaded_file, period
|
133 |
|
134 |
-
#
|
135 |
uploaded_file, period = create_sidebar()
|
136 |
|
137 |
-
# Gradio interface for output display (results shown below)
|
138 |
output_plots = [
|
139 |
gr.Plot(label="📈 Historical vs Predicted Sales"),
|
140 |
gr.Plot(label="🔮 Forecasted Sales Data"),
|
141 |
gr.DataFrame(label="📊 Sales Growth")
|
142 |
]
|
143 |
|
144 |
-
# Create and launch the gradio interface
|
145 |
iface = gr.Interface(
|
146 |
fn=upload_and_forecast,
|
147 |
inputs=[uploaded_file, period],
|
@@ -149,7 +98,7 @@ iface = gr.Interface(
|
|
149 |
live=True,
|
150 |
title="Sales Forecasting System ✨",
|
151 |
description="Upload your sales data to start forecasting 🚀",
|
152 |
-
css=open("styles.css", "r").read() #
|
153 |
)
|
154 |
|
155 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
|
|
4 |
import plotly.graph_objects as go
|
5 |
+
import joblib # Para cargar el modelo ARIMA guardado
|
6 |
+
from datetime import datetime
|
|
|
|
|
7 |
|
8 |
+
# Función para cargar el modelo ARIMA guardado
|
9 |
+
def load_arima_model():
|
10 |
+
model = joblib.load('arima_sales_model.pkl') # Cargar el modelo ARIMA desde un archivo guardado (.pkl)
|
11 |
+
return model
|
12 |
|
13 |
+
# Cargar el modelo ARIMA al inicio
|
14 |
+
arima_model = load_arima_model()
|
15 |
|
16 |
+
# Funciones de preprocesamiento y cálculo de crecimiento de ventas
|
17 |
def drop(dataframe):
|
18 |
+
pass # Implementar según sea necesario
|
19 |
|
20 |
def date_format(dataframe):
|
21 |
+
pass # Implementar según sea necesario
|
22 |
|
23 |
def group_to_three(dataframe):
|
24 |
+
pass # Implementar según sea necesario
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def get_forecast_period(period):
|
27 |
+
return period # Retornar el periodo de pronóstico
|
28 |
|
29 |
+
def sales_growth(df, forecasted_series):
|
30 |
+
return forecasted_series.diff() # Calcular el crecimiento de ventas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def merge_forecast_data(actual, predicted, future):
|
33 |
+
return pd.DataFrame({
|
34 |
+
"Actual Sales": actual,
|
35 |
+
"Predicted Sales": predicted,
|
36 |
+
"Forecasted Future Sales": future
|
37 |
+
})
|
38 |
|
39 |
+
# Función principal para la carga de archivo y la predicción
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
def upload_and_forecast(uploaded_file, period):
|
41 |
if uploaded_file is None:
|
42 |
return "⚠️ Please upload a file to proceed."
|
43 |
|
|
|
44 |
df = pd.read_csv(uploaded_file)
|
45 |
df = drop(df)
|
46 |
df = date_format(df)
|
|
|
47 |
series = group_to_three(df)
|
48 |
+
|
49 |
+
# Realizar la predicción con el modelo ARIMA
|
50 |
forecast_period = get_forecast_period(period)
|
51 |
+
forecasted_values, confint = arima_model.predict(n_periods=forecast_period, return_conf_int=True)
|
52 |
|
53 |
+
# Crear serie con los valores pronosticados
|
54 |
+
forecasted_series = pd.Series(forecasted_values)
|
55 |
+
forecasted_series.index = pd.date_range(df['Date'].iloc[-1], periods=forecast_period, freq='3D')
|
|
|
|
|
56 |
|
57 |
+
# Calcular el crecimiento de las ventas
|
58 |
+
future_sales_growth = sales_growth(df, forecasted_series)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Combinar los datos para graficar
|
61 |
+
merged_data = merge_forecast_data(df['Sales'], series, forecasted_series)
|
62 |
|
63 |
+
# Crear gráficos
|
64 |
fig_compare = go.Figure()
|
65 |
fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
|
66 |
fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Predicted Sales'], mode='lines', name='Predicted Sales', line=dict(color='#006400')))
|
|
|
68 |
|
69 |
fig_forecast = go.Figure()
|
70 |
fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
|
71 |
+
fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=forecasted_series, mode='lines', name='Forecasted Sales'))
|
72 |
fig_forecast.update_layout(title='🔮 Forecasted Sales Data', xaxis_title='Date', yaxis_title='Sales')
|
73 |
|
|
|
74 |
return fig_compare, fig_forecast, future_sales_growth
|
75 |
|
76 |
+
# Interfaz de Gradio
|
77 |
def create_sidebar():
|
78 |
with gr.Column():
|
79 |
gr.Markdown("### 📂 Upload your sales data (CSV)")
|
|
|
82 |
period = gr.Slider(minimum=30, maximum=90, step=1, label="Forecast period (in days)")
|
83 |
return uploaded_file, period
|
84 |
|
85 |
+
# Crear el sidebar y la interfaz principal
|
86 |
uploaded_file, period = create_sidebar()
|
87 |
|
|
|
88 |
output_plots = [
|
89 |
gr.Plot(label="📈 Historical vs Predicted Sales"),
|
90 |
gr.Plot(label="🔮 Forecasted Sales Data"),
|
91 |
gr.DataFrame(label="📊 Sales Growth")
|
92 |
]
|
93 |
|
|
|
94 |
iface = gr.Interface(
|
95 |
fn=upload_and_forecast,
|
96 |
inputs=[uploaded_file, period],
|
|
|
98 |
live=True,
|
99 |
title="Sales Forecasting System ✨",
|
100 |
description="Upload your sales data to start forecasting 🚀",
|
101 |
+
css=open("styles.css", "r").read() # Cargar el archivo CSS para los estilos personalizados
|
102 |
)
|
103 |
|
104 |
+
iface.launch() # Lanzar la interfaz
|