Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,40 +4,23 @@ import soundfile as sf
|
|
4 |
import os
|
5 |
|
6 |
# --- Model Loading ---
|
7 |
-
# We switched to 'superb/wav2vec2-base-superb-er' as it's a well-established and public model for emotion recognition.
|
8 |
try:
|
9 |
classifier = pipeline("audio-classification", model="superb/wav2vec2-base-superb-er")
|
10 |
except Exception as e:
|
11 |
-
# If there's an error during model loading, we can display it in the Gradio interface
|
12 |
def error_fn(audio_file):
|
13 |
return {"error": f"Failed to load the model. Please check the logs. Error: {str(e)}"}
|
14 |
classifier = None
|
15 |
|
16 |
# --- Prediction Function ---
|
17 |
def predict_emotion(audio_file):
|
18 |
-
"""
|
19 |
-
Predicts emotions from an audio file.
|
20 |
-
|
21 |
-
Args:
|
22 |
-
audio_file (str or tuple): Path to the audio file (from upload) or a tuple
|
23 |
-
(samplerate, audio_array) from microphone input.
|
24 |
-
Returns:
|
25 |
-
dict: A dictionary of emotion labels and their probabilities.
|
26 |
-
"""
|
27 |
-
# Handle case where the model failed to load
|
28 |
if classifier is None:
|
29 |
-
return {"error": "The AI model could not be loaded.
|
30 |
-
|
31 |
if audio_file is None:
|
32 |
-
return {"error": "No audio input provided.
|
33 |
|
34 |
-
# Gradio's Audio component can return a path to a temp file for file uploads,
|
35 |
-
# or a tuple (samplerate, numpy_array) for microphone input.
|
36 |
if isinstance(audio_file, str):
|
37 |
-
# Handle file path (e.g., from file upload)
|
38 |
audio_path = audio_file
|
39 |
elif isinstance(audio_file, tuple):
|
40 |
-
# Handle microphone input (samplerate, numpy_array)
|
41 |
sample_rate, audio_array = audio_file
|
42 |
temp_audio_path = "temp_audio_from_mic.wav"
|
43 |
sf.write(temp_audio_path, audio_array, sample_rate)
|
@@ -46,17 +29,12 @@ def predict_emotion(audio_file):
|
|
46 |
return {"error": f"Invalid audio input format: {type(audio_file)}"}
|
47 |
|
48 |
try:
|
49 |
-
# Perform inference
|
50 |
results = classifier(audio_path, top_k=5)
|
51 |
-
|
52 |
-
# Process results into a dictionary for better display
|
53 |
emotion_scores = {item['label']: round(item['score'], 3) for item in results}
|
54 |
-
|
55 |
return emotion_scores
|
56 |
except Exception as e:
|
57 |
return {"error": f"An error occurred during prediction: {str(e)}"}
|
58 |
finally:
|
59 |
-
# Clean up temporary file if created
|
60 |
if 'temp_audio_path' in locals() and os.path.exists(temp_audio_path):
|
61 |
os.remove(temp_audio_path)
|
62 |
|
@@ -67,9 +45,11 @@ iface = gr.Interface(
|
|
67 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="Upload Audio or Record with Microphone"),
|
68 |
outputs=gr.Label(num_top_classes=5, label="Emotion Probabilities"),
|
69 |
title="AI Audio Emotion Detector",
|
70 |
-
description="Upload an audio file or record your voice to detect emotions.
|
|
|
|
|
71 |
)
|
72 |
|
73 |
# Launch the Gradio app with the API queue enabled
|
74 |
if __name__ == "__main__":
|
75 |
-
iface.queue().launch()
|
|
|
4 |
import os
|
5 |
|
6 |
# --- Model Loading ---
|
|
|
7 |
try:
|
8 |
classifier = pipeline("audio-classification", model="superb/wav2vec2-base-superb-er")
|
9 |
except Exception as e:
|
|
|
10 |
def error_fn(audio_file):
|
11 |
return {"error": f"Failed to load the model. Please check the logs. Error: {str(e)}"}
|
12 |
classifier = None
|
13 |
|
14 |
# --- Prediction Function ---
|
15 |
def predict_emotion(audio_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
if classifier is None:
|
17 |
+
return {"error": "The AI model could not be loaded."}
|
|
|
18 |
if audio_file is None:
|
19 |
+
return {"error": "No audio input provided."}
|
20 |
|
|
|
|
|
21 |
if isinstance(audio_file, str):
|
|
|
22 |
audio_path = audio_file
|
23 |
elif isinstance(audio_file, tuple):
|
|
|
24 |
sample_rate, audio_array = audio_file
|
25 |
temp_audio_path = "temp_audio_from_mic.wav"
|
26 |
sf.write(temp_audio_path, audio_array, sample_rate)
|
|
|
29 |
return {"error": f"Invalid audio input format: {type(audio_file)}"}
|
30 |
|
31 |
try:
|
|
|
32 |
results = classifier(audio_path, top_k=5)
|
|
|
|
|
33 |
emotion_scores = {item['label']: round(item['score'], 3) for item in results}
|
|
|
34 |
return emotion_scores
|
35 |
except Exception as e:
|
36 |
return {"error": f"An error occurred during prediction: {str(e)}"}
|
37 |
finally:
|
|
|
38 |
if 'temp_audio_path' in locals() and os.path.exists(temp_audio_path):
|
39 |
os.remove(temp_audio_path)
|
40 |
|
|
|
45 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="Upload Audio or Record with Microphone"),
|
46 |
outputs=gr.Label(num_top_classes=5, label="Emotion Probabilities"),
|
47 |
title="AI Audio Emotion Detector",
|
48 |
+
description="Upload an audio file or record your voice to detect emotions.",
|
49 |
+
# THIS IS THE CRITICAL CHANGE TO CREATE A NAMED ENDPOINT
|
50 |
+
api_name="predict"
|
51 |
)
|
52 |
|
53 |
# Launch the Gradio app with the API queue enabled
|
54 |
if __name__ == "__main__":
|
55 |
+
iface.queue().launch()
|