CoolFace
Apppublic

sleepynlp/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py77 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9 10@tool11def convert_to_utc(local_time: str, timezone: str) -> str:12    """A tool that converts a given local time from a specified timezone to UTC.13    14    Args:15        local_time: A string representing the local time in the format 'YYYY-MM-DD HH:MM:SS'.16        timezone: A string representing the valid timezone of the provided local time (e.g., 'America/New_York').17    """18    try:19        # Parse the local time string20        local_dt = datetime.datetime.strptime(local_time, "%Y-%m-%d %H:%M:%S")21        22        # Assign the specified timezone23        tz = pytz.timezone(timezone)24        localized_dt = tz.localize(local_dt)25 26        # Convert to UTC27        utc_dt = localized_dt.astimezone(pytz.utc)28 29        return f"The UTC equivalent of {local_time} in {timezone} is {utc_dt.strftime('%Y-%m-%d %H:%M:%S')} UTC."30    except Exception as e:31        return f"Error converting time '{local_time}' from timezone '{timezone}' to UTC: {str(e)}"32 33@tool34def get_current_time_in_timezone(timezone: str) -> str:35    """A tool that fetches the current local time in a specified timezone.36    Args:37        timezone: A string representing a valid timezone (e.g., 'America/New_York').38    """39    try:40        # Create timezone object41        tz = pytz.timezone(timezone)42        # Get current time in that timezone43        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")44        return f"The current local time in {timezone} is: {local_time}"45    except Exception as e:46        return f"Error fetching time for timezone '{timezone}': {str(e)}"47 48 49final_answer = FinalAnswerTool()50model = HfApiModel(51max_tokens=2096,52temperature=0.5,53model_id='Qwen/Qwen2.5-Coder-32B-Instruct',54custom_role_conversions=None,55)56 57 58# Import tool from Hub59image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)60 61with open("prompts.yaml", 'r') as stream:62    prompt_templates = yaml.safe_load(stream)63    64agent = CodeAgent(65    model=model,66    tools=[final_answer, get_current_time_in_timezone, convert_to_utc], ## add your tools here (don't remove final answer)67    max_steps=6,68    verbosity_level=1,69    grammar=None,70    planning_interval=None,71    name=None,72    description=None,73    prompt_templates=prompt_templates74)75 76 77GradioUI(agent).launch()