File size: 10,313 Bytes
2f0f726
 
8c3f5a7
79e3022
35adeb3
45187e0
3bb9ebe
9c07e19
45187e0
 
 
 
 
 
 
 
 
 
 
 
40ce9c1
2f0f726
40ce9c1
2f0f726
 
 
 
 
 
 
3718a42
40ce9c1
fc6ef93
 
 
 
2f0f726
45187e0
 
 
 
 
40ce9c1
 
 
 
 
 
 
 
 
 
 
 
 
45187e0
 
e9721f6
8f0d526
d2d6920
8f0d526
 
d2d6920
fc6ef93
 
 
2f0f726
5acb78d
40ce9c1
2f0f726
0d8f5f1
5acb78d
79e3022
fc6ef93
 
5acb78d
 
 
 
79e3022
 
f24a8bf
 
7cfd0fd
f24a8bf
79e3022
40ce9c1
 
 
 
 
fc6ef93
 
d2d6920
8f0d526
d2d6920
 
 
e9721f6
f24a8bf
3718a42
20626f2
e176be5
 
 
 
 
 
 
 
 
5acb78d
fc6ef93
2f0f726
 
4f5b63d
 
 
2f0f726
 
4a8e90e
2f963ad
b2681d4
 
fc6ef93
b2681d4
 
01cc10c
018a213
01cc10c
db8fd46
91a246e
9df1d23
01cc10c
b2681d4
4371ba2
 
 
 
 
 
fc6ef93
4371ba2
 
 
 
 
 
 
fc6ef93
4371ba2
 
 
 
91a246e
9df1d23
 
4186d32
5acb78d
40ce9c1
01cc10c
 
 
 
 
 
 
40ce9c1
292df70
91a246e
4186d32
 
91a246e
 
fc6ef93
01cc10c
9df1d23
01cc10c
292df70
5acb78d
292df70
e5238c4
 
badba95
 
91a246e
9df1d23
 
 
 
 
91a246e
 
9df1d23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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)