|
|
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 |
|
|
|
|
|
|
|
|
hf_token = os.getenv("HF_HUB_TOKEN") |
|
|
if not hf_token: |
|
|
raise RuntimeError("HF_HUB_TOKEN env var not set") |
|
|
login(hf_token) |
|
|
|
|
|
|
|
|
@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() |
|
|
|
|
|
|
|
|
model = InferenceClientModel( |
|
|
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", |
|
|
max_tokens=2096, |
|
|
temperature=0.5, |
|
|
api_key=hf_token |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
image_gen = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
|
web_search = DuckDuckGoSearchTool() |
|
|
|
|
|
|
|
|
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 |
|
|
) |
|
|
|
|
|
|
|
|
GradioUI(agent).launch() |
|
|
|