Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 tools and error logs test
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -7,16 +8,9 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the first argument
|
| 17 |
-
arg2: the second argument
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -25,33 +19,67 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
|
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
model = HfApiModel(
|
| 39 |
-
max_tokens=2096,
|
| 40 |
-
temperature=0.5,
|
| 41 |
-
model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B'
|
| 42 |
-
custom_role_conversions=None,
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
# Import tool from Hub
|
| 47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
-
tools=[final_answer
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
@@ -61,5 +89,8 @@ agent = CodeAgent(
|
|
| 61 |
prompt_templates=prompt_templates
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
|
|
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
# Configure logging to capture error level logs
|
| 12 |
+
logging.basicConfig(level=logging.ERROR)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 19 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 20 |
"""
|
| 21 |
try:
|
|
|
|
| 22 |
tz = pytz.timezone(timezone)
|
|
|
|
| 23 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 24 |
return f"The current local time in {timezone} is: {local_time}"
|
| 25 |
except Exception as e:
|
| 26 |
+
logger.error(f"Error fetching time for timezone '{timezone}': {e}")
|
| 27 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 28 |
|
| 29 |
+
@tool
|
| 30 |
+
def duckduckgo_search(query: str) -> str:
|
| 31 |
+
"""A tool that performs a search using DuckDuckGo.
|
| 32 |
+
Args:
|
| 33 |
+
query: the search query string.
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
ddg_tool = DuckDuckGoSearchTool()
|
| 37 |
+
results = ddg_tool.search(query)
|
| 38 |
+
return str(results)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
logger.error(f"Error performing DuckDuckGo search for query '{query}': {e}")
|
| 41 |
+
return f"Error performing DuckDuckGo search: {str(e)}"
|
| 42 |
+
|
| 43 |
+
@tool
|
| 44 |
+
def weather_search(location: str) -> str:
|
| 45 |
+
"""A tool that fetches weather information for a given location.
|
| 46 |
+
Args:
|
| 47 |
+
location: The location (city, region, or country) to get the weather for.
|
| 48 |
+
"""
|
| 49 |
+
try:
|
| 50 |
+
url = f"https://wttr.in/{location}?format=3"
|
| 51 |
+
response = requests.get(url)
|
| 52 |
+
if response.status_code == 200:
|
| 53 |
+
return response.text
|
| 54 |
+
else:
|
| 55 |
+
logger.error(f"HTTP error: Unable to fetch weather data for '{location}'. HTTP Status code: {response.status_code}")
|
| 56 |
+
return f"Error: Unable to fetch weather data. HTTP Status code: {response.status_code}"
|
| 57 |
+
except Exception as e:
|
| 58 |
+
logger.error(f"Error fetching weather data for location '{location}': {e}")
|
| 59 |
+
return f"Error fetching weather data: {str(e)}"
|
| 60 |
|
| 61 |
final_answer = FinalAnswerTool()
|
| 62 |
model = HfApiModel(
|
| 63 |
+
max_tokens=2096,
|
| 64 |
+
temperature=0.5,
|
| 65 |
+
model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B', # it is possible that this model may be overloaded
|
| 66 |
+
custom_role_conversions=None,
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
# Import tool from Hub
|
| 70 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 71 |
|
| 72 |
+
# Load prompt templates with error handling
|
| 73 |
+
try:
|
| 74 |
+
with open("prompts.yaml", 'r') as stream:
|
| 75 |
+
prompt_templates = yaml.safe_load(stream)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
logger.error(f"Error loading prompts.yaml: {e}")
|
| 78 |
+
prompt_templates = {}
|
| 79 |
+
|
| 80 |
agent = CodeAgent(
|
| 81 |
model=model,
|
| 82 |
+
tools=[final_answer, duckduckgo_search, weather_search, get_current_time_in_timezone],
|
| 83 |
max_steps=6,
|
| 84 |
verbosity_level=1,
|
| 85 |
grammar=None,
|
|
|
|
| 89 |
prompt_templates=prompt_templates
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# Launch Gradio UI with error logging
|
| 93 |
+
try:
|
| 94 |
+
GradioUI(agent).launch()
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logger.error(f"Error launching GradioUI: {e}")
|