Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,59 @@
|
|
1 |
-
from smolagents import CodeAgent,
|
|
|
|
|
|
|
|
|
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:
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
arg1:
|
17 |
-
arg2:
|
18 |
"""
|
19 |
-
return "
|
20 |
|
|
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
24 |
Args:
|
25 |
-
timezone: A string
|
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
|
33 |
except Exception as e:
|
34 |
-
return f"
|
35 |
-
|
36 |
|
|
|
37 |
final_answer = FinalAnswerTool()
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
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=6,
|
60 |
verbosity_level=1,
|
61 |
-
|
62 |
-
planning_interval=None,
|
63 |
-
name=None,
|
64 |
-
description=None,
|
65 |
-
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
-
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, load_tool, tool
|
2 |
+
from smolagents.models import InferenceClientModel
|
3 |
+
from tools.final_answer import FinalAnswerTool
|
4 |
+
from Gradio_UI import GradioUI
|
5 |
+
|
6 |
import datetime
|
|
|
7 |
import pytz
|
8 |
import yaml
|
|
|
|
|
|
|
9 |
|
10 |
+
# Example Tool 1: Custom tool
|
11 |
@tool
|
12 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
13 |
+
"""A tool that returns a string combining the two inputs.
|
|
|
14 |
Args:
|
15 |
+
arg1: a string
|
16 |
+
arg2: an integer
|
17 |
"""
|
18 |
+
return f"You passed arg1={arg1} and arg2={arg2}. Magic happens!"
|
19 |
|
20 |
+
# Example Tool 2: Timezone tool
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
24 |
Args:
|
25 |
+
timezone: A string like 'America/New_York'
|
26 |
"""
|
27 |
try:
|
|
|
28 |
tz = pytz.timezone(timezone)
|
|
|
29 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
30 |
+
return f"The time in {timezone} is: {local_time}"
|
31 |
except Exception as e:
|
32 |
+
return f"Invalid timezone '{timezone}': {str(e)}"
|
|
|
33 |
|
34 |
+
# Tools
|
35 |
final_answer = FinalAnswerTool()
|
36 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
37 |
|
38 |
+
# Model
|
39 |
+
model = InferenceClientModel(
|
40 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
41 |
+
max_tokens=2048,
|
42 |
+
temperature=0.5,
|
|
|
|
|
|
|
43 |
)
|
44 |
|
45 |
+
# Prompts
|
46 |
+
with open("prompts.yaml", "r") as f:
|
47 |
+
prompt_templates = yaml.safe_load(f)
|
48 |
|
49 |
+
# Agent
|
|
|
|
|
|
|
|
|
|
|
50 |
agent = CodeAgent(
|
51 |
model=model,
|
52 |
+
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, image_generation_tool],
|
53 |
max_steps=6,
|
54 |
verbosity_level=1,
|
55 |
+
prompt_templates=prompt_templates,
|
|
|
|
|
|
|
|
|
56 |
)
|
57 |
|
58 |
+
# Gradio UI
|
59 |
+
GradioUI(agent).launch()
|