CoolFace
Apppublic

serahverg/First_agent_template

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
app.py92 linesDownload Raw Back to root
1from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool2from smolagents.models import HfApiModel3import datetime4import requests5import pytz6import yaml7from typing import Any8from tools.final_answer import FinalAnswerTool9from Gradio_UI import GradioUI10 11# 1. SETUP THE IMAGE TOOL12# We load the base tool from the course repo13base_image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)14 15# We wrap it in a function called 'image_generation_tool' 16# This matches the name the agent naturally wants to use17@tool18def image_generation_tool(prompt: str) -> Any:19    """A tool that generates an image from a text prompt and returns the raw pixels.20    Args:21        prompt: The visual description of the image you want to generate.22    """23    output = base_image_tool(prompt)24    25    # EXTRACTION FIX: This ensures Gradio shows the picture, not text.26    # We return the .raw attribute which contains the PIL Image.27    if hasattr(output, 'raw'):28        return output.raw29    return output30 31@tool32def get_greeting(name: str) -> str:33    """A tool that returns a friendly greeting.34    Args:35        name: the name of the user 36    """37    return f"What magic will you build today, {name}?"38 39@tool40def get_current_time_in_timezone(timezone: str) -> str:41    """A tool that fetches the current local time in a specified timezone.42    Args:43        timezone: A string representing a valid timezone (e.g., 'America/New_York').44    """45    try:46        tz = pytz.timezone(timezone)47        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")48        return f"The current local time in {timezone} is: {local_time}"49    except Exception as e:50        return f"Error fetching time for timezone '{timezone}': {str(e)}"51 52 53@tool54def final_answer_image(image: Any) -> Any:55    """Returns the generated image as the final answer.56    Args:57        image: The image object generated by image_generation_tool.58    """59    return image60 61final_answer_text = FinalAnswerTool()62 63# 2. CONFIGURE THE MODEL64model = HfApiModel(65    max_tokens=2096,66    temperature=0.5,67    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',68    custom_role_conversions=None,69)70 71# 3. LOAD PROMPT TEMPLATES72with open("prompts.yaml", 'r') as stream:73    prompt_templates = yaml.safe_load(stream)74 75# 4. INITIALIZE AGENT76agent = CodeAgent(77    model=model,78    tools=[79        final_answer_text, 80        get_greeting, 81        get_current_time_in_timezone, 82        image_generation_tool, 83        final_answer_image, # Our custom-named tool84        DuckDuckGoSearchTool()85    ],86    max_steps=6,87    verbosity_level=1,88    prompt_templates=prompt_templates89)90 91# 5. LAUNCH UI92GradioUI(agent).launch()