Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,18 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
|
|
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
-
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
#
|
11 |
-
@tool
|
12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
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:
|
23 |
-
"""
|
|
|
24 |
Args:
|
25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
26 |
"""
|
@@ -33,29 +25,79 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
|
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint
|
40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
41 |
-
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +107,5 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
+
import os
|
7 |
+
from typing import Optional, Dict, Any, List
|
8 |
from tools.final_answer import FinalAnswerTool
|
|
|
9 |
from Gradio_UI import GradioUI
|
10 |
|
11 |
+
# Timezone tools
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
@tool
|
13 |
def get_current_time_in_timezone(timezone: str) -> str:
|
14 |
+
"""Fetches the current local time in a specified timezone.
|
15 |
+
|
16 |
Args:
|
17 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
18 |
"""
|
|
|
25 |
except Exception as e:
|
26 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
27 |
|
28 |
+
@tool
|
29 |
+
def list_available_timezones() -> str:
|
30 |
+
"""Lists all available timezone names grouped by region."""
|
31 |
+
all_timezones = pytz.all_timezones
|
32 |
+
timezone_dict = {}
|
33 |
+
|
34 |
+
for tz in all_timezones:
|
35 |
+
region = tz.split('/', 1)[0] if '/' in tz else 'Other'
|
36 |
+
if region not in timezone_dict:
|
37 |
+
timezone_dict[region] = []
|
38 |
+
timezone_dict[region].append(tz)
|
39 |
+
|
40 |
+
result = []
|
41 |
+
for region, zones in timezone_dict.items():
|
42 |
+
result.append(f"{region}: {', '.join(zones[:5])}" +
|
43 |
+
(f" and {len(zones)-5} more..." if len(zones) > 5 else ""))
|
44 |
+
|
45 |
+
return "\n".join(result)
|
46 |
+
|
47 |
+
# Weather tool
|
48 |
+
@tool
|
49 |
+
def get_weather(location: str) -> str:
|
50 |
+
"""Fetches current weather information for a specified location.
|
51 |
+
|
52 |
+
Args:
|
53 |
+
location: A string representing a city or location (e.g., 'New York').
|
54 |
+
"""
|
55 |
+
# This is a mock implementation - in a real app, you would use a weather API
|
56 |
+
# Example with OpenWeatherMap: https://openweathermap.org/api
|
57 |
+
return f"Weather information for {location}: Simulated weather data (add API key and implement actual API call)"
|
58 |
+
|
59 |
+
# Load prompt templates
|
60 |
+
try:
|
61 |
+
with open("prompts.yaml", 'r') as stream:
|
62 |
+
prompt_templates = yaml.safe_load(stream)
|
63 |
+
except Exception as e:
|
64 |
+
print(f"Error loading prompt templates: {e}")
|
65 |
+
prompt_templates = {}
|
66 |
|
67 |
+
# Initialize tools
|
68 |
final_answer = FinalAnswerTool()
|
69 |
|
70 |
+
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint
|
|
|
|
|
71 |
model = HfApiModel(
|
72 |
+
max_tokens=2096,
|
73 |
+
temperature=0.5,
|
74 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
75 |
+
custom_role_conversions=None,
|
76 |
)
|
77 |
|
78 |
+
# Load image generation tool
|
79 |
+
try:
|
80 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
81 |
+
except Exception as e:
|
82 |
+
print(f"Failed to load image generation tool: {e}")
|
83 |
+
image_generation_tool = None
|
84 |
|
85 |
+
# Setup the agent with all tools
|
86 |
+
tools = [
|
87 |
+
final_answer,
|
88 |
+
get_current_time_in_timezone,
|
89 |
+
list_available_timezones,
|
90 |
+
get_weather
|
91 |
+
]
|
92 |
|
93 |
+
# Conditionally add image generation if loaded successfully
|
94 |
+
if image_generation_tool:
|
95 |
+
tools.append(image_generation_tool)
|
96 |
+
|
97 |
+
# Initialize the agent
|
98 |
agent = CodeAgent(
|
99 |
model=model,
|
100 |
+
tools=tools,
|
101 |
max_steps=6,
|
102 |
verbosity_level=1,
|
103 |
grammar=None,
|
|
|
107 |
prompt_templates=prompt_templates
|
108 |
)
|
109 |
|
110 |
+
# Launch the Gradio UI
|
111 |
GradioUI(agent).launch()
|