Commit
·
1b9a2bc
1
Parent(s):
6458b87
Guardar mis cambios locales
Browse files
app.py
CHANGED
|
@@ -1,51 +1,48 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from statsmodels.tsa.arima.model import ARIMA
|
| 5 |
import pickle
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
df = pd.read_csv(uploaded_file)
|
| 13 |
-
if 'Date' in df.columns and 'Sale' in df.columns:
|
| 14 |
-
st.success("File uploaded successfully!")
|
| 15 |
-
st.write(df.head())
|
| 16 |
-
df['Date'] = pd.to_datetime(df['Date'])
|
| 17 |
-
df_arima = df.rename(columns={'Date': 'ds', 'Sale': 'y'})
|
| 18 |
-
|
| 19 |
-
with open('Forecast_TimeSeries_Sales\arima_sales_model.pkl', 'rb') as f:
|
| 20 |
-
arima_model = pickle.load(f)
|
| 21 |
-
|
| 22 |
-
forecast_period = 30
|
| 23 |
-
forecast = arima_model.get_forecast(steps=forecast_period)
|
| 24 |
-
forecast_index = pd.date_range(df['Date'].max(), periods=forecast_period + 1, freq='D')[1:]
|
| 25 |
-
forecast_df = pd.DataFrame({'Date': forecast_index, 'Sales Forecast': forecast.predicted_mean})
|
| 26 |
-
|
| 27 |
-
fig, ax = plt.subplots(figsize=(10, 6))
|
| 28 |
-
ax.plot(df['Date'], df['Sale'], label='Historical Sales', color='blue')
|
| 29 |
-
ax.plot(forecast_df['Date'], forecast_df['Sales Forecast'], label='Sales Forecast', color='red', linestyle='--')
|
| 30 |
-
ax.set_xlabel('Date')
|
| 31 |
-
ax.set_ylabel('Sales')
|
| 32 |
-
ax.set_title('Sales Forecasting with ARIMA')
|
| 33 |
-
ax.legend()
|
| 34 |
-
st.pyplot(fig)
|
| 35 |
-
|
| 36 |
-
st.sidebar.title("Adjust Forecast Range")
|
| 37 |
-
start_date = st.sidebar.date_input('Start Date', df['Date'].min())
|
| 38 |
-
end_date = st.sidebar.date_input('End Date', df['Date'].max())
|
| 39 |
-
filtered_df = df[(df['Date'] >= pd.to_datetime(start_date)) & (df['Date'] <= pd.to_datetime(end_date))]
|
| 40 |
-
fig_filtered, ax_filtered = plt.subplots(figsize=(10, 6))
|
| 41 |
-
ax_filtered.plot(filtered_df['Date'], filtered_df['Sale'], label=f'Sales from {start_date} to {end_date}')
|
| 42 |
-
ax_filtered.set_xlabel('Date')
|
| 43 |
-
ax_filtered.set_ylabel('Sale')
|
| 44 |
-
ax_filtered.set_title(f'Sales Forecasting from {start_date} to {end_date}')
|
| 45 |
-
ax_filtered.legend()
|
| 46 |
-
st.pyplot(fig_filtered)
|
| 47 |
-
else:
|
| 48 |
-
st.error("The uploaded file must contain at least 'Date' and 'Sales' columns.")
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
|
|
|
|
|
|
|
|
|
| 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 |
|
| 7 |
+
def load_model():
|
| 8 |
+
with open('Forecast_TimeSeries_Sales/arima_sales_model.pkl', 'rb') as f:
|
| 9 |
+
arima_model = pickle.load(f)
|
| 10 |
+
return arima_model
|
| 11 |
+
|
| 12 |
+
def forecast_sales(uploaded_file, forecast_period=30):
|
| 13 |
+
df = pd.read_csv(uploaded_file)
|
| 14 |
+
if 'Date' in df.columns and 'Sale' in df.columns:
|
| 15 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 16 |
+
df = df.rename(columns={'Date': 'ds', 'Sale': 'y'})
|
| 17 |
+
|
| 18 |
+
arima_model = load_model()
|
| 19 |
+
forecast = arima_model.get_forecast(steps=forecast_period)
|
| 20 |
+
forecast_index = pd.date_range(df['ds'].max(), periods=forecast_period + 1, freq='D')[1:]
|
| 21 |
+
forecast_df = pd.DataFrame({'Date': forecast_index, 'Sales Forecast': forecast.predicted_mean})
|
| 22 |
+
|
| 23 |
+
# Create the plot
|
| 24 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 25 |
+
ax.plot(df['ds'], df['y'], label='Historical Sales', color='blue')
|
| 26 |
+
ax.plot(forecast_df['Date'], forecast_df['Sales Forecast'], label='Sales Forecast', color='red', linestyle='--')
|
| 27 |
+
ax.set_xlabel('Date')
|
| 28 |
+
ax.set_ylabel('Sales')
|
| 29 |
+
ax.set_title('Sales Forecasting with ARIMA')
|
| 30 |
+
ax.legend()
|
| 31 |
+
return fig
|
| 32 |
+
else:
|
| 33 |
+
return "The uploaded file must contain at least 'Date' and 'Sales' columns."
|
| 34 |
+
|
| 35 |
+
def setup_interface():
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("## MLCast v1.1 - Intelligent Sales Forecasting System")
|
| 38 |
+
with gr.Row():
|
| 39 |
+
file_input = gr.File(label="Upload your store data here (must contain Date and Sales)")
|
| 40 |
+
forecast_button = gr.Button("Forecast Sales")
|
| 41 |
+
output_plot = gr.Plot()
|
| 42 |
+
forecast_button.click(forecast_sales, inputs=[file_input], outputs=[output_plot])
|
| 43 |
|
| 44 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
+
interface = setup_interface()
|
| 48 |
+
interface.launch()
|