Update app.py
#2
by
rainwaters11
- opened
app.py
CHANGED
@@ -1,28 +1,32 @@
|
|
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
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
arg1:
|
17 |
-
arg2:
|
|
|
|
|
18 |
"""
|
19 |
-
return "What magic will you build
|
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 representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
26 |
"""
|
27 |
try:
|
28 |
# Create timezone object
|
@@ -33,33 +37,53 @@ 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 |
model = HfApiModel(
|
39 |
-
max_tokens=2096,
|
40 |
-
temperature=0.5,
|
41 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
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 |
with open("prompts.yaml", 'r') as stream:
|
50 |
prompt_templates = yaml.safe_load(stream)
|
51 |
-
|
|
|
52 |
agent = CodeAgent(
|
53 |
model=model,
|
54 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
55 |
max_steps=6,
|
56 |
verbosity_level=1,
|
57 |
grammar=None,
|
58 |
planning_interval=None,
|
59 |
name=None,
|
60 |
description=None,
|
61 |
-
prompt_templates=prompt_templates
|
62 |
)
|
63 |
|
64 |
-
|
65 |
-
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 random # Import for random.choice
|
7 |
from tools.final_answer import FinalAnswerTool
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
# Example of a tool that does nothing (placeholder for creativity)
|
11 |
@tool
|
12 |
+
def my_custom_tool(arg1: str, arg2: int) -> str: # Fix typo in function name
|
13 |
+
"""A tool that does nothing yet.
|
|
|
14 |
Args:
|
15 |
+
arg1: The first argument.
|
16 |
+
arg2: The second argument.
|
17 |
+
Returns:
|
18 |
+
A string indicating this tool is currently a placeholder.
|
19 |
"""
|
20 |
+
return "What magic will you build?"
|
21 |
|
22 |
+
# Tool to fetch the current time in a specific timezone
|
23 |
@tool
|
24 |
def get_current_time_in_timezone(timezone: str) -> str:
|
25 |
"""A tool that fetches the current local time in a specified timezone.
|
26 |
Args:
|
27 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
28 |
+
Returns:
|
29 |
+
A string with the current local time in the given timezone or an error message.
|
30 |
"""
|
31 |
try:
|
32 |
# Create timezone object
|
|
|
37 |
except Exception as e:
|
38 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
39 |
|
40 |
+
# Creative tool to share a random fun fact
|
41 |
+
@tool
|
42 |
+
def get_fun_fact() -> str:
|
43 |
+
"""A tool that shares a random fun fact.
|
44 |
+
Returns:
|
45 |
+
A string containing a fun fact.
|
46 |
+
"""
|
47 |
+
fun_facts = [
|
48 |
+
"Honey never spoils. Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old!",
|
49 |
+
"Octopuses have three hearts!",
|
50 |
+
"Bananas are berries, but strawberries aren't.",
|
51 |
+
"Your heartbeat changes with the music you listen to."
|
52 |
+
]
|
53 |
+
return random.choice(fun_facts)
|
54 |
|
55 |
+
# Final answer and agent setup
|
56 |
final_answer = FinalAnswerTool()
|
57 |
model = HfApiModel(
|
58 |
+
max_tokens=2096,
|
59 |
+
temperature=0.5,
|
60 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
61 |
+
custom_role_conversions=None,
|
62 |
)
|
63 |
|
64 |
+
# Load image generation tool
|
|
|
65 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
66 |
|
67 |
with open("prompts.yaml", 'r') as stream:
|
68 |
prompt_templates = yaml.safe_load(stream)
|
69 |
+
|
70 |
+
# Create the agent and add tools
|
71 |
agent = CodeAgent(
|
72 |
model=model,
|
73 |
+
tools=[
|
74 |
+
final_answer,
|
75 |
+
get_current_time_in_timezone,
|
76 |
+
get_fun_fact, # Add fun fact tool
|
77 |
+
image_generation_tool, # Add image generation tool
|
78 |
+
],
|
79 |
max_steps=6,
|
80 |
verbosity_level=1,
|
81 |
grammar=None,
|
82 |
planning_interval=None,
|
83 |
name=None,
|
84 |
description=None,
|
85 |
+
prompt_templates=prompt_templates,
|
86 |
)
|
87 |
|
88 |
+
# Launch Gradio UI
|
89 |
+
GradioUI(agent).launch()
|