aashnaj commited on
Commit
e728ee1
·
verified ·
1 Parent(s): d7672c4
Files changed (1) hide show
  1. app.py +17 -29
app.py CHANGED
@@ -2,11 +2,9 @@ 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"
@@ -22,21 +20,13 @@ def search_recipes(ingredient, cuisine, dietary):
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."
@@ -50,36 +40,34 @@ def get_recipe_details(selected_title):
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
 
 
2
  import requests
3
 
4
  SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
5
+ recipe_id_map = {} # Global variable to store title → ID mapping
6
 
 
 
7
 
 
8
  def search_recipes(ingredient, cuisine, dietary):
9
  global recipe_id_map
10
  url = "https://api.spoonacular.com/recipes/complexSearch"
 
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="")
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="Please select a recipe from the dropdown above.")
 
 
 
 
 
 
 
28
 
29
 
 
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."
 
40
 
41
  title = data.get("title", "Unknown Title")
42
  time = data.get("readyInMinutes", "N/A")
43
+ instructions = data.get("instructions") or "No instructions available."
44
 
45
+ return f"### 🍽️ {title}\n**⏱️ Cook Time:** {time} minutes\n\n**📋 Instructions:**\n{instructions}"
46
 
47
 
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("## 🥗 Spoonacular Recipe Finder")
 
50
 
51
  with gr.Row():
52
+ ingredient = gr.Textbox(label="Preferred Ingredient", placeholder="e.g., chicken")
53
+ cuisine = gr.Textbox(label="Preferred Cuisine", placeholder="e.g., Italian")
54
+ diet = gr.Textbox(label="Dietary Restrictions", placeholder="e.g., vegetarian")
55
 
56
+ search_button = gr.Button("Search Recipes")
57
  recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
58
  recipe_output = gr.Markdown()
59
 
60
+ search_button.click(
61
+ fn=search_recipes,
62
+ inputs=[ingredient, cuisine, diet],
 
63
  outputs=[recipe_dropdown, recipe_output]
64
  )
65
 
66
  recipe_dropdown.change(
67
+ fn=get_recipe_details,
68
  inputs=recipe_dropdown,
69
  outputs=recipe_output
70
  )
71
 
72
+ demo.launch()
73