Spaces:
Sleeping
Sleeping
Commit
·
cd9e006
1
Parent(s):
9360f57
Update app.py
Browse files
app.py
CHANGED
@@ -8,8 +8,6 @@ st.set_page_config(layout="wide")
|
|
8 |
# load data
|
9 |
f = open('data.json')
|
10 |
recipes_json = json.load(f)
|
11 |
-
|
12 |
-
# prepare data
|
13 |
recipes = pd.DataFrame(recipes_json)
|
14 |
ingredients = pd.DataFrame(recipes_json).explode('ingredients')
|
15 |
bar_dict = [
|
@@ -20,7 +18,25 @@ bar_dict = [
|
|
20 |
{"ingredients":"cointreau", "have": False}
|
21 |
]
|
22 |
bar_df = pd.DataFrame(bar_dict)
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
if 'filter_bar' not in st.session_state:
|
25 |
st.session_state.filter_bar = recipes['name'].to_list()
|
26 |
|
@@ -28,8 +44,8 @@ if 'bar_df' not in st.session_state:
|
|
28 |
st.session_state.bar_df = bar_df
|
29 |
st.session_state.bar_df_edited = st.session_state.bar_df.copy()
|
30 |
|
|
|
31 |
st.header("Recipe Finder")
|
32 |
-
|
33 |
with st.expander("Find recipes by name, ingredients, and type"):
|
34 |
# name search
|
35 |
name_search = st.text_input('Search recipes by name')
|
@@ -80,14 +96,6 @@ with st.expander("Find recipes by name, ingredients, and type"):
|
|
80 |
filter_1 = recipes['recipe_type'].isin(type_options)
|
81 |
filter_type = recipes.loc[filter_1, 'name'].to_list()
|
82 |
|
83 |
-
# def submit_bar():
|
84 |
-
|
85 |
-
|
86 |
-
# def reset_bar():
|
87 |
-
# # st.session_state.bar_df = bar_df
|
88 |
-
# st.session_state.bar_df_edited = bar_df
|
89 |
-
# st.session_state.filter_bar = recipes['name'].to_list()
|
90 |
-
|
91 |
|
92 |
with st.expander("Find recipes by what you can make with your bar at home"):
|
93 |
# input home bar
|
@@ -110,26 +118,25 @@ with st.expander("Find recipes that are similar to one another"):
|
|
110 |
#simlar_select = st.selectbox("Select a recipe:", pd.concat([pd.Series(Null),recipes['name']]))
|
111 |
simlar_select = st.multiselect("Select a recipe:", recipes['name'], max_selections=1)
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
sim = ratings.dot(ratings.T) + epsilon
|
116 |
-
elif kind == 'item':
|
117 |
-
sim = ratings.T.dot(ratings) + epsilon
|
118 |
-
norms = np.array([np.sqrt(np.diagonal(sim))])
|
119 |
-
return (sim / norms / norms.T)
|
120 |
-
|
121 |
-
pivot = ingredients[['name', 'ingredients']].copy()
|
122 |
-
pivot['count'] = 1
|
123 |
-
pivot = pivot.set_index(['name', 'ingredients'])['count'].unstack().reset_index()
|
124 |
-
pivot.rename_axis(None, axis=1, inplace=True)
|
125 |
-
pivot.fillna(0, inplace=True)
|
126 |
-
pivot_names = pivot.columns
|
127 |
-
pivot_np = np.array(pivot.set_index('name'))
|
128 |
-
recipe_similarity = pd.DataFrame(similarity(pivot_np, kind='user'))
|
129 |
-
recipe_similarity.columns = pivot['name'].values
|
130 |
-
recipe_similarity.index = pivot['name'].values
|
131 |
|
|
|
132 |
|
|
|
133 |
st.header("Recipes")
|
134 |
filter_all = list(set(filter_name) & set(filter_ingredient) & set(filter_source) & set(filter_type) & set(st.session_state.filter_bar))
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# load data
|
9 |
f = open('data.json')
|
10 |
recipes_json = json.load(f)
|
|
|
|
|
11 |
recipes = pd.DataFrame(recipes_json)
|
12 |
ingredients = pd.DataFrame(recipes_json).explode('ingredients')
|
13 |
bar_dict = [
|
|
|
18 |
{"ingredients":"cointreau", "have": False}
|
19 |
]
|
20 |
bar_df = pd.DataFrame(bar_dict)
|
21 |
+
def similarity(ratings, kind='user', epsilon=1e-9):
|
22 |
+
if kind == 'user':
|
23 |
+
sim = ratings.dot(ratings.T) + epsilon
|
24 |
+
elif kind == 'item':
|
25 |
+
sim = ratings.T.dot(ratings) + epsilon
|
26 |
+
norms = np.array([np.sqrt(np.diagonal(sim))])
|
27 |
+
return (sim / norms / norms.T)
|
28 |
+
pivot = ingredients[['name', 'ingredients']].copy()
|
29 |
+
pivot['count'] = 1
|
30 |
+
pivot = pivot.set_index(['name', 'ingredients'])['count'].unstack().reset_index()
|
31 |
+
pivot.rename_axis(None, axis=1, inplace=True)
|
32 |
+
pivot.fillna(0, inplace=True)
|
33 |
+
pivot_names = pivot.columns
|
34 |
+
pivot_np = np.array(pivot.set_index('name'))
|
35 |
+
recipe_similarity = pd.DataFrame(similarity(pivot_np, kind='user'))
|
36 |
+
recipe_similarity.columns = pivot['name'].values
|
37 |
+
recipe_similarity.index = pivot['name'].values
|
38 |
+
|
39 |
+
# manage session state
|
40 |
if 'filter_bar' not in st.session_state:
|
41 |
st.session_state.filter_bar = recipes['name'].to_list()
|
42 |
|
|
|
44 |
st.session_state.bar_df = bar_df
|
45 |
st.session_state.bar_df_edited = st.session_state.bar_df.copy()
|
46 |
|
47 |
+
# recipe finder section
|
48 |
st.header("Recipe Finder")
|
|
|
49 |
with st.expander("Find recipes by name, ingredients, and type"):
|
50 |
# name search
|
51 |
name_search = st.text_input('Search recipes by name')
|
|
|
96 |
filter_1 = recipes['recipe_type'].isin(type_options)
|
97 |
filter_type = recipes.loc[filter_1, 'name'].to_list()
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
with st.expander("Find recipes by what you can make with your bar at home"):
|
101 |
# input home bar
|
|
|
118 |
#simlar_select = st.selectbox("Select a recipe:", pd.concat([pd.Series(Null),recipes['name']]))
|
119 |
simlar_select = st.multiselect("Select a recipe:", recipes['name'], max_selections=1)
|
120 |
|
121 |
+
|
122 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
|
125 |
|
126 |
+
# Recipes section
|
127 |
st.header("Recipes")
|
128 |
filter_all = list(set(filter_name) & set(filter_ingredient) & set(filter_source) & set(filter_type) & set(st.session_state.filter_bar))
|
129 |
+
|
130 |
+
if len(similar_select) > 0:
|
131 |
+
st.dataframe(
|
132 |
+
recipes.set_index('name') \
|
133 |
+
.join(recipe_similarity[simlar_select], on='name') \
|
134 |
+
.reset_index() \
|
135 |
+
.sort_values(by=simlar_select, ascending=False) \
|
136 |
+
.query('name in @filter_all'),
|
137 |
+
#column_config={"Name": st.column_config.Column(width="medium")
|
138 |
+
hide_index=True) \
|
139 |
+
|
140 |
+
#recipes[recipes['name'].isin(filter_all)]
|
141 |
+
else:
|
142 |
+
st.dataframe(recipes[recipes['name'].isin(filter_all)], hide_index=True)
|