Spaces:
Sleeping
Sleeping
File size: 4,728 Bytes
080f221 9b5b26a c19d193 6aae614 9b5b26a 080f221 9b5b26a 080f221 9b5b26a 080f221 9b5b26a 8c01ffb 6aae614 ae7a494 080f221 ae7a494 080f221 e121372 080f221 13d500a 8c01ffb 080f221 861422e 080f221 8c01ffb 8fe992b 080f221 8c01ffb 861422e 8fe992b 080f221 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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
@tool
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')
"""
# Fix: Use the requests.get() method properly and handle the response
url = f"http://api.weatherapi.com/v1/current.json?key=65b5427878074cd4bba163957250503&q={city}&aqi=no"
try:
response = requests.get(url) # Fixed: Changed requests() to requests.get()
response.raise_for_status() # Raise an exception for HTTP errors
weather_data = response.json()
# Format the weather data in a more readable way
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 the formatted data as a string
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)}"
@tool
def get_weather_image(city: str, weather_description: str) -> str:
"""A tool that generates an image of the weather in a city.
Args:
city: A string representing a city (e.g., 'Paris','Roma')
weather_description: A string describing the current weather conditions
"""
try:
# Create a descriptive prompt for the image generation
time_of_day = "daytime" # This could be improved by checking actual local time
prompt = f"A photorealistic view of {city} during {time_of_day} with {weather_description} weather conditions. Beautiful high-quality image showing the current weather."
# Return the prompt that will be used with the image generation tool
return prompt
except Exception as e:
return f"Error preparing weather image prompt: {str(e)}"
@tool
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_weather_image,
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()
|