Rehman1603 commited on
Commit
b335a84
·
verified ·
1 Parent(s): 782e8d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -31
app.py CHANGED
@@ -2,57 +2,79 @@ 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('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
- last_date_value = df['Date'].iloc[-2]
34
  # Future forecast based on the time frame
35
  future_periods = {
36
- '7 days': 7 ,
37
- '10 days': 10 ,
38
- '15 days': 15 ,
39
- '1 month': 30
 
40
  }
41
 
42
- # Get the future time based on the selected time frame
43
- future_time = model.make_future_dataframe(periods=future_periods[time_frame])
44
- current_time = pd.Timestamp.now()
45
 
46
- # Set the last historical date in the format 'MM/DD/YYYY HH:MM'
47
- last_historical_date = pd.to_datetime(last_date_value, format='%m/%d/%Y %H:%M')
48
 
49
- # Filter future_time to include rows from the current time and onwards, including future hours and minutes
50
- future_only = future_time[(future_time['ds'] >= current_time) & (future_time['ds'] > last_historical_date)]
51
  forecast = model.predict(future_only)
52
 
53
- # Display the forecasted data
54
- forecast_table = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(future_periods[time_frame])
55
-
 
 
 
 
 
 
 
 
 
 
56
  # Create a Plotly graph
57
  fig = go.Figure()
58
  fig.add_trace(go.Scatter(
@@ -63,26 +85,42 @@ def predict_sales(time_frame):
63
  marker=dict(size=6),
64
  hovertemplate='Date: %{x}<br>Forecasted Sales: %{y}<extra></extra>'
65
  ))
66
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  fig.update_layout(
68
  title='Sales Forecast using Prophet',
69
- xaxis_title='Date and Time',
70
  yaxis_title='Sales Price',
71
- xaxis=dict(tickformat="%Y-%m-%d %H:%M"),
72
  yaxis=dict(autorange=True)
73
  )
74
 
75
- return forecast_table, fig
76
 
77
  # Gradio interface
78
  def run_gradio():
79
  # Create the Gradio Interface
80
- time_options = ['7 days', '10 days', '15 days', '1 month']
81
  gr.Interface(
82
  fn=predict_sales, # Function to be called
83
  inputs=gr.components.Dropdown(time_options, label="Select Forecast Time Range"), # User input
84
  outputs=[
85
  gr.components.Dataframe(label="Forecasted Sales Table"), # Forecasted data in tabular form
 
86
  gr.components.Plot(label="Sales Forecast Plot") # Plotly graph output
87
  ],
88
  title="Sales Forecasting with Prophet",
 
2
  from prophet import Prophet
3
  import gradio as gr
4
  import plotly.graph_objs as go
5
+ import numpy as np
6
 
7
  # Function to train the model and generate forecast
8
  def predict_sales(time_frame):
9
+ all_sales_data = pd.read_csv('/content/All sales - House of Pizza.csv')
10
 
11
+ # Clean up the 'Total paid' column by splitting based on '₨' symbol and converting to float
12
+ def clean_total_paid(val):
13
+ if isinstance(val, str): # Only process if the value is a string
14
+ amounts = [float(x.replace(',', '').strip()) for x in val.split('₨') if x.strip()]
15
+ return sum(amounts) # Sum if multiple values exist
16
+ elif pd.isna(val): # Handle NaN values
17
+ return 0.0
18
+ return val # If it's already a float, return it as-is
19
+
20
+ # Apply the cleaning function to the 'Total paid' column
21
+ all_sales_data['Total paid'] = all_sales_data['Total paid'].apply(clean_total_paid)
22
 
23
  # Convert the 'Date' column to datetime, coercing errors
24
  all_sales_data['Date'] = pd.to_datetime(all_sales_data['Date'], format='%m/%d/%Y %H:%M', errors='coerce')
25
 
26
  # Drop rows with invalid dates
27
  all_sales_data = all_sales_data.dropna(subset=['Date'])
28
+ all_sales_data['date_only'] = all_sales_data['Date'].dt.date
29
+ daily_sales = all_sales_data.groupby('date_only').agg(total_sales=('Total paid', 'sum')).reset_index()
30
 
31
+ # Prepare the DataFrame for Prophet
32
  df = pd.DataFrame({
33
+ 'Date': daily_sales['date_only'],
34
+ 'Total paid': daily_sales['total_sales']
35
  })
36
 
37
+ # Apply log transformation
38
+ df['y'] = np.log1p(df['Total paid']) # Using log1p to avoid log(0)
39
+
40
  # Prepare Prophet model
41
+ model = Prophet(weekly_seasonality=True) # Enable weekly seasonality
42
  df['ds'] = df['Date']
 
43
  model.fit(df[['ds', 'y']])
44
+
45
  # Future forecast based on the time frame
46
  future_periods = {
47
+ 'Next Day': 1,
48
+ '7 days': 7,
49
+ '10 days': 10,
50
+ '15 days': 15,
51
+ '1 month': 30
52
  }
53
 
54
+ # Get the last historical date and calculate the start date for the forecast
55
+ last_date_value = df['Date'].iloc[-1]
56
+ forecast_start_date = pd.Timestamp(last_date_value) + pd.Timedelta(days=1) # Start the forecast from the next day
57
 
58
+ # Generate the future time DataFrame starting from the day after the last date
59
+ future_time = model.make_future_dataframe(periods=future_periods[time_frame], freq='D')
60
 
61
+ # Filter future_time to include only future dates starting from forecast_start_date
62
+ future_only = future_time[future_time['ds'] >= forecast_start_date]
63
  forecast = model.predict(future_only)
64
 
65
+ # Exponentiate the forecast to revert back to the original scale
66
+ forecast['yhat'] = np.expm1(forecast['yhat']) # Use expm1 to handle the log transformation
67
+ forecast['yhat_lower'] = np.expm1(forecast['yhat_lower']) # Exponentiate lower bound
68
+ forecast['yhat_upper'] = np.expm1(forecast['yhat_upper']) # Exponentiate upper bound
69
+
70
+ # Create a DataFrame for weekends only
71
+ forecast['day_of_week'] = forecast['ds'].dt.day_name() # Get the day name from the date
72
+ weekends = forecast[forecast['day_of_week'].isin(['Saturday', 'Sunday'])] # Filter for weekends
73
+
74
+ # Display the forecasted data for the specified period
75
+ forecast_table = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head(future_periods[time_frame])
76
+ weekend_forecast_table = weekends[['ds', 'yhat', 'yhat_lower', 'yhat_upper']] # Weekend forecast
77
+
78
  # Create a Plotly graph
79
  fig = go.Figure()
80
  fig.add_trace(go.Scatter(
 
85
  marker=dict(size=6),
86
  hovertemplate='Date: %{x}<br>Forecasted Sales: %{y}<extra></extra>'
87
  ))
88
+
89
+ # Add lines for yhat_lower and yhat_upper
90
+ fig.add_trace(go.Scatter(
91
+ x=forecast['ds'], y=forecast['yhat_lower'],
92
+ mode='lines',
93
+ name='Lower Bound',
94
+ line=dict(color='red', dash='dash')
95
+ ))
96
+
97
+ fig.add_trace(go.Scatter(
98
+ x=forecast['ds'], y=forecast['yhat_upper'],
99
+ mode='lines',
100
+ name='Upper Bound',
101
+ line=dict(color='green', dash='dash')
102
+ ))
103
+
104
  fig.update_layout(
105
  title='Sales Forecast using Prophet',
106
+ xaxis_title='Date',
107
  yaxis_title='Sales Price',
108
+ xaxis=dict(tickformat="%Y-%m-%d"),
109
  yaxis=dict(autorange=True)
110
  )
111
 
112
+ return forecast_table, weekend_forecast_table, fig # Return the forecast table, weekend forecast, and plot
113
 
114
  # Gradio interface
115
  def run_gradio():
116
  # Create the Gradio Interface
117
+ time_options = ['Next Day', '7 days', '10 days', '15 days', '1 month']
118
  gr.Interface(
119
  fn=predict_sales, # Function to be called
120
  inputs=gr.components.Dropdown(time_options, label="Select Forecast Time Range"), # User input
121
  outputs=[
122
  gr.components.Dataframe(label="Forecasted Sales Table"), # Forecasted data in tabular form
123
+ gr.components.Dataframe(label="Weekend Forecasted Sales Table"), # Weekend forecast data
124
  gr.components.Plot(label="Sales Forecast Plot") # Plotly graph output
125
  ],
126
  title="Sales Forecasting with Prophet",