Spaces:
Sleeping
Sleeping
File size: 6,772 Bytes
d2ecfeb 9b5b26a c19d193 d2ecfeb 6aae614 8fe992b 9b5b26a d2ecfeb 9b5b26a 8c01ffb d2ecfeb 8c01ffb 6aae614 e121372 d2ecfeb 13d500a 8c01ffb 9b5b26a 8c01ffb 861422e 9b5b26a 8c01ffb 8fe992b d2ecfeb 8c01ffb 861422e 8fe992b 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 |
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
@tool
def my_cutom_tool(arg1: str, arg2: int) -> str:
"""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:
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 '{timezone}': {str(e)}"
# --- Helper functions for Spotify and environmental data ---
def fetch_spotify_access_token():
url = "https://accounts.spotify.com/api/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {
"grant_type": "client_credentials",
"client_id": os.getenv("SPOTIFY_CLIENT_ID"),
"client_secret": os.getenv("SPOTIFY_CLIENT_SECRET")
}
response = requests.post(url, headers=headers, data=data)
return response.json().get("access_token")
def fetch_user_location_data():
url = "http://ip-api.com/json/"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data.get("city"), data.get("country"), data.get("timezone")
return None, None, None
def fetch_weather_for_city(city: str):
WEATHER_API_KEY = os.getenv("WEATHER_API_KEY")
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={WEATHER_API_KEY}&units=metric"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data["weather"][0]["main"]
return None
def map_mood_to_params(mood: str, weather_condition: str = None) -> dict:
# Base mapping of mood words to Spotify's target parameters and seed genre
default_mappings = {
"happy": {"target_valence": 0.9, "target_energy": 0.8, "seed_genre": "pop"},
"sad": {"target_valence": 0.2, "target_energy": 0.3, "seed_genre": "acoustic"},
"energetic": {"target_valence": 0.7, "target_energy": 0.9, "seed_genre": "work-out"},
"chill": {"target_valence": 0.6, "target_energy": 0.4, "seed_genre": "chill"}
}
mapping = default_mappings.get(mood.lower(), {"target_valence": 0.5, "target_energy": 0.5, "seed_genre": "pop"})
# Adjust parameters based on weather conditions if provided
if weather_condition:
weather = weather_condition.lower()
if weather in ["rain", "thunderstorm"]:
mapping["target_energy"] = max(mapping["target_energy"] - 0.2, 0.0)
elif weather in ["clear"]:
mapping["target_energy"] = min(mapping["target_energy"] + 0.1, 1.0)
return mapping
# --- Environmental Tools ---
@tool
def get_user_location() -> str:
"""Fetches the user's location using ip-api.com.
Returns a string with city, country, and timezone.
"""
city, country, timezone = fetch_user_location_data()
if city and country and timezone:
return f"City: {city}, Country: {country}, Timezone: {timezone}"
return "Error fetching location."
@tool
def get_weather(city: str) -> str:
"""Fetches weather data for a given city using OpenWeatherMap API.
Args:
city: The city name.
"""
weather = fetch_weather_for_city(city)
if weather:
return f"Current weather in {city} is {weather}."
return "Error fetching weather data."
@tool
def get_songs_by_mood(mood: str, local: bool = False) -> str:
"""Fetches a playlist of songs that fits the user's mood using Spotify's Recommendations API.
Mood is expressed in terms of target_valence and target_energy (energy replaces arousal).
The tool internally maps mood words (e.g., 'happy', 'sad', 'energetic', 'chill') to target parameters:
- "happy": target_valence ~ 0.9, target_energy ~ 0.8, seed_genre "pop"
- "sad": target_valence ~ 0.2, target_energy ~ 0.3, seed_genre "acoustic"
- "energetic": target_valence ~ 0.7, target_energy ~ 0.9, seed_genre "work-out"
- "chill": target_valence ~ 0.6, target_energy ~ 0.4, seed_genre "chill"
If the optional parameter 'local' is True, the tool also fetches the user's location and current weather,
and adjusts the mood mapping accordingly (e.g., reducing energy on rainy days).
"""
weather_condition = None
if local:
city, country, timezone = fetch_user_location_data()
if not city:
return "Error: Unable to determine user location."
weather_condition = fetch_weather_for_city(city)
mapping = map_mood_to_params(mood, weather_condition)
access_token = fetch_spotify_access_token()
if not access_token:
return "Error: Unable to retrieve Spotify access token."
params = {
"seed_genres": mapping["seed_genre"],
"target_valence": mapping["target_valence"],
"target_energy": mapping["target_energy"],
"limit": 10
}
url = "https://api.spotify.com/v1/recommendations"
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
tracks = response.json().get("tracks", [])
if not tracks:
return "No tracks found for the specified mood."
playlist = [f"{track['name']} - {track['artists'][0]['name']}" for track in tracks]
return "\n".join(playlist)
return f"Error: {response.json()}"
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
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,
my_cutom_tool,
get_current_time_in_timezone,
get_user_location,
get_weather,
get_songs_by_mood
],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch() |