Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,78 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
-
import uuid
|
| 3 |
import datetime
|
| 4 |
-
import requests
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
-
|
| 12 |
@tool
|
| 13 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
| 14 |
-
|
| 15 |
-
"""A tool that does nothing yet
|
| 16 |
Args:
|
| 17 |
arg1: the first argument
|
| 18 |
arg2: the second argument
|
| 19 |
"""
|
| 20 |
return "What magic will you build ?"
|
| 21 |
|
|
|
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
-
"""
|
| 25 |
Args:
|
| 26 |
-
timezone: A
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
-
# Create timezone object
|
| 30 |
tz = pytz.timezone(timezone)
|
| 31 |
-
# Get current time in that timezone
|
| 32 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 33 |
return f"The current local time in {timezone} is: {local_time}"
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
-
# Import tool from Hub
|
| 38 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
@tool
|
| 41 |
-
def
|
| 42 |
-
"""Generate an image from a text prompt and return
|
| 43 |
Args:
|
| 44 |
prompt: The text description of the image to generate.
|
| 45 |
"""
|
| 46 |
-
img =
|
| 47 |
out_path = f"/tmp/{uuid.uuid4().hex}.png"
|
| 48 |
img.save(out_path)
|
| 49 |
-
return out_path
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
|
| 54 |
final_answer = FinalAnswerTool()
|
| 55 |
|
| 56 |
-
|
| 57 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 58 |
-
|
| 59 |
-
model = HfApiModel(
|
| 60 |
-
max_tokens=2096,
|
| 61 |
-
temperature=0.5,
|
| 62 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 63 |
-
custom_role_conversions=None,
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
with open("prompts.yaml", 'r') as stream:
|
| 67 |
prompt_templates = yaml.safe_load(stream)
|
| 68 |
-
|
| 69 |
agent = CodeAgent(
|
| 70 |
model=model,
|
| 71 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
max_steps=6,
|
| 73 |
verbosity_level=1,
|
| 74 |
-
|
| 75 |
-
planning_interval=None,
|
| 76 |
-
name=None,
|
| 77 |
-
description=None,
|
| 78 |
-
prompt_templates=prompt_templates
|
| 79 |
)
|
| 80 |
|
| 81 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
|
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
+
from smolagents.agent_types import AgentImage
|
| 8 |
+
from tools.final_answer import FinalAnswerTool
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
|
| 12 |
@tool
|
| 13 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 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 |
+
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
+
"""Fetch the current local time in a specified timezone.
|
| 25 |
Args:
|
| 26 |
+
timezone: A valid timezone string (e.g., 'America/New_York').
|
| 27 |
"""
|
| 28 |
try:
|
|
|
|
| 29 |
tz = pytz.timezone(timezone)
|
|
|
|
| 30 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 31 |
return f"The current local time in {timezone} is: {local_time}"
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 34 |
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Model
|
| 37 |
+
model = HfApiModel(
|
| 38 |
+
max_tokens=2096,
|
| 39 |
+
temperature=0.5,
|
| 40 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 41 |
+
custom_role_conversions=None,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Load the low-level image tool (keep this private)
|
| 45 |
+
_image_generator = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 46 |
+
|
| 47 |
+
# Expose a UI-friendly wrapper tool that returns AgentImage
|
| 48 |
@tool
|
| 49 |
+
def generate_image(prompt: str) -> AgentImage:
|
| 50 |
+
"""Generate an image from a text prompt and return it as an AgentImage for Gradio rendering.
|
| 51 |
Args:
|
| 52 |
prompt: The text description of the image to generate.
|
| 53 |
"""
|
| 54 |
+
img = _image_generator(prompt=prompt) # returns a PIL image in your tests
|
| 55 |
out_path = f"/tmp/{uuid.uuid4().hex}.png"
|
| 56 |
img.save(out_path)
|
| 57 |
+
return AgentImage(out_path)
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
final_answer = FinalAnswerTool()
|
| 61 |
|
| 62 |
+
with open("prompts.yaml", "r") as stream:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
prompt_templates = yaml.safe_load(stream)
|
| 64 |
+
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
+
tools=[
|
| 68 |
+
final_answer,
|
| 69 |
+
generate_image, # <-- this is what you want the agent to call
|
| 70 |
+
get_current_time_in_timezone,
|
| 71 |
+
my_custom_tool,
|
| 72 |
+
],
|
| 73 |
max_steps=6,
|
| 74 |
verbosity_level=1,
|
| 75 |
+
prompt_templates=prompt_templates,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
+
GradioUI(agent).launch()
|