CoolFace
Apppublic

hardknee/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py105 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool,Tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7from PIL import Image8 9from Gradio_UI import GradioUI10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type14    #Keep this format for the description / args / args description but feel free to modify the tool15    """A tool that does nothing yet 16    Args:17        arg1: the first argument18        arg2: the second argument19    """20    return "What magic will you build ?"21 22# @tool23# def get_web_search_results(query: str)-> str: #it's import to specify the return type24#     #Keep this format for the description / args / args description but feel free to modify the tool25#     """A tool that searchs the web using a query.26#     Args:27#         query: A string representing a valid web query. IMPORTANT: Follow type str format strictly e.g. 'pictures of cats'.28#     """29#     search_results_tool = DuckDuckGoSearchTool()30#     try:31#         results = search_results_tool(query)32#         if results and results != "None":33#             return results34#         else:35#             return "Your query may be incorrectly formatted or there may be an issue with search engine."36#     except Exception as e:37#         return f"{query} is an invalid query"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        # Create timezone object47        tz = pytz.timezone(timezone)48        # Get current time in that timezone49        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")50        return f"The current local time in {timezone} is: {local_time}"51    except Exception as e:52        return f"Error fetching time for timezone '{timezone}': {str(e)}"53 54# @tool55# def generate_image(description: str) -> str:56#     """A tool that generates an image from a text description. The generated image should be displayed using Markdown.57#     Args:58#         description: A string representing the text description to generate an image of.59#     """60#     image_generation_tool = Tool.from_space(61#     "black-forest-labs/FLUX.1-schnell",62#     name="image_generator",63#     description="Generate an image from a prompt"64# )65#     # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)66#     image = image_generation_tool(description)67#     image.save("my picture.png")68#     return f"Image generated for prompt: {description} is {image}."69 70    71web_search = DuckDuckGoSearchTool()72 73# Import tool from Hub74image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)75 76final_answer = FinalAnswerTool()77 78# 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:79 80 81model = HfApiModel(82max_tokens=2096,83temperature=0.5,84model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',85# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded86custom_role_conversions=None,87)88 89with open("prompts.yaml", 'r') as stream:90    prompt_templates = yaml.safe_load(stream)91    92agent = CodeAgent(93    model=model,94    tools=[final_answer, image_generation_tool], ## add your tools here (don't remove final answer)95    max_steps=6,96    verbosity_level=1,97    grammar=None,98    planning_interval=None,99    name=None,100    description=None,101    prompt_templates=prompt_templates102)103 104 105GradioUI(agent).launch()
hardknee/First_agent_template · CoolFace