CoolFace
Apppublic

pdbdb/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py84 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7from tools.visit_webpage import VisitWebpageTool8from tools.web_search import DuckDuckGoSearchTool9 10import os11hf_token = os.getenv('HF_TOKEN')12 13from Gradio_UI import GradioUI14 15 16@tool17def get_stock(stock_symbol: str) -> str:18    """19    A tool that returns stock info for a given stock symbol.20    Args:21        stock_symbol: A string representing a valid stock symbol (e.g., 'TSLA').22    """23    url = f"http://api.marketstack.com/v1/eod?access_key=0b5d933a8cc566cbd95c1a56cb6b3382&symbols={stock_symbol}"24    response = requests.get(url)25    data = response.json()["data"][0]["close"]26    return str(data)27 28# Below is an example of a tool that does nothing. Amaze us with your creativity !29@tool30def unpopular_opinion()-> str: #it's import to specify the return type31    """A tool that returns an unpopular opinion32    """33    return "You are wrong and I am right"34 35@tool36def get_current_time_in_timezone(timezone: str) -> str:37    """A tool that fetches the current local time in a specified timezone.38    Args:39        timezone: A string representing a valid timezone (e.g., 'America/New_York').40    """41    try:42        # Create timezone object43        tz = pytz.timezone(timezone)44        # Get current time in that timezone45        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")46        return f"The current local time in {timezone} is: {local_time}"47    except Exception as e:48        return f"Error fetching time for timezone '{timezone}': {str(e)}"49 50 51final_answer = FinalAnswerTool()52visite_webpage = VisitWebpageTool()53search_duck = DuckDuckGoSearchTool()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 64 65# Import tool from Hub66image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)67 68with open("prompts.yaml", 'r') as stream:69    prompt_templates = yaml.safe_load(stream)70    71agent = CodeAgent(72    model=model,73    tools=[final_answer, get_current_time_in_timezone, get_stock], ## add your tools here (don't remove final answer)74    max_steps=6,75    verbosity_level=1,76    grammar=None,77    planning_interval=None,78    name=None,79    description=None,80    prompt_templates=prompt_templates81)82 83 84GradioUI(agent).launch()