Team Ai
Apppublic

CodeItSolo/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.py88 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 fibonacci_series_string(n:int)-> str:12    """13    A tool that generates a Fibonacci series up to n terms and returns it as a comma-separated string.14 15    Args:16        n: The number of Fibonacci terms to generate (must be a non-negative integer).17 18    Returns:19        A string containing the Fibonacci series, with items separated by commas.20        Returns an empty string if n is 0.21        Raises ValueError if n is negative.22 23    Examples:24        fibonacci_series_string(5) == "0,1,1,2,3"25        fibonacci_series_string(0) == ""26    """27 28    if n < 0:29        raise ValueError("n must be a non-negative integer.")30 31    if n == 0:32        return ""33 34    fib_series = []35    a, b = 0, 136 37    for _ in range(n):38        fib_series.append(str(a))  # Append as string39        a, b = b, a + b40 41    return ",".join(fib_series)42 43@tool44def get_current_time_in_timezone(timezone: str) -> str:45    """A tool that fetches the current local time in a specified timezone.46    Args:47        timezone: A string representing a valid timezone (e.g., 'America/New_York').48    """49    try:50        # Create timezone object51        tz = pytz.timezone(timezone)52        # Get current time in that timezone53        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")54        return f"The current local time in {timezone} is: {local_time}"55    except Exception as e:56        return f"Error fetching time for timezone '{timezone}': {str(e)}"57 58 59final_answer = FinalAnswerTool()60 61model = HfApiModel(62max_tokens=2096,63temperature=0.5,64model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',65custom_role_conversions=None,66)67 68 69# Import tool from Hub70image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)71 72with open("prompts.yaml", 'r') as stream:73    prompt_templates = yaml.safe_load(stream)74    75agent = CodeAgent(76    model=model,77    tools=[final_answer, get_current_time_in_timezone, fibonacci_series_string],78    max_steps=6,79    verbosity_level=1,80    grammar=None,81    planning_interval=None,82    name=None,83    description=None,84    prompt_templates=prompt_templates85)86 87 88GradioUI(agent).launch()