Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,81 +11,31 @@ from bs4 import BeautifulSoup
|
|
| 11 |
from typing import List, Dict, Any, Optional
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
-
|
| 15 |
-
@tool
|
| 16 |
-
def extract_recipe_details(url: str) -> Dict[str, Any]:
|
| 17 |
-
"""Extract recipe details including ingredients, instructions, and nutrition from a webpage.
|
| 18 |
-
|
| 19 |
-
url:
|
| 20 |
-
The complete URL of the recipe webpage to scrape.
|
| 21 |
-
|
| 22 |
-
Returns:
|
| 23 |
-
A dictionary containing ingredients list, cooking instructions, and nutritional information.
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
soup = BeautifulSoup(response.text, "html.parser")
|
| 31 |
-
|
| 32 |
-
# Extract ingredients
|
| 33 |
-
possible_ingredient_selectors = ["ul", "li", ".ingredients", ".recipe-ingredients", ".ingredient-list"]
|
| 34 |
-
ingredients = []
|
| 35 |
-
|
| 36 |
-
for selector in possible_ingredient_selectors:
|
| 37 |
-
for tag in soup.select(selector):
|
| 38 |
-
items = [item.get_text(strip=True) for item in tag.find_all("li")]
|
| 39 |
-
if items:
|
| 40 |
-
ingredients.extend(items)
|
| 41 |
-
|
| 42 |
-
# Extract instructions
|
| 43 |
-
possible_instruction_selectors = ["ol", "li", ".instructions", ".recipe-instructions", ".step", ".method"]
|
| 44 |
-
instructions = []
|
| 45 |
-
|
| 46 |
-
for selector in possible_instruction_selectors:
|
| 47 |
-
for tag in soup.select(selector):
|
| 48 |
-
steps = [step.get_text(strip=True) for step in tag.find_all("li")]
|
| 49 |
-
if steps:
|
| 50 |
-
instructions.extend(steps)
|
| 51 |
-
|
| 52 |
-
# Extract nutritional information (Look for tables or structured text)
|
| 53 |
-
possible_nutrition_selectors = [".nutrition", ".nutritional-info", ".nutrition-facts", "table"]
|
| 54 |
-
nutrition_info = []
|
| 55 |
-
|
| 56 |
-
for selector in possible_nutrition_selectors:
|
| 57 |
-
for tag in soup.select(selector):
|
| 58 |
-
text = tag.get_text(strip=True)
|
| 59 |
-
if text and len(text) > 20:
|
| 60 |
-
nutrition_info.append(text)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
"instructions": instructions if instructions else ["Instructions not found. Check the source link."],
|
| 68 |
-
"nutritional_benefits": nutrition_text
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
except Exception as e:
|
| 72 |
-
return {
|
| 73 |
-
"ingredients": [f"Error extracting ingredients: {str(e)}"],
|
| 74 |
-
"instructions": [f"Error extracting instructions: {str(e)}"],
|
| 75 |
-
"nutritional_benefits": f"Error extracting nutritional info: {str(e)}"
|
| 76 |
-
}
|
| 77 |
|
| 78 |
-
@tool
|
| 79 |
-
def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
|
| 80 |
-
"""Search and extract healthy sweet dish recipes based on diet type.
|
| 81 |
-
|
| 82 |
-
diet_type:
|
| 83 |
-
The specific diet category (e.g., Keto, Paleo, Vegan, Vegetarian, Non-Vegetarian).
|
| 84 |
-
|
| 85 |
Returns:
|
| 86 |
-
A list of
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
"""
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
try:
|
| 91 |
# Perform web search using DuckDuckGo
|
|
@@ -97,20 +47,9 @@ def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
|
|
| 97 |
recipes = []
|
| 98 |
|
| 99 |
for result in search_results:
|
| 100 |
-
url = result.get("href", "")
|
| 101 |
-
recipe_details = extract_recipe_details(url) if url else {
|
| 102 |
-
"ingredients": ["No source available."],
|
| 103 |
-
"instructions": ["No source available."],
|
| 104 |
-
"nutritional_benefits": "No source available."
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
recipes.append({
|
| 108 |
"recipe_name": result.get("title", "Unknown Recipe"),
|
| 109 |
-
"
|
| 110 |
-
"nutritional_benefits": recipe_details["nutritional_benefits"],
|
| 111 |
-
"adaptations": f"This recipe follows a {diet_type} diet. Some modifications may be needed.",
|
| 112 |
-
"instructions": recipe_details["instructions"],
|
| 113 |
-
"source": url
|
| 114 |
})
|
| 115 |
|
| 116 |
return recipes
|
|
@@ -118,9 +57,6 @@ def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
|
|
| 118 |
except Exception as e:
|
| 119 |
return [{"error": f"An error occurred while searching: {str(e)}"}]
|
| 120 |
|
| 121 |
-
|
| 122 |
-
final_answer = FinalAnswerTool()
|
| 123 |
-
|
| 124 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 125 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 126 |
|
|
|
|
| 11 |
from typing import List, Dict, Any, Optional
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
+
def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
+
You are a smart culinary research assistant specializing in healthy alternatives for sweet dishes.
|
| 17 |
+
Your role is to search the internet and compile healthy recipes and cooking methods that cater to specific diet types.
|
| 18 |
+
|
| 19 |
+
When a user provides a diet type—such as Keto, Paleo, Vegan, Vegetarian, or Non-Vegetarian—retrieve and summarize
|
| 20 |
+
reliable, up-to-date information on healthy sweet dish alternatives.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
Ensure that your responses include:
|
| 23 |
+
- Clear Recipe Details: List ingredients and nutritional benefits.
|
| 24 |
+
- Diet-Specific Adaptations: Highlight modifications and substitutions relevant to the specified diet type.
|
| 25 |
+
- Credible Sources: Rely on trusted culinary and nutritional resources to ensure accuracy and quality.
|
| 26 |
|
| 27 |
+
Args:
|
| 28 |
+
diet_type (str): User-provided diet type.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
Returns:
|
| 31 |
+
List[Dict[str, Any]]: A list of dictionaries with the following structure:
|
| 32 |
+
{
|
| 33 |
+
"recipe_name": str, # The name of the recipe.
|
| 34 |
+
"source": str # A credible source or URL where the recipe was found.
|
| 35 |
+
}
|
| 36 |
"""
|
| 37 |
+
|
| 38 |
+
query = f"healthy {diet_type} sweet dish recipe with ingredients and instructions"
|
| 39 |
|
| 40 |
try:
|
| 41 |
# Perform web search using DuckDuckGo
|
|
|
|
| 47 |
recipes = []
|
| 48 |
|
| 49 |
for result in search_results:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
recipes.append({
|
| 51 |
"recipe_name": result.get("title", "Unknown Recipe"),
|
| 52 |
+
"source": result.get("href", "No source available")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
})
|
| 54 |
|
| 55 |
return recipes
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
return [{"error": f"An error occurred while searching: {str(e)}"}]
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 61 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 62 |
|