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 3 |
+
import shap
|
| 4 |
+
from shap.plots._force_matplotlib import draw_additive_plot
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import xgboost as xgb
|
| 10 |
+
#import gradio.outputs as gro
|
| 11 |
+
from scipy import stats
|
| 12 |
+
from gradio import Interface
|
| 13 |
+
from gradio.components import Markdown, Row, Column, Slider, Dropdown, CheckboxGroup, Button, Textbox, Dataframe
|
| 14 |
+
import category_encoders
|
| 15 |
+
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')
|
| 23 |
+
|
| 24 |
+
# #Set up DF for percentiles (currently only includes beers with 500 or more reviews)
|
| 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.csv')
|
| 29 |
+
# aslin_example_df = aslin_example_df_full.drop(['Brewery', 'Number of Ratings Beer'], axis=1)
|
| 30 |
+
# aslin_example_df['ABV'] = aslin_example_df['ABV']*100
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
#Open individual Brewery list
|
| 34 |
+
with open('unique_brewery_file.pickle', 'rb') as file:
|
| 35 |
+
unique_breweries_list = pickle.load(file)
|
| 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']
|
| 50 |
+
|
| 51 |
+
style_choices = ['Altbier', 'Australian Sparkling Ale', 'Barleywine - American', 'Barleywine - English', 'Barleywine - Other', 'Belgian Blonde', 'Belgian Dubbel', 'Belgian Enkel / Patersbier', 'Belgian Quadrupel',
|
| 52 |
+
'Belgian Strong Dark Ale', 'Belgian Strong Golden Ale', 'Belgian Tripel', 'Bitter - Best', 'Bitter - Extra Special / Strong (ESB)', 'Bitter - Session / Ordinary', 'Bière de Champagne / Bière Brut',
|
| 53 |
+
'Black & Tan', 'Blonde Ale', 'Bock - Doppelbock', 'Bock - Eisbock', 'Bock - Hell / Maibock / Lentebock', 'Bock - Single / Traditional', 'Bock - Weizenbock', 'Bock - Weizendoppelbock', 'Brett Beer',
|
| 54 |
+
'Brown Ale - American', 'Brown Ale - Belgian', 'Brown Ale - English', 'Brown Ale - Imperial / Double', 'Brown Ale - Other', 'California Common', 'Chilli / Chile Beer', 'Cider - Dry', 'Cider - Graff',
|
| 55 |
+
'Cider - Herbed / Spiced / Hopped', 'Cider - Ice / Applewine', 'Cider - Other Fruit', 'Cider - Perry / Poiré', 'Cider - Rosé', 'Cider - Sweet', 'Cider - Traditional / Apfelwein', 'Corn Beer / Chicha de Jora',
|
| 56 |
+
'Cream Ale', 'Dark Ale', 'Farmhouse Ale - Bière de Coupage', 'Farmhouse Ale - Bière de Garde', 'Farmhouse Ale - Bière de Mars', 'Farmhouse Ale - Grisette', 'Farmhouse Ale - Other', 'Farmhouse Ale - Sahti',
|
| 57 |
+
'Farmhouse Ale - Saison', 'Festbier', 'Flavored Malt Beverage', 'Freeze-Distilled Beer', 'Fruit Beer', 'Gluten-Free', 'Golden Ale - American', 'Golden Ale - English', 'Golden Ale - Other', 'Grape Ale - Italian',
|
| 58 |
+
'Grape Ale - Other', 'Grodziskie / Grätzer', 'Hard Ginger Beer', 'Hard Kombucha / Jun', 'Hard Seltzer', 'Historical Beer - Adambier', 'Historical Beer - Broyhan', 'Historical Beer - Burton Ale', 'Historical Beer - Dampfbier',
|
| 59 |
+
'Historical Beer - Gruit / Ancient Herbed Ale', 'Historical Beer - Kentucky Common', 'Historical Beer - Kottbusser', 'Historical Beer - Kuit / Kuyt / Koyt', 'Historical Beer - Lichtenhainer',
|
| 60 |
+
'Historical Beer - Mumme', 'Historical Beer - Other', 'Historical Beer - Steinbier', 'Honey Beer', 'IPA - American', 'IPA - Belgian', 'IPA - Black / Cascadian Dark Ale', 'IPA - Brett',
|
| 61 |
+
'IPA - Brown', 'IPA - Brut', 'IPA - Cold', 'IPA - English', 'IPA - Farmhouse', 'IPA - Fruited', 'IPA - Imperial / Double', 'IPA - Imperial / Double Black', 'IPA - Imperial / Double Milkshake',
|
| 62 |
+
'IPA - Imperial / Double New England / Hazy', 'IPA - Milkshake', 'IPA - New England / Hazy', 'IPA - New Zealand', 'IPA - Other', 'IPA - Quadruple', 'IPA - Red', 'IPA - Rye', 'IPA - Session',
|
| 63 |
+
'IPA - Sour', 'IPA - Triple', 'IPA - Triple New England / Hazy', 'IPA - White / Wheat', 'Kellerbier / Zwickelbier', 'Koji / Ginjo Beer', 'Kvass', 'Kölsch', 'Lager - Amber / Red', 'Lager - American',
|
| 64 |
+
'Lager - American Amber / Red', 'Lager - American Light', 'Lager - Dark', 'Lager - Dortmunder / Export', 'Lager - Helles', 'Lager - IPL (India Pale Lager)', 'Lager - Japanese Rice', 'Lager - Leichtbier',
|
| 65 |
+
'Lager - Mexican', 'Lager - Munich Dunkel', 'Lager - Other', 'Lager - Pale', 'Lager - Strong', 'Lager - Vienna', 'Lager - Winter', 'Lambic - Framboise', 'Lambic - Fruit', 'Lambic - Gueuze', 'Lambic - Kriek',
|
| 66 |
+
'Lambic - Other', 'Lambic - Traditional', 'Malt Beer', 'Malt Liquor', 'Mead - Acerglyn / Maple Wine', 'Mead - Bochet', 'Mead - Braggot', 'Mead - Cyser', 'Mead - Melomel', 'Mead - Metheglin', 'Mead - Other',
|
| 67 |
+
'Mead - Pyment', 'Mead - Session / Short', 'Mead - Traditional', 'Mild - Dark', 'Mild - Light', 'Mild - Other', 'Märzen', 'Non-Alcoholic Beer - IPA', 'Non-Alcoholic Beer - Lager', 'Non-Alcoholic Beer - Other',
|
| 68 |
+
'Non-Alcoholic Beer - Porter / Stout', 'Non-Alcoholic Beer - Sour', 'Non-Alcoholic Beer - Wheat Beer', 'Old Ale', 'Other', 'Pale Ale - American', 'Pale Ale - Australian', 'Pale Ale - Belgian', 'Pale Ale - English',
|
| 69 |
+
'Pale Ale - Milkshake', 'Pale Ale - New England / Hazy', 'Pale Ale - New Zealand', 'Pale Ale - Other', 'Pale Ale - XPA (Extra Pale)', 'Pilsner - Czech / Bohemian', 'Pilsner - German', 'Pilsner - Imperial / Double',
|
| 70 |
+
'Pilsner - Italian', 'Pilsner - New Zealand', 'Pilsner - Other', 'Porter - American', 'Porter - Baltic', 'Porter - Coffee', 'Porter - English', 'Porter - Imperial / Double', 'Porter - Imperial / Double Baltic',
|
| 71 |
+
'Porter - Imperial / Double Coffee', 'Porter - Other', 'Pumpkin / Yam Beer', 'Rauchbier', 'Red Ale - American Amber / Red', 'Red Ale - Imperial / Double', 'Red Ale - Irish', 'Red Ale - Other', 'Roggenbier', 'Root Beer',
|
| 72 |
+
'Rye Beer', 'Rye Wine', 'Schwarzbier', 'Scotch Ale / Wee Heavy', 'Scottish Ale', 'Scottish Export Ale', 'Shandy / Radler', 'Smoked Beer', 'Sorghum / Millet Beer', 'Sour - Berliner Weisse', 'Sour - Flanders Oud Bruin',
|
| 73 |
+
'Sour - Flanders Red Ale', 'Sour - Fruited', 'Sour - Fruited Berliner Weisse', 'Sour - Fruited Gose', 'Sour - Other', 'Sour - Other Gose', 'Sour - Smoothie / Pastry', 'Sour - Traditional Gose', 'Specialty Grain',
|
| 74 |
+
'Spiced / Herbed Beer', 'Stout - American', 'Stout - Belgian', 'Stout - Coffee', 'Stout - English', 'Stout - Foreign / Export', 'Stout - Imperial / Double', 'Stout - Imperial / Double Coffee', 'Stout - Imperial / Double Milk',
|
| 75 |
+
'Stout - Imperial / Double Oatmeal', 'Stout - Imperial / Double Pastry', 'Stout - Imperial / Double White / Golden', 'Stout - Irish Dry', 'Stout - Milk / Sweet', 'Stout - Oatmeal', 'Stout - Other', 'Stout - Oyster',
|
| 76 |
+
'Stout - Pastry', 'Stout - Russian Imperial', 'Stout - White / Golden', 'Strong Ale - American', 'Strong Ale - English', 'Strong Ale - Other', 'Table Beer', 'Traditional Ale', 'Wheat Beer - American Pale Wheat',
|
| 77 |
+
'Wheat Beer - Dunkelweizen', 'Wheat Beer - Hefeweizen', 'Wheat Beer - Hefeweizen Light / Leicht', 'Wheat Beer - Kristallweizen', 'Wheat Beer - Other', 'Wheat Beer - Wheat Wine', 'Wheat Beer - Witbier / Blanche',
|
| 78 |
+
'Wild Ale - American', 'Wild Ale - Other', 'Winter Ale', 'Winter Warmer']
|
| 79 |
+
|
| 80 |
+
brewery_style_choices = ['Brew Pub', 'Cidery', 'Contract Brewery','Macro Brewery','Micro Brewery',
|
| 81 |
+
'Nano Brewery', 'OTHER', 'Regional Brewery']
|
| 82 |
+
|
| 83 |
+
# brewery_style_choices = ['Bar / Restaurant / Store', 'Brew Pub', 'Cidery', 'Contract Brewery', 'Home / Non-Commercial Brewery', 'Macro Brewery', 'Meadery',
|
| 84 |
+
# 'Micro Brewery', 'Nano Brewery', 'OTHER', 'Regional Brewery']
|
| 85 |
+
|
| 86 |
+
state_choices = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MISSING', 'MN', 'MO', 'MS', 'MT', 'NC',
|
| 87 |
+
'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SAINT CROIX ISLAND VIRGIN ISLANDS', 'SAINT JOHN ISLAND VIRGIN ISLANDS', 'SAINT THOMAS ISLAND VIRGIN ISLANDS',
|
| 88 |
+
'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY']
|
| 89 |
+
|
| 90 |
+
# Define flavors and hops
|
| 91 |
+
flavors_list = ['Apple', 'Apricot', 'Berry', 'Bitter', 'Caramel', 'Chocolate', 'Citrus', 'Clove', 'Coffee',
|
| 92 |
+
'Dry', 'Earthy', 'Fig', 'Floral', 'Fruity', 'Funky', 'Grapefruit', 'Hazelnut', 'Herbal',
|
| 93 |
+
'Malt', 'Nutmeg', 'Nutty', 'Peach', 'Pear', 'Peat', 'Pepper', 'Pine', 'Plum', 'Resin',
|
| 94 |
+
'Salty', 'Smoky', 'Sour', 'Spicy', 'Strawberry', 'Sweet', 'Tart', 'Toast', 'Toffee',
|
| 95 |
+
'Tropical', 'Vanilla']
|
| 96 |
+
|
| 97 |
+
hops_list = ['Amarillo', 'Cascade', 'Centennial', 'Chinook', 'Citra', 'Columbus', 'Crystal', 'Fuggle',
|
| 98 |
+
'Galaxy', 'Golding', 'Hallertau', 'Magnum', 'Mosaic', 'Noble', 'Nugget', 'Saaz', 'Simcoe', 'Tettnang',
|
| 99 |
+
'Warrior', 'Willamette']
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 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
|
| 109 |
+
|
| 110 |
+
filtered_df = bb_df[(bb_df['Style'] == style) & (bb_df['State'] == state)].copy()
|
| 111 |
+
filtered_df['ABV_diff'] = abs(filtered_df['ABV'] - target_abv)
|
| 112 |
+
filtered_df.loc[filtered_df['ABV_diff'] > target_abv, 'ABV_diff'] = target_abv
|
| 113 |
+
filtered_df = filtered_df[filtered_df['ABV_diff'] <= target_abv]
|
| 114 |
+
sorted_df = filtered_df.sort_values(by='Number of Ratings Beer', ascending=False)
|
| 115 |
+
limited_df = sorted_df.head(5)[['Brewery', 'Beer Name', 'Average Rating Beer', 'Number of Ratings Beer', 'Style', 'ABV', 'IBU', 'State']]
|
| 116 |
+
|
| 117 |
+
limited_df = limited_df.rename(columns={'Average Rating Beer': 'Avg Rating', 'Number of Ratings Beer': '# Ratings'})
|
| 118 |
+
|
| 119 |
+
limited_df['ABV'] = (limited_df['ABV'] * 100).round(2).astype(str) + '%'
|
| 120 |
+
limited_df['Avg Rating'] = limited_df['Avg Rating'].round(2)
|
| 121 |
+
limited_df['IBU'] = limited_df['IBU'].astype(int)
|
| 122 |
+
limited_df['# Ratings'] = limited_df['# Ratings'].apply(lambda x: '{:,}'.format(x)) # Add commas to # Ratings
|
| 123 |
+
|
| 124 |
+
return limited_df
|
| 125 |
+
|
| 126 |
+
# #Define percentiles before main function runs
|
| 127 |
+
# percentile_overall = 0
|
| 128 |
+
# percentile_state = 0
|
| 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 |
+
|
| 142 |
+
# Transform the new_row using the loaded encoder
|
| 143 |
+
new_row_class = new_row
|
| 144 |
+
new_row_regress = new_row
|
| 145 |
+
new_row_encoded_class = loaded_enc_classification.transform(new_row_class)
|
| 146 |
+
new_row_encoded_regressor = loaded_enc_regressor.transform(new_row_regress)
|
| 147 |
+
|
| 148 |
+
prob = loaded_model.predict_proba(new_row_encoded_class)
|
| 149 |
+
|
| 150 |
+
score_predict = loaded_model_regressor.predict(new_row_encoded_regressor)
|
| 151 |
+
score_predict = score_predict[0]
|
| 152 |
+
score_predict = round(score_predict, 2)
|
| 153 |
+
score_predict_str = str(score_predict)
|
| 154 |
+
score_predict_float = float(score_predict)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
#Build SHAP
|
| 158 |
+
shap_values = explainer(new_row_encoded_regressor)
|
| 159 |
+
|
| 160 |
+
plot = shap.plots.bar(shap_values[0], max_display=7, order=shap.Explanation.abs, show_data='auto', show=False)
|
| 161 |
+
|
| 162 |
+
plt.tight_layout()
|
| 163 |
+
local_plot = plt.gcf()
|
| 164 |
+
plt.close()
|
| 165 |
+
|
| 166 |
+
#Build Similar Beers
|
| 167 |
+
similar_beers = filter_beers(Style, State) # fetch beers matching the style and state
|
| 168 |
+
|
| 169 |
+
#Read in variables for percentiles
|
| 170 |
+
nr_state_p = new_row['State'][0]
|
| 171 |
+
nr_style_p = new_row['Style'][0]
|
| 172 |
+
|
| 173 |
+
#Show Percentiles for prediction
|
| 174 |
+
overall_df = bb_df_percentile
|
| 175 |
+
state_df = bb_df_percentile[bb_df_percentile['State'] == nr_state_p]
|
| 176 |
+
style_overall_df = bb_df_percentile[bb_df_percentile['Style'] == nr_style_p]
|
| 177 |
+
style_state_df = bb_df_percentile[(bb_df_percentile['Style'] == nr_style_p) & (bb_df_percentile['State'] == nr_state_p)]
|
| 178 |
+
|
| 179 |
+
# Calculate the percentile of a beer
|
| 180 |
+
|
| 181 |
+
percentile_overall = stats.percentileofscore(overall_df['Average Rating Beer'], score_predict_float).round(1)/100
|
| 182 |
+
percentile_state = stats.percentileofscore(state_df['Average Rating Beer'], score_predict_float).round(1)/100
|
| 183 |
+
percentile_style_overall = stats.percentileofscore(style_overall_df['Average Rating Beer'], score_predict_float).round(1)/100
|
| 184 |
+
percentile_style_state = stats.percentileofscore(style_state_df['Average Rating Beer'], score_predict_float).round(1)/100
|
| 185 |
+
|
| 186 |
+
percentile_dict0 = {
|
| 187 |
+
"USA Overall": [percentile_overall],
|
| 188 |
+
f"{nr_state_p} Overall": [percentile_state],
|
| 189 |
+
f"USA by Style {nr_style_p}": [percentile_style_overall],
|
| 190 |
+
f"{nr_state_p} by Style {nr_style_p}": [percentile_style_state]
|
| 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
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
#"Below 4.0": float(prob[0][0]), "Above 4.0": 1-float(prob[0][0])},
|
| 210 |
+
|
| 211 |
+
#,percentile_overall, percentile_state, percentile_style_overall, percentile_style_state
|
| 212 |
+
|
| 213 |
+
# main_func('',.045, 41, 'IPA - Session', 'Micro Brewery', 'Far West', 'CA', [], [])
|
| 214 |
+
|
| 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. Mean Average Error (MAE) of <b>.12</b> and Root Mean Squared Error (RMSE) of <b>.16</b>.
|
| 219 |
+
The input variables in this model explain <b>65%</b> of the variation in the Untappd beer score """
|
| 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 |
+
|
| 227 |
+
with gr.Blocks(title=title, theme = theme) as demo:
|
| 228 |
+
Markdown(f"# {title}")
|
| 229 |
+
Markdown(description1)
|
| 230 |
+
# Markdown("""---""")
|
| 231 |
+
# Markdown(description2)
|
| 232 |
+
# Markdown("""---""")
|
| 233 |
+
|
| 234 |
+
submit_btn1 = gr.Button("Predict")
|
| 235 |
+
with Row():
|
| 236 |
+
with Column():
|
| 237 |
+
# BeerName = gr.components.Textbox(label='Beer Name (not required)', value = 'New Beer 1')
|
| 238 |
+
# ABV = gr.components.Slider(label="ABV %", minimum=0, maximum=20, value=4.5, step=.1)
|
| 239 |
+
# IBU = gr.components.Slider(label="IBU", minimum=0.0, maximum=200, value=41, step=1)
|
| 240 |
+
# Style = gr.components.Dropdown(choices=style_choices, label="Select Beer Style", value= 'IPA - Session')
|
| 241 |
+
# BreweryStyle = gr.components.Dropdown(choices=brewery_style_choices, label="Select Brewery Style", value= 'Micro Brewery')
|
| 242 |
+
# Region = gr.components.Dropdown(choices=region_choices, label="Select USA Region", value= 'Far West')
|
| 243 |
+
# State = gr.components.Dropdown(choices=state_choices, label="Select State", value= 'CA')
|
| 244 |
+
# # Grouped checkboxes
|
| 245 |
+
# Flavor_Group = gr.components.CheckboxGroup(choices=flavors_list, label="Select Flavors")
|
| 246 |
+
# Hop_Group = gr.components.CheckboxGroup(choices=hops_list, label="Select Hops")
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
BeerName = Textbox(label='Beer Name (not required)', value = 'New Beer 1')
|
| 250 |
+
ABV = Slider(label="ABV %", minimum=0, maximum=20, value=4.5, step=.1)
|
| 251 |
+
IBU = Slider(label="IBU", minimum=0.0, maximum=200, value=41, step=1)
|
| 252 |
+
Style = Dropdown(choices=style_choices, label="Select Beer Style", value='IPA - Session')
|
| 253 |
+
BreweryStyle = Dropdown(choices=brewery_style_choices, label="Select Brewery Style", value='Micro Brewery')
|
| 254 |
+
Region = Dropdown(choices=region_choices, label="Select USA Region", value='Far West')
|
| 255 |
+
State = Dropdown(choices=state_choices, label="Select State", value='CA')
|
| 256 |
+
# Grouped checkboxes
|
| 257 |
+
Flavor_Group = CheckboxGroup(choices=flavors_list, label="Select Flavors")
|
| 258 |
+
Hop_Group = CheckboxGroup(choices=hops_list, label="Select Hops")
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
#CREATE OUTPUTS
|
| 262 |
+
with gr.Column(visible=True) as output_col:
|
| 263 |
+
gr.Markdown("<h2><center><b>Untappd Score Prediction</b></center></h2>")
|
| 264 |
+
score_predict_str = gr.Label(label="XGBoost Regressor")
|
| 265 |
+
|
| 266 |
+
gr.Markdown("<h2><center><b>Prediction Drivers</b></center></h2>")
|
| 267 |
+
local_plot = gr.Plot(label = 'Shap:')
|
| 268 |
+
|
| 269 |
+
gr.Markdown("<h2><center><b>Percentiles for Beer</b></center></h2>")
|
| 270 |
+
percentile_dict0 = gr.Label(label ='test', show_label=False)
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
#percentile_df = gr.BarPlot(title = 'test bar plot', x ="type", y="value", vertical=False).style(container=False,)
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
#label = gr.Label(label = "Predicted Label")
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
submit_btn2 = gr.Button("Predict")
|
| 280 |
+
|
| 281 |
+
Markdown("""---""")
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
# Create a separate row for the output of filter_beers function
|
| 286 |
+
with gr.Row():
|
| 287 |
+
with gr.Column():
|
| 288 |
+
gr.Markdown("<h1><center><b>Similar Beers</b></center></h1>")
|
| 289 |
+
#similar_beers_df = gro.Dataframe(label="", type="pandas")
|
| 290 |
+
similar_beers = Dataframe(label="", type="pandas")
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
#Button Click Events
|
| 294 |
+
submit_btn1.click(
|
| 295 |
+
main_func,
|
| 296 |
+
[BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
|
| 297 |
+
[local_plot,similar_beers,score_predict_str,percentile_dict0],
|
| 298 |
+
api_name="Untappd_Rating_Model")
|
| 299 |
+
|
| 300 |
+
submit_btn2.click(
|
| 301 |
+
main_func,
|
| 302 |
+
[BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group],
|
| 303 |
+
[local_plot,similar_beers,score_predict_str,percentile_dict0],
|
| 304 |
+
api_name="Untappd_Rating_Model1")
|
| 305 |
+
|
| 306 |
+
#EXAMPLES
|
| 307 |
+
example_list = aslin_example_df.values.tolist()
|
| 308 |
+
gr.Markdown("<h1><center><b>Aslin Beers Example Inputs</b></center></h1>")
|
| 309 |
+
gr.Examples(example_list[:10],
|
| 310 |
+
[BeerName, ABV, IBU, Style, BreweryStyle, Region, State, Flavor_Group, Hop_Group], # Flavor_Group, Hop_Group
|
| 311 |
+
[local_plot, similar_beers, score_predict_str,percentile_dict0],
|
| 312 |
+
main_func,
|
| 313 |
+
cache_examples=True, label = "Aslin Beer List")
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
demo.launch()
|