Spaces:
Sleeping
Sleeping
adding comments
Browse files
app.py
CHANGED
|
@@ -116,11 +116,11 @@ print(top_results)
|
|
| 116 |
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 117 |
|
| 118 |
|
| 119 |
-
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
|
| 120 |
-
recipe_id_map = {}
|
| 121 |
|
| 122 |
# search for recipes
|
| 123 |
-
def search_recipes(ingredient, cuisine, dietary):
|
| 124 |
global recipe_id_map
|
| 125 |
url = "https://api.spoonacular.com/recipes/complexSearch"
|
| 126 |
params = {
|
|
@@ -129,19 +129,19 @@ def search_recipes(ingredient, cuisine, dietary):
|
|
| 129 |
"diet": dietary,
|
| 130 |
"number": 3,
|
| 131 |
"apiKey": SPOONACULAR_API_KEY
|
| 132 |
-
}
|
| 133 |
|
| 134 |
res = requests.get(url, params=params)
|
| 135 |
data = res.json()
|
| 136 |
|
| 137 |
if "results" not in data or not data["results"]:
|
| 138 |
recipe_id_map = {}
|
| 139 |
-
return gr.update(choices=[], visible=True, label="No recipes found"), gr.update(value="No recipes found.")
|
| 140 |
|
| 141 |
-
recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
|
| 142 |
return gr.update(choices=list(recipe_id_map.keys()), visible=True, label="Select a recipe"), gr.update(value="Select a recipe from the dropdown above.")
|
| 143 |
-
|
| 144 |
-
# get recipe details
|
| 145 |
def get_recipe_details(selected_title):
|
| 146 |
if not selected_title or selected_title not in recipe_id_map:
|
| 147 |
return "Please select a valid recipe."
|
|
@@ -161,26 +161,26 @@ def get_recipe_details(selected_title):
|
|
| 161 |
# UI
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
gr.Markdown("## 🥗 The BiteBot")
|
| 164 |
-
|
| 165 |
with gr.Row():
|
| 166 |
ingredient = gr.Textbox(label="Preferred Ingredient", placeholder="e.g., chicken")
|
| 167 |
cuisine = gr.Textbox(label="Preferred Cuisine", placeholder="e.g., Indian")
|
| 168 |
diet = gr.Textbox(label="Dietary Restrictions", placeholder="e.g., vegetarian")
|
| 169 |
-
|
| 170 |
search_button = gr.Button("Search Recipes")
|
| 171 |
recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
|
| 172 |
recipe_output = gr.Markdown()
|
| 173 |
-
|
| 174 |
search_button.click(
|
| 175 |
fn=search_recipes,
|
| 176 |
inputs=[ingredient, cuisine, diet],
|
| 177 |
outputs=[recipe_dropdown, recipe_output]
|
| 178 |
-
)
|
| 179 |
|
| 180 |
recipe_dropdown.change(
|
| 181 |
fn=get_recipe_details,
|
| 182 |
inputs=recipe_dropdown,
|
| 183 |
outputs=recipe_output
|
| 184 |
-
)
|
| 185 |
|
| 186 |
demo.launch()
|
|
|
|
| 116 |
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 117 |
|
| 118 |
|
| 119 |
+
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5" #our api key
|
| 120 |
+
recipe_id_map = {} #creating dictionary to keep track of which recipe title for which recipe id
|
| 121 |
|
| 122 |
# search for recipes
|
| 123 |
+
def search_recipes(ingredient, cuisine, dietary): #filter results based on these sends requests
|
| 124 |
global recipe_id_map
|
| 125 |
url = "https://api.spoonacular.com/recipes/complexSearch"
|
| 126 |
params = {
|
|
|
|
| 129 |
"diet": dietary,
|
| 130 |
"number": 3,
|
| 131 |
"apiKey": SPOONACULAR_API_KEY
|
| 132 |
+
} #updates the dropdown with these titles and stores ids for later
|
| 133 |
|
| 134 |
res = requests.get(url, params=params)
|
| 135 |
data = res.json()
|
| 136 |
|
| 137 |
if "results" not in data or not data["results"]:
|
| 138 |
recipe_id_map = {}
|
| 139 |
+
return gr.update(choices=[], visible=True, label="No recipes found"), gr.update(value="No recipes found.") #if no recipe found
|
| 140 |
|
| 141 |
+
recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
|
| 142 |
return gr.update(choices=list(recipe_id_map.keys()), visible=True, label="Select a recipe"), gr.update(value="Select a recipe from the dropdown above.")
|
| 143 |
+
#asks user to choose a recipe from dropdown
|
| 144 |
+
# get recipe details from the recipe the user selects
|
| 145 |
def get_recipe_details(selected_title):
|
| 146 |
if not selected_title or selected_title not in recipe_id_map:
|
| 147 |
return "Please select a valid recipe."
|
|
|
|
| 161 |
# UI
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
gr.Markdown("## 🥗 The BiteBot")
|
| 164 |
+
#creates interface using rows and columns layout
|
| 165 |
with gr.Row():
|
| 166 |
ingredient = gr.Textbox(label="Preferred Ingredient", placeholder="e.g., chicken")
|
| 167 |
cuisine = gr.Textbox(label="Preferred Cuisine", placeholder="e.g., Indian")
|
| 168 |
diet = gr.Textbox(label="Dietary Restrictions", placeholder="e.g., vegetarian")
|
| 169 |
+
#creates 3 input fields side by side for ingredient, cuisine, restrictions
|
| 170 |
search_button = gr.Button("Search Recipes")
|
| 171 |
recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
|
| 172 |
recipe_output = gr.Markdown()
|
| 173 |
+
#triggers the search, displays up to 3 recipes, shows the recipe selected
|
| 174 |
search_button.click(
|
| 175 |
fn=search_recipes,
|
| 176 |
inputs=[ingredient, cuisine, diet],
|
| 177 |
outputs=[recipe_dropdown, recipe_output]
|
| 178 |
+
) #when user clicks search runs search recipes function and fills dropdown
|
| 179 |
|
| 180 |
recipe_dropdown.change(
|
| 181 |
fn=get_recipe_details,
|
| 182 |
inputs=recipe_dropdown,
|
| 183 |
outputs=recipe_output
|
| 184 |
+
) #when user picks option, runs get recipe details and displays full recipe
|
| 185 |
|
| 186 |
demo.launch()
|