aashnaj commited on
Commit
6b6fe5a
·
verified ·
1 Parent(s): d60147a

going back

Browse files
Files changed (1) hide show
  1. app.py +60 -23
app.py CHANGED
@@ -1,34 +1,71 @@
1
  import gradio as gr
2
  import requests
3
 
4
- from huggingface_hub import InferenceClient
5
- import requests
6
-
7
- API_KEY = "71259036cfb3405aa5d49c1220a988c5"
8
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
 
10
- def get_recipe(cuisine, ingredient, diet):
 
 
11
  url = "https://api.spoonacular.com/recipes/complexSearch"
12
  params = {
 
13
  "cuisine": cuisine,
14
- "includeIngredients": ingredient,
15
- "diet": diet,
16
- "number": 1,
17
- "addRecipeInformation": True,
18
- "apiKey": API_KEY
19
  }
20
 
21
  res = requests.get(url, params=params)
22
  data = res.json()
23
- if not data.get("results"):
24
- return None, None
25
-
26
- recipe = data["results"][0]
27
- title = recipe["title"]
28
- instructions = recipe.get("instructions", "No instructions provided.")
29
- return title, instructions
30
-
31
- def reword_instruction(instructions):
32
- prompt = f"Reword these cooking instructions to sound friendly and helpful:\n\n{instructions}"
33
- response = client.text_generation(prompt=prompt, max_new_tokens=200, temperature=0.7)
34
- return response.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()