Spaces:
Sleeping
Sleeping
File size: 8,805 Bytes
9b5b26a c19d193 112d0fb 6aae614 8fe992b 9b5b26a 112d0fb 5df72d6 9b5b26a 3d1237b 9b5b26a 8c01ffb 112d0fb 8c01ffb 6aae614 ae7a494 e121372 bf6d34c 29ec968 fe328e0 13d500a 8c01ffb 9b5b26a 8c01ffb 861422e 9b5b26a 8c01ffb 8fe992b 112d0fb 8c01ffb 861422e 8fe992b 9b5b26a 8c01ffb | 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
import os
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
from dotenv import load_dotenv
load_dotenv(os.path.expanduser("~/Projekte/MOOC/OpenCampus/codespace/.env"))
if not os.environ.get("HF_TOKEN"):
# os.environ["HF_TOKEN"] = getpass.getpass("Enter API key for Hugging Face: ")
raise EnvironmentError("HF_TOKEN not found in the .env file.")
if not os.environ.get("OPEN_WEATHER_API_KEY"):
# os.environ["OPEN_WEATHER_API_KEY"] = getpass.getpass("Enter API key for OpenWeather: ")
raise EnvironmentError("OPEN_WEATHER_API_KEY not found in the .env file.")
if not os.environ.get("OPENAI_API_KEY"):
# os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
raise EnvironmentError("OPENAI_API_KEY not found in the .env file.")
# Below is an example of a tool that does nothing. Amaze us with your creativity !
@tool
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""A tool that does nothing yet
Args:
arg1: the first argument
arg2: the second argument
"""
return "What magic will you build ?"
@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)}"
@tool
def get_weather_by_city(city: str, country_code: str = "") -> str:
"""A tool that fetches current weather information for a specific city.
Args:
city: The name of the city (e.g., 'London', 'New York')
country_code: Optional 2-letter country code (e.g., 'US', 'GB')
"""
try:
api_key = os.environ.get("OPEN_WEATHER_API_KEY")
if not api_key:
return "Error: OpenWeather API key not found"
# Build location string
location = city
if country_code:
location += f",{country_code}"
# Get current weather
url = f"https://api.openweathermap.org/data/2.5/weather"
params = {
'q': location,
'appid': api_key,
'units': 'metric' # Celsius
}
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
# Extract relevant information
main = data['main']
weather = data['weather'][0]
wind = data.get('wind', {})
weather_info = f"""Current weather in {data['name']}, {data['sys']['country']}:
- Temperature: {main['temp']}°C (feels like {main['feels_like']}°C)
- Condition: {weather['description'].title()}
- Humidity: {main['humidity']}%
- Pressure: {main['pressure']} hPa"""
if 'speed' in wind:
weather_info += f"\n- Wind Speed: {wind['speed']} m/s"
return weather_info
except requests.exceptions.RequestException as e:
return f"Error fetching weather data: {str(e)}"
except KeyError as e:
return f"Error parsing weather data: Missing field {str(e)}"
except Exception as e:
return f"Unexpected error: {str(e)}"
@tool
def get_weather_forecast(city: str, country_code: str = "", days: int = 3) -> str:
"""A tool that fetches weather forecast for a specific city.
Args:
city: The name of the city (e.g., 'London', 'New York')
country_code: Optional 2-letter country code (e.g., 'US', 'GB')
days: Number of days to forecast (1-5, default: 3)
"""
try:
api_key = os.environ.get("OPEN_WEATHER_API_KEY")
if not api_key:
return "Error: OpenWeather API key not found"
# Limit days to 5 (API limitation)
days = min(max(1, days), 5)
# Build location string
location = city
if country_code:
location += f",{country_code}"
# Get forecast
url = f"https://api.openweathermap.org/data/2.5/forecast"
params = {
'q': location,
'appid': api_key,
'units': 'metric'
}
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
forecast_info = f"Weather forecast for {data['city']['name']}, {data['city']['country']}:\n\n"
# Group forecasts by date
daily_forecasts = {}
for item in data['list'][:days*8]: # 8 forecasts per day (3-hour intervals)
date = datetime.datetime.fromtimestamp(item['dt']).strftime('%Y-%m-%d')
if date not in daily_forecasts:
daily_forecasts[date] = []
daily_forecasts[date].append(item)
# Summarize each day
for date, forecasts in list(daily_forecasts.items())[:days]:
day_name = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%A, %B %d')
temps = [f['main']['temp'] for f in forecasts]
conditions = [f['weather'][0]['description'] for f in forecasts]
min_temp = min(temps)
max_temp = max(temps)
most_common_condition = max(set(conditions), key=conditions.count)
forecast_info += f"📅 {day_name}:\n"
forecast_info += f" Temperature: {min_temp:.1f}°C - {max_temp:.1f}°C\n"
forecast_info += f" Condition: {most_common_condition.title()}\n\n"
return forecast_info.strip()
except requests.exceptions.RequestException as e:
return f"Error fetching forecast data: {str(e)}"
except Exception as e:
return f"Unexpected error: {str(e)}"
@tool
def search_local_events(city: str, query: str = "events") -> str:
"""A tool that searches for local events in a specific city using web search.
Args:
city: The name of the city to search events in
query: Optional search query to refine results (default: 'events')
"""
try:
# Use DuckDuckGo search to find local events
search_tool = DuckDuckGoSearchTool()
search_query = f"{query} in {city} today this week upcoming"
results = search_tool(search_query)
if not results or "No results found" in str(results):
return f"No events found for '{query}' in {city}. Try searching with different keywords."
return f"Local events in {city}:\n\n{results}"
except Exception as e:
return f"Error searching for events: {str(e)}"
@tool
def get_current_date_time() -> str:
"""A tool that returns the current date and time in UTC and local system time.
"""
try:
utc_time = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
local_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return f"Current UTC time: {utc_time}\nLocal system time: {local_time}"
except Exception as e:
return f"Error getting current time: {str(e)}"
final_answer = FinalAnswerTool()
# 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:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
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,
)
# Import tool from Hub
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,
get_weather_by_city,
get_weather_forecast,
search_local_events,
get_current_date_time,
my_custom_tool
], ## add your tools here (don't remove final answer)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch() |