Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,103 +24,47 @@ def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
|
|
| 24 |
Returns:
|
| 25 |
A list of recipe dictionaries, each containing:
|
| 26 |
- recipe_name: The name of the recipe
|
| 27 |
-
- ingredients: List of ingredients needed
|
| 28 |
-
- instructions: Step-by-step cooking instructions
|
| 29 |
-
- nutritional_benefits: Health benefits and nutrition info
|
| 30 |
-
- adaptations: Diet-specific modifications
|
| 31 |
- source: URL of the recipe source
|
| 32 |
|
| 33 |
-
Input example: "I'm looking for healthy dessert recipes for the Keto diet."
|
| 34 |
-
|
| 35 |
-
get_healthy_cheat_meal:
|
| 36 |
-
[
|
| 37 |
-
{
|
| 38 |
-
"recipe_name": "Keto Chocolate Avocado Mousse",
|
| 39 |
-
"ingredients": ["Avocado", "Cocoa powder", "Sweetener", ...], # Up to 10 ingredients
|
| 40 |
-
"instructions": ["Blend all ingredients...", "Chill for 2 hours...", ...], # Up to 5 instructions
|
| 41 |
-
"nutritional_benefits": "High in healthy fats, low in carbs...",
|
| 42 |
-
"adaptations": "Use a sugar-free sweetener...",
|
| 43 |
-
"source": "https://www.example-keto-recipe.com/chocolate-mousse"
|
| 44 |
-
},
|
| 45 |
-
{
|
| 46 |
-
"recipe_name": "Keto Coconut Macaroons",
|
| 47 |
-
"ingredients": ["Coconut flakes", "Egg whites", "Sweetener", ...], # Up to 10 ingredients
|
| 48 |
-
"instructions": ["Mix ingredients...", "Bake at 350...", ...], # Up to 5 instructions
|
| 49 |
-
"nutritional_benefits": "Gluten-free, dairy-free...",
|
| 50 |
-
"adaptations": "Use a sugar substitute...",
|
| 51 |
-
"source": "https://www.another-keto-recipe.com/macaroons"
|
| 52 |
-
},
|
| 53 |
-
# ... up to 5 recipes in total
|
| 54 |
-
]
|
| 55 |
-
The get_healthy_cheat_meal tool MUST ALWAYS return a JSON list of dictionaries, where each dictionary represents a recipe and contains the keys "recipe_name", "ingredients", "instructions", "nutritional_benefits", "adaptations", and "source". The "ingredients" value MUST be a list of strings (up to 10 ingredients). The "instructions" value MUST be a list of strings (up to 5 instructions). If no recipes are found, the tool MUST return: '{"error": "No recipes found. Try a different diet type or check your query."}'. If there is an error during recipe processing, the tool MUST return: '{"error": "Unable to process recipes. Please try again."}'. The tool should prioritize returning detailed recipe information (ingredients, instructions) whenever possible. If detailed information cannot be extracted, the tool should still return a list of dictionaries with at least the "recipe_name" and "source" for each recipe.
|
| 56 |
"""
|
| 57 |
query = f"healthy {diet_type} sweet dish recipe"
|
| 58 |
|
| 59 |
try:
|
| 60 |
-
ddgs = DDGS()
|
| 61 |
-
search_results = ddgs.text(query, region="us-en", safesearch="moderate", max_results=
|
| 62 |
|
| 63 |
if not search_results:
|
| 64 |
-
return
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
for result in search_results:
|
| 69 |
try:
|
| 70 |
-
title = result.get('title', '').split(' - ')[0].strip()
|
| 71 |
-
url = result.get('href', '')
|
| 72 |
-
|
| 73 |
-
if not url: # If URL is missing, skip the result
|
| 74 |
-
continue
|
| 75 |
-
|
| 76 |
-
response = requests.get(url, timeout=10, headers={
|
| 77 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 78 |
-
})
|
| 79 |
|
| 80 |
-
if
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
ingredients.append(ingredient.get_text().strip())
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
recipe = {
|
| 92 |
-
"recipe_name": title,
|
| 93 |
-
"ingredients": ingredients[:10] if ingredients else ["Please check source for ingredients"],
|
| 94 |
-
"instructions": instructions[:5] if instructions else ["Please check source for instructions"],
|
| 95 |
-
"nutritional_benefits": f"This {diet_type} recipe is designed to be a healthier alternative to traditional sweet dishes.",
|
| 96 |
-
"adaptations": f"This recipe is suitable for {diet_type} diet. Adjust sweeteners and ingredients according to your specific dietary needs.",
|
| 97 |
-
"source": url
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
recipes.append(recipe)
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
-
print(f"Error processing
|
| 104 |
-
continue # Skip
|
| 105 |
-
time.sleep(1) # Added a small delay to respect rate limits
|
| 106 |
-
|
| 107 |
|
| 108 |
-
if not
|
| 109 |
-
|
| 110 |
-
for result in search_results:
|
| 111 |
-
recipes.append({
|
| 112 |
-
"recipe_name": result.get('title', '').split(' - ')[0].strip(), # Use get and handle missing keys
|
| 113 |
-
"ingredients": ["Please check source for full ingredients list"],
|
| 114 |
-
"instructions": ["Please check source for detailed instructions"],
|
| 115 |
-
"nutritional_benefits": f"This {diet_type} recipe is designed to be a healthier alternative to traditional sweet dishes.",
|
| 116 |
-
"adaptations": f"This recipe is suitable for {diet_type} diet. Adjust sweeteners and ingredients according to your specific dietary needs.",
|
| 117 |
-
"source": result.get('href', '') # Use get and handle missing keys
|
| 118 |
-
})
|
| 119 |
|
| 120 |
-
return
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
-
return
|
| 124 |
|
| 125 |
|
| 126 |
|
|
|
|
| 24 |
Returns:
|
| 25 |
A list of recipe dictionaries, each containing:
|
| 26 |
- recipe_name: The name of the recipe
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
- source: URL of the recipe source
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"""
|
| 30 |
query = f"healthy {diet_type} sweet dish recipe"
|
| 31 |
|
| 32 |
try:
|
| 33 |
+
ddgs = DDGS()
|
| 34 |
+
search_results = ddgs.text(query, region="us-en", safesearch="moderate", max_results=20) # Get more results
|
| 35 |
|
| 36 |
if not search_results:
|
| 37 |
+
return "No recipes found. Try a different diet type or check your query."
|
| 38 |
|
| 39 |
+
formatted_recipes = ""
|
| 40 |
+
count = 0 # Keep track of how many recipes we've added
|
| 41 |
|
| 42 |
for result in search_results:
|
| 43 |
try:
|
| 44 |
+
title = result.get('title', '').split(' - ')[0].strip()
|
| 45 |
+
url = result.get('href', '')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
if not url:
|
| 48 |
+
continue # Skip if no URL
|
| 49 |
|
| 50 |
+
formatted_recipes += f"**{title}**\n" # Bold title
|
| 51 |
+
formatted_recipes += f"Source: {url}\n\n" # Add URL
|
|
|
|
| 52 |
|
| 53 |
+
count += 1
|
| 54 |
+
if count >= 10: # Stop after 10 recipes
|
| 55 |
+
break # exit for loop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
print(f"Error processing recipe: {e}")
|
| 59 |
+
continue # Skip if there's an error
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
if not formatted_recipes: # If no recipes were added
|
| 62 |
+
return "No valid recipes found. Please try a different search."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
return formatted_recipes # Return the formatted string
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
+
return f"An error occurred: {str(e)}"
|
| 68 |
|
| 69 |
|
| 70 |
|