Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,117 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool,
|
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 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
|
|
|
15 |
Args:
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
"""
|
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 |
|
39 |
-
#
|
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 |
-
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
|
|
53 |
with open("prompts.yaml", 'r') as stream:
|
54 |
prompt_templates = yaml.safe_load(stream)
|
55 |
|
|
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer
|
59 |
-
max_steps=
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
62 |
planning_interval=None,
|
@@ -65,5 +120,5 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
-
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, VisitWebpageTool, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
7 |
from Gradio_UI import GradioUI
|
8 |
+
import xml.etree.ElementTree as ET
|
9 |
|
|
|
10 |
@tool
|
11 |
+
def pull_ecb_eur_usd_rate(requested_date: str = None) -> str:
|
12 |
+
"""
|
13 |
+
A tool that fetches the EUR-to-USD exchange rate from the European Central Bank for today.
|
14 |
+
|
15 |
Args:
|
16 |
+
requested_date: (Optional) A string in "YYYY-MM-DD" format. If provided and it is not equal to today's date,
|
17 |
+
the function returns an error.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
A string indicating the exchange rate as of the ECB data's date, or an error message if:
|
21 |
+
- the requested date is not today, or
|
22 |
+
- the ECB data is not updated for today.
|
23 |
"""
|
24 |
+
try:
|
25 |
+
# Determine today's date in the ECB's (Central European) timezone.
|
26 |
+
brussels_tz = pytz.timezone("Europe/Brussels")
|
27 |
+
today = datetime.datetime.now(brussels_tz).date()
|
28 |
+
|
29 |
+
# If a date is provided, check that it matches today's date.
|
30 |
+
if requested_date:
|
31 |
+
try:
|
32 |
+
req_date = datetime.datetime.strptime(requested_date, "%Y-%m-%d").date()
|
33 |
+
except Exception:
|
34 |
+
return f"Error: The provided date '{requested_date}' is not in the correct format (YYYY-MM-DD)."
|
35 |
+
if req_date != today:
|
36 |
+
return f"Error: The requested date {req_date} is not available. Only today's data ({today}) is supported."
|
37 |
+
|
38 |
+
# Fetch the ECB daily XML data.
|
39 |
+
url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
|
40 |
+
response = requests.get(url)
|
41 |
+
if response.status_code != 200:
|
42 |
+
return f"Error: Received status code {response.status_code} from ECB."
|
43 |
+
|
44 |
+
xml_content = response.content
|
45 |
+
tree = ET.ElementTree(ET.fromstring(xml_content))
|
46 |
+
root = tree.getroot()
|
47 |
+
|
48 |
+
# Define namespaces used in the ECB XML.
|
49 |
+
ns = {
|
50 |
+
'gesmes': 'http://www.gesmes.org/xml/2002-08-01',
|
51 |
+
'def': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'
|
52 |
+
}
|
53 |
+
|
54 |
+
# Locate the Cube element with the time attribute.
|
55 |
+
time_cube = root.find('.//def:Cube/def:Cube', ns)
|
56 |
+
if time_cube is not None:
|
57 |
+
data_date = time_cube.get('time')
|
58 |
+
# If ECB's data date is not today, return an error.
|
59 |
+
if data_date != str(today):
|
60 |
+
return f"Error: ECB data is not updated for today. Latest available data is for {data_date}."
|
61 |
+
|
62 |
+
# Find the Cube element for USD.
|
63 |
+
usd_cube = time_cube.find("def:Cube[@currency='USD']", ns)
|
64 |
+
if usd_cube is not None:
|
65 |
+
rate = usd_cube.get('rate')
|
66 |
+
return f"As of {data_date}, EUR 1 is equivalent to {rate} USD."
|
67 |
+
|
68 |
+
return "Error: USD rate not found in ECB data."
|
69 |
+
except Exception as e:
|
70 |
+
return f"Error fetching ECB rate: {str(e)}"
|
71 |
|
72 |
@tool
|
73 |
def get_current_time_in_timezone(timezone: str) -> str:
|
74 |
+
"""
|
75 |
+
A tool that fetches the current local time in a specified timezone.
|
76 |
+
|
77 |
Args:
|
78 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
79 |
+
|
80 |
+
Returns:
|
81 |
+
A string with the current local time in the specified timezone.
|
82 |
"""
|
83 |
try:
|
|
|
84 |
tz = pytz.timezone(timezone)
|
|
|
85 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
86 |
return f"The current local time in {timezone} is: {local_time}"
|
87 |
except Exception as e:
|
88 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
89 |
|
90 |
+
# Tools for final answer and additional capabilities.
|
91 |
final_answer = FinalAnswerTool()
|
92 |
+
search_result = DuckDuckGoSearchTool()
|
93 |
+
visit_webpage = VisitWebpageTool()
|
94 |
|
95 |
+
# Initialize the model.
|
|
|
|
|
96 |
model = HfApiModel(
|
97 |
+
max_tokens=2096,
|
98 |
+
temperature=0.5,
|
99 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
100 |
+
custom_role_conversions=None,
|
101 |
)
|
102 |
|
103 |
+
# Import image generation tool from Hub (if needed).
|
|
|
104 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
105 |
|
106 |
+
# Load prompt templates from the YAML file.
|
107 |
with open("prompts.yaml", 'r') as stream:
|
108 |
prompt_templates = yaml.safe_load(stream)
|
109 |
|
110 |
+
# Create the CodeAgent and register the tools (including the updated ECB rate tool).
|
111 |
agent = CodeAgent(
|
112 |
model=model,
|
113 |
+
tools=[final_answer, get_current_time_in_timezone, pull_ecb_eur_usd_rate],
|
114 |
+
max_steps=10,
|
115 |
verbosity_level=1,
|
116 |
grammar=None,
|
117 |
planning_interval=None,
|
|
|
120 |
prompt_templates=prompt_templates
|
121 |
)
|
122 |
|
123 |
+
# Launch the Gradio UI for interacting with the agent.
|
124 |
+
GradioUI(agent).launch()
|