Spaces:
Sleeping
Sleeping
Download app.py from phoenix99i0/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/spaces/phoenix99i0/First_agent_template/resolve/main/app.py
- Command line
-
hf download hf://spaces/phoenix99i0/First_agent_template/app.py
-
curl -L -o app.py https://huggingface.co/spaces/phoenix99i0/First_agent_template/resolve/main/app.py
3.35 kB
| from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, InferenceClientModel,load_tool, tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from typing import List | |
| from Gradio_UI import GradioUI | |
| def get_current_time_in_timezone(timezone:str) -> str: | |
| """A tool that fetches the current local time in a specified timezone. | |
| Args: | |
| timezone: A string representing a valid timezone (e.g. , 'America/New_York') | |
| """ | |
| try: | |
| tz = pytz.timezone(timezone) | |
| local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
| return f"The current local time in {timezone} is {local_time}" | |
| except Exception as e: | |
| return f"Error fetching time for '{timezone}':{str(e)}" | |
| def web_search(searchstring:str) -> List[str]: | |
| """A tool that conducts search about a topic and gives the first 3 results. | |
| Args: | |
| searchstring: A string containing the topic to search for (e.g. , 'GlobalWarming/Covid-19/BloomsTaxonomy') | |
| """ | |
| try: | |
| search_provider = DuckDuckGoSearchTool() | |
| raw_results = search_provider.forward(searchstring) | |
| if isinstance(raw_results, str): | |
| results = raw_results.split("\n") | |
| else: | |
| results = raw_results | |
| return results[:3] | |
| except Exception as e: | |
| return [f"Search Failed :{str(e)}"] | |
| def get_weather_forecast(city: str) -> str: | |
| """A tool that fetches a 3 day weather forecast. | |
| Args: | |
| city: The name of the city (e.g. 'Paris', 'Tokyo'). | |
| """ | |
| try: | |
| geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json" | |
| geo_res = requests.get(geo_url).json() | |
| if not geo_res.get('results'): | |
| return f"Could no tfind co-odinates for {city}" | |
| lat = geo_res['results'][0]['latitude'] | |
| lon = geo_res['results'][0]['longitude'] | |
| #Forecast | |
| weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&daily=weather_code,temperature_2m_max,precipitation_probability_max&timezone=auto" | |
| weather_res = requests.get(weather_url).json() | |
| days = weather_res['daily'] | |
| summary = f"Forecast for {city}" | |
| for i in range(3): | |
| date = days['time'][i] | |
| temp = days['temperature_2m_max'][i] | |
| rain_probability = days['precipitation_probability_max'][i] | |
| summary += f"-{date} : {temp}°C, Rain probability : {rain_probability}%\n" | |
| return summary | |
| except Exception as e: | |
| return f"Error fetching weather : {str(e)}" | |
| final_answer = FinalAnswerTool() | |
| model = InferenceClientModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| custom_role_conversions=None | |
| ) | |
| image_generation_tool = load_tool("agents-course/text-to-image",trust_remote_code=True) | |
| with open('prompts.yaml', 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[final_answer,get_current_time_in_timezone,web_search,image_generation_tool,get_weather_forecast], | |
| max_steps=6, | |
| verbosity_level=1, | |
| prompt_templates=prompt_templates, | |
| additional_authorized_imports=['requests', 'pytz', 'datetime', 'yaml'] | |
| ) | |
| GradioUI(agent).launch() |