File size: 13,737 Bytes
8b04ef6 6849c54 8b04ef6 63406cf 051f255 8b04ef6 63406cf 8b04ef6 63406cf 051f255 385ef9c 051f255 63406cf 051f255 8b04ef6 6849c54 051f255 6849c54 385ef9c 6849c54 385ef9c 6849c54 385ef9c 6849c54 385ef9c 6849c54 385ef9c 6849c54 385ef9c 6849c54 385ef9c 6849c54 8b04ef6 63406cf 051f255 6849c54 051f255 63406cf 6849c54 051f255 385ef9c 051f255 63406cf 6849c54 051f255 385ef9c 051f255 63406cf 6849c54 051f255 385ef9c 051f255 63406cf 6849c54 051f255 385ef9c 051f255 63406cf 051f255 385ef9c 051f255 63406cf 051f255 385ef9c 051f255 63406cf |
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 316 317 318 319 320 321 322 323 324 |
import os
import json
import numpy as np
import librosa
import warnings
import pandas as pd
from langchain_groq import ChatGroq
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.schema import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
from huggingface_hub import InferenceClient
warnings.filterwarnings("ignore")
# Enhanced LLM support with multiple providers
def generate_music_insight(analysis_text, provider="groq", model="llama3-70b-8192"):
try:
prompt_template = PromptTemplate(
input_variables=["analysis_text"],
template="""
You're an AI music expert. Based on the musical analysis below, provide insight on:
- Genre and mood
- Instrumentation and music type
- Suggestions for improvement
- Commercial/viral potential
- Suitable platforms or audience
Musical Analysis: {analysis_text}
Answer in a concise paragraph.
"""
)
final_prompt = prompt_template.format(analysis_text=analysis_text)
# Support for multiple LLM providers
if provider.lower() == "groq":
llm = ChatGroq(
model_name=model,
groq_api_key=os.getenv("GROQ_API_KEY", "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")
)
elif provider.lower() == "openai":
llm = ChatOpenAI(
model_name=model,
openai_api_key=os.getenv("OPENAI_API_KEY", "sk-demo-key")
)
elif provider.lower() == "huggingface":
# For HuggingFace models, we'll use their Inference API
client = InferenceClient(
model=model,
token=os.getenv("HF_API_TOKEN", "hf_demo_token")
)
return client.text_generation(
final_prompt,
max_new_tokens=512,
temperature=0.7,
return_full_text=False
)
else:
raise ValueError(f"Unsupported provider: {provider}")
return llm.invoke(final_prompt)
except Exception as e:
return f"LLM Error: {str(e)}"
# AudioFeatureExtractor (unchanged)
class AudioFeatureExtractor:
def __init__(self, file_path):
self.file_path = file_path
self.y = None
self.sr = None
self.features = {}
def load_audio(self):
try:
self.y, self.sr = librosa.load(self.file_path, sr=None)
return True
except Exception as e:
print("Audio loading error:", e)
return False
def extract_basic_features(self):
duration = len(self.y) / self.sr
tempo, beat_frames = librosa.beat.beat_track(y=self.y, sr=self.sr)
rms = librosa.feature.rms(y=self.y)[0]
zcr = librosa.feature.zero_crossing_rate(self.y)[0]
y_harmonic, y_percussive = librosa.effects.hpss(self.y)
self.features['basic'] = {
'duration': float(duration),
'tempo': float(tempo),
'num_beats': len(beat_frames),
'avg_rms_energy': float(np.mean(rms)),
'avg_zero_crossing_rate': float(np.mean(zcr)),
'harmonic_percussive_ratio': float(np.sum(np.abs(y_harmonic)) / (np.sum(np.abs(y_percussive)) + 1e-10))
}
def extract_spectral_features(self):
centroid = librosa.feature.spectral_centroid(y=self.y, sr=self.sr)[0]
contrast = librosa.feature.spectral_contrast(y=self.y, sr=self.sr)
rolloff = librosa.feature.spectral_rolloff(y=self.y, sr=self.sr)[0]
mfccs = librosa.feature.mfcc(y=self.y, sr=self.sr, n_mfcc=13)
self.features['spectral'] = {
'avg_spectral_centroid': float(np.mean(centroid)),
'avg_spectral_contrast': [float(np.mean(band)) for band in contrast],
'avg_spectral_rolloff': float(np.mean(rolloff)),
'avg_mfccs': [float(np.mean(m)) for m in mfccs]
}
def extract_harmonic_features(self):
chroma = librosa.feature.chroma_stft(y=self.y, sr=self.sr)
tonnetz = librosa.feature.tonnetz(y=self.y, sr=self.sr)
chroma_avg = np.mean(librosa.feature.chroma_cqt(y=self.y, sr=self.sr), axis=1)
key_names = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
self.features['harmonic'] = {
'chroma_energy': [float(np.mean(c)) for c in chroma],
'tonnetz_features': [float(np.mean(t)) for t in tonnetz],
'estimated_key': key_names[np.argmax(chroma_avg)],
'key_strength': float(np.max(chroma_avg) / (np.sum(chroma_avg) + 1e-10))
}
def detect_instruments(self):
perc_ratio = self.features['basic']['harmonic_percussive_ratio']
centroid = self.features['spectral']['avg_spectral_centroid']
mfccs = self.features['spectral']['avg_mfccs']
instruments = []
if perc_ratio < 0.8:
instruments.append("Drums/Percussion")
if centroid < 1000:
instruments.append("Bass")
if 1000 <= centroid <= 3000:
instruments.append("Guitar/Piano")
if centroid > 3000:
instruments.append("High-pitched instruments")
if 1500 < centroid < 4000 and abs(mfccs[2]) > 5:
instruments.append("Vocals")
self.features['detected_instruments'] = instruments
def analyze_mood(self):
tempo = self.features['basic']['tempo']
energy = self.features['basic']['avg_rms_energy']
centroid = self.features['spectral']['avg_spectral_centroid']
moods = []
if tempo < 80:
moods.append("Slow/Relaxed")
elif tempo <= 120:
moods.append("Moderate/Balanced")
else:
moods.append("Fast/Energetic")
if energy < 0.1:
moods.append("Calm")
elif energy > 0.2:
moods.append("Intense")
if centroid < 1500:
moods.append("Dark/Warm")
elif centroid > 3000:
moods.append("Bright/Sharp")
self.features['mood_indicators'] = moods
def extract_all_features(self):
if not self.load_audio():
return False
self.extract_basic_features()
self.extract_spectral_features()
self.extract_harmonic_features()
self.detect_instruments()
self.analyze_mood()
return True
def to_json(self):
summary = {
"file_name": os.path.basename(self.file_path),
"duration": self.features['basic']['duration'],
"tempo": self.features['basic']['tempo'],
"key": self.features['harmonic']['estimated_key'],
"energy": self.features['basic']['avg_rms_energy'],
"detected_instruments": self.features['detected_instruments'],
"mood_indicators": self.features['mood_indicators'],
"spectral_centroid": self.features['spectral']['avg_spectral_centroid'],
"harmonic_percussive_ratio": self.features['basic']['harmonic_percussive_ratio']
}
return json.dumps(summary, indent=2)
class MusicAnalysisAgent:
def __init__(self, model="llama3-70b-8192", provider="groq"):
self.model = model
self.provider = provider
# Configure LLM based on provider
if provider.lower() == "groq":
groq_api_key = os.getenv("GROQ_API_KEY", "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")
self.llm = ChatGroq(model_name=model, groq_api_key=groq_api_key)
elif provider.lower() == "openai":
openai_api_key = os.getenv("OPENAI_API_KEY", "sk-demo-key")
self.llm = ChatOpenAI(model_name=model, openai_api_key=openai_api_key)
elif provider.lower() == "huggingface":
# For Hugging Face, we'll initialize during the chain execution
self.hf_client = InferenceClient(
model=model,
token=os.getenv("HF_API_TOKEN", "hf_demo_token")
)
self.llm = None
else:
raise ValueError(f"Unsupported provider: {provider}")
def _run_chain(self, template, features):
prompt = PromptTemplate.from_template(template)
if self.provider.lower() == "huggingface":
# For Hugging Face, use direct inference
try:
full_prompt = prompt.format(features=features)
return self.hf_client.text_generation(
full_prompt,
max_new_tokens=512,
temperature=0.7,
return_full_text=False
)
except Exception as e:
print(f"HuggingFace inference error: {str(e)}")
return f"Error with HuggingFace inference: {str(e)}"
else:
# For other providers, use LangChain
chain = prompt | self.llm | StrOutputParser()
try:
return chain.invoke({"features": features})
except Exception as e:
print(f"Chain execution error: {str(e)}")
return f"Error in LLM chain: {str(e)}"
def analyze_song_features(self, features):
try:
return self._run_chain("""
You are a music producer. Analyze: {features}
Discuss sound profile, genre, emotional tone, and how features interact.
""", features)
except Exception as e:
print(f"Song feature analysis error: {str(e)}")
return f"Song analysis error. Please check the {self.provider} API connection."
def get_song_improvement_suggestions(self, features):
try:
return self._run_chain("""
Suggest improvements in production, instrumentation, mix, structure, and effects: {features}
""", features)
except Exception as e:
print(f"Improvement suggestions error: {str(e)}")
return f"Improvement suggestions unavailable. Please check the {self.provider} API connection."
def assess_workout_playlist_fit(self, features):
try:
return self._run_chain("""
Evaluate if suitable for workout playlists. Consider tempo, energy, emotion, instruments, context: {features}
""", features)
except Exception as e:
print(f"Workout playlist assessment error: {str(e)}")
return f"Workout playlist assessment unavailable. Please check the {self.provider} API connection."
def suggest_marketing_channels(self, features):
try:
return self._run_chain("""
Recommend marketing channels: platforms, social media, audience targeting, playlist strategy: {features}
""", features)
except Exception as e:
print(f"Marketing channels suggestion error: {str(e)}")
return f"Marketing channel suggestions unavailable. Please check the {self.provider} API connection."
def recommend_genre_classification(self, features):
try:
return self._run_chain("""
Suggest a musical genre based on the features below. Be specific and explain: {features}
""", features)
except Exception as e:
print(f"Genre classification error: {str(e)}")
return f"Genre classification unavailable. Please check the {self.provider} API connection."
def recommend_music_category(self, features):
try:
return self._run_chain("""
Classify this music into a category like Chill, Romantic, Party, Study, Sad, Workout, etc. {features}
""", features)
except Exception as e:
print(f"Music category recommendation error: {str(e)}")
return f"Music category classification unavailable. Please check the {self.provider} API connection."
# New enhanced methods for more specific music analysis
def analyze_lyric_improvement(self, features, current_lyrics=None):
"""Suggests improvements for song lyrics based on the audio analysis"""
try:
return self._run_chain("""
Based on this audio analysis: {features}
Provide recommendations for lyrical content and style that would complement
the musical qualities. Consider the mood, tempo, and emotional tone.
"""+ (f"\n\nCurrent lyrics: {current_lyrics}" if current_lyrics else ""), features)
except Exception as e:
print(f"Lyric improvement analysis error: {str(e)}")
return f"Lyric improvement suggestions unavailable. Please check the {self.provider} API connection."
def analyze_commercial_potential(self, features):
"""Analyzes commercial potential of the track"""
try:
return self._run_chain("""
Analyze the commercial potential of this track based on: {features}
Consider:
1. Current music market trends
2. Similar successful tracks
3. Target audience size and engagement
4. Streaming potential
5. Licensing opportunities
Provide a detailed assessment with specific recommendations.
""", features)
except Exception as e:
print(f"Commercial potential analysis error: {str(e)}")
return f"Commercial potential analysis unavailable. Please check the {self.provider} API connection." |