Spaces:
Sleeping
Sleeping
Commit
·
d217d7f
1
Parent(s):
153a0b5
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,7 @@ bb_df = pd.read_csv('beer_brewery_imputed.csv')
|
|
| 25 |
bb_df_percentile = pd.read_csv('bb_df_testing.csv')
|
| 26 |
|
| 27 |
#Set up DF for Aslin Examples
|
| 28 |
-
aslin_example_df_full = pd.read_csv('
|
| 29 |
aslin_example_df = aslin_example_df_full.drop(['Number of Ratings Beer'], axis=1)
|
| 30 |
aslin_example_df['ABV'] = aslin_example_df['ABV']*100
|
| 31 |
|
|
@@ -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("
|
| 40 |
# load the classification model from disk
|
| 41 |
-
loaded_model = pickle.load(open("
|
| 42 |
|
| 43 |
|
| 44 |
#Categorical Variable Encoder
|
| 45 |
-
loaded_enc_regressor = pickle.load(open("
|
| 46 |
-
loaded_enc_classification = pickle.load(open("
|
| 47 |
|
| 48 |
#Define Choices
|
| 49 |
region_choices = ['Far West','Great Lakes','Mideast','Non-Con','Northeast','OTHER','Plains','Rocky Mountain','Southeast','Southwest']
|
|
@@ -103,6 +103,15 @@ hops_list = ['Amarillo', 'Cascade', 'Centennial', 'Chinook', 'Citra', 'Columbus'
|
|
| 103 |
# Setup SHAP
|
| 104 |
explainer = shap.Explainer(loaded_model_regressor) # PLEASE DO NOT CHANGE THIS.
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
#Set up similar beers function
|
| 107 |
def filter_beers(style, state):
|
| 108 |
target_abv = 0.03 # Set the target ABV value within the function
|
|
@@ -129,13 +138,13 @@ def filter_beers(style, state):
|
|
| 129 |
# percentile_style_overall = 0
|
| 130 |
# percentile_style_state = 0
|
| 131 |
|
| 132 |
-
def main_func(BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group):
|
| 133 |
|
| 134 |
flavors_selected = [flavor for flavor in flavors_list if flavor in Flavor_Group]
|
| 135 |
hops_selected = [hop for hop in hops_list if hop in Hop_Group]
|
| 136 |
|
| 137 |
-
new_row = pd.DataFrame(columns=['ABV', 'IBU', 'Style', 'Brewery Style', 'Region', 'State'] + flavors_list + hops_list)
|
| 138 |
-
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]
|
| 139 |
new_row[['ABV', 'IBU']] = new_row[['ABV', 'IBU']].astype(float)
|
| 140 |
new_row['ABV'] = new_row['ABV']/100
|
| 141 |
|
|
@@ -188,18 +197,9 @@ def main_func(BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Gro
|
|
| 188 |
f"{nr_state_p} Overall": [percentile_state],
|
| 189 |
f"{nr_style_p} in USA": [percentile_style_overall],
|
| 190 |
f"{nr_style_p} in {nr_state_p}": [percentile_style_overall]
|
|
|
|
| 191 |
}
|
| 192 |
|
| 193 |
-
# percentile_dict1 = {"Percentile Overall": percentile_overall}
|
| 194 |
-
# percentile_dict2 = {f"Percentile {nr_state_p}": percentile_state}
|
| 195 |
-
# percentile_dict3 = {f"Percentile {nr_style_p} Overall": percentile_style_overall}
|
| 196 |
-
# percentile_dict4 = {f"Percentile {nr_style_p} {nr_state_p}": percentile_style_state}
|
| 197 |
-
|
| 198 |
-
# #Convert to dataframe
|
| 199 |
-
# percentile_df = pd.DataFrame(
|
| 200 |
-
# { "type": ["USA Overall",f"{nr_state_p} Overall",f"USA by Style {nr_style_p}",f"{nr_state_p} by Style {nr_style_p}"],
|
| 201 |
-
# "value": [percentile_overall,percentile_state,percentile_style_overall,percentile_style_state],})
|
| 202 |
-
|
| 203 |
|
| 204 |
|
| 205 |
return local_plot, similar_beers,score_predict_str,percentile_dict0
|
|
@@ -215,12 +215,8 @@ def main_func(BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Gro
|
|
| 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 .
|
| 219 |
-
The input variables in this model<b> explain
|
| 220 |
-
|
| 221 |
-
# description2 = """
|
| 222 |
-
# To use the app, click on one of the examples, or adjust the values of the seven beer score predictors, and click on Analyze. ✨
|
| 223 |
-
# """
|
| 224 |
|
| 225 |
theme = gr.themes.Default()#primary_hue="amber"
|
| 226 |
|
|
@@ -234,17 +230,19 @@ with gr.Blocks(title=title, theme = theme) as demo:
|
|
| 234 |
submit_btn1 = gr.Button("Predict")
|
| 235 |
with Row():
|
| 236 |
with Column():
|
| 237 |
-
|
|
|
|
| 238 |
BeerName = Textbox(label='Beer Name (not required)', value = 'New Beer 1')
|
|
|
|
| 239 |
ABV = Slider(label="ABV %", minimum=0, maximum=20, value=4.5, step=.1)
|
| 240 |
IBU = Slider(label="IBU", minimum=0.0, maximum=200, value=41, step=1)
|
| 241 |
-
Style = Dropdown(choices=style_choices, label="
|
| 242 |
-
BreweryStyle = Dropdown(choices=brewery_style_choices, label="
|
| 243 |
-
Region = Dropdown(choices=region_choices, label="
|
| 244 |
-
State = Dropdown(choices=state_choices, label="
|
| 245 |
# Grouped checkboxes
|
| 246 |
-
Flavor_Group = CheckboxGroup(choices=flavors_list, label="
|
| 247 |
-
Hop_Group = CheckboxGroup(choices=hops_list, label="
|
| 248 |
|
| 249 |
|
| 250 |
#CREATE OUTPUTS
|
|
@@ -255,7 +253,7 @@ with gr.Blocks(title=title, theme = theme) as demo:
|
|
| 255 |
gr.Markdown("<h2><center><b>Prediction Drivers</b></center></h2>")
|
| 256 |
local_plot = gr.Plot(label = 'Shap:')
|
| 257 |
|
| 258 |
-
gr.Markdown("<h2><center><b>Percentiles for Beer</b></center></h2>")
|
| 259 |
percentile_dict0 = gr.Label(label ='test', show_label=False)
|
| 260 |
|
| 261 |
|
|
@@ -282,21 +280,47 @@ with gr.Blocks(title=title, theme = theme) as demo:
|
|
| 282 |
#Button Click Events
|
| 283 |
submit_btn1.click(
|
| 284 |
main_func,
|
| 285 |
-
[BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
|
| 286 |
[local_plot,similar_beers,score_predict_str,percentile_dict0],
|
| 287 |
api_name="Untappd_Rating_Model")
|
| 288 |
|
| 289 |
submit_btn2.click(
|
| 290 |
main_func,
|
| 291 |
-
[BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
|
| 292 |
[local_plot,similar_beers,score_predict_str,percentile_dict0],
|
| 293 |
api_name="Untappd_Rating_Model1")
|
|
|
|
|
|
|
| 294 |
|
| 295 |
#EXAMPLES
|
| 296 |
-
example_list = aslin_example_df.values.tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
gr.Markdown("<h1><center><b>Aslin Beers Example Inputs</b></center></h1>")
|
| 298 |
-
gr.Examples(
|
| 299 |
-
[BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group], # Flavor_Group, Hop_Group
|
| 300 |
[local_plot, similar_beers, score_predict_str,percentile_dict0],
|
| 301 |
main_func,
|
| 302 |
cache_examples=True, label = "Aslin Beer List")
|
|
|
|
| 25 |
bb_df_percentile = pd.read_csv('bb_df_testing.csv')
|
| 26 |
|
| 27 |
#Set up DF for Aslin Examples
|
| 28 |
+
aslin_example_df_full = pd.read_csv('App_Example_Aslin_Brewery_Update.csv')
|
| 29 |
aslin_example_df = aslin_example_df_full.drop(['Number of Ratings Beer'], axis=1)
|
| 30 |
aslin_example_df['ABV'] = aslin_example_df['ABV']*100
|
| 31 |
|
|
|
|
| 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']
|
|
|
|
| 103 |
# Setup SHAP
|
| 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:
|
| 111 |
+
if row[prefix + item] == 1: # access the DataFrame column directly with the item name
|
| 112 |
+
selected_items.append(item)
|
| 113 |
+
return selected_items
|
| 114 |
+
|
| 115 |
#Set up similar beers function
|
| 116 |
def filter_beers(style, state):
|
| 117 |
target_abv = 0.03 # Set the target ABV value within the function
|
|
|
|
| 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 |
|
|
|
|
| 197 |
f"{nr_state_p} Overall": [percentile_state],
|
| 198 |
f"{nr_style_p} in USA": [percentile_style_overall],
|
| 199 |
f"{nr_style_p} in {nr_state_p}": [percentile_style_overall]
|
| 200 |
+
|
| 201 |
}
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
|
| 205 |
return local_plot, similar_beers,score_predict_str,percentile_dict0
|
|
|
|
| 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 |
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
|
|
|
|
| 253 |
gr.Markdown("<h2><center><b>Prediction Drivers</b></center></h2>")
|
| 254 |
local_plot = gr.Plot(label = 'Shap:')
|
| 255 |
|
| 256 |
+
gr.Markdown(f"<h2><center><b>Percentiles for Beer </b></center></h2>")
|
| 257 |
percentile_dict0 = gr.Label(label ='test', show_label=False)
|
| 258 |
|
| 259 |
|
|
|
|
| 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'])
|
| 311 |
+
example.append(row['Brewery Style'])
|
| 312 |
+
example.append(row['Region'])
|
| 313 |
+
example.append(row['State'])
|
| 314 |
+
|
| 315 |
+
# Convert binary columns to lists of selected flavors and hops
|
| 316 |
+
example.append(extract_selected_items(row, flavors_list, ''))
|
| 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")
|