-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
159 lines (122 loc) · 6.85 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import streamlit as st
import pickle
import requests
from recommend import recommend1, recommend2
data = pickle.load(open("data.pkl", 'rb'))
recipe_list = data['Title'].values
####################################################################################################
# Function 1: Show Recommended Recipes with Similar Ingredients
####################################################################################################
st.header("Recipe Recommendation System")
with st.container():
selectValue1 = st.selectbox("Select a recipe from dropdown:", recipe_list)
if st.button("Show Recommended Recipes with Similar Ingredients"):
data = pickle.load(open("data.pkl", 'rb'))
recommended_recipes_titles, recommended_recipes_images, recommended_recipes_stats, recommended_recipes_instructions = recommend1(selectValue1, data)
for i in range(len(recommended_recipes_titles)):
col1, col2 = st.columns([1, 3])
with col1:
st.image(recommended_recipes_images[i])
with col2:
with st.expander(recommended_recipes_titles[i]):
st.image(recommended_recipes_images[i])
st.text(recommended_recipes_stats[i])
st.markdown("### Recipe Instruction")
st.write(recommended_recipes_instructions[i])
st.markdown("---")
####################################################################################################
# Function 2: Show Recommended Recipes with Given Ingredients
####################################################################################################
with st.container():
inputValue1 = st.text_input("Enter ingredients you don't want to cook with:")
inputValue2 = st.text_input("Enter ingredients you would like to cook with:")
if st.button("Show Recommended Recipes with Given Ingredients"):
data = pickle.load(open("data.pkl", 'rb'))
recommended_recipes_titles, recommended_recipes_images, recommended_recipes_stats,recommended_recipes_instructions = recommend2(inputValue1, inputValue2, data)
for i in range(len(recommended_recipes_titles)):
col1, col2 = st.columns([1, 3])
with col1:
st.image(recommended_recipes_images[i])
with col2:
with st.expander(recommended_recipes_titles[i]):
st.image(recommended_recipes_images[i])
st.text(recommended_recipes_stats[i])
st.markdown("### Recipe Instruction")
st.write(recommended_recipes_instructions[i])
st.markdown("---")
####################################################################################################
# Function 3: Show Recommended Recipes with Given Natural Language
####################################################################################################
def query(payload):
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.text
except requests.exceptions.RequestException as e:
print("Error:", e)
return None
with st.container():
inputValue3 = st.text_input("Enter natural language to describe the ingredients you would like to cook with:")
if st.button("Show Recommended Recipes with Given Natural Language"):
data = pickle.load(open("data.pkl", 'rb'))
API_URL = "https://api-inference.huggingface.co/models/ilsilfverskiold/tech-keywords-extractor"
headers = {"Authorization": "Bearer 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"}
output = query(inputValue3)
recommended_recipes_titles, recommended_recipes_images, recommended_recipes_stats, recommended_recipes_instructions = recommend2("", inputValue3, data)
for i in range(len(recommended_recipes_titles)):
col1, col2 = st.columns([1, 3])
with col1:
st.image(recommended_recipes_images[i])
with col2:
with st.expander(recommended_recipes_titles[i]):
st.image(recommended_recipes_images[i])
st.text(recommended_recipes_stats[i])
st.markdown("### Recipe Instruction")
st.write(recommended_recipes_instructions[i])
st.markdown("---")
####################################################################################################
# Function 4: Search and Rate Recipes
####################################################################################################
def search(title, df):
searched_df = df[df['Title'] == title]
searched_recipes_titles = []
searched_recipes_images = []
# searched_recipes_ratings = []
searched_recipes_stats = []
searched_recipes_instructions =[]
for recipes_title in searched_df['Title']:
searched_recipes_titles.append(recipes_title)
for recipes_image in searched_df['Image_Name']:
searched_recipes_images.append('archive/images/' + recipes_image + '.jpg')
# for recipes_rating in searched_df['Rating']:
# searched_recipes_ratings.append(recipes_rating)
for _, row in searched_df.iterrows():
searched_recipes_stats.append(str(row['Rating']))
for recipes_instruction in searched_df['Instructions']:
searched_recipes_instructions.append(recipes_instruction)
return searched_recipes_titles, searched_recipes_images, searched_recipes_stats, searched_recipes_instructions
def update_rating(title, rating, df):
df.at[df[df['Title'] == title].index[0], 'Rating'] = rating
df.to_pickle("data.pkl")
with st.container():
inputValue4 = st.text_input("Search a recipe to rate:")
if st.button("Search"):
data = pickle.load(open("data.pkl", 'rb'))
searched_recipes_titles, searched_recipes_images, searched_recipes_stats, searched_recipes_instructions = search(inputValue4, data)
if not searched_recipes_titles:
st.error("Recipe doesn't exist!")
for i in range(len(searched_recipes_titles)):
col1, col2 = st.columns([1, 3])
with col1:
st.image(searched_recipes_images[i])
with col2:
with st.expander(searched_recipes_titles[i]):
st.image(searched_recipes_images[i])
st.text(searched_recipes_stats[i])
st.markdown("### Recipe Instruction")
st.write(searched_recipes_instructions[i])
user_rating = st.slider("Rate this recipe:", 1, 100, 50)
if st.button("Submit Rating"):
data = pickle.load(open("data.pkl", 'rb'))
update_rating(inputValue4, user_rating, data)
st.success("Thank you for rating!")