root
commited on
Commit
·
4af3315
1
Parent(s):
9e21eef
showastresult
Browse files- app.py +26 -15
- example.py +7 -1
app.py
CHANGED
|
@@ -261,7 +261,7 @@ def detect_music(audio_data):
|
|
| 261 |
label = result["label"].lower()
|
| 262 |
if any(music_term in label for music_term in ["music", "song", "singing", "instrument"]):
|
| 263 |
music_confidence = max(music_confidence, result["score"])
|
| 264 |
-
return music_confidence >= 0.5
|
| 265 |
|
| 266 |
# Second attempt: Use manually loaded model components
|
| 267 |
elif 'music_processor' in globals() and 'music_model' in globals():
|
|
@@ -284,33 +284,38 @@ def detect_music(audio_data):
|
|
| 284 |
|
| 285 |
# Check for music-related classes
|
| 286 |
music_confidence = 0.0
|
|
|
|
|
|
|
| 287 |
for i, (value, index) in enumerate(zip(values[0], indices[0])):
|
| 288 |
label = labels[index.item()].lower()
|
|
|
|
|
|
|
|
|
|
| 289 |
if any(music_term in label for music_term in ["music", "song", "singing", "instrument"]):
|
| 290 |
-
music_confidence = max(music_confidence,
|
| 291 |
|
| 292 |
-
return music_confidence >= 0.5
|
| 293 |
|
| 294 |
else:
|
| 295 |
raise ValueError("No music detection model available")
|
| 296 |
|
| 297 |
except Exception as e:
|
| 298 |
print(f"Error in music detection: {str(e)}")
|
| 299 |
-
return False
|
| 300 |
|
| 301 |
def process_audio(audio_file):
|
| 302 |
"""Main function to process audio file, classify genre, and generate lyrics."""
|
| 303 |
if audio_file is None:
|
| 304 |
-
return "Please upload an audio file.", None
|
| 305 |
|
| 306 |
try:
|
| 307 |
# Extract audio features
|
| 308 |
audio_data = extract_audio_features(audio_file)
|
| 309 |
|
| 310 |
# First check if it's music
|
| 311 |
-
is_music = detect_music(audio_data)
|
| 312 |
if not is_music:
|
| 313 |
-
return "The uploaded audio does not appear to be music. Please upload a music file.", None
|
| 314 |
|
| 315 |
# Classify genre
|
| 316 |
top_genres = classify_genre(audio_data)
|
|
@@ -325,10 +330,10 @@ def process_audio(audio_file):
|
|
| 325 |
primary_genre, _ = top_genres[0]
|
| 326 |
lyrics = generate_lyrics(primary_genre, audio_data["duration"], emotion_results)
|
| 327 |
|
| 328 |
-
return genre_results, lyrics
|
| 329 |
|
| 330 |
except Exception as e:
|
| 331 |
-
return f"Error processing audio: {str(e)}", None
|
| 332 |
|
| 333 |
# Create Gradio interface
|
| 334 |
with gr.Blocks(title="Music Genre Classifier & Lyrics Generator") as demo:
|
|
@@ -343,15 +348,16 @@ with gr.Blocks(title="Music Genre Classifier & Lyrics Generator") as demo:
|
|
| 343 |
with gr.Column():
|
| 344 |
genre_output = gr.Textbox(label="Detected Genres", lines=5)
|
| 345 |
emotion_output = gr.Textbox(label="Emotion Analysis", lines=5)
|
|
|
|
| 346 |
lyrics_output = gr.Textbox(label="Generated Lyrics", lines=15)
|
| 347 |
|
| 348 |
def display_results(audio_file):
|
| 349 |
if audio_file is None:
|
| 350 |
-
return "Please upload an audio file.", "No emotion analysis available.", None
|
| 351 |
|
| 352 |
try:
|
| 353 |
-
# Process audio and get genre and
|
| 354 |
-
genre_results, lyrics = process_audio(audio_file)
|
| 355 |
|
| 356 |
# Format emotion analysis results
|
| 357 |
emotion_results = music_analyzer.analyze_music(audio_file)
|
|
@@ -360,14 +366,19 @@ with gr.Blocks(title="Music Genre Classifier & Lyrics Generator") as demo:
|
|
| 360 |
emotion_text += f"Primary Emotion: {emotion_results['summary']['primary_emotion']}\n"
|
| 361 |
emotion_text += f"Primary Theme: {emotion_results['summary']['primary_theme']}"
|
| 362 |
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
except Exception as e:
|
| 365 |
-
return f"Error: {str(e)}", "Error in emotion analysis", None
|
| 366 |
|
| 367 |
submit_btn.click(
|
| 368 |
fn=display_results,
|
| 369 |
inputs=[audio_input],
|
| 370 |
-
outputs=[genre_output, emotion_output, lyrics_output]
|
| 371 |
)
|
| 372 |
|
| 373 |
gr.Markdown("### How it works")
|
|
|
|
| 261 |
label = result["label"].lower()
|
| 262 |
if any(music_term in label for music_term in ["music", "song", "singing", "instrument"]):
|
| 263 |
music_confidence = max(music_confidence, result["score"])
|
| 264 |
+
return music_confidence >= 0.5, results
|
| 265 |
|
| 266 |
# Second attempt: Use manually loaded model components
|
| 267 |
elif 'music_processor' in globals() and 'music_model' in globals():
|
|
|
|
| 284 |
|
| 285 |
# Check for music-related classes
|
| 286 |
music_confidence = 0.0
|
| 287 |
+
results = []
|
| 288 |
+
|
| 289 |
for i, (value, index) in enumerate(zip(values[0], indices[0])):
|
| 290 |
label = labels[index.item()].lower()
|
| 291 |
+
score = value.item()
|
| 292 |
+
results.append({"label": label, "score": score})
|
| 293 |
+
|
| 294 |
if any(music_term in label for music_term in ["music", "song", "singing", "instrument"]):
|
| 295 |
+
music_confidence = max(music_confidence, score)
|
| 296 |
|
| 297 |
+
return music_confidence >= 0.5, results
|
| 298 |
|
| 299 |
else:
|
| 300 |
raise ValueError("No music detection model available")
|
| 301 |
|
| 302 |
except Exception as e:
|
| 303 |
print(f"Error in music detection: {str(e)}")
|
| 304 |
+
return False, []
|
| 305 |
|
| 306 |
def process_audio(audio_file):
|
| 307 |
"""Main function to process audio file, classify genre, and generate lyrics."""
|
| 308 |
if audio_file is None:
|
| 309 |
+
return "Please upload an audio file.", None, None
|
| 310 |
|
| 311 |
try:
|
| 312 |
# Extract audio features
|
| 313 |
audio_data = extract_audio_features(audio_file)
|
| 314 |
|
| 315 |
# First check if it's music
|
| 316 |
+
is_music, ast_results = detect_music(audio_data)
|
| 317 |
if not is_music:
|
| 318 |
+
return "The uploaded audio does not appear to be music. Please upload a music file.", None, None
|
| 319 |
|
| 320 |
# Classify genre
|
| 321 |
top_genres = classify_genre(audio_data)
|
|
|
|
| 330 |
primary_genre, _ = top_genres[0]
|
| 331 |
lyrics = generate_lyrics(primary_genre, audio_data["duration"], emotion_results)
|
| 332 |
|
| 333 |
+
return genre_results, lyrics, ast_results
|
| 334 |
|
| 335 |
except Exception as e:
|
| 336 |
+
return f"Error processing audio: {str(e)}", None, None
|
| 337 |
|
| 338 |
# Create Gradio interface
|
| 339 |
with gr.Blocks(title="Music Genre Classifier & Lyrics Generator") as demo:
|
|
|
|
| 348 |
with gr.Column():
|
| 349 |
genre_output = gr.Textbox(label="Detected Genres", lines=5)
|
| 350 |
emotion_output = gr.Textbox(label="Emotion Analysis", lines=5)
|
| 351 |
+
ast_output = gr.Textbox(label="Audio Classification Results (AST)", lines=5)
|
| 352 |
lyrics_output = gr.Textbox(label="Generated Lyrics", lines=15)
|
| 353 |
|
| 354 |
def display_results(audio_file):
|
| 355 |
if audio_file is None:
|
| 356 |
+
return "Please upload an audio file.", "No emotion analysis available.", "No audio classification available.", None
|
| 357 |
|
| 358 |
try:
|
| 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 |
emotion_results = music_analyzer.analyze_music(audio_file)
|
|
|
|
| 366 |
emotion_text += f"Primary Emotion: {emotion_results['summary']['primary_emotion']}\n"
|
| 367 |
emotion_text += f"Primary Theme: {emotion_results['summary']['primary_theme']}"
|
| 368 |
|
| 369 |
+
# Format AST classification results
|
| 370 |
+
ast_text = "Audio Classification Results (AST Model):\n"
|
| 371 |
+
for result in ast_results[:5]: # Show top 5 results
|
| 372 |
+
ast_text += f"{result['label']}: {result['score']*100:.2f}%\n"
|
| 373 |
+
|
| 374 |
+
return genre_results, emotion_text, ast_text, lyrics
|
| 375 |
except Exception as e:
|
| 376 |
+
return f"Error: {str(e)}", "Error in emotion analysis", "Error in audio classification", None
|
| 377 |
|
| 378 |
submit_btn.click(
|
| 379 |
fn=display_results,
|
| 380 |
inputs=[audio_input],
|
| 381 |
+
outputs=[genre_output, emotion_output, ast_output, lyrics_output]
|
| 382 |
)
|
| 383 |
|
| 384 |
gr.Markdown("### How it works")
|
example.py
CHANGED
|
@@ -21,7 +21,7 @@ def main():
|
|
| 21 |
print(f"Processing audio file: {audio_file}")
|
| 22 |
|
| 23 |
# Call the main processing function
|
| 24 |
-
genre_results, lyrics = process_audio(audio_file)
|
| 25 |
|
| 26 |
# Get emotion analysis results
|
| 27 |
emotion_results = music_analyzer.analyze_music(audio_file)
|
|
@@ -40,6 +40,12 @@ def main():
|
|
| 40 |
print(f"Primary Emotion: {emotion_results['summary']['primary_emotion']}")
|
| 41 |
print(f"Primary Theme: {emotion_results['summary']['primary_theme']}")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
print("\n" + "="*50)
|
| 44 |
print("GENERATED LYRICS:")
|
| 45 |
print("="*50)
|
|
|
|
| 21 |
print(f"Processing audio file: {audio_file}")
|
| 22 |
|
| 23 |
# Call the main processing function
|
| 24 |
+
genre_results, lyrics, ast_results = process_audio(audio_file)
|
| 25 |
|
| 26 |
# Get emotion analysis results
|
| 27 |
emotion_results = music_analyzer.analyze_music(audio_file)
|
|
|
|
| 40 |
print(f"Primary Emotion: {emotion_results['summary']['primary_emotion']}")
|
| 41 |
print(f"Primary Theme: {emotion_results['summary']['primary_theme']}")
|
| 42 |
|
| 43 |
+
print("\n" + "="*50)
|
| 44 |
+
print("AUDIO CLASSIFICATION RESULTS (AST):")
|
| 45 |
+
print("="*50)
|
| 46 |
+
for result in ast_results[:5]: # Show top 5 results
|
| 47 |
+
print(f"{result['label']}: {result['score']*100:.2f}%")
|
| 48 |
+
|
| 49 |
print("\n" + "="*50)
|
| 50 |
print("GENERATED LYRICS:")
|
| 51 |
print("="*50)
|