Spaces:
Sleeping
Sleeping
New test
Browse files- number2 +0 -114
- number2.py +145 -0
number2
DELETED
@@ -1,114 +0,0 @@
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
number2.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
!pip install -q sentence-transformers
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
import torch
|
6 |
+
|
7 |
+
|
8 |
+
SPOONACULAR_API_KEY = "71259036cfb3405aa5d49c1220a988c5"
|
9 |
+
recipe_id_map = {}
|
10 |
+
|
11 |
+
data = ""
|
12 |
+
# # Search recipes
|
13 |
+
def search_recipes(ingredient, cuisine, dietary):
|
14 |
+
global recipe_id_map
|
15 |
+
url = "https://api.spoonacular.com/recipes/complexSearch"
|
16 |
+
params = {
|
17 |
+
"query": ingredient,
|
18 |
+
"cuisine": cuisine,
|
19 |
+
"diet": dietary,
|
20 |
+
"number": 3,
|
21 |
+
"apiKey": SPOONACULAR_API_KEY
|
22 |
+
}
|
23 |
+
res = requests.get(url, params=params)
|
24 |
+
data = res.json()
|
25 |
+
|
26 |
+
|
27 |
+
# if "results" not in data or not data["results"]:
|
28 |
+
# recipe_id_map = {}
|
29 |
+
# return gr.update(choices=[], visible=True, label="No recipes found"), gr.update(value="No recipes found.")
|
30 |
+
|
31 |
+
# recipe_id_map = {r["title"]: r["id"] for r in data["results"]}
|
32 |
+
# return gr.update(choices=list(recipe_id_map.keys()), visible=True), gr.update(value="Select a recipe from the dropdown.")
|
33 |
+
|
34 |
+
# # Get recipe details
|
35 |
+
# def get_recipe_details(selected_title):
|
36 |
+
# if not selected_title or selected_title not in recipe_id_map:
|
37 |
+
# return "Please select a valid recipe."
|
38 |
+
|
39 |
+
# recipe_id = recipe_id_map[selected_title]
|
40 |
+
# url = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
|
41 |
+
# params = {"apiKey": SPOONACULAR_API_KEY}
|
42 |
+
# res = requests.get(url, params=params)
|
43 |
+
# data = res.json()
|
44 |
+
|
45 |
+
# title = data.get("title", "Unknown Title")
|
46 |
+
# time = data.get("readyInMinutes", "N/A")
|
47 |
+
# instructions = data.get("instructions") or "No instructions available."
|
48 |
+
# ingredients_list = data.get("extendedIngredients", [])
|
49 |
+
# ingredients = "\n".join([f"- {item.get('original')}" for item in ingredients_list])
|
50 |
+
|
51 |
+
# return f"### 🍽️ {title}\n**⏱️ Cook Time:** {time} minutes\n\n**📋 Instructions:**\n{instructions}"
|
52 |
+
# gr.Markdown("💬 Go to the next tab to ask our chatbot your questions on the recipe!")
|
53 |
+
# # Handle chatbot questions
|
54 |
+
# def ask_recipe_bot(message, history):
|
55 |
+
# # Try to find a recipe ID from previous dropdown results
|
56 |
+
# if not recipe_id_map:
|
57 |
+
# return "Please use the dropdown tab first to search for a recipe."
|
58 |
+
|
59 |
+
# # Use the first recipe ID from the map
|
60 |
+
# recipe_id = list(recipe_id_map.values())[0]
|
61 |
+
# url = f"https://api.spoonacular.com/recipes/{recipe_id}/nutritionWidget.json"
|
62 |
+
# params = {"apiKey": SPOONACULAR_API_KEY}
|
63 |
+
# res = requests.get(url, params=params)
|
64 |
+
|
65 |
+
# if res.status_code != 200:
|
66 |
+
# return "Sorry, I couldn't retrieve nutrition info."
|
67 |
+
|
68 |
+
# data = res.json()
|
69 |
+
# calories = data.get("calories", "N/A")
|
70 |
+
# carbs = data.get("carbs", "N/A")
|
71 |
+
# protein = data.get("protein", "N/A")
|
72 |
+
# fat = data.get("fat", "N/A")
|
73 |
+
|
74 |
+
# if "calorie" in message.lower():
|
75 |
+
# return f"This recipe has {calories}."
|
76 |
+
# elif "protein" in message.lower():
|
77 |
+
# return f"It contains {protein}."
|
78 |
+
# elif "carb" in message.lower():
|
79 |
+
# return f"It has {carbs}."
|
80 |
+
# elif "fat" in message.lower():
|
81 |
+
# return f"The fat content is {fat}."
|
82 |
+
# elif "scale" in message.lower() or "double" in message.lower():
|
83 |
+
# return "You can scale ingredients by multiplying each quantity. For example, to double the recipe, multiply every amount by 2."
|
84 |
+
# elif "substitute" in message.lower():
|
85 |
+
# return "Let me know the ingredient you'd like to substitute, and I’ll try to help!"
|
86 |
+
# else:
|
87 |
+
# return "You can ask about calories, protein, carbs, fat, substitutes, or scaling tips."
|
88 |
+
|
89 |
+
def respond(message, history):
|
90 |
+
|
91 |
+
context = search_recipes(ingredient, cuisine, diet)
|
92 |
+
messages = [
|
93 |
+
{
|
94 |
+
"role": "system",
|
95 |
+
"content": f"You give recipes from {context}."
|
96 |
+
}
|
97 |
+
]
|
98 |
+
if history:
|
99 |
+
messages.extend(history)
|
100 |
+
messages.append({"role": "user", "content": message})
|
101 |
+
stream = client.chat_completion(
|
102 |
+
messages,
|
103 |
+
max_tokens=300,
|
104 |
+
temperature=1.2,
|
105 |
+
stream=True,
|
106 |
+
)
|
107 |
+
for message in stream:
|
108 |
+
token = message.choices[0].delta.content
|
109 |
+
if token is not None:
|
110 |
+
response += token
|
111 |
+
yield response
|
112 |
+
|
113 |
+
|
114 |
+
# # Gradio layout
|
115 |
+
with gr.Blocks() as demo:
|
116 |
+
gr.Markdown("## 🧠🍴 The BiteBot")
|
117 |
+
|
118 |
+
with gr.Tabs():
|
119 |
+
# with gr.Tab("Search Recipes"):
|
120 |
+
with gr.Row():
|
121 |
+
ingredient = gr.Textbox(label="Preferred Ingredient")
|
122 |
+
cuisine = gr.Textbox(label="Preferred Cuisine")
|
123 |
+
diet = gr.Textbox(label="Dietary Restrictions")
|
124 |
+
|
125 |
+
# search_button = gr.Button("Search Recipes")
|
126 |
+
# recipe_dropdown = gr.Dropdown(label="Select a recipe", visible=False)
|
127 |
+
# recipe_output = gr.Markdown()
|
128 |
+
|
129 |
+
# search_button.click(
|
130 |
+
# fn=search_recipes,
|
131 |
+
# inputs=[ingredient, cuisine, diet],
|
132 |
+
# outputs=[recipe_dropdown, recipe_output]
|
133 |
+
# )
|
134 |
+
|
135 |
+
# recipe_dropdown.change(
|
136 |
+
# fn=get_recipe_details,
|
137 |
+
# inputs=recipe_dropdown,
|
138 |
+
# outputs=recipe_output
|
139 |
+
# )
|
140 |
+
|
141 |
+
# with gr.Tab("Ask BiteBot"):
|
142 |
+
chatbot = gr.ChatInterface(fn=respond, chatbot=gr.Chatbot(height=300))
|
143 |
+
gr.Markdown("💬 Ask about calories, macros, scaling, or substitutions. (Run a recipe search first!)")
|
144 |
+
|
145 |
+
demo.launch()
|