aashnaj commited on
Commit
b1ae429
·
verified ·
1 Parent(s): 51b4a98

create num2

Browse files
Files changed (1) hide show
  1. number2 +114 -0
number2 ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
5
+ recipe_id_map = {}
6
+
7
+ # Search 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
+ res = requests.get(url, params=params)
19
+ data = res.json()
20
+
21
+ if "results" not in data or not data["results"]:
22
+ recipe_id_map = {}
23
+ return gr.update(choices=[], visible=True, label="No recipes found"), gr.update(value="No recipes found.")
24
+
25
+ recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
26
+ return gr.update(choices=list(recipe_id_map.keys()), visible=True), gr.update(value="Select a recipe from the dropdown.")
27
+
28
+ # Get recipe details
29
+ def get_recipe_details(selected_title):
30
+ if not selected_title or selected_title not in recipe_id_map:
31
+ return "Please select a valid recipe."
32
+
33
+ recipe_id = recipe_id_map[selected_title]
34
+ url = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
35
+ params = {"apiKey": SPOONACULAR_API_KEY}
36
+ res = requests.get(url, params=params)
37
+ data = res.json()
38
+
39
+ title = data.get("title", "Unknown Title")
40
+ time = data.get("readyInMinutes", "N/A")
41
+ instructions = data.get("instructions") or "No instructions available."
42
+ ingredients_list = data.get("extendedIngredients", [])
43
+ ingredients = "\n".join([f"- {item.get('original')}" for item in ingredients_list])
44
+
45
+ return f"### 🍽️ {title}\n**⏱️ Cook Time:** {time} minutes\n\n**📋 Instructions:**\n{instructions}"
46
+ gr.Markdown("💬 Go to the next tab to ask our chatbot your questions on the recipe!")
47
+ # Handle chatbot questions
48
+ def ask_recipe_bot(message, history):
49
+ # Try to find a recipe ID from previous dropdown results
50
+ if not recipe_id_map:
51
+ return "Please use the dropdown tab first to search for a recipe."
52
+
53
+ # Use the first recipe ID from the map
54
+ recipe_id = list(recipe_id_map.values())[0]
55
+ url = f"https://api.spoonacular.com/recipes/{recipe_id}/nutritionWidget.json"
56
+ params = {"apiKey": SPOONACULAR_API_KEY}
57
+ res = requests.get(url, params=params)
58
+
59
+ if res.status_code != 200:
60
+ return "Sorry, I couldn't retrieve nutrition info."
61
+
62
+ data = res.json()
63
+ calories = data.get("calories", "N/A")
64
+ carbs = data.get("carbs", "N/A")
65
+ protein = data.get("protein", "N/A")
66
+ fat = data.get("fat", "N/A")
67
+
68
+ if "calorie" in message.lower():
69
+ return f"This recipe has {calories}."
70
+ elif "protein" in message.lower():
71
+ return f"It contains {protein}."
72
+ elif "carb" in message.lower():
73
+ return f"It has {carbs}."
74
+ elif "fat" in message.lower():
75
+ return f"The fat content is {fat}."
76
+ elif "scale" in message.lower() or "double" in message.lower():
77
+ return "You can scale ingredients by multiplying each quantity. For example, to double the recipe, multiply every amount by 2."
78
+ elif "substitute" in message.lower():
79
+ return "Let me know the ingredient you'd like to substitute, and I’ll try to help!"
80
+ else:
81
+ return "You can ask about calories, protein, carbs, fat, substitutes, or scaling tips."
82
+
83
+ # Gradio layout
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("## 🧠🍴 The BiteBot")
86
+
87
+ with gr.Tabs():
88
+ with gr.Tab("Search Recipes"):
89
+ with gr.Row():
90
+ ingredient = gr.Textbox(label="Preferred Ingredient")
91
+ cuisine = gr.Textbox(label="Preferred Cuisine")
92
+ diet = gr.Textbox(label="Dietary Restrictions")
93
+
94
+ search_button = gr.Button("Search Recipes")
95
+ recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
96
+ recipe_output = gr.Markdown()
97
+
98
+ search_button.click(
99
+ fn=search_recipes,
100
+ inputs=[ingredient, cuisine, diet],
101
+ outputs=[recipe_dropdown, recipe_output]
102
+ )
103
+
104
+ recipe_dropdown.change(
105
+ fn=get_recipe_details,
106
+ inputs=recipe_dropdown,
107
+ outputs=recipe_output
108
+ )
109
+
110
+ with gr.Tab("Ask BiteBot"):
111
+ chatbot = gr.ChatInterface(fn=ask_recipe_bot, chatbot=gr.Chatbot(height=300))
112
+ gr.Markdown("💬 Ask about calories, macros, scaling, or substitutions. (Run a recipe search first!)")
113
+
114
+ demo.launch()