CoolFace
Apppublic

sergedoub/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py85 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# Below is an example of a tool that does nothing. Amaze us with your creativity !11@tool12def album_rating(arg1: str, arg2: int, arg3: int) -> str:13    """A tool that assigns a rating to an album based on its name length.14 15    Args:16        arg1: The name of the album to be rated.17        arg2: The expected character length of the album name.18        arg3: A placeholder for rating (automatically computed).19    """20    # Validate that arg2 matches the actual length21    actual_length = len(arg1)22    if actual_length != arg2:23        return f"Error: Provided length ({arg2}) does not match actual length ({actual_length})."24 25    # Assign rating based on name length (longer = higher rating)26    if actual_length < 5:27        rating = 228    elif actual_length < 10:29        rating = 530    elif actual_length < 20:31        rating = 832    else:33        rating = 1034 35    return f"Album '{arg1}' has a rating of {rating}/10 based on its name length."36 37@tool38def get_current_time_in_timezone(timezone: str) -> str:39    """A tool that fetches the current local time in a specified timezone.40    Args:41        timezone: A string representing a valid timezone (e.g., 'America/New_York').42    """43    try:44        # Create timezone object45        tz = pytz.timezone(timezone)46        # Get current time in that timezone47        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 53final_answer = FinalAnswerTool()54 55# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:56# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 57 58model = HfApiModel(59max_tokens=2096,60temperature=0.5,61model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded62custom_role_conversions=None,63)64 65 66# Import tool from Hub67image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)68 69with open("prompts.yaml", 'r') as stream:70    prompt_templates = yaml.safe_load(stream)71    72agent = CodeAgent(73    model=model,74    tools=[album_rating, image_generation_tool, final_answer], ## add your tools here (don't remove final answer)75    max_steps=6,76    verbosity_level=1,77    grammar=None,78    planning_interval=None,79    name=None,80    description=None,81    prompt_templates=prompt_templates82)83 84 85GradioUI(agent).launch()