smg123's picture
Update app.py
fc6ef93 verified
import pandas as pd
import pickle
import joblib
from datetime import datetime
def add_new_columns(df2):
df = df2.copy()
# easy to implement
df['day_of_week'] = df['Date'].dt.dayofweek
df['day_of_month'] = df['Date'].dt.day
df['last_weekend'] = ((df['day_of_week'] == 4) | (df['day_of_week'] == 5) | (df['day_of_week'] == 6)) & (df['day_of_month'] + 7 > df['Date'].dt.days_in_month)
df['Day_of_week_numeric'] = df['Date'].dt.dayofweek
df['Day_of_year_numeric'] = df['Date'].dt.dayofyear
df['Week_numeric'] = df['Date'].dt.isocalendar().week
# df['Avg_Sale_Weekday'] = df.groupby(['Location', df['Date'].dt.dayofweek])['Number_of_burgers'].transform('mean')
return df
def predict_without_columns(X, yhat, day,month,year,temp,rain,wind,sun,snow, day_of_the_week,location,parken_event, general_event,is_holiday,rf,gb,hgb,xgr,lgbr,reg):
new_row = {name:0 for name in X}
new_row['yhat'] = yhat
new_row['Day'] = day
new_row['Month'] = month
new_row['Year'] = year
new_row['Temperature'] = temp
new_row['Wind'] = wind
new_row['Rain'] = rain
new_row['Sun'] = sun
new_row['Snow'] = snow
new_row['holiday'] = is_holiday
new_row['Day_of_the_week_' + day_of_the_week] = 1
new_row['Location_' + location] = 1
new_row['Parken_Event_' + parken_event] = 1
new_row['General_Event_' + general_event] = 1
new_pd = pd.DataFrame([new_row])
new_pd['Date'] = pd.to_datetime(new_pd[['Year', 'Month', 'Day']])
new_pd = add_new_columns(new_pd)
new_pd.drop('Date', axis=1, inplace=True)
columns = ['yhat', 'Day', 'Month', 'Year', 'Temperature', 'Rain', 'Wind', 'Sun',
'Snow', 'day_of_week', 'day_of_month',
'last_weekend', 'Day_of_week_numeric', 'Day_of_year_numeric',
'Week_numeric', 'holiday', 'Day_of_the_week_Friday',
'Day_of_the_week_Monday', 'Day_of_the_week_Saturday',
'Day_of_the_week_Sunday', 'Day_of_the_week_Thursday',
'Day_of_the_week_Tuesday', 'Day_of_the_week_Wednesday', 'Location_DGH',
'Location_HDM', 'Location_HPNS', 'Location_LGV', 'Location_NHG',
'Location_VDV', 'Parken_Event_Concert', 'Parken_Event_Football',
'Parken_Event_None', 'General_Event_Cultural', 'General_Event_Fashion',
'General_Event_Festivitie', 'General_Event_None',
'General_Event_Social', 'General_Event_Sport',
'General_Event_Worldwide']
new_pd = new_pd[columns]
prediction_reg = reg.predict(new_pd)
prediction_rf = rf.predict(new_pd)
prediction_xgr = xgr.predict(new_pd)
prediction_gb = gb.predict(new_pd)
prediction_hgb = hgb.predict(new_pd)
prediction_lgbr = lgbr.predict(new_pd)
mean_pred = (prediction_reg + prediction_xgr + prediction_lgbr + prediction_hgb + prediction_gb) / 6
output = 'On ' + str(day_of_the_week) + ' , ' + str(day) + ' - ' + str(month) + ' - ' + str(year) + ', the location ' + str(location) + ' is predicting to sell ' + str(round(mean_pred[0])) + ' burgers'
return output
def predict(day,month,year,location,temp,rain,wind,sun,parken_event,general_event):
snow=0
day, month, year = int(day), int(month), int(year)
temp, rain, wind, sun, snow = float(temp), float(rain), float(wind), float(sun), float(snow)
location, parken_event, general_event = str(location), str(parken_event), str(general_event)
date_obj = datetime(year, month, day)
day_of_week_num = date_obj.weekday()
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
day_of_the_week = days[day_of_week_num]
prophet_file = f'prophet_df_{location}.csv'
df_prophet = pd.read_csv(prophet_file)
df_prophet = df_prophet[['ds','yhat']].copy()
df_prophet.set_index('ds',inplace=True)
date_string = f'{year}-{month}-{day}'
datetime_obj = str(datetime.strptime(date_string, '%Y-%m-%d').date())
yhat = df_prophet.loc[datetime_obj]['yhat']
holidays_dk = pd.read_csv('holidays_denmark.csv')
holidays_dk = holidays_dk[['ds','holiday']].copy()
holidays_dk.set_index('ds',inplace=True)
is_holiday = 1 if datetime_obj in holidays_dk.index else 0
print(is_holiday)
rf = joblib.load("filename_rf.joblib")
gb = joblib.load("filename_gb.joblib")
hgb = joblib.load("filename_hgb.joblib")
xgr = joblib.load("filename_xgr.joblib")
lgbr = joblib.load("filename_lgbr.joblib")
reg = joblib.load("filename_reg.joblib")
X = ['yhat', 'Day', 'Month', 'Year', 'Temperature', 'Rain', 'Wind', 'Sun', 'Snow',
'Day_of_the_week_Friday', 'Day_of_the_week_Monday',
'Day_of_the_week_Saturday', 'Day_of_the_week_Sunday',
'Day_of_the_week_Thursday', 'Day_of_the_week_Tuesday',
'Day_of_the_week_Wednesday', 'Location_DGH', 'Location_HDM',
'Location_HPNS', 'Location_LGV', 'Location_NHG', 'Location_VDV',
'Parken_Event_Concert', 'Parken_Event_Football', 'Parken_Event_None',
'General_Event_Cultural', 'General_Event_Fashion',
'General_Event_Festivitie', 'General_Event_None',
'General_Event_Social', 'General_Event_Sport',
'General_Event_Worldwide']
actual_prediction = predict_without_columns(X,yhat,day,month,year,temp,rain,wind,sun,snow,day_of_the_week,location,parken_event, general_event,is_holiday,rf,gb,hgb,xgr,lgbr,reg)
return actual_prediction
import gradio as gr
theme = gr.themes.Soft(
primary_hue="green",
secondary_hue="emerald"
)
# Launch a Gradio interface
title = 'Predicting number of burgers - Gasoline Grill'
description = 'This is an AI model that predicts the number of burgers based on different parameters. These parameters are:\n1. Day: current day of the month (from 1 to 31)\n2. Month: Current month (from 1 to 12)\n3. Year: current year.\n4. Day of the week: current day of the week (from Monday to Sunday)\n5. Location: current location (LGV, NHG, HDM, VDV, DGH, HPNS)\n6. Temperature: expected medium temperature of the day (in ºC)\n7. Rain: expected rain of the day (in mm)\n8. Wind: expected wind of the day (in m/s)\n9. Sun: expected hours of sunlight of the day (in hours)\n10. Parken event: Is there an event in Parken? (Football, Concert, None)\n11. General Event: Is there a general event in Copenhagen? (Cultural, Fashion, Festivity, None, Social, Sport, Worldwide)\n\nFor temperature, wind, rain and sun, check the DMI webpage https://www.dmi.dk/vejrarkiv or check the weather app on your phone :)\n\nOnce you have all the data, you must put it in order, separated by commas and without spaces between the data.\n\nSome examples are shown below. Click on them to see a real example'
examples = ['12,2,2024,Monday,VDV,1.8,5.6,2.4,0,None,None','29,5,2024,Wednesday,LGV,10,2,1.4,5,Football,Social']
# demo = gr.Interface(fn=predict, title=title, description=description)
with gr.Blocks(theme=theme) as demo:
with gr.Row():
gr.Markdown(
"""
# Predicting number of burgers - Gasoline Grill 🍔
This is an AI model that predicts the number of burgers to be sold based on different parameters. These parameters are:
1. Day, Month , Year
2. Location (LGV, DGH, NHG, HDM, VDV, HPNS)
3. Temperature: expected medium temperature of the day (in ºC)
4. Rain: expected rain of the day (in mm)
5. Wind: expected wind of the day (in m/s)
6. Sun: expected hours of sunlight of the day (in hours)
7. Parken event: Is there an event in Parken?
8. General Event: Is there a general event in Copenhagen?
For temperature, wind, rain and sun, check the DMI webpage https://www.dmi.dk/vejrarkiv or the weather app on your phone :)
Once you have all the data, click the Predict button.
"""
)
with gr.Row():
day = gr.Dropdown([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,21,22,23,24,25,26,27,28,29,30,31],label='Day')
month = gr.Dropdown([1,2,3,4,5,6,7,8,9,10,11,12],label='Month')
year = gr.Dropdown([2024],label='Year', value=2024)
# day_of_the_week = gr.Dropdown(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], label='Day of the week')
location = gr.Dropdown(['LGV', 'DGH','NHG' ,'HDM', 'VDV','HPNS'], label='Location')
with gr.Row():
with gr.Column():
temp = gr.Slider(minimum=-10, maximum=40, label='Temperature (°C)')
sun = gr.Slider(minimum=0, maximum=17, label='Sunlight (hours)')
with gr.Column():
rain = gr.Slider(minimum=0, maximum=30, label='Rain (mm)')
wind = gr.Slider(minimum=0, maximum=12, label='Wind (m/s)')
# snow = gr.Slider(minimum=0, maximum=3, label='Snow')
with gr.Row():
parken_event = gr.Dropdown(['Football', 'Concert', 'None'], label='Parken Event', value='None')
general_event = gr.Dropdown(['Cultural', 'Fashion', 'Festivity', 'None', 'Social', 'Sport', 'Worldwide'], label='General Event', value='None')
with gr.Row():
predict_2 = gr.Button(value = 'Predict')
with gr.Column():
prediction = gr.Textbox()
predict_2.click(predict, inputs = [day,month,year,location,temp,rain,wind,sun,parken_event,general_event], outputs = prediction)
# demo.launch(auth=('gasolgrill','gasolgrill'))
demo.launch()
# gr.Interface(predict,
# inputs = [
# gr.Row([gr.Textbox(label='Day'),gr.Textbox(label='Month'),gr.Textbox(label='Year'),gr.Dropdown(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], label='Day of the week'),gr.Dropdown(['LGV', 'NHG', 'DGH', 'HDM', 'VDV'], label='Location')]),
# gr.Row([gr.Slider(minimum=-10, maximum=40, label='Temperature (°C)'), gr.Slider(minimum=0, maximum=17, label='Sunlight (hours)'), gr.Slider(minimum=0, maximum=30, label='Rain (mm)'), gr.Slider(minimum=0, maximum=12, label='Wind (m/s)')]),
# gr.Row([gr.Dropdown(['Football', 'Concert', 'None'], label='Parken Event'), gr.Dropdown(['Cultural', 'Fashion', 'Festivity', 'None', 'Social', 'Sport', 'Worldwide'], label='General Event')]),
# ],
# outputs="textbox",
# title=title, description = description
# ).launch(share=True)