Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +94 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from statsmodels.tsa.arima.model import ARIMA
|
| 5 |
+
|
| 6 |
+
# Load dataset
|
| 7 |
+
df = pd.read_csv("train.csv")
|
| 8 |
+
df["date"] = pd.to_datetime(df["date"])
|
| 9 |
+
|
| 10 |
+
stores = sorted(df["store"].unique())
|
| 11 |
+
items = sorted(df["item"].unique())
|
| 12 |
+
|
| 13 |
+
def forecast_single(store_id, item_id, days):
|
| 14 |
+
data = df[(df["store"] == store_id) & (df["item"] == item_id)]
|
| 15 |
+
data = data[["date", "sales"]].sort_values("date")
|
| 16 |
+
|
| 17 |
+
if len(data) < 50:
|
| 18 |
+
return None, None, "Not enough data for forecasting"
|
| 19 |
+
|
| 20 |
+
ts = data.set_index("date")["sales"]
|
| 21 |
+
|
| 22 |
+
model = ARIMA(ts, order=(1, 1, 1))
|
| 23 |
+
model_fit = model.fit()
|
| 24 |
+
forecast = model_fit.forecast(steps=days)
|
| 25 |
+
|
| 26 |
+
forecast_df = forecast.reset_index()
|
| 27 |
+
forecast_df.columns = ["Date", "Forecasted_Sales"]
|
| 28 |
+
|
| 29 |
+
fig, ax = plt.subplots()
|
| 30 |
+
ax.plot(ts, label="Historical Sales")
|
| 31 |
+
ax.plot(forecast, label="Forecast")
|
| 32 |
+
ax.set_title(f"Store {store_id} - Item {item_id}")
|
| 33 |
+
ax.legend()
|
| 34 |
+
|
| 35 |
+
return forecast_df, fig, forecast_df
|
| 36 |
+
|
| 37 |
+
def forecast_all_stores(item_id, days):
|
| 38 |
+
results = []
|
| 39 |
+
|
| 40 |
+
for store_id in stores:
|
| 41 |
+
data = df[(df["store"] == store_id) & (df["item"] == item_id)]
|
| 42 |
+
data = data[["date", "sales"]].sort_values("date")
|
| 43 |
+
|
| 44 |
+
if len(data) < 50:
|
| 45 |
+
continue
|
| 46 |
+
|
| 47 |
+
ts = data.set_index("date")["sales"]
|
| 48 |
+
model = ARIMA(ts, order=(1, 1, 1))
|
| 49 |
+
model_fit = model.fit()
|
| 50 |
+
forecast = model_fit.forecast(steps=days)
|
| 51 |
+
|
| 52 |
+
forecast_df = forecast.reset_index()
|
| 53 |
+
forecast_df.columns = ["Date", "Forecasted_Sales"]
|
| 54 |
+
forecast_df["Store"] = store_id
|
| 55 |
+
results.append(forecast_df)
|
| 56 |
+
|
| 57 |
+
final_df = pd.concat(results)
|
| 58 |
+
return final_df, final_df
|
| 59 |
+
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
gr.Markdown("# Demand Forecasting Tool")
|
| 62 |
+
|
| 63 |
+
gr.Markdown("## Forecast for a Single Store and Item")
|
| 64 |
+
store = gr.Dropdown(stores, label="Select Store", value=1)
|
| 65 |
+
item = gr.Dropdown(items, label="Select Item", value=1)
|
| 66 |
+
days = gr.Slider(7, 90, value=30, label="Forecast Days")
|
| 67 |
+
|
| 68 |
+
run_single = gr.Button("Generate Forecast")
|
| 69 |
+
|
| 70 |
+
table_single = gr.Dataframe()
|
| 71 |
+
plot_single = gr.Plot()
|
| 72 |
+
download_single = gr.File()
|
| 73 |
+
|
| 74 |
+
run_single.click(
|
| 75 |
+
forecast_single,
|
| 76 |
+
inputs=[store, item, days],
|
| 77 |
+
outputs=[table_single, plot_single, download_single]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
gr.Markdown("## Forecast One Item Across All Stores")
|
| 81 |
+
item_all = gr.Dropdown(items, label="Select Item")
|
| 82 |
+
days_all = gr.Slider(7, 90, value=30, label="Forecast Days")
|
| 83 |
+
run_all = gr.Button("Forecast All Stores")
|
| 84 |
+
|
| 85 |
+
table_all = gr.Dataframe()
|
| 86 |
+
download_all = gr.File()
|
| 87 |
+
|
| 88 |
+
run_all.click(
|
| 89 |
+
forecast_all_stores,
|
| 90 |
+
inputs=[item_all, days_all],
|
| 91 |
+
outputs=[table_all, download_all]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
gradio
|
| 3 |
+
statsmodels
|
| 4 |
+
matplotlib
|