Spaces:
Sleeping
Sleeping
adding chatbot to another tab
Browse files
app.py
CHANGED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
43 |
+
return f"### 🍽️ {title}\n**⏱️ Cook Time:** {time} minutes\n\n**📋 Instructions:**\n{instructions}"
|
44 |
+
|
45 |
+
# Handle chatbot questions
|
46 |
+
def ask_recipe_bot(message, history):
|
47 |
+
# Try to find a recipe ID from previous dropdown results
|
48 |
+
if not recipe_id_map:
|
49 |
+
return "Please use the dropdown tab first to search for a recipe."
|
50 |
+
|
51 |
+
# Use the first recipe ID from the map
|
52 |
+
recipe_id = list(recipe_id_map.values())[0]
|
53 |
+
url = f"https://api.spoonacular.com/recipes/{recipe_id}/nutritionWidget.json"
|
54 |
+
params = {"apiKey": SPOONACULAR_API_KEY}
|
55 |
+
res = requests.get(url, params=params)
|
56 |
+
|
57 |
+
if res.status_code != 200:
|
58 |
+
return "Sorry, I couldn't retrieve nutrition info."
|
59 |
+
|
60 |
+
data = res.json()
|
61 |
+
calories = data.get("calories", "N/A")
|
62 |
+
carbs = data.get("carbs", "N/A")
|
63 |
+
protein = data.get("protein", "N/A")
|
64 |
+
fat = data.get("fat", "N/A")
|
65 |
+
|
66 |
+
if "calorie" in message.lower():
|
67 |
+
return f"This recipe has {calories}."
|
68 |
+
elif "protein" in message.lower():
|
69 |
+
return f"It contains {protein}."
|
70 |
+
elif "carb" in message.lower():
|
71 |
+
return f"It has {carbs}."
|
72 |
+
elif "fat" in message.lower():
|
73 |
+
return f"The fat content is {fat}."
|
74 |
+
elif "scale" in message.lower() or "double" in message.lower():
|
75 |
+
return "You can scale ingredients by multiplying each quantity. For example, to double the recipe, multiply every amount by 2."
|
76 |
+
elif "substitute" in message.lower():
|
77 |
+
return "Let me know the ingredient you'd like to substitute, and I’ll try to help!"
|
78 |
+
else:
|
79 |
+
return "You can ask about calories, protein, carbs, fat, substitutes, or scaling tips."
|
80 |
+
|
81 |
+
# Gradio layout
|
82 |
+
with gr.Blocks() as demo:
|
83 |
+
gr.Markdown("## 🧠🍴 The BiteBot")
|
84 |
+
|
85 |
+
with gr.Tabs():
|
86 |
+
with gr.Tab("Search Recipes"):
|
87 |
+
with gr.Row():
|
88 |
+
ingredient = gr.Textbox(label="Preferred Ingredient")
|
89 |
+
cuisine = gr.Textbox(label="Preferred Cuisine")
|
90 |
+
diet = gr.Textbox(label="Dietary Restrictions")
|
91 |
+
|
92 |
+
search_button = gr.Button("Search Recipes")
|
93 |
+
recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
|
94 |
+
recipe_output = gr.Markdown()
|
95 |
+
|
96 |
+
search_button.click(
|
97 |
+
fn=search_recipes,
|
98 |
+
inputs=[ingredient, cuisine, diet],
|
99 |
+
outputs=[recipe_dropdown, recipe_output]
|
100 |
+
)
|
101 |
+
|
102 |
+
recipe_dropdown.change(
|
103 |
+
fn=get_recipe_details,
|
104 |
+
inputs=recipe_dropdown,
|
105 |
+
outputs=recipe_output
|
106 |
+
)
|
107 |
+
|
108 |
+
with gr.Tab("Ask BiteBot"):
|
109 |
+
chatbot = gr.ChatInterface(fn=ask_recipe_bot, chatbot=gr.Chatbot(height=300))
|
110 |
+
gr.Markdown("💬 Ask about calories, macros, scaling, or substitutions. (Run a recipe search first!)")
|
111 |
+
|
112 |
+
demo.launch()
|