choose recipe
Browse files
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
url = "https://api.spoonacular.com/recipes/complexSearch"
|
| 10 |
params = {
|
| 11 |
"query": ingredient,
|
|
@@ -14,73 +17,69 @@ def get_recipes(ingredient, cuisine, dietary):
|
|
| 14 |
"number": 3,
|
| 15 |
"apiKey": SPOONACULAR_API_KEY
|
| 16 |
}
|
|
|
|
| 17 |
res = requests.get(url, params=params)
|
| 18 |
data = res.json()
|
| 19 |
|
| 20 |
if "results" not in data or not data["results"]:
|
| 21 |
-
return "No recipes found
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
for r in data["results"]
|
| 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 |
-
# Launch both UIs
|
| 85 |
-
with gr.TabbedInterface([recipe_interface, chat_interface], tab_names=["🍴 Recipe Finder", "💬 Chatbot"]) as demo:
|
| 86 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
|
| 5 |
|
| 6 |
+
# Global variable to store recipe ID mappings
|
| 7 |
+
recipe_id_map = {}
|
| 8 |
+
|
| 9 |
+
# Step 1: Search for recipes
|
| 10 |
+
def search_recipes(ingredient, cuisine, dietary):
|
| 11 |
+
global recipe_id_map
|
| 12 |
url = "https://api.spoonacular.com/recipes/complexSearch"
|
| 13 |
params = {
|
| 14 |
"query": ingredient,
|
|
|
|
| 17 |
"number": 3,
|
| 18 |
"apiKey": SPOONACULAR_API_KEY
|
| 19 |
}
|
| 20 |
+
|
| 21 |
res = requests.get(url, params=params)
|
| 22 |
data = res.json()
|
| 23 |
|
| 24 |
if "results" not in data or not data["results"]:
|
| 25 |
+
return gr.Dropdown(choices=[], label="No recipes found", interactive=False), gr.update(visible=False)
|
| 26 |
+
|
| 27 |
+
# Map recipe titles to their IDs
|
| 28 |
+
recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
|
| 29 |
+
|
| 30 |
+
dropdown = gr.Dropdown(
|
| 31 |
+
choices=list(recipe_id_map.keys()),
|
| 32 |
+
label="Select a recipe to view full instructions",
|
| 33 |
+
interactive=True
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return dropdown, gr.update(visible=True)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Step 2: Fetch recipe details by ID
|
| 40 |
+
def get_recipe_details(selected_title):
|
| 41 |
+
if not selected_title or selected_title not in recipe_id_map:
|
| 42 |
+
return "Please select a valid recipe."
|
| 43 |
+
|
| 44 |
+
recipe_id = recipe_id_map[selected_title]
|
| 45 |
+
|
| 46 |
+
url = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
|
| 47 |
+
params = {"apiKey": SPOONACULAR_API_KEY}
|
| 48 |
+
res = requests.get(url, params=params)
|
| 49 |
+
data = res.json()
|
| 50 |
+
|
| 51 |
+
title = data.get("title", "Unknown Title")
|
| 52 |
+
time = data.get("readyInMinutes", "N/A")
|
| 53 |
+
instructions = data.get("instructions", "No instructions available.")
|
| 54 |
+
|
| 55 |
+
return f"🍽️ **{title}**\n⏱️ Ready in: {time} minutes\n\n📋 Instructions:\n{instructions}"
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Interface
|
| 59 |
+
with gr.Blocks() as recipe_app:
|
| 60 |
+
gr.Markdown("## 🥗 Spoonacular Recipe Finder\nFind recipes based on your preferences.")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
ingredient_input = gr.Textbox(label="Preferred Ingredient")
|
| 64 |
+
cuisine_input = gr.Textbox(label="Preferred Cuisine")
|
| 65 |
+
dietary_input = gr.Textbox(label="Dietary Restrictions")
|
| 66 |
+
|
| 67 |
+
search_btn = gr.Button("🔍 Search Recipes")
|
| 68 |
+
recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
|
| 69 |
+
recipe_output = gr.Markdown()
|
| 70 |
+
|
| 71 |
+
# Actions
|
| 72 |
+
search_btn.click(
|
| 73 |
+
search_recipes,
|
| 74 |
+
inputs=[ingredient_input, cuisine_input, dietary_input],
|
| 75 |
+
outputs=[recipe_dropdown, recipe_output]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
recipe_dropdown.change(
|
| 79 |
+
get_recipe_details,
|
| 80 |
+
inputs=recipe_dropdown,
|
| 81 |
+
outputs=recipe_output
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
recipe_app.launch()
|
| 85 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|