Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,66 @@
|
|
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:
|
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 generate_cat_image(description: str = "a cute cat sitting on a windowsill", size: str = "512x512") -> str:
|
23 |
-
"""
|
24 |
-
Generates an image of a cat based on a text prompt.
|
25 |
-
|
26 |
Args:
|
27 |
description: Description of the cat image.
|
28 |
size: Image resolution, e.g., "512x512".
|
29 |
"""
|
30 |
-
|
31 |
-
# Assuming the tool returns a URL or displayable image path
|
32 |
-
return image_result
|
33 |
|
|
|
34 |
@tool
|
35 |
def get_current_time_in_timezone(timezone: str) -> str:
|
36 |
-
"""
|
37 |
Args:
|
38 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
39 |
"""
|
40 |
try:
|
41 |
-
# Create timezone object
|
42 |
tz = pytz.timezone(timezone)
|
43 |
-
# Get current time in that timezone
|
44 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
45 |
return f"The current local time in {timezone} is: {local_time}"
|
46 |
except Exception as e:
|
47 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
48 |
|
49 |
-
|
50 |
-
tools=[final_answer, generate_cat_image],
|
51 |
-
|
52 |
-
|
53 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
54 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
55 |
-
|
56 |
model = HfApiModel(
|
57 |
-
max_tokens=2096,
|
58 |
-
temperature=0.5,
|
59 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
60 |
-
custom_role_conversions=None,
|
61 |
)
|
62 |
|
63 |
-
|
64 |
-
# Import tool from Hub
|
65 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
66 |
-
|
67 |
-
# Load system prompt from prompt.yaml file
|
68 |
-
with open("prompts.yaml", 'r') as stream:
|
69 |
-
prompt_templates = yaml.safe_load(stream)
|
70 |
-
|
71 |
agent = CodeAgent(
|
72 |
model=model,
|
73 |
-
tools=[final_answer, generate_cat_image
|
74 |
max_steps=6,
|
75 |
verbosity_level=1,
|
76 |
grammar=None,
|
@@ -79,4 +69,6 @@ agent = CodeAgent(
|
|
79 |
description=None,
|
80 |
prompt_templates=prompt_templates
|
81 |
)
|
82 |
-
|
|
|
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
|
|
3 |
import pytz
|
4 |
import yaml
|
5 |
from tools.final_answer import FinalAnswerTool
|
|
|
6 |
from Gradio_UI import GradioUI
|
7 |
|
8 |
+
# Load system prompt from prompt.yaml file
|
9 |
+
with open("prompts.yaml", 'r') as stream:
|
10 |
+
prompt_templates = yaml.safe_load(stream)
|
11 |
+
|
12 |
+
# Import tool from Hub
|
13 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
14 |
+
|
15 |
+
# Define FinalAnswerTool
|
16 |
+
final_answer = FinalAnswerTool()
|
17 |
+
|
18 |
+
# Example custom tool
|
19 |
@tool
|
20 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
21 |
"""A tool that does nothing yet
|
22 |
Args:
|
23 |
arg1: the first argument
|
24 |
arg2: the second argument
|
25 |
"""
|
26 |
+
return "What magic will you build?"
|
27 |
+
|
28 |
+
# Cat image generator tool
|
29 |
@tool
|
30 |
def generate_cat_image(description: str = "a cute cat sitting on a windowsill", size: str = "512x512") -> str:
|
31 |
+
"""Generates an image of a cat based on a text prompt.
|
|
|
|
|
32 |
Args:
|
33 |
description: Description of the cat image.
|
34 |
size: Image resolution, e.g., "512x512".
|
35 |
"""
|
36 |
+
return image_generation_tool(prompt=description, size=size)
|
|
|
|
|
37 |
|
38 |
+
# Time tool
|
39 |
@tool
|
40 |
def get_current_time_in_timezone(timezone: str) -> str:
|
41 |
+
"""Fetches the current local time in a specified timezone.
|
42 |
Args:
|
43 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
44 |
"""
|
45 |
try:
|
|
|
46 |
tz = pytz.timezone(timezone)
|
|
|
47 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
48 |
return f"The current local time in {timezone} is: {local_time}"
|
49 |
except Exception as e:
|
50 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
51 |
|
52 |
+
# Define the model
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
model = HfApiModel(
|
54 |
+
max_tokens=2096,
|
55 |
+
temperature=0.5,
|
56 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
57 |
+
custom_role_conversions=None,
|
58 |
)
|
59 |
|
60 |
+
# Create the agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
agent = CodeAgent(
|
62 |
model=model,
|
63 |
+
tools=[final_answer, generate_cat_image, get_current_time_in_timezone, my_custom_tool],
|
64 |
max_steps=6,
|
65 |
verbosity_level=1,
|
66 |
grammar=None,
|
|
|
69 |
description=None,
|
70 |
prompt_templates=prompt_templates
|
71 |
)
|
72 |
+
|
73 |
+
# Launch the UI
|
74 |
+
GradioUI(agent).launch()
|