Spaces:
Sleeping
Sleeping
create app
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
|
5 |
+
recipe_id_map = {}
|
6 |
+
|
7 |
+
# search for recipes
|
8 |
+
def search_recipes(ingredient, cuisine, dietary):
|
9 |
+
global recipe_id_map
|
10 |
+
url = "https://api.spoonacular.com/recipes/complexSearch"
|
11 |
+
params = {
|
12 |
+
"query": ingredient,
|
13 |
+
"cuisine": cuisine,
|
14 |
+
"diet": dietary,
|
15 |
+
"number": 3,
|
16 |
+
"apiKey": SPOONACULAR_API_KEY
|
17 |
+
}
|
18 |
+
|
19 |
+
res = requests.get(url, params=params)
|
20 |
+
data = res.json()
|
21 |
+
|
22 |
+
if "results" not in data or not data["results"]:
|
23 |
+
recipe_id_map = {}
|
24 |
+
return gr.update(choices=[], visible=True, label="No recipes found"), gr.update(value="No recipes found.")
|
25 |
+
|
26 |
+
recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
|
27 |
+
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.")
|
28 |
+
|
29 |
+
# get recipe details
|
30 |
+
def get_recipe_details(selected_title):
|
31 |
+
if not selected_title or selected_title not in recipe_id_map:
|
32 |
+
return "Please select a valid recipe."
|
33 |
+
|
34 |
+
recipe_id = recipe_id_map[selected_title]
|
35 |
+
url = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
|
36 |
+
params = {"apiKey": SPOONACULAR_API_KEY}
|
37 |
+
res = requests.get(url, params=params)
|
38 |
+
data = res.json()
|
39 |
+
|
40 |
+
title = data.get("title", "Unknown Title")
|
41 |
+
time = data.get("readyInMinutes", "N/A")
|
42 |
+
instructions = data.get("instructions") or "No instructions available."
|
43 |
+
return f"### 🍽️ {title}\n**⏱️ Cook Time:** {time} minutes\n\n**📋 Instructions:**\n{instructions}"
|
44 |
+
|
45 |
+
|
46 |
+
# UI
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
gr.Markdown("## 🥗 The BiteBot")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
ingredient = gr.Textbox(label="Preferred Ingredient", placeholder="e.g., chicken")
|
52 |
+
cuisine = gr.Textbox(label="Preferred Cuisine", placeholder="e.g., Indian")
|
53 |
+
diet = gr.Textbox(label="Dietary Restrictions", placeholder="e.g., vegetarian")
|
54 |
+
|
55 |
+
search_button = gr.Button("Search Recipes")
|
56 |
+
recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
|
57 |
+
recipe_output = gr.Markdown()
|
58 |
+
|
59 |
+
search_button.click(
|
60 |
+
fn=search_recipes,
|
61 |
+
inputs=[ingredient, cuisine, diet],
|
62 |
+
outputs=[recipe_dropdown, recipe_output]
|
63 |
+
)
|
64 |
+
|
65 |
+
recipe_dropdown.change(
|
66 |
+
fn=get_recipe_details,
|
67 |
+
inputs=recipe_dropdown,
|
68 |
+
outputs=recipe_output
|
69 |
+
)
|
70 |
+
|
71 |
+
demo.launch()
|