Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,108 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 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 get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
-
"""
|
| 24 |
Args:
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 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 current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
# 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:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 64 |
-
description=
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
from smolagents import (
|
| 4 |
+
CodeAgent,
|
| 5 |
+
DuckDuckGoSearchTool,
|
| 6 |
+
InferenceClientModel,
|
| 7 |
+
load_tool,
|
| 8 |
+
tool,
|
| 9 |
+
)
|
| 10 |
import datetime
|
| 11 |
+
import re
|
| 12 |
import requests
|
| 13 |
import pytz
|
| 14 |
import yaml
|
| 15 |
+
|
| 16 |
+
# In the template, FinalAnswerTool lives in a local module
|
| 17 |
from tools.final_answer import FinalAnswerTool
|
| 18 |
|
| 19 |
+
# Simple Gradio wrapper provided by the template Space
|
| 20 |
from Gradio_UI import GradioUI
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# ---- Custom Tools ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
+
"""Fetch the current local time in a specified timezone.
|
| 28 |
Args:
|
| 29 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 30 |
"""
|
| 31 |
try:
|
|
|
|
| 32 |
tz = pytz.timezone(timezone)
|
|
|
|
| 33 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
return f"The current local time in {timezone} is: {local_time}"
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 37 |
|
| 38 |
|
| 39 |
+
@tool
|
| 40 |
+
def fetch_page_title(url: str) -> str:
|
| 41 |
+
"""Fetch the HTML page at a URL and return its <title>.
|
| 42 |
+
Args:
|
| 43 |
+
url: The web page to fetch (http/https).
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
resp = requests.get(url, timeout=10)
|
| 47 |
+
resp.raise_for_status()
|
| 48 |
+
# crude but robust title extraction
|
| 49 |
+
m = re.search(r"<title[^>]*>(.*?)</title>", resp.text, flags=re.IGNORECASE | re.DOTALL)
|
| 50 |
+
title = re.sub(r"\s+", " ", m.group(1)).strip() if m else "(no <title> found)"
|
| 51 |
+
return f"Title: {title}"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Error fetching title from '{url}': {e}"
|
| 54 |
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
@tool
|
| 57 |
+
def echo_and_length(text: str) -> str:
|
| 58 |
+
"""Return the text back (trimmed) and its character length.
|
| 59 |
+
Args:
|
| 60 |
+
text: Any text.
|
| 61 |
+
"""
|
| 62 |
+
t = text.strip()
|
| 63 |
+
return f"Echo: {t}\nLength: {len(t)}"
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# ---- Model & Tools wiring ----
|
| 67 |
|
| 68 |
+
final_answer = FinalAnswerTool()
|
| 69 |
+
|
| 70 |
+
model = InferenceClientModel(
|
| 71 |
+
max_tokens=2096,
|
| 72 |
+
temperature=0.5,
|
| 73 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 74 |
+
custom_role_conversions=None,
|
| 75 |
+
)
|
| 76 |
|
| 77 |
+
# Import a community tool from the Hub (text-to-image)
|
| 78 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 79 |
|
| 80 |
+
# Built-in search tool
|
| 81 |
+
web_search = DuckDuckGoSearchTool()
|
| 82 |
+
|
| 83 |
+
# Load system prompt templates from YAML
|
| 84 |
+
with open("prompts.yaml", "r", encoding="utf-8") as stream:
|
| 85 |
prompt_templates = yaml.safe_load(stream)
|
| 86 |
+
|
| 87 |
+
# Create the Agent with ALL the tools wired in
|
| 88 |
agent = CodeAgent(
|
| 89 |
model=model,
|
| 90 |
+
tools=[
|
| 91 |
+
final_answer, # required: allows the agent to end with a final answer
|
| 92 |
+
web_search, # web search capability
|
| 93 |
+
image_generation_tool, # text-to-image generation
|
| 94 |
+
get_current_time_in_timezone,
|
| 95 |
+
fetch_page_title,
|
| 96 |
+
echo_and_length,
|
| 97 |
+
],
|
| 98 |
+
max_steps=8,
|
| 99 |
verbosity_level=1,
|
| 100 |
grammar=None,
|
| 101 |
planning_interval=None,
|
| 102 |
+
name="My First smolagents Agent",
|
| 103 |
+
description="A tiny agent that can search, fetch titles, tell time, and generate images.",
|
| 104 |
+
prompt_templates=prompt_templates, # use the system prompts provided in the template
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# Launch the Gradio UI
|
| 108 |
+
GradioUI(agent).launch()
|