Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
|
26 |
-
#
|
27 |
-
|
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."
|