asad231 commited on
Commit
ba2a6fb
·
verified ·
1 Parent(s): 9a97fa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -37
app.py CHANGED
@@ -1,47 +1,40 @@
1
- import gradio as gr
2
  import torch
3
- import torchaudio
4
- from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor
5
  import numpy as np
 
 
6
 
7
- # Load model and processor
8
- model_name = "ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
9
- processor = Wav2Vec2Processor.from_pretrained(model_name)
10
- model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
11
-
12
- # Emotion labels and emojis
13
- id2label = {
14
- 0: "angry 😠",
15
- 1: "calm 😌",
16
- 2: "happy 😄",
17
- 3: "sad 😢"
18
  }
19
 
20
- # Audio processing and prediction
21
- def predict_emotion(audio):
22
- if audio is None:
23
- return "No audio provided"
24
 
25
- speech_array, sampling_rate = torchaudio.load(audio)
26
- if sampling_rate != 16000:
27
- resampler = torchaudio.transforms.Resample(orig_freq=sampling_rate, new_freq=16000)
28
- speech_array = resampler(speech_array)
29
 
30
- input_values = processor(speech_array.squeeze(), return_tensors="pt", sampling_rate=16000).input_values
31
- with torch.no_grad():
32
- logits = model(input_values).logits
 
33
 
34
- predicted_id = torch.argmax(logits, dim=-1).item()
35
- return f"Detected Emotion: {id2label[predicted_id]}"
 
 
 
36
 
37
- # Gradio UI
38
- app = gr.Interface(
39
- fn=predict_emotion,
40
- inputs=gr.Audio(source="upload", type="filepath", label="Upload or Record Audio"),
41
- outputs=gr.Textbox(label="Detected Emotion with Emoji"),
42
- title="🎙️ Voice Emotion Detector with Emoji",
43
- description="Upload or record your voice. The model will detect your emotion and display an emoji."
44
- )
45
 
46
- if __name__ == "__main__":
47
- app.launch()
 
1
+ import streamlit as st
2
  import torch
3
+ import librosa
 
4
  import numpy as np
5
+ from transformers import Wav2Vec2Processor, Wav2Vec2ForSequenceClassification
6
+ import torchaudio
7
 
8
+ # Emojis for emotions
9
+ EMOTION_EMOJI = {
10
+ "angry": "😠",
11
+ "happy": "😄",
12
+ "sad": "😢",
13
+ "neutral": "😐"
 
 
 
 
 
14
  }
15
 
16
+ # Load processor and model
17
+ processor = Wav2Vec2Processor.from_pretrained("ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition")
18
+ model = Wav2Vec2ForSequenceClassification.from_pretrained("ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition")
 
19
 
20
+ # Title
21
+ st.title("🎙️ Voice Emotion Detector with Emoji")
 
 
22
 
23
+ # Upload audio
24
+ uploaded_file = st.file_uploader("Upload a WAV file", type=["wav"])
25
+ if uploaded_file is not None:
26
+ st.audio(uploaded_file, format="audio/wav")
27
 
28
+ # Load and preprocess audio
29
+ speech_array, sampling_rate = torchaudio.load(uploaded_file)
30
+ if sampling_rate != 16000:
31
+ speech_array = torchaudio.transforms.Resample(orig_freq=sampling_rate, new_freq=16000)(speech_array)
32
+ speech = speech_array.squeeze().numpy()
33
 
34
+ inputs = processor(speech, sampling_rate=16000, return_tensors="pt", padding=True)
35
+ with torch.no_grad():
36
+ logits = model(**inputs).logits
37
+ predicted_class_id = torch.argmax(logits).item()
38
+ emotion = model.config.id2label[predicted_class_id]
 
 
 
39
 
40
+ st.markdown(f"### Emotion Detected: **{emotion}** {EMOTION_EMOJI.get(emotion, '')}")