Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from prophet import Prophet
|
3 |
+
import gradio as gr
|
4 |
+
import plotly.graph_objs as go
|
5 |
+
|
6 |
+
# Function to train the model and generate forecast
|
7 |
+
def predict_sales(time_frame):
|
8 |
+
all_sales_data = pd.read_csv('/content/All sales - House of Pizza.csv')
|
9 |
+
|
10 |
+
# Clean up the 'Total paid' column
|
11 |
+
amount = all_sales_data['Total paid'].str.replace('₨', '', regex=False)
|
12 |
+
amount = amount.str.replace(',', '', regex=False)
|
13 |
+
amount = amount.str.strip()
|
14 |
+
amount = amount.astype(float)
|
15 |
+
|
16 |
+
# Convert the 'Date' column to datetime, coercing errors
|
17 |
+
all_sales_data['Date'] = pd.to_datetime(all_sales_data['Date'], format='%m/%d/%Y %H:%M', errors='coerce')
|
18 |
+
|
19 |
+
# Drop rows with invalid dates
|
20 |
+
all_sales_data = all_sales_data.dropna(subset=['Date'])
|
21 |
+
|
22 |
+
# Prepare the DataFrame
|
23 |
+
df = pd.DataFrame({
|
24 |
+
'Date': all_sales_data['Date'],
|
25 |
+
'Total paid': amount
|
26 |
+
})
|
27 |
+
|
28 |
+
# Prepare Prophet model
|
29 |
+
model = Prophet()
|
30 |
+
df['ds'] = df['Date']
|
31 |
+
df['y'] = df['Total paid']
|
32 |
+
model.fit(df[['ds', 'y']])
|
33 |
+
|
34 |
+
# Future forecast based on the time frame
|
35 |
+
future_periods = {
|
36 |
+
'24 hours': 1 * 24 * 60,
|
37 |
+
'7 days': 7 * 24 * 60,
|
38 |
+
'10 days': 10 * 24 * 60,
|
39 |
+
'15 days': 15 * 24 * 60,
|
40 |
+
'1 month': 30 * 24 * 60
|
41 |
+
}
|
42 |
+
|
43 |
+
# Get the future time based on the selected time frame
|
44 |
+
future_time = model.make_future_dataframe(periods=future_periods[time_frame], freq='T')
|
45 |
+
forecast = model.predict(future_time)
|
46 |
+
|
47 |
+
# Display the forecasted data
|
48 |
+
forecast_table = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(future_periods[time_frame])
|
49 |
+
|
50 |
+
# Create a Plotly graph
|
51 |
+
fig = go.Figure()
|
52 |
+
fig.add_trace(go.Scatter(
|
53 |
+
x=forecast['ds'], y=forecast['yhat'],
|
54 |
+
mode='lines+markers',
|
55 |
+
name='Forecasted Sales',
|
56 |
+
line=dict(color='orange'),
|
57 |
+
marker=dict(size=6),
|
58 |
+
hovertemplate='Date: %{x}<br>Forecasted Sales: %{y}<extra></extra>'
|
59 |
+
))
|
60 |
+
|
61 |
+
fig.update_layout(
|
62 |
+
title='Sales Forecast using Prophet',
|
63 |
+
xaxis_title='Date and Time',
|
64 |
+
yaxis_title='Sales Price',
|
65 |
+
xaxis=dict(tickformat="%Y-%m-%d %H:%M"),
|
66 |
+
yaxis=dict(autorange=True)
|
67 |
+
)
|
68 |
+
|
69 |
+
return forecast_table, fig
|
70 |
+
|
71 |
+
# Gradio interface
|
72 |
+
def run_gradio():
|
73 |
+
# Create the Gradio Interface
|
74 |
+
time_options = ['24 hours', '7 days', '10 days', '15 days', '1 month']
|
75 |
+
gr.Interface(
|
76 |
+
fn=predict_sales, # Function to be called
|
77 |
+
inputs=gr.components.Dropdown(time_options, label="Select Forecast Time Range"), # User input
|
78 |
+
outputs=[
|
79 |
+
gr.components.Dataframe(label="Forecasted Sales Table"), # Forecasted data in tabular form
|
80 |
+
gr.components.Plot(label="Sales Forecast Plot") # Plotly graph output
|
81 |
+
],
|
82 |
+
title="Sales Forecasting with Prophet",
|
83 |
+
description="Select a time range for the forecast and click on the button to train the model and see the results."
|
84 |
+
).launch(debug=True)
|
85 |
+
|
86 |
+
# Run the Gradio interface
|
87 |
+
if __name__ == '__main__':
|
88 |
+
run_gradio()
|