Diggz10 commited on
Commit
57dd5e9
·
verified ·
1 Parent(s): 6667345

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -12
app.py CHANGED
@@ -9,7 +9,6 @@ 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
- # This helps in debugging issues directly on the Hugging Face Space.
13
  def error_fn(audio_file):
14
  return {"error": f"Failed to load the model. Please check the logs. Error: {str(e)}"}
15
  classifier = None
@@ -40,7 +39,6 @@ def predict_emotion(audio_file):
40
  elif isinstance(audio_file, tuple):
41
  # Handle microphone input (samplerate, numpy_array)
42
  sample_rate, audio_array = audio_file
43
- # Save the numpy array to a temporary WAV file as the pipeline expects a file path or direct bytes
44
  temp_audio_path = "temp_audio_from_mic.wav"
45
  sf.write(temp_audio_path, audio_array, sample_rate)
46
  audio_path = temp_audio_path
@@ -49,7 +47,7 @@ def predict_emotion(audio_file):
49
 
50
  try:
51
  # Perform inference
52
- results = classifier(audio_path, top_k=5) # top_k ensures we get all relevant emotion scores
53
 
54
  # Process results into a dictionary for better display
55
  emotion_scores = {item['label']: round(item['score'], 3) for item in results}
@@ -64,20 +62,14 @@ def predict_emotion(audio_file):
64
 
65
 
66
  # --- Gradio Interface ---
67
- # Define the Gradio interface
68
  iface = gr.Interface(
69
  fn=predict_emotion,
70
- # THIS IS THE CORRECTED LINE:
71
  inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="Upload Audio or Record with Microphone"),
72
- outputs=gr.Label(num_top_classes=5, label="Emotion Probabilities"), # This model has 4 emotions + 'no-emotion'
73
  title="AI Audio Emotion Detector",
74
  description="Upload an audio file or record your voice to detect emotions. This model is trained to recognize 'anger', 'happiness', 'neutral', 'sadness', and 'no-emotion'.",
75
- examples=[
76
- # You can add example audio files to your Hugging Face Space and reference them here.
77
- # For now, we'll leave this empty.
78
- ]
79
  )
80
 
81
- # Launch the Gradio app
82
  if __name__ == "__main__":
83
- iface.launch()
 
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
 
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)
44
  audio_path = temp_audio_path
 
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}
 
62
 
63
 
64
  # --- Gradio Interface ---
 
65
  iface = gr.Interface(
66
  fn=predict_emotion,
 
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. This model is trained to recognize 'anger', 'happiness', 'neutral', 'sadness', and 'no-emotion'.",
 
 
 
 
71
  )
72
 
73
+ # Launch the Gradio app with the API queue enabled
74
  if __name__ == "__main__":
75
+ iface.queue().launch() # <-- THE FIX IS HERE!