Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,40 @@
|
|
1 |
-
import
|
2 |
import torch
|
3 |
-
import
|
4 |
-
from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor
|
5 |
import numpy as np
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
id2label = {
|
14 |
-
0: "angry 😠",
|
15 |
-
1: "calm 😌",
|
16 |
-
2: "happy 😄",
|
17 |
-
3: "sad 😢"
|
18 |
}
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
return "No audio provided"
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
resampler = torchaudio.transforms.Resample(orig_freq=sampling_rate, new_freq=16000)
|
28 |
-
speech_array = resampler(speech_array)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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 |
-
|
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, '')}")
|
|