chrisclark1016 commited on
Commit
46a5516
·
1 Parent(s): 565f529

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -28
app.py CHANGED
@@ -16,7 +16,7 @@ from category_encoders import TargetEncoder
16
  from datasets import Dataset, load_dataset
17
 
18
 
19
- ##Load DFs
20
 
21
  #Set up DF for filtered beers
22
  bb_df = pd.read_csv('beer_brewery_imputed.csv')
@@ -36,14 +36,14 @@ with open('unique_brewery_file.pickle', 'rb') as file:
36
 
37
 
38
  # load the regressor model from disk
39
- loaded_model_regressor = pickle.load(open("XGB_Untappd_regressor_Brewery_Final.pkl", 'rb'))
40
  # load the classification model from disk
41
- loaded_model = pickle.load(open("XGB_Untappd_4_classifier_Brewery_Final.pkl", 'rb'))
42
 
43
 
44
  #Categorical Variable Encoder
45
- loaded_enc_regressor = pickle.load(open("target_encoder_regressor_brewery_final.pkl", 'rb'))
46
- loaded_enc_classification = pickle.load(open("target_encoder_classification_brewery_final.pkl", 'rb'))
47
 
48
  #Define Choices
49
  region_choices = ['Far West','Great Lakes','Mideast','Non-Con','Northeast','OTHER','Plains','Rocky Mountain','Southeast','Southwest']
@@ -104,7 +104,7 @@ hops_list = ['Amarillo', 'Cascade', 'Centennial', 'Chinook', 'Citra', 'Columbus'
104
  explainer = shap.Explainer(loaded_model_regressor) # PLEASE DO NOT CHANGE THIS.
105
 
106
 
107
- #Extract Lists for Examples Function
108
  def extract_selected_items(row, items_list, prefix):
109
  selected_items = []
110
  for item in items_list:
@@ -138,13 +138,13 @@ def filter_beers(style, state):
138
  # percentile_style_overall = 0
139
  # percentile_style_state = 0
140
 
141
- def main_func(BeerName, Brewery, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group):
142
 
143
  flavors_selected = [flavor for flavor in flavors_list if flavor in Flavor_Group]
144
  hops_selected = [hop for hop in hops_list if hop in Hop_Group]
145
 
146
- new_row = pd.DataFrame(columns=['Brewery', 'ABV', 'IBU', 'Style', 'Brewery Style', 'Region', 'State'] + flavors_list + hops_list)
147
- new_row.loc[0] = [Brewery, float(ABV), float(IBU), Style, BreweryStyle, Region, State] + [1 if flavor in Flavor_Group else 0 for flavor in flavors_list] + [1 if hop in Hop_Group else 0 for hop in hops_list]
148
  new_row[['ABV', 'IBU']] = new_row[['ABV', 'IBU']].astype(float)
149
  new_row['ABV'] = new_row['ABV']/100
150
 
@@ -200,6 +200,16 @@ def main_func(BeerName, Brewery, ABV, IBU, Style, BreweryStyle, Region, State, F
200
 
201
  }
202
 
 
 
 
 
 
 
 
 
 
 
203
 
204
 
205
  return local_plot, similar_beers,score_predict_str,percentile_dict0
@@ -215,8 +225,12 @@ def main_func(BeerName, Brewery, ABV, IBU, Style, BreweryStyle, Region, State, F
215
  ## Create the UI
216
  title = "<center><b>🍻 **Untappd Beer Rating Predictor**🍻</b></center>"
217
  description1 = """
218
- This app predicts beers scores based on Untappd data pulled in June 2023. <b> Mean Average Error (MAE) of .09</b> and <b> Root Mean Squared Error (RMSE) of .13</b>.
219
- The input variables in this model <b> explain 77% of the variation </b>in the Untappd beer score """
 
 
 
 
220
 
221
  theme = gr.themes.Default()#primary_hue="amber"
222
 
@@ -230,19 +244,28 @@ with gr.Blocks(title=title, theme = theme) as demo:
230
  submit_btn1 = gr.Button("Predict")
231
  with Row():
232
  with Column():
 
 
 
 
 
 
 
 
 
 
233
 
234
 
235
  BeerName = Textbox(label='Beer Name (not required)', value = 'New Beer 1')
236
- Brewery = Dropdown(choices=unique_breweries_list, label="Brewery", value='Aslin Beer Company')
237
  ABV = Slider(label="ABV %", minimum=0, maximum=20, value=4.5, step=.1)
238
  IBU = Slider(label="IBU", minimum=0.0, maximum=200, value=41, step=1)
239
- Style = Dropdown(choices=style_choices, label="Beer Style", value='IPA - Session')
240
- BreweryStyle = Dropdown(choices=brewery_style_choices, label="Brewery Style", value='Regional Brewery')
241
- Region = Dropdown(choices=region_choices, label="USA Region", value='Southeast')
242
- State = Dropdown(choices=state_choices, label="State", value='GA')
243
  # Grouped checkboxes
244
- Flavor_Group = CheckboxGroup(choices=flavors_list, label="Flavors")
245
- Hop_Group = CheckboxGroup(choices=hops_list, label="Hops")
246
 
247
 
248
  #CREATE OUTPUTS
@@ -280,31 +303,26 @@ with gr.Blocks(title=title, theme = theme) as demo:
280
  #Button Click Events
281
  submit_btn1.click(
282
  main_func,
283
- [Brewery,BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
284
  [local_plot,similar_beers,score_predict_str,percentile_dict0],
285
  api_name="Untappd_Rating_Model")
286
 
287
  submit_btn2.click(
288
  main_func,
289
- [Brewery,BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
290
  [local_plot,similar_beers,score_predict_str,percentile_dict0],
291
  api_name="Untappd_Rating_Model1")
292
-
293
-
294
 
295
  #EXAMPLES
296
  #example_list = aslin_example_df.values.tolist()
297
 
298
-
299
-
300
- # Create examples
301
  examples = []
302
  for _, row in aslin_example_df.iterrows():
303
  example = []
304
 
305
  # Add other inputs to example here
306
  example.append(row['Beer Name'])
307
- example.append(row['Brewery'])
308
  example.append(row['ABV'])
309
  example.append(row['IBU'])
310
  example.append(row['Style'])
@@ -317,10 +335,10 @@ with gr.Blocks(title=title, theme = theme) as demo:
317
  example.append(extract_selected_items(row, hops_list, ''))
318
 
319
  examples.append(example)
320
-
321
  gr.Markdown("<h1><center><b>Aslin Beers Example Inputs</b></center></h1>")
322
  gr.Examples(examples[:10],
323
- [BeerName, Brewery, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group], # Flavor_Group, Hop_Group
324
  [local_plot, similar_beers, score_predict_str,percentile_dict0],
325
  main_func,
326
  cache_examples=True, label = "Aslin Beer List")
 
16
  from datasets import Dataset, load_dataset
17
 
18
 
19
+ #Load DFs
20
 
21
  #Set up DF for filtered beers
22
  bb_df = pd.read_csv('beer_brewery_imputed.csv')
 
36
 
37
 
38
  # load the regressor model from disk
39
+ loaded_model_regressor = pickle.load(open("XGB_Untappd_regressor_FlavorBreakout.pkl", 'rb'))
40
  # load the classification model from disk
41
+ loaded_model = pickle.load(open("XGB_Untappd_4_classifier_FlavorBreakout.pkl", 'rb'))
42
 
43
 
44
  #Categorical Variable Encoder
45
+ loaded_enc_regressor = pickle.load(open("target_encoder_regressor_flavorbreakout.pkl", 'rb'))
46
+ loaded_enc_classification = pickle.load(open("target_encoder_classification_flavorbreakout.pkl", 'rb'))
47
 
48
  #Define Choices
49
  region_choices = ['Far West','Great Lakes','Mideast','Non-Con','Northeast','OTHER','Plains','Rocky Mountain','Southeast','Southwest']
 
104
  explainer = shap.Explainer(loaded_model_regressor) # PLEASE DO NOT CHANGE THIS.
105
 
106
 
107
+ #Extract Lists for Examples Function for checkboxes in examples
108
  def extract_selected_items(row, items_list, prefix):
109
  selected_items = []
110
  for item in items_list:
 
138
  # percentile_style_overall = 0
139
  # percentile_style_state = 0
140
 
141
+ def main_func(BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group):
142
 
143
  flavors_selected = [flavor for flavor in flavors_list if flavor in Flavor_Group]
144
  hops_selected = [hop for hop in hops_list if hop in Hop_Group]
145
 
146
+ new_row = pd.DataFrame(columns=['ABV', 'IBU', 'Style', 'Brewery Style', 'Region', 'State'] + flavors_list + hops_list)
147
+ new_row.loc[0] = [float(ABV), float(IBU), Style, BreweryStyle, Region, State] + [1 if flavor in Flavor_Group else 0 for flavor in flavors_list] + [1 if hop in Hop_Group else 0 for hop in hops_list]
148
  new_row[['ABV', 'IBU']] = new_row[['ABV', 'IBU']].astype(float)
149
  new_row['ABV'] = new_row['ABV']/100
150
 
 
200
 
201
  }
202
 
203
+ # percentile_dict1 = {"Percentile Overall": percentile_overall}
204
+ # percentile_dict2 = {f"Percentile {nr_state_p}": percentile_state}
205
+ # percentile_dict3 = {f"Percentile {nr_style_p} Overall": percentile_style_overall}
206
+ # percentile_dict4 = {f"Percentile {nr_style_p} {nr_state_p}": percentile_style_state}
207
+
208
+ # #Convert to dataframe
209
+ # percentile_df = pd.DataFrame(
210
+ # { "type": ["USA Overall",f"{nr_state_p} Overall",f"USA by Style {nr_style_p}",f"{nr_state_p} by Style {nr_style_p}"],
211
+ # "value": [percentile_overall,percentile_state,percentile_style_overall,percentile_style_state],})
212
+
213
 
214
 
215
  return local_plot, similar_beers,score_predict_str,percentile_dict0
 
225
  ## Create the UI
226
  title = "<center><b>🍻 **Untappd Beer Rating Predictor**🍻</b></center>"
227
  description1 = """
228
+ This app predicts beers scores based on Untappd data pulled in June 2023. <b> Mean Average Error (MAE) of .12</b> and <b> Root Mean Squared Error (RMSE) of .16</b>.
229
+ The input variables in this model <b> explain 65% of the variation </b>in the Untappd beer score """
230
+
231
+ # description2 = """
232
+ # To use the app, click on one of the examples, or adjust the values of the seven beer score predictors, and click on Analyze. ✨
233
+ # """
234
 
235
  theme = gr.themes.Default()#primary_hue="amber"
236
 
 
244
  submit_btn1 = gr.Button("Predict")
245
  with Row():
246
  with Column():
247
+ # BeerName = gr.components.Textbox(label='Beer Name (not required)', value = 'New Beer 1')
248
+ # ABV = gr.components.Slider(label="ABV %", minimum=0, maximum=20, value=4.5, step=.1)
249
+ # IBU = gr.components.Slider(label="IBU", minimum=0.0, maximum=200, value=41, step=1)
250
+ # Style = gr.components.Dropdown(choices=style_choices, label="Select Beer Style", value= 'IPA - Session')
251
+ # BreweryStyle = gr.components.Dropdown(choices=brewery_style_choices, label="Select Brewery Style", value= 'Micro Brewery')
252
+ # Region = gr.components.Dropdown(choices=region_choices, label="Select USA Region", value= 'Far West')
253
+ # State = gr.components.Dropdown(choices=state_choices, label="Select State", value= 'CA')
254
+ # # Grouped checkboxes
255
+ # Flavor_Group = gr.components.CheckboxGroup(choices=flavors_list, label="Select Flavors")
256
+ # Hop_Group = gr.components.CheckboxGroup(choices=hops_list, label="Select Hops")
257
 
258
 
259
  BeerName = Textbox(label='Beer Name (not required)', value = 'New Beer 1')
 
260
  ABV = Slider(label="ABV %", minimum=0, maximum=20, value=4.5, step=.1)
261
  IBU = Slider(label="IBU", minimum=0.0, maximum=200, value=41, step=1)
262
+ Style = Dropdown(choices=style_choices, label="Select Beer Style", value='IPA - Session')
263
+ BreweryStyle = Dropdown(choices=brewery_style_choices, label="Select Brewery Style", value='Micro Brewery')
264
+ Region = Dropdown(choices=region_choices, label="Select USA Region", value='Far West')
265
+ State = Dropdown(choices=state_choices, label="Select State", value='CA')
266
  # Grouped checkboxes
267
+ Flavor_Group = CheckboxGroup(choices=flavors_list, label="Select Flavors")
268
+ Hop_Group = CheckboxGroup(choices=hops_list, label="Select Hops")
269
 
270
 
271
  #CREATE OUTPUTS
 
303
  #Button Click Events
304
  submit_btn1.click(
305
  main_func,
306
+ [BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
307
  [local_plot,similar_beers,score_predict_str,percentile_dict0],
308
  api_name="Untappd_Rating_Model")
309
 
310
  submit_btn2.click(
311
  main_func,
312
+ [BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
313
  [local_plot,similar_beers,score_predict_str,percentile_dict0],
314
  api_name="Untappd_Rating_Model1")
 
 
315
 
316
  #EXAMPLES
317
  #example_list = aslin_example_df.values.tolist()
318
 
319
+ # Create examples
 
 
320
  examples = []
321
  for _, row in aslin_example_df.iterrows():
322
  example = []
323
 
324
  # Add other inputs to example here
325
  example.append(row['Beer Name'])
 
326
  example.append(row['ABV'])
327
  example.append(row['IBU'])
328
  example.append(row['Style'])
 
335
  example.append(extract_selected_items(row, hops_list, ''))
336
 
337
  examples.append(example)
338
+
339
  gr.Markdown("<h1><center><b>Aslin Beers Example Inputs</b></center></h1>")
340
  gr.Examples(examples[:10],
341
+ [BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group], # Flavor_Group, Hop_Group
342
  [local_plot, similar_beers, score_predict_str,percentile_dict0],
343
  main_func,
344
  cache_examples=True, label = "Aslin Beer List")