CoolFace
Apppublic

julesrd/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py82 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from datetime import datetime7from tools.final_answer import FinalAnswerTool8 9from Gradio_UI import GradioUI10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def days_difference(given_date: str) -> str:14    """Calculate the number of days between the given date and today.15    Args:16        given_date: A string in 'YYYY-MM-DD' format.17    """18    given_date_obj = datetime.strptime(given_date, "%Y-%m-%d").date()19    today = datetime.today().date()20    return str((given_date_obj - today).days)21 22 23@tool24def web_search(query: str) -> str:25    """Make a web search for the query and return several website content in a json string26    Args:27        query: A query to research on internet28    """29    search = DuckDuckGoSearchResults(output_format="json")30 31    return search.invoke(query)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(56max_tokens=2096,57temperature=0.5,58model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded59custom_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=[final_answer, days_difference, get_current_time_in_timezone], ## add your tools here (don't remove final answer)72    max_steps=6,73    verbosity_level=1,74    grammar=None,75    planning_interval=None,76    name=None,77    description=None,78    prompt_templates=prompt_templates79)80 81 82GradioUI(agent).launch()