Update app.py
#383
by
alexander1198
- opened
app.py
CHANGED
@@ -1,69 +1,99 @@
|
|
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 |
-
|
9 |
-
|
10 |
-
#
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
|
17 |
-
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
|
|
|
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
-
"""
|
24 |
Args:
|
25 |
-
timezone:
|
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"
|
33 |
except Exception as e:
|
34 |
-
return f"Error fetching time
|
35 |
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
|
|
|
|
|
|
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=
|
44 |
-
temperature=0.
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
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=[
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
verbosity_level=1,
|
61 |
-
grammar=None,
|
62 |
-
planning_interval=None,
|
63 |
-
name=None,
|
64 |
-
description=None,
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
+
from tools.final_answer import FinalAnswerTool
|
3 |
+
from Gradio_UI import GradioUI
|
4 |
import datetime
|
|
|
5 |
import pytz
|
6 |
+
import requests
|
7 |
import yaml
|
|
|
8 |
|
9 |
+
# -------------------------
|
10 |
+
# 1) Real custom tool: translate text via LibreTranslate API
|
11 |
+
# -------------------------
|
12 |
@tool
|
13 |
+
def translate_text(text: str, target_lang: str) -> str:
|
14 |
+
"""Translate a given text into the target language using LibreTranslate.
|
|
|
15 |
Args:
|
16 |
+
text: the source text to translate
|
17 |
+
target_lang: ISO code of the target language (e.g. 'it', 'en', 'es')
|
18 |
"""
|
19 |
+
try:
|
20 |
+
response = requests.post(
|
21 |
+
'https://libretranslate.com/translate',
|
22 |
+
json={
|
23 |
+
'q': text,
|
24 |
+
'source': 'auto',
|
25 |
+
'target': target_lang,
|
26 |
+
'format': 'text'
|
27 |
+
},
|
28 |
+
timeout=5
|
29 |
+
)
|
30 |
+
data = response.json()
|
31 |
+
return data.get('translatedText', 'Errore nella traduzione')
|
32 |
+
except Exception as e:
|
33 |
+
return f"Translation error: {e}"
|
34 |
|
35 |
+
# -------------------------
|
36 |
+
# 2) Prebuilt tool: current time in timezone
|
37 |
+
# -------------------------
|
38 |
@tool
|
39 |
def get_current_time_in_timezone(timezone: str) -> str:
|
40 |
+
"""Fetch the current local time in a specified timezone.
|
41 |
Args:
|
42 |
+
timezone: e.g., 'Europe/Rome', 'America/New_York'
|
43 |
"""
|
44 |
try:
|
|
|
45 |
tz = pytz.timezone(timezone)
|
|
|
46 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
47 |
+
return f"Current local time in {timezone}: {local_time}"
|
48 |
except Exception as e:
|
49 |
+
return f"Error fetching time: {e}"
|
50 |
|
51 |
+
# -------------------------
|
52 |
+
# 3) Load additional tools
|
53 |
+
# -------------------------
|
54 |
+
# DuckDuckGo web search tool
|
55 |
+
duck_search = DuckDuckGoSearchTool()
|
56 |
|
57 |
+
# Image generation tool from Hub
|
58 |
+
image_gen_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
59 |
|
60 |
+
# Final answer formatter
|
61 |
+
tool_final = FinalAnswerTool()
|
62 |
|
63 |
+
# -------------------------
|
64 |
+
# 4) Model setup
|
65 |
+
# -------------------------
|
66 |
model = HfApiModel(
|
67 |
+
max_tokens=1500,
|
68 |
+
temperature=0.7,
|
69 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
|
|
70 |
)
|
71 |
|
72 |
+
# -------------------------
|
73 |
+
# 5) Load prompt templates
|
74 |
+
# -------------------------
|
75 |
+
with open("prompts.yaml", 'r') as f:
|
76 |
+
prompt_templates = yaml.safe_load(f)
|
77 |
|
78 |
+
# -------------------------
|
79 |
+
# 6) Agent construction
|
80 |
+
# -------------------------
|
|
|
|
|
|
|
81 |
agent = CodeAgent(
|
82 |
model=model,
|
83 |
+
tools=[
|
84 |
+
tool_final,
|
85 |
+
translate_text,
|
86 |
+
get_current_time_in_timezone,
|
87 |
+
duck_search,
|
88 |
+
image_gen_tool,
|
89 |
+
],
|
90 |
+
max_steps=8,
|
91 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
92 |
prompt_templates=prompt_templates
|
93 |
)
|
94 |
|
95 |
+
# -------------------------
|
96 |
+
# 7) Launch Gradio UI
|
97 |
+
# -------------------------
|
98 |
+
if __name__ == "__main__":
|
99 |
+
GradioUI(agent).launch()
|