CoolFace
Apppublic

Dave67350/First_agent_template

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py97 linesDownload Raw Back to root
1from smolagents import CodeAgent, HfApiModel, load_tool, tool, VisitWebpageTool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7from duckduckgo_search import DDGS8 9 10from Gradio_UI import GradioUI11 12 13# Below is an example of a tool that does nothing. Amaze us with your creativity !14@tool15def my_custom_tool(arg1: str, arg2: int) -> str:  # it's important to specify the return type16    """A tool that does nothing yet 17    Args:18        arg1: the first argument19        arg2: the second argument20    """21    return "What magic will you build ?"22 23@tool24def get_current_time_in_timezone(timezone: str) -> str:25    """A tool that fetches the current local time in a specified timezone.26    Args:27        timezone: A string representing a valid timezone (e.g., 'America/New_York').28    """29    try:30        # Create timezone object31        tz = pytz.timezone(timezone)32        # Get current time in that timezone33        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")34        return f"The current local time in {timezone} is: {local_time}"35    except Exception as e:36        return f"Error fetching time for timezone '{timezone}': {str(e)}"37 38@tool39def ai_act_search_tool(query: str, max_results: int = 5) -> list:40    """Searches for highly relevant articles on the AI Act using DuckDuckGo.41    Args:42        query: The search query.43        max_results: The maximum number of results to return.44    Returns:45        A list of URLs to relevant articles.46    """47    with DDGS() as ddgs:48        search_results = ddgs.text(query + " AI Act", max_results)49    return [{"title": result["title"], "link": result["href"]} for result in search_results] if search_results else ["No relevant articles found."]50 51 52 53#@tool54#def fetch_url(url: str) -> str:55#    """Fetches the content of a given URL and returns the raw text.56#    Args:57#        url: The URL to fetch content from.58#    Returns:59#        The raw text content of the page, or an error message if fetching fails.60#    """61#    response = requests.get(url)62#    return response.text if response.status_code == 200 else "Failed to fetch content."63 64final_answer = FinalAnswerTool()65visit_webpage = VisitWebpageTool()66 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(72    max_tokens=2096,73    temperature=0.5,74    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',  # it is possible that this model may be overloaded75    custom_role_conversions=None,76)77 78# Import tool from Hub79image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)80 81with open("prompts.yaml", 'r') as stream:82    prompt_templates = yaml.safe_load(stream)83    84agent = CodeAgent(85    model=model,86    tools=[final_answer, image_generation_tool, ai_act_search_tool, visit_webpage],  # Added AI Act search tool87    max_steps=6,88    verbosity_level=1,89    grammar=None,90    planning_interval=None,91    name=None,92    description=None,93    prompt_templates=prompt_templates94)95 96GradioUI(agent).launch()97