Rehman1603 commited on
Commit
c3b38ee
·
verified ·
1 Parent(s): d69c662

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -3,30 +3,48 @@ 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('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({
@@ -121,7 +139,7 @@ def run_gradio():
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",
127
  description="Select a time range for the forecast and click on the button to train the model and see the results."
 
3
  import gradio as gr
4
  import plotly.graph_objs as go
5
  import numpy as np
6
+ import requests
7
  # Function to train the model and generate forecast
8
  def predict_sales(time_frame):
9
+ #login
10
+ url="https://livesystem.hisabkarlay.com/auth/login"
11
+ payload={
12
+ 'username':'user@123',
13
+ 'password':'user@123',
14
+ 'client_secret':'kNqJjlPkxyHdIKt3szCt4PYFWtFOdUheb8QVN8vQ',
15
+ 'client_id':'5',
16
+ 'grant_type':'password'
17
+ }
18
+ response=requests.post(url,data=payload)
19
+ #print(response.text)
20
+ access_token=response.json()['access_token']
21
+ #print(access_token)
22
+ #fetch all sell data
23
+ per_page=-1
24
+ url=f"https://livesystem.hisabkarlay.com/connector/api/sell?per_page={per_page}"
25
+ headers={
26
+ 'Authorization':f'Bearer {access_token}'
27
+ }
28
+ response=requests.get(url,headers=headers)
29
+ data=response.json()['data']
30
+ date=[]
31
+ amount=[]
32
+ for item in data:
33
+ date.append(item.get('transaction_date'))
34
+ amount.append(float(item.get('final_total')))
35
+ data_dict={
36
+ 'date':date,
37
+ 'amount':amount
38
+ }
39
+ data_frame=pd.DataFrame(data_dict)
40
+ # Convert 'date' column to datetime format
41
+ data_frame['date'] = pd.to_datetime(data_frame['date'])
42
 
43
+ # Extract only the date part
44
+ data_frame['date_only'] = data_frame['date'].dt.date
45
 
46
+ # Group by date and calculate total sales
47
+ daily_sales = data_frame.groupby('date_only').agg(total_sales=('amount', 'sum')).reset_index()
 
 
48
 
49
  # Prepare the DataFrame for Prophet
50
  df = pd.DataFrame({
 
139
  outputs=[
140
  gr.components.Dataframe(label="Forecasted Sales Table"), # Forecasted data in tabular form
141
  gr.components.Dataframe(label="Weekend Forecasted Sales Table"), # Weekend forecast data
142
+ gr.components.Plot(label="Sales Forecast Plot",min_width=500,scale=2) # Plotly graph output
143
  ],
144
  title="Sales Forecasting with Prophet",
145
  description="Select a time range for the forecast and click on the button to train the model and see the results."