aashnaj commited on
Commit
d7672c4
·
verified ·
1 Parent(s): 728a28e

choose recipe

Browse files
Files changed (1) hide show
  1. app.py +68 -69
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
- # Function to get recipes from Spoonacular
8
- def get_recipes(ingredient, cuisine, dietary):
 
 
 
 
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. Try different inputs."
22
-
23
- output = ""
24
- for r in data["results"]:
25
- output += f"🍽️ {r['title']}\n"
26
-
27
- return output.strip()
28
-
29
-
30
- # Gradio Interface for Recipe Finder
31
- recipe_interface = gr.Interface(
32
- fn=get_recipes,
33
- inputs=[
34
- gr.Textbox(label="Preferred Ingredient (e.g., chicken, tofu)"),
35
- gr.Textbox(label="Preferred Cuisine (e.g., Indian, Italian, Thai)"),
36
- gr.Textbox(label="Dietary Restrictions (e.g., vegetarian, vegan, gluten free)")
37
- ],
38
- outputs="text",
39
- title="🥗 Spoonacular Recipe Finder",
40
- description="Get 3 recipe suggestions based on your ingredient, cuisine, and dietary preferences."
41
- )
42
-
43
-
44
- # Chatbot setup using HuggingFace InferenceClient
45
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
46
-
47
- def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
48
- messages = [{"role": "system", "content": system_message}]
49
-
50
- for val in history:
51
- if val[0]:
52
- messages.append({"role": "user", "content": val[0]})
53
- if val[1]:
54
- messages.append({"role": "assistant", "content": val[1]})
55
-
56
- messages.append({"role": "user", "content": message})
57
-
58
- response = ""
59
- for message in client.chat_completion(
60
- messages,
61
- max_tokens=max_tokens,
62
- stream=True,
63
- temperature=temperature,
64
- top_p=top_p,
65
- ):
66
- token = message.choices[0].delta.content
67
- response += token
68
- yield response
69
-
70
-
71
- # Gradio ChatInterface
72
- chat_interface = gr.ChatInterface(
73
- respond,
74
- additional_inputs=[
75
- gr.Textbox(value="You are a friendly chatbot.", label="System message"),
76
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
77
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
78
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
79
- ],
80
- title="💬 AI Chatbot"
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
+