Spaces:
Runtime error
Runtime error
| from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from tools.final_answer import FinalAnswerTool | |
| from Gradio_UI import GradioUI | |
| def get_weather(city: str) -> str: | |
| """A tool that fetches the current weather for a city. | |
| Args: | |
| city: A string representing a city (e.g., 'Paris','Roma') | |
| """ | |
| url = f"http://api.weatherapi.com/v1/current.json?key=65b5427878074cd4bba163957250503&q={city}&aqi=no" | |
| try: | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| weather_data = response.json() | |
| location = weather_data['location'] | |
| current = weather_data['current'] | |
| result = { | |
| "location": { | |
| "name": location['name'], | |
| "region": location['region'], | |
| "country": location['country'], | |
| "localtime": location['localtime'] | |
| }, | |
| "current": { | |
| "temp_c": current['temp_c'], | |
| "temp_f": current['temp_f'], | |
| "condition": current['condition']['text'], | |
| "wind_kph": current['wind_kph'], | |
| "wind_dir": current['wind_dir'], | |
| "humidity": current['humidity'], | |
| "feelslike_c": current['feelslike_c'], | |
| "uv": current['uv'] | |
| } | |
| } | |
| return f"""Weather in {result['location']['name']}, {result['location']['country']}: | |
| - Temperature: {result['current']['temp_c']}°C ({result['current']['temp_f']}°F) | |
| - Feels like: {result['current']['feelslike_c']}°C | |
| - Condition: {result['current']['condition']} | |
| - Wind: {result['current']['wind_kph']} kph, {result['current']['wind_dir']} | |
| - Humidity: {result['current']['humidity']}% | |
| - UV Index: {result['current']['uv']} | |
| - Local time: {result['location']['localtime']}""" | |
| except requests.exceptions.RequestException as e: | |
| return f"Error fetching weather data: {str(e)}" | |
| 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: | |
| # Create timezone object | |
| tz = pytz.timezone(timezone) | |
| # Get current time in that 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 '{timezone}': {str(e)}" | |
| final_answer = FinalAnswerTool() | |
| # Load the image generation tool from Hub | |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| # Set up the model | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded | |
| custom_role_conversions=None, | |
| ) | |
| # Load prompt templates | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| # Create the agent with all tools | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[ | |
| get_weather, | |
| get_current_time_in_timezone, | |
| image_generation_tool, | |
| final_answer | |
| ], # Added your custom tools here | |
| max_steps=6, | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| prompt_templates=prompt_templates | |
| ) | |
| # Launch the Gradio UI | |
| GradioUI(agent).launch() | |