Matthightech's picture
Update app.py
4d64b6a verified
import os
from huggingface_hub import login
import datetime
import pytz
from smolagents import (
CodeAgent,
DuckDuckGoSearchTool,
InferenceClientModel,
load_tool,
tool
)
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# 1) Authenticate with your HF token
hf_token = os.getenv("HF_HUB_TOKEN")
if not hf_token:
raise RuntimeError("HF_HUB_TOKEN env var not set")
login(hf_token)
# 2) Define tools with compliant docstrings
@tool
def my_custom_tool(arg1: str, arg2: int) -> str:
"""
Placeholder tool.
Args:
arg1: A sample string.
arg2: A sample integer.
Returns:
A placeholder response.
"""
return "What magic will you build?"
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""
Fetch the current time in a specified timezone.
Args:
timezone: A tz database name (e.g., 'Europe/Paris').
Returns:
Local time as 'YYYY-MM-DD HH:MM:SS', or an error message.
"""
try:
tz = pytz.timezone(timezone)
now = datetime.datetime.now(tz)
return now.strftime("The current local time in %Z (%z) is %Y-%m-%d %H:%M:%S")
except Exception as e:
return f"Error fetching time: {e}"
final_answer = FinalAnswerTool()
# 3) Load the model WITHOUT an unsupported provider override
model = InferenceClientModel(
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
max_tokens=2096,
temperature=0.5,
api_key=hf_token # ensure your token is passed here
# provider omitted to use default "auto"
)
# 4) Load built-in tools
image_gen = load_tool("agents-course/text-to-image", trust_remote_code=True)
web_search = DuckDuckGoSearchTool()
# 5) Instantiate CodeAgent using default prompt templates
agent = CodeAgent(
model=model,
tools=[
final_answer,
my_custom_tool,
get_current_time_in_timezone,
image_gen,
web_search,
],
max_steps=6,
verbosity_level=1,
add_base_tools=False
)
# 6) Launch Gradio UI
GradioUI(agent).launch()