Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,20 +9,20 @@ from tools.web_search import DuckDuckGoSearchTool
|
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
import re
|
| 12 |
-
from typing import List, Dict, Any
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
import time # For rate limiting
|
| 15 |
|
| 16 |
|
| 17 |
@tool
|
| 18 |
-
def get_healthy_cheat_meal(diet_type: str) -> str:
|
| 19 |
"""Search for healthy sweet dish alternatives based on specific diet types.
|
| 20 |
|
| 21 |
Args:
|
| 22 |
diet_type: The specific diet type to search for (e.g., Keto, Paleo, Vegan, Vegetarian, Non-Vegetarian).
|
| 23 |
|
| 24 |
Returns:
|
| 25 |
-
A
|
| 26 |
|
| 27 |
"""
|
| 28 |
query = f"healthy {diet_type} sweet dish recipe"
|
|
@@ -64,7 +64,35 @@ def get_healthy_cheat_meal(diet_type: str) -> str:
|
|
| 64 |
except Exception as e:
|
| 65 |
return f"An error occurred: {str(e)}"
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Initialize tools
|
| 70 |
final_answer = FinalAnswerTool()
|
|
@@ -89,7 +117,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 89 |
|
| 90 |
agent = CodeAgent(
|
| 91 |
model=model,
|
| 92 |
-
tools=[get_healthy_cheat_meal], ## add your tools here (don't remove final answer)
|
| 93 |
max_steps=6,
|
| 94 |
verbosity_level=1,
|
| 95 |
grammar=None,
|
|
|
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
import re
|
| 12 |
+
from typing import List, Dict, Any
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
import time # For rate limiting
|
| 15 |
|
| 16 |
|
| 17 |
@tool
|
| 18 |
+
def get_healthy_cheat_meal(diet_type: str) -> List[str]:
|
| 19 |
"""Search for healthy sweet dish alternatives based on specific diet types.
|
| 20 |
|
| 21 |
Args:
|
| 22 |
diet_type: The specific diet type to search for (e.g., Keto, Paleo, Vegan, Vegetarian, Non-Vegetarian).
|
| 23 |
|
| 24 |
Returns:
|
| 25 |
+
A list of strings containing the recipe name/title and the website url.
|
| 26 |
|
| 27 |
"""
|
| 28 |
query = f"healthy {diet_type} sweet dish recipe"
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
return f"An error occurred: {str(e)}"
|
| 66 |
|
| 67 |
+
@tool
|
| 68 |
+
def visit_webpage(url: str) -> str:
|
| 69 |
+
"""Visits a webpage and extracts its text content.
|
| 70 |
+
|
| 71 |
+
Args:
|
| 72 |
+
url: The URL of the webpage to visit.
|
| 73 |
|
| 74 |
+
Returns:
|
| 75 |
+
The text content of the webpage, or an error message if the page could not be accessed or parsed.
|
| 76 |
+
"""
|
| 77 |
+
try:
|
| 78 |
+
response = requests.get(url, timeout=10, headers={
|
| 79 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 80 |
+
})
|
| 81 |
+
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
|
| 82 |
+
|
| 83 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 84 |
+
|
| 85 |
+
# Extract all text, removing script and style elements
|
| 86 |
+
for script in soup(["script", "style"]):
|
| 87 |
+
script.extract()
|
| 88 |
+
text = soup.get_text(strip=True)
|
| 89 |
+
|
| 90 |
+
return text
|
| 91 |
+
|
| 92 |
+
except requests.exceptions.RequestException as e:
|
| 93 |
+
return f"Error accessing webpage: {e}"
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"An error occurred while processing the webpage: {e}"
|
| 96 |
|
| 97 |
# Initialize tools
|
| 98 |
final_answer = FinalAnswerTool()
|
|
|
|
| 117 |
|
| 118 |
agent = CodeAgent(
|
| 119 |
model=model,
|
| 120 |
+
tools=[get_healthy_cheat_meal,visit_webpage,final_answer], ## add your tools here (don't remove final answer)
|
| 121 |
max_steps=6,
|
| 122 |
verbosity_level=1,
|
| 123 |
grammar=None,
|