CoolFace
Apppublic

galactus333/First_agent_template

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
app.py98 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 my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type13    #Keep this format for the description / args / args description but feel free to modify the tool14    """A tool that does nothing yet 15    Args:16        arg1: the first argument17        arg2: the second argument18    """19    return "What magic will you build ?"20 21@tool22def get_current_time_in_timezone(timezone: str) -> str:23    """A tool that fetches the current local time in a specified timezone.24    Args:25        timezone: A string representing a valid timezone (e.g., 'America/New_York').26    """27    try:28        # Create timezone object29        tz = pytz.timezone(timezone)30        # Get current time in that timezone31        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")32        return f"The current local time in {timezone} is: {local_time}"33    except Exception as e:34        return f"Error fetching time for timezone '{timezone}': {str(e)}"35 36@tool37def solve_quadratic(a: float, b: float, c: float) -> str:38    """Solve the quadratic equation ax² + bx + c = 0 and return the roots.39    40    This tool finds real roots of a quadratic equation. 41    It handles three cases: two real roots, one real root (repeated), or no real roots.42    43    Args:44        a: Coefficient of x² (quadratic term). Should not be zero.45        b: Coefficient of x (linear term).46        c: Constant term.47    48    Returns:49        A formatted string describing the roots.50    """51    import math52    53    discriminant = b**2 - 4*a*c54    55    if discriminant < 0:56        return "No real roots (complex solutions exist)"57    elif discriminant == 0:58        root = -b / (2*a)59        return f"One real root: x = {root:.6f}"60    else:61        root1 = (-b + math.sqrt(discriminant)) / (2*a)62        root2 = (-b - math.sqrt(discriminant)) / (2*a)63        return f"Two real roots:\n  x₁ = {root1:.6f}\n  x₂ = {root2:.6f}"64 65 66final_answer = FinalAnswerTool()67 68# 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:69# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 70 71model = HfApiModel(72max_tokens=2096,73temperature=0.5,74model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded75custom_role_conversions=None,76)77 78 79# Import tool from Hub80image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)81 82with open("prompts.yaml", 'r') as stream:83    prompt_templates = yaml.safe_load(stream)84    85agent = CodeAgent(86    model=model,87    tools=[final_answer, solve_quadratic], ## add your tools here (don't remove final answer)88    max_steps=6,89    verbosity_level=1,90    grammar=None,91    planning_interval=None,92    name=None,93    description=None,94    prompt_templates=prompt_templates95)96 97 98GradioUI(agent).launch()