CoolFace
Apppublic

kernel-memory-dump/HuggingFaceAgentsCourse_SmolAgent1

sourceHugging Faceupdated 2y agoView on Hugging Face
6likes
app.py86 linesDownload Raw Back to root
1from dotenv import load_dotenv2 3from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool4import datetime5import requests6import pytz7import yaml8from tools.final_answer import FinalAnswerTool9from tools.tech_priest_review import tech_priest_review10 11from Gradio_UI import GradioUI12import os13 14 15load_dotenv()16 17HF_TOKEN = os.getenv("HF_TOKEN")18 19 20# Below is an example of a tool that does nothing. Amaze us with your creativity !21@tool22def my_custom_tool(23    arg1: str, arg2: int24) -> str:  # it's import to specify the return type25    # Keep this format for the description / args / args description but feel free to modify the tool26    """A tool that does nothing yet27    Args:28        arg1: the first argument29        arg2: the second argument30    """31    return "What magic will you build ?"32 33 34@tool35def get_current_time_in_timezone(timezone: str) -> str:36    """A tool that fetches the current local time in a specified timezone.37    Args:38        timezone: A string representing a valid timezone (e.g., 'America/New_York').39    """40    try:41        # Create timezone object42        tz = pytz.timezone(timezone)43        # Get current time in that timezone44        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")45        return f"The current local time in {timezone} is: {local_time}"46    except Exception as e:47        return f"Error fetching time for timezone '{timezone}': {str(e)}"48 49 50final_answer = FinalAnswerTool()51 52# 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:53# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'54 55model = HfApiModel(56    max_tokens=2096,57    temperature=0.5,58    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",  # it is possible that this model may be overloaded59    custom_role_conversions=None,60)61 62 63# Import tool from Hub64image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)65 66with open("prompts.yaml", "r") as stream:67    prompt_templates = yaml.safe_load(stream)68 69agent = CodeAgent(70    model=model,71    tools=[72        final_answer,73        tech_priest_review,74    ],  ## 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="Tech-Priest",80    description="I am a venerable Tech-Priest of the Adeptus Mechanicus, here to review sacred code with divine insights and blessed judgment from the Omnissiah.",81    prompt_templates=prompt_templates,82)83 84 85GradioUI(agent, initial_message=prompt_templates["initial_prompt"]).launch()86