Spaces:
Sleeping
Sleeping
Update tool.py
Browse files
tool.py
CHANGED
@@ -7,43 +7,96 @@ class SimpleTool(Tool):
|
|
7 |
inputs = {"ingredients":{"type":"string","description":"A comma-separated string of available ingredients."},"diet":{"type":"string","nullable":True,"description":"Dietary restrictions such as 'vegetarian', 'vegan', or 'gluten free'. Defaults to None."}}
|
8 |
output_type = "string"
|
9 |
|
10 |
-
|
|
|
11 |
"""
|
12 |
-
Gets a recipe suggestion based on
|
|
|
|
|
13 |
|
14 |
Args:
|
15 |
ingredients: A comma-separated string of available ingredients.
|
16 |
diet: Dietary restrictions such as 'vegetarian', 'vegan', or 'gluten free'. Defaults to None.
|
|
|
|
|
|
|
|
|
17 |
"""
|
18 |
import os
|
19 |
import requests
|
20 |
|
21 |
-
# Retrieve your Spoonacular API key from the environment
|
22 |
api_key = "0cbb3d6a82f5497e818d1f63dc736218"
|
23 |
if not api_key:
|
24 |
return "Spoonacular API key not set. Please set the SPOONACULAR_API_KEY environment variable."
|
25 |
|
26 |
-
|
|
|
27 |
params = {
|
28 |
"ingredients": ingredients,
|
29 |
-
"number": 1, #
|
30 |
"ranking": 1,
|
31 |
"apiKey": api_key,
|
32 |
}
|
|
|
|
|
33 |
if diet:
|
34 |
params["diet"] = diet
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
if not data:
|
41 |
-
return "No recipes found with these ingredients and dietary restrictions."
|
42 |
-
recipe = data[0]
|
43 |
-
title = recipe.get("title", "Untitled Recipe")
|
44 |
-
used_count = recipe.get("usedIngredientCount", 0)
|
45 |
-
missed_count = recipe.get("missedIngredientCount", 0)
|
46 |
-
return (f"Recipe suggestion: {title} "
|
47 |
-
f"(used {used_count} of your ingredients, missing {missed_count} additional ingredient(s)).")
|
48 |
except Exception as e:
|
49 |
-
return f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
inputs = {"ingredients":{"type":"string","description":"A comma-separated string of available ingredients."},"diet":{"type":"string","nullable":True,"description":"Dietary restrictions such as 'vegetarian', 'vegan', or 'gluten free'. Defaults to None."}}
|
8 |
output_type = "string"
|
9 |
|
10 |
+
|
11 |
+
def forward(self, ingredients: str, diet: Optional[str] = None, laziness: Optional[int] = 5) -> str:
|
12 |
"""
|
13 |
+
Gets a recipe suggestion based on provided ingredients, dietary preference,
|
14 |
+
and your laziness level (1=active chef, 10=super lazy). After finding a recipe, it
|
15 |
+
retrieves detailed recipe information.
|
16 |
|
17 |
Args:
|
18 |
ingredients: A comma-separated string of available ingredients.
|
19 |
diet: Dietary restrictions such as 'vegetarian', 'vegan', or 'gluten free'. Defaults to None.
|
20 |
+
laziness: An integer from 1 (active) to 10 (super lazy); higher means recipes with quicker prep.
|
21 |
+
|
22 |
+
Returns:
|
23 |
+
A string with detailed information about the recommended recipe.
|
24 |
"""
|
25 |
import os
|
26 |
import requests
|
27 |
|
|
|
28 |
api_key = "0cbb3d6a82f5497e818d1f63dc736218"
|
29 |
if not api_key:
|
30 |
return "Spoonacular API key not set. Please set the SPOONACULAR_API_KEY environment variable."
|
31 |
|
32 |
+
# Step 1: Search for recipes using the ingredients
|
33 |
+
base_search_url = "https://api.spoonacular.com/recipes/findByIngredients"
|
34 |
params = {
|
35 |
"ingredients": ingredients,
|
36 |
+
"number": 1, # We want one suggestion
|
37 |
"ranking": 1,
|
38 |
"apiKey": api_key,
|
39 |
}
|
40 |
+
|
41 |
+
# Add diet if provided
|
42 |
if diet:
|
43 |
params["diet"] = diet
|
44 |
|
45 |
+
# Incorporate the laziness factor: filter by maxReadyTime if needed.
|
46 |
+
try:
|
47 |
+
laziness = int(laziness)
|
48 |
+
except ValueError:
|
49 |
+
return "Laziness must be an integer from 1 to 10."
|
50 |
+
if laziness >= 8:
|
51 |
+
params["maxReadyTime"] = 15 # 15 minutes for super lazy cooks
|
52 |
+
elif 5 <= laziness < 8:
|
53 |
+
params["maxReadyTime"] = 30 # 30 minutes for moderately lazy cooks
|
54 |
+
|
55 |
+
try:
|
56 |
+
search_response = requests.get(base_search_url, params=params)
|
57 |
+
search_response.raise_for_status()
|
58 |
+
search_data = search_response.json()
|
59 |
+
except Exception as e:
|
60 |
+
return f"Error during recipe search: {e}"
|
61 |
+
|
62 |
+
if not search_data:
|
63 |
+
return "No recipes found with these parameters. Try adjusting your ingredients, diet, or laziness level."
|
64 |
+
|
65 |
+
# Assume the first result is the best match
|
66 |
+
recipe = search_data[0]
|
67 |
+
recipe_id = recipe.get("id")
|
68 |
+
if not recipe_id:
|
69 |
+
return "Recipe ID not found in the search result."
|
70 |
+
|
71 |
+
# Step 2: Retrieve detailed recipe information using the recipe id
|
72 |
+
info_url = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
|
73 |
+
info_params = {
|
74 |
+
"apiKey": api_key,
|
75 |
+
}
|
76 |
try:
|
77 |
+
info_response = requests.get(info_url, params=info_params)
|
78 |
+
info_response.raise_for_status()
|
79 |
+
info_data = info_response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
except Exception as e:
|
81 |
+
return f"Error retrieving detailed recipe information: {e}"
|
82 |
+
|
83 |
+
# Extract detailed information
|
84 |
+
title = info_data.get("title", "Untitled Recipe")
|
85 |
+
ready_time = info_data.get("readyInMinutes", "N/A")
|
86 |
+
servings = info_data.get("servings", "N/A")
|
87 |
+
instructions = info_data.get("instructions", "No instructions provided.")
|
88 |
+
|
89 |
+
# Extract ingredients list (names only)
|
90 |
+
ingredients_list = info_data.get("extendedIngredients", [])
|
91 |
+
ingredients_names = [ing.get("name") for ing in ingredients_list if ing.get("name")]
|
92 |
+
ingredients_str = ", ".join(ingredients_names) if ingredients_names else "N/A"
|
93 |
+
|
94 |
+
# Build the detailed summary message
|
95 |
+
detailed_info = (
|
96 |
+
f"Recipe suggestion: {title}\n"
|
97 |
+
f"Ready in: {ready_time} minutes | Servings: {servings}\n"
|
98 |
+
f"Ingredients: {ingredients_str}\n\n"
|
99 |
+
f"Instructions:\n{instructions}"
|
100 |
+
)
|
101 |
+
|
102 |
+
return detailed_info
|