forked from leewanxian/wine_recommender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
145 lines (115 loc) · 6.96 KB
/
app.py
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
# Import python lib
import streamlit as st
import time
import pandas as pd
import numpy as np
from surprise import Dataset, Reader
from surprise import KNNBaseline
# Import wine dataframes
df_wine_model = pd.read_pickle('./data/df_wine_us_rate.pkl')
df_wine_combi = pd.read_pickle('./data/df_wine_combi.pkl')
# Instantiate the list of wine traits
all_traits = ['almond', 'anise', 'apple', 'apricot', 'baked', 'baking_spices', 'berry', 'black_cherry', 'black_currant', 'black_pepper', 'black_tea', 'blackberry', 'blueberry',
'boysenberry', 'bramble', 'bright', 'butter', 'candy', 'caramel', 'cardamom', 'cassis', 'cedar', 'chalk', 'cherry', 'chocolate', 'cinnamon', 'citrus', 'clean', 'closed',
'clove', 'cocoa', 'coffee', 'cola', 'complex', 'concentrated', 'cranberry', 'cream', 'crisp', 'dark', 'dark_chocolate', 'dense', 'depth', 'dried_herb', 'dry', 'dust',
'earth', 'edgy', 'elderberry', 'elegant', 'fennel', 'firm', 'flower', 'forest_floor', 'french_oak', 'fresh', 'fruit', 'full_bodied', 'game', 'grapefruit', 'graphite',
'green', 'gripping', 'grippy', 'hearty', 'herb', 'honey', 'honeysuckle', 'jam', 'juicy', 'lavender', 'leafy', 'lean', 'leather', 'lemon', 'lemon_peel', 'length', 'licorice',
'light_bodied', 'lime', 'lush', 'meaty', 'medium_bodied', 'melon', 'milk_chocolate', 'minerality', 'mint', 'nutmeg', 'oak', 'olive', 'orange', 'orange_peel', 'peach',
'pear', 'pencil_lead', 'pepper', 'pine', 'pineapple', 'plum', 'plush', 'polished', 'pomegranate', 'powerful', 'purple', 'purple_flower', 'raspberry', 'refreshing',
'restrained', 'rich', 'ripe', 'robust', 'rose', 'round', 'sage', 'salt', 'savory', 'sharp', 'silky', 'smoke', 'smoked_meat', 'smooth', 'soft', 'sparkling', 'spice',
'steel', 'stone', 'strawberry', 'succulent', 'supple', 'sweet', 'tangy', 'tannin', 'tar', 'tart', 'tea', 'thick', 'thyme', 'tight', 'toast', 'tobacco', 'tropical_fruit',
'vanilla', 'velvety', 'vibrant', 'violet', 'warm', 'weight', 'wet_rocks', 'white', 'white_pepper', 'wood']
#---------------------------------------------------------------------------------------------------------
# Function to instantiate the model & return the est recsys scores
def recommend_scores():
# Instantiate reader & data for surprise
reader = Reader(rating_scale=(88, 100))
data = Dataset.load_from_df(df_wine_model, reader)
# Instantiate recsys model
sim_options={'name':'cosine'}
model = KNNBaseline(k=35, min_k=1, sim_options=sim_options, verbose=False)
# Train & fit the data into model
train=data.build_full_trainset()
model.fit(train)
# Start the model to compute the best estimate match score on wine list
recommend_list = []
user_wines = df_wine_model[df_wine_model.taster_name == 'mockuser']['title'].unique()
not_user_wines = []
for wine in df_wine_model['title'].unique():
if wine not in user_wines:
not_user_wines.append(wine)
for wine in not_user_wines:
wine_compatibility = []
prediction = model.predict(uid='mockuser', iid=wine)
wine_compatibility.append(prediction.iid)
wine_compatibility.append(prediction.est)
recommend_list.append(wine_compatibility)
result_df = pd.DataFrame(recommend_list, columns = ['title', 'est_match_pts'])
return result_df
# Function for background image
def add_bg_from_url():
st.markdown(
f"""
<style>
[data-testid="stAppViewContainer"] {{
background-image: url("https://images.pexels.com/photos/391213/pexels-photo-391213.jpeg");
background-attachment: fixed;
background-size: cover
}}
[data-testid="stVerticalBlock"] {{
background-color: rgba(255,255,255,0.5)
}}
</style>
""",
unsafe_allow_html=True
)
#----------------------------------------------------------------------------------------------------------
st.title("Which wine should I get?")
st.write("By Lee Wan Xian")
st.write("[GitHub](https://github.com/leewanxian) | [LinkedIn](https://www.linkedin.com/in/wanxianlee)")
st.write("You can type the wine traits that you want in the dropdown list below")
add_bg_from_url()
select_temptrait = st.multiselect(label = " ", options = all_traits, label_visibility = "collapsed")
if st.button('Show me the wines!'):
with st.spinner('Should you have some wine now?'):
time.sleep(2)
# Instantiate selected wine traits
if len(select_temptrait) == 0:
selected_traits = all_traits
else:
selected_traits = select_temptrait
# Run recommender model
recommend_df = recommend_scores()
# Instantiate traits filter
trait_filter = ['title']
# Add on any traits selected by user
trait_filter.extend(selected_traits)
# Create dataframe for wine name and traits
df_temp_traits = df_wine_combi.drop(columns=['taster_name', 'points', 'variety', 'designation', 'winery', 'country', 'province', 'region_1', 'region_2', 'price', 'description',
'desc_wd_count', 'traits'])
# Code to start filtering out wines with either one of the selected traits
df_temp_traits = df_temp_traits[trait_filter]
df_temp_traits['sum'] = df_temp_traits.sum(axis=1, numeric_only=True)
df_temp_traits = df_temp_traits[df_temp_traits['sum'] != 0]
# Merge the selected wines traits with recommend scores
df_selectrec_temp = df_temp_traits.merge(recommend_df, on='title', how='left')
# Merge the selected wines with recommendations with df on details
df_selectrec_detail = df_selectrec_temp.merge(df_wine_combi, on='title', how='left')
df_selectrec_detail.drop_duplicates(inplace=True)
# Pull out the top 10 recommendations (raw)
df_rec_raw = df_selectrec_detail.sort_values('est_match_pts', ascending=False).head(10)
# Prepare the display for the top 10 recommendations
df_rec_final = df_rec_raw[['title', 'points', 'price', 'variety', 'country', 'province', 'winery', 'description', 'traits']].reset_index(drop=True)
df_rec_final.index = df_rec_final.index + 1
df_rec_final['traits']=df_rec_final['traits'].str.replace(" ", " | ")
df_rec_final.rename(columns={'title':'Name',
'country':'Country',
'province':'State/Province',
'variety':'Type',
'winery':'Winery',
'points':'Rating (Out of 100)',
'price':'Price',
'description':'Review',
'traits':'Key Traits'}, inplace=True)
st.balloons()
st.dataframe(df_rec_final.style.format({"Price": "${:,.2f}"}))