File size: 10,485 Bytes
5889992 |
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
from abc import ABC, abstractmethod
from spotipy import Spotify
from spotipy.oauth2 import SpotifyClientCredentials
from typing import Dict, List, Optional
import os
import logging
from dataclasses import dataclass
import json
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
# Configure logging
logger = logging.getLogger(__name__)
# -------------------------
# Data Structures
# -------------------------
@dataclass
class TrackRecommendation:
uri: str
name: str
artist: str
preview_url: Optional[str]
audio_features: Dict
@dataclass
class RecommendationParameters:
seed_genres: List[str]
target_features: Dict
limit: int = 20
market: str = "US"
# -------------------------
# Core Interfaces
# -------------------------
class IMusicRecommendationStrategy(ABC):
@abstractmethod
def generate_recommendations(self, emotion: str, context: Dict) -> List[TrackRecommendation]:
pass
class IAudioAnalyzer(ABC):
@abstractmethod
def analyze_track(self, track_uri: str) -> Dict:
pass
# -------------------------
# Spotify Client
# -------------------------
class SpotifyClient:
"""Handles Spotify authentication and basic API operations"""
def __init__(self):
self.client_credentials_manager = SpotifyClientCredentials(
client_id=os.getenv("SPOTIFY_CLIENT_ID"),
client_secret=os.getenv("SPOTIFY_CLIENT_SECRET")
)
self.client = Spotify(client_credentials_manager=self.client_credentials_manager)
def get_recommendations(self, params: RecommendationParameters) -> List[Dict]:
"""Base recommendation API call"""
try:
response = self.client.recommendations(
seed_genres=params.seed_genres,
target_features=params.target_features,
limit=params.limit,
market=params.market
)
return response['tracks']
except Exception as e:
logger.error(f"Recommendation failed: {str(e)}")
raise
# -------------------------
# Emotion Mapping System
# -------------------------
class EmotionAudioProfile:
"""Maps emotions to audio characteristics with cultural adaptation"""
def __init__(self):
self.base_profiles = {
"sad": {"target_valence": 0.2, "target_energy": 0.3},
"happy": {"target_valence": 0.8, "target_energy": 0.7},
"anxious": {"target_valence": 0.5, "target_energy": 0.4},
"angry": {"target_valence": 0.3, "target_energy": 0.8}
}
self.cultural_adjustments = {
"US": {"happy": {"target_danceability": 0.8}},
"JP": {"happy": {"target_danceability": 0.6}}
}
def get_profile(self, emotion: str, country: str = "US") -> Dict:
"""Get culturally adjusted audio profile"""
profile = self.base_profiles.get(emotion, {}).copy()
profile.update(self.cultural_adjustments.get(country, {}).get(emotion, {}))
return profile
class GenreMapper:
"""Hierarchical genre mapping system with fallbacks"""
def __init__(self, spotify_client: SpotifyClient):
self.spotify = spotify_client
self.genre_hierarchy = {
"sad": ["blues", "soul", "acoustic"],
"happy": ["pop", "dance", "disco"],
"anxious": ["ambient", "classical"],
"angry": ["rock", "metal"]
}
self.available_genres = self._load_available_genres()
def _load_available_genres(self) -> List[str]:
"""Get valid Spotify genres"""
return self.spotify.client.recommendation_genre_seeds()['genres']
def get_genres(self, emotion: str) -> List[str]:
"""Get best available genres for emotion"""
for genre in self.genre_hierarchy.get(emotion, []):
if genre in self.available_genres:
return [genre]
return ["pop"]
# -------------------------
# AI Integration
# -------------------------
class LLMEnhancer:
"""Enhances recommendations using LLM context analysis"""
def __init__(self):
from langchain_google_genai import ChatGoogleGenerativeAI
self.llm = ChatGoogleGenerativeAI(model="gemini-pro")
def enhance_params(self, context: Dict) -> Dict:
"""Analyze conversation context for musical attributes"""
prompt = f"""
Analyze this therapeutic context to suggest music parameters:
{json.dumps(context, indent=2)}
Return JSON with:
- target_energy (0-1)
- target_danceability (0-1)
- target_tempo
- seed_artist (main artist name)
- seed_track (main track name)
"""
try:
response = self.llm.invoke(prompt)
return json.loads(response.content)
except Exception as e:
logger.warning(f"LLM enhancement failed: {str(e)}")
return {}
# -------------------------
# Recommendation Engine
# -------------------------
class TherapeuticMusicRecommender(IMusicRecommendationStrategy):
"""Main recommendation engine with multiple strategies"""
def __init__(self):
self.spotify = SpotifyClient()
self.audio_profiler = EmotionAudioProfile()
self.genre_mapper = GenreMapper(self.spotify)
self.llm_enhancer = LLMEnhancer()
self.cache = RecommendationCache()
def generate_recommendations(self, emotion: str, context: Dict) -> List[TrackRecommendation]:
"""Generate context-aware recommendations"""
# Check cache first
cache_key = self._generate_cache_key(emotion, context)
if cached := self.cache.get(cache_key):
return cached
# Build parameters
params = self._build_recommendation_params(emotion, context)
# Get raw recommendations
raw_tracks = self.spotify.get_recommendations(params)
# Process and enrich tracks
processed = self._process_tracks(raw_tracks)
# Cache results
self.cache.store(cache_key, processed)
return processed
def _build_recommendation_params(self, emotion: str, context: Dict) -> RecommendationParameters:
"""Construct recommendation parameters"""
base_features = self.audio_profiler.get_profile(
emotion,
context.get('user', {}).get('country', 'US')
)
llm_features = self.llm_enhancer.enhance_params(context)
return RecommendationParameters(
seed_genres=self.genre_mapper.get_genres(emotion),
target_features={**base_features, **llm_features},
market=context.get('user', {}).get('country', 'US'),
limit=context.get('limit', 20)
)
def _process_tracks(self, raw_tracks: List[Dict]) -> List[TrackRecommendation]:
"""Convert raw tracks to enriched recommendations"""
return [
TrackRecommendation(
uri=track['uri'],
name=track['name'],
artist=track['artists'][0]['name'],
preview_url=track.get('preview_url'),
audio_features=self.spotify.client.audio_features(track['uri'])[0]
) for track in raw_tracks
]
def _generate_cache_key(self, emotion: str, context: Dict) -> str:
"""Generate unique cache key"""
return f"{emotion}-{context.get('user', {}).get('id', 'anonymous')}"
# -------------------------
# Advanced Features
# -------------------------
class RecommendationCache:
"""LRU cache for recommendations"""
def __init__(self, max_size: int = 100):
self.cache = {}
self.max_size = max_size
self.order = []
def get(self, key: str) -> Optional[List[TrackRecommendation]]:
if key in self.cache:
self.order.remove(key)
self.order.append(key)
return self.cache[key]
return None
def store(self, key: str, recommendations: List[TrackRecommendation]):
if len(self.cache) >= self.max_size:
oldest = self.order.pop(0)
del self.cache[oldest]
self.cache[key] = recommendations
self.order.append(key)
class MoodTransitionEngine:
"""Creates playlists that transition between emotional states"""
def __init__(self, recommender: TherapeuticMusicRecommender):
self.recommender = recommender
def create_transition_playlist(self, start_emotion: str, end_emotion: str, context: Dict) -> List[TrackRecommendation]:
"""Generate mood transition sequence"""
steps = self._calculate_transition_steps(start_emotion, end_emotion)
playlist = []
for step in steps:
context['transition_step'] = step
playlist += self.recommender.generate_recommendations(
emotion=step['emotion'],
context=context
)
return playlist
def _calculate_transition_steps(self, start: str, end: str) -> List[Dict]:
"""Determine intermediate emotional states"""
transitions = {
('sad', 'happy'): [{'emotion': 'sad', 'intensity': 0.8},
{'emotion': 'neutral', 'intensity': 0.5},
{'emotion': 'happy', 'intensity': 0.7}],
# Add other transition paths
}
return transitions.get((start, end), [])
# -------------------------
# Usage Example
# -------------------------
if __name__ == "__main__":
# Initialize system
recommender = TherapeuticMusicRecommender()
# Sample context from therapy session
context = {
"user": {
"id": "user123",
"country": "US",
"time_of_day": datetime.now().hour
},
"conversation": {
"emotion": "anxious",
"key_phrases": ["work stress", "sleep issues"],
"therapist_notes": "Needs calming music with nature sounds"
}
}
# Generate recommendations
recommendations = recommender.generate_recommendations(
emotion="anxious",
context=context
)
# Output results
print(f"Generated {len(recommendations)} tracks:")
for track in recommendations[:3]:
print(f"- {track.artist}: {track.name} ({track.audio_features['tempo']} BPM)") |