CoolFace
Apppublic

icefrog45/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py86 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 12@tool13def my_cutom_tool(query: str, max_results: int = 3) -> str:14    """15    A tool that performs a DuckDuckGo web search for the given query16    and returns a short summary of the top results.17 18    Args:19        query: The search query string20        max_results: The number of top search results to include21    """22    # Instantiate the DuckDuckGo search tool23    ddg_search = DuckDuckGoSearchTool()24 25    # Execute the search26    search_results = ddg_search.run(query)27 28    if not search_results:29        return "No results found for this query."30 31    # Format a summary of the top results32    summary = "DuckDuckGo search results:\n\n"33    for i, result in enumerate(search_results[:max_results], 1):34        summary += (35            f"{i}. Title: {result.get('title', 'No title')}\n"36            f"   Link: {result.get('link', 'No link')}\n"37            f"   Snippet: {result.get('snippet', 'No snippet')}\n\n"38        )39 40    return summary41 42@tool43def get_current_time_in_timezone(timezone: str) -> str:44    """A tool that fetches the current local time in a specified timezone.45    Args:46        timezone: A string representing a valid timezone (e.g., 'America/New_York').47    """48    try:49        # Create timezone object50        tz = pytz.timezone(timezone)51        # Get current time in that timezone52        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")53        return f"The current local time in {timezone} is: {local_time}"54    except Exception as e:55        return f"Error fetching time for timezone '{timezone}': {str(e)}"56 57 58final_answer = FinalAnswerTool()59model = HfApiModel(60max_tokens=2096,61temperature=0.5,62model_id='https://jc26mwg228mkj8dw.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded63custom_role_conversions=None,64)65 66 67# Import tool from Hub68image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)69 70with open("prompts.yaml", 'r') as stream:71    prompt_templates = yaml.safe_load(stream)72    73agent = CodeAgent(74    model=model,75    tools=[final_answer], ## add your tools here (don't remove final answer)76    max_steps=6,77    verbosity_level=1,78    grammar=None,79    planning_interval=None,80    name=None,81    description=None,82    prompt_templates=prompt_templates83)84 85 86GradioUI(agent).launch()