root
commited on
Commit
·
00af04f
1
Parent(s):
4af3315
showastresult
Browse files- app.py +74 -35
- example.py +29 -18
app.py
CHANGED
@@ -104,22 +104,29 @@ music_analyzer = MusicAnalyzer()
|
|
104 |
|
105 |
def extract_audio_features(audio_file):
|
106 |
"""Extract audio features from an audio file."""
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
def classify_genre(audio_data):
|
125 |
"""Classify the genre of the audio using the loaded model."""
|
@@ -313,27 +320,46 @@ def process_audio(audio_file):
|
|
313 |
audio_data = extract_audio_features(audio_file)
|
314 |
|
315 |
# First check if it's music
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
317 |
if not is_music:
|
318 |
-
return "The uploaded audio does not appear to be music. Please upload a music file.", None,
|
319 |
|
320 |
# Classify genre
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
|
|
|
|
|
|
325 |
|
326 |
# Analyze music emotions and themes
|
327 |
-
|
|
|
|
|
|
|
|
|
|
|
328 |
|
329 |
# Generate lyrics based on top genre and emotion analysis
|
330 |
-
|
331 |
-
|
|
|
|
|
|
|
|
|
332 |
|
333 |
return genre_results, lyrics, ast_results
|
334 |
|
335 |
except Exception as e:
|
336 |
-
|
|
|
|
|
337 |
|
338 |
# Create Gradio interface
|
339 |
with gr.Blocks(title="Music Genre Classifier & Lyrics Generator") as demo:
|
@@ -359,21 +385,34 @@ with gr.Blocks(title="Music Genre Classifier & Lyrics Generator") as demo:
|
|
359 |
# Process audio and get genre, lyrics, and AST results
|
360 |
genre_results, lyrics, ast_results = process_audio(audio_file)
|
361 |
|
|
|
|
|
|
|
|
|
362 |
# Format emotion analysis results
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
|
|
|
|
|
|
|
|
368 |
|
369 |
# Format AST classification results
|
370 |
-
|
371 |
-
|
372 |
-
|
|
|
|
|
|
|
373 |
|
374 |
return genre_results, emotion_text, ast_text, lyrics
|
375 |
except Exception as e:
|
376 |
-
|
|
|
|
|
377 |
|
378 |
submit_btn.click(
|
379 |
fn=display_results,
|
|
|
104 |
|
105 |
def extract_audio_features(audio_file):
|
106 |
"""Extract audio features from an audio file."""
|
107 |
+
try:
|
108 |
+
# Load the audio file using utility function
|
109 |
+
y, sr = load_audio(audio_file, SAMPLE_RATE)
|
110 |
+
|
111 |
+
if y is None or sr is None:
|
112 |
+
raise ValueError("Failed to load audio data")
|
113 |
+
|
114 |
+
# Get audio duration in seconds
|
115 |
+
duration = extract_audio_duration(y, sr)
|
116 |
+
|
117 |
+
# Extract MFCCs for genre classification (may not be needed with the pipeline)
|
118 |
+
mfccs_mean = extract_mfcc_features(y, sr, n_mfcc=20)
|
119 |
+
|
120 |
+
return {
|
121 |
+
"features": mfccs_mean,
|
122 |
+
"duration": duration,
|
123 |
+
"waveform": y,
|
124 |
+
"sample_rate": sr,
|
125 |
+
"path": audio_file # Keep path for the pipeline
|
126 |
+
}
|
127 |
+
except Exception as e:
|
128 |
+
print(f"Error extracting audio features: {str(e)}")
|
129 |
+
raise ValueError(f"Failed to extract audio features: {str(e)}")
|
130 |
|
131 |
def classify_genre(audio_data):
|
132 |
"""Classify the genre of the audio using the loaded model."""
|
|
|
320 |
audio_data = extract_audio_features(audio_file)
|
321 |
|
322 |
# First check if it's music
|
323 |
+
try:
|
324 |
+
is_music, ast_results = detect_music(audio_data)
|
325 |
+
except Exception as e:
|
326 |
+
print(f"Error in music detection: {str(e)}")
|
327 |
+
return f"Error in music detection: {str(e)}", None, []
|
328 |
+
|
329 |
if not is_music:
|
330 |
+
return "The uploaded audio does not appear to be music. Please upload a music file.", None, ast_results
|
331 |
|
332 |
# Classify genre
|
333 |
+
try:
|
334 |
+
top_genres = classify_genre(audio_data)
|
335 |
+
# Format genre results using utility function
|
336 |
+
genre_results = format_genre_results(top_genres)
|
337 |
+
except Exception as e:
|
338 |
+
print(f"Error in genre classification: {str(e)}")
|
339 |
+
return f"Error in genre classification: {str(e)}", None, ast_results
|
340 |
|
341 |
# Analyze music emotions and themes
|
342 |
+
try:
|
343 |
+
emotion_results = music_analyzer.analyze_music(audio_file)
|
344 |
+
except Exception as e:
|
345 |
+
print(f"Error in emotion analysis: {str(e)}")
|
346 |
+
# Continue even if emotion analysis fails
|
347 |
+
emotion_results = {"summary": {"tempo": 0, "key": "Unknown", "mode": "", "primary_emotion": "Unknown", "primary_theme": "Unknown"}}
|
348 |
|
349 |
# Generate lyrics based on top genre and emotion analysis
|
350 |
+
try:
|
351 |
+
primary_genre, _ = top_genres[0]
|
352 |
+
lyrics = generate_lyrics(primary_genre, audio_data["duration"], emotion_results)
|
353 |
+
except Exception as e:
|
354 |
+
print(f"Error generating lyrics: {str(e)}")
|
355 |
+
lyrics = f"Error generating lyrics: {str(e)}"
|
356 |
|
357 |
return genre_results, lyrics, ast_results
|
358 |
|
359 |
except Exception as e:
|
360 |
+
error_msg = f"Error processing audio: {str(e)}"
|
361 |
+
print(error_msg)
|
362 |
+
return error_msg, None, []
|
363 |
|
364 |
# Create Gradio interface
|
365 |
with gr.Blocks(title="Music Genre Classifier & Lyrics Generator") as demo:
|
|
|
385 |
# Process audio and get genre, lyrics, and AST results
|
386 |
genre_results, lyrics, ast_results = process_audio(audio_file)
|
387 |
|
388 |
+
# Check if we got an error message instead of results
|
389 |
+
if isinstance(genre_results, str) and genre_results.startswith("Error"):
|
390 |
+
return genre_results, "Error in emotion analysis", "Error in audio classification", None
|
391 |
+
|
392 |
# Format emotion analysis results
|
393 |
+
try:
|
394 |
+
emotion_results = music_analyzer.analyze_music(audio_file)
|
395 |
+
emotion_text = f"Tempo: {emotion_results['summary']['tempo']:.1f} BPM\n"
|
396 |
+
emotion_text += f"Key: {emotion_results['summary']['key']} {emotion_results['summary']['mode']}\n"
|
397 |
+
emotion_text += f"Primary Emotion: {emotion_results['summary']['primary_emotion']}\n"
|
398 |
+
emotion_text += f"Primary Theme: {emotion_results['summary']['primary_theme']}"
|
399 |
+
except Exception as e:
|
400 |
+
print(f"Error in emotion analysis: {str(e)}")
|
401 |
+
emotion_text = f"Error in emotion analysis: {str(e)}"
|
402 |
|
403 |
# Format AST classification results
|
404 |
+
if ast_results and isinstance(ast_results, list):
|
405 |
+
ast_text = "Audio Classification Results (AST Model):\n"
|
406 |
+
for result in ast_results[:5]: # Show top 5 results
|
407 |
+
ast_text += f"{result['label']}: {result['score']*100:.2f}%\n"
|
408 |
+
else:
|
409 |
+
ast_text = "No valid audio classification results available."
|
410 |
|
411 |
return genre_results, emotion_text, ast_text, lyrics
|
412 |
except Exception as e:
|
413 |
+
error_msg = f"Error: {str(e)}"
|
414 |
+
print(error_msg)
|
415 |
+
return error_msg, "Error in emotion analysis", "Error in audio classification", None
|
416 |
|
417 |
submit_btn.click(
|
418 |
fn=display_results,
|
example.py
CHANGED
@@ -23,33 +23,44 @@ def main():
|
|
23 |
# Call the main processing function
|
24 |
genre_results, lyrics, ast_results = process_audio(audio_file)
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
print("\n" + "="*50)
|
31 |
-
print("GENRE CLASSIFICATION RESULTS:")
|
32 |
-
print("="*50)
|
33 |
-
print(genre_results)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
print("\n" + "="*50)
|
44 |
print("AUDIO CLASSIFICATION RESULTS (AST):")
|
45 |
print("="*50)
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
print("\n" + "="*50)
|
50 |
print("GENERATED LYRICS:")
|
51 |
print("="*50)
|
52 |
-
print(lyrics)
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
main()
|
|
|
23 |
# Call the main processing function
|
24 |
genre_results, lyrics, ast_results = process_audio(audio_file)
|
25 |
|
26 |
+
# Check if we got an error message
|
27 |
+
if isinstance(genre_results, str) and genre_results.startswith("Error"):
|
28 |
+
print(f"Error processing audio: {genre_results}")
|
29 |
+
return
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Get emotion analysis results
|
32 |
+
try:
|
33 |
+
emotion_results = music_analyzer.analyze_music(audio_file)
|
34 |
+
|
35 |
+
# Print results
|
36 |
+
print("\n" + "="*50)
|
37 |
+
print("GENRE CLASSIFICATION RESULTS:")
|
38 |
+
print("="*50)
|
39 |
+
print(genre_results)
|
40 |
+
|
41 |
+
print("\n" + "="*50)
|
42 |
+
print("EMOTION ANALYSIS RESULTS:")
|
43 |
+
print("="*50)
|
44 |
+
print(f"Tempo: {emotion_results['summary']['tempo']:.1f} BPM")
|
45 |
+
print(f"Key: {emotion_results['summary']['key']} {emotion_results['summary']['mode']}")
|
46 |
+
print(f"Primary Emotion: {emotion_results['summary']['primary_emotion']}")
|
47 |
+
print(f"Primary Theme: {emotion_results['summary']['primary_theme']}")
|
48 |
+
except Exception as e:
|
49 |
+
print(f"\nError in emotion analysis: {str(e)}")
|
50 |
|
51 |
print("\n" + "="*50)
|
52 |
print("AUDIO CLASSIFICATION RESULTS (AST):")
|
53 |
print("="*50)
|
54 |
+
if ast_results and isinstance(ast_results, list) and len(ast_results) > 0:
|
55 |
+
for result in ast_results[:5]: # Show top 5 results
|
56 |
+
print(f"{result['label']}: {result['score']*100:.2f}%")
|
57 |
+
else:
|
58 |
+
print("No audio classification results available.")
|
59 |
|
60 |
print("\n" + "="*50)
|
61 |
print("GENERATED LYRICS:")
|
62 |
print("="*50)
|
63 |
+
print(lyrics if lyrics else "No lyrics generated.")
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
main()
|