manikanta2026 commited on
Commit
23a08b3
·
1 Parent(s): b2c1545
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -9,6 +9,7 @@ import matplotlib.pyplot as plt
9
  import matplotlib
10
  matplotlib.use('Agg') # Use non-interactive backend
11
  from io import BytesIO
 
12
  import warnings
13
 
14
  # Suppress warnings and logs
@@ -69,12 +70,13 @@ def create_mel_spectrogram(audio, sr):
69
  plt.title('Mel Spectrogram')
70
  plt.tight_layout()
71
 
72
- # Save to BytesIO and return the plot
73
  buf = BytesIO()
74
  plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
75
  buf.seek(0)
 
76
  plt.close()
77
- return buf
78
 
79
  def create_polar_plot(emotion_probabilities):
80
  """Create polar plot of emotion probabilities"""
@@ -101,12 +103,13 @@ def create_polar_plot(emotion_probabilities):
101
  ax.set_title("Emotion Probabilities", va='bottom', fontsize=14, color="darkblue", pad=20)
102
  plt.tight_layout()
103
 
104
- # Save to BytesIO and return the plot
105
  buf = BytesIO()
106
  plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
107
  buf.seek(0)
 
108
  plt.close()
109
- return buf
110
 
111
  def create_waveform_plot(audio, sr):
112
  """Create waveform plot"""
@@ -117,12 +120,13 @@ def create_waveform_plot(audio, sr):
117
  plt.ylabel('Amplitude')
118
  plt.tight_layout()
119
 
120
- # Save to BytesIO and return the plot
121
  buf = BytesIO()
122
  plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
123
  buf.seek(0)
 
124
  plt.close()
125
- return buf
126
 
127
  def predict_emotion(audio_file):
128
  try:
@@ -213,6 +217,21 @@ with gr.Blocks(title="🎤 Emotion Recognition from Audio", theme=gr.themes.Soft
213
  outputs=[predicted_emotion, emotion_probs, mel_spec_plot, polar_plot, waveform_plot]
214
  )
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
 
217
  # Launch the interface
218
  if __name__ == "__main__":
 
9
  import matplotlib
10
  matplotlib.use('Agg') # Use non-interactive backend
11
  from io import BytesIO
12
+ from PIL import Image
13
  import warnings
14
 
15
  # Suppress warnings and logs
 
70
  plt.title('Mel Spectrogram')
71
  plt.tight_layout()
72
 
73
+ # Save to BytesIO and convert to PIL Image
74
  buf = BytesIO()
75
  plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
76
  buf.seek(0)
77
+ img = Image.open(buf)
78
  plt.close()
79
+ return img
80
 
81
  def create_polar_plot(emotion_probabilities):
82
  """Create polar plot of emotion probabilities"""
 
103
  ax.set_title("Emotion Probabilities", va='bottom', fontsize=14, color="darkblue", pad=20)
104
  plt.tight_layout()
105
 
106
+ # Save to BytesIO and convert to PIL Image
107
  buf = BytesIO()
108
  plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
109
  buf.seek(0)
110
+ img = Image.open(buf)
111
  plt.close()
112
+ return img
113
 
114
  def create_waveform_plot(audio, sr):
115
  """Create waveform plot"""
 
120
  plt.ylabel('Amplitude')
121
  plt.tight_layout()
122
 
123
+ # Save to BytesIO and convert to PIL Image
124
  buf = BytesIO()
125
  plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
126
  buf.seek(0)
127
+ img = Image.open(buf)
128
  plt.close()
129
+ return img
130
 
131
  def predict_emotion(audio_file):
132
  try:
 
217
  outputs=[predicted_emotion, emotion_probs, mel_spec_plot, polar_plot, waveform_plot]
218
  )
219
 
220
+ gr.Markdown(
221
+ """
222
+ ### 📝 How it works:
223
+ 1. **Upload** an audio file or **record** directly using your microphone
224
+ 2. The system extracts audio features (MFCCs, Chroma, Spectral features, etc.)
225
+ 3. A trained neural network predicts the emotion
226
+ 4. View the results with detailed visualizations:
227
+ - **Waveform**: Shows the audio signal over time
228
+ - **Mel Spectrogram**: Visual representation of the audio's frequency content
229
+ - **Radar Chart**: Probability distribution across all emotion categories
230
+
231
+ ### 🎭 Supported Emotions:
232
+ Depending on your model training, this may include emotions like: Happy, Sad, Angry, Fear, Disgust, Surprise, Neutral, and others.
233
+ """
234
+ )
235
 
236
  # Launch the interface
237
  if __name__ == "__main__":