Craftino's picture
Update app.py
6f92737 verified
# app.py
"""
Fully‑working SmolAgents chat‑based agent for Hugging Face Spaces.
Paste this file into the root of your duplicated Space.
"""
from smolagents import (
CodeAgent,
DuckDuckGoSearchTool,
HfApiModel,
load_tool,
tool, # decorator that auto‑wraps functions as Tool objects
)
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
import datetime
import pytz
import yaml
# ────────────────────────────────
# 1. Custom tools (decorated)
# ────────────────────────────────
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Return the current local time in an IANA timezone.
Args:
timezone: e.g. 'Asia/Karachi'
"""
try:
tz = pytz.timezone(timezone)
now = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current time in {timezone} is {now}"
except Exception as e:
return f"Error: {e}"
@tool
def my_custom_tool(arg1: str, arg2: int) -> str:
"""A playful demo tool.
Args:
arg1: any text
arg2: any integer
"""
return f"You sent {arg1!r} and {arg2}. Now build something cooler 😉"
# ────────────────────────────────
# 2. Hub & built‑in tools
# ────────────────────────────────
image_generation_tool = load_tool(
"agents-course/text-to-image",
trust_remote_code=True
)
duck_search_tool = DuckDuckGoSearchTool() # simple web search
# ────────────────────────────────
# 3. LLM backend
# ────────────────────────────────
model = HfApiModel(
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
max_tokens=2048,
temperature=0.5,
)
# ────────────────────────────────
# 4. Prompt templates
# ────────────────────────────────
with open("prompts.yaml") as f:
prompt_templates = yaml.safe_load(f)
# ────────────────────────────────
# 5. Assemble the agent
# ────────────────────────────────
agent = CodeAgent(
model=model,
tools=[
FinalAnswerTool(), # ← keep this first
duck_search_tool,
image_generation_tool,
get_current_time_in_timezone,
my_custom_tool,
],
max_steps=6,
verbosity_level=1,
prompt_templates=prompt_templates,
)
# ────────────────────────────────
# 6. Launch Gradio
# ────────────────────────────────
if __name__ == "__main__":
GradioUI(agent).launch()