appp
Browse files- app.py +41 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import librosa
|
4 |
+
from transformers import HubertForSequenceClassification, Wav2Vec2FeatureExtractor
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# Title of the app
|
8 |
+
st.title("Emotion Recognition from Speech")
|
9 |
+
|
10 |
+
# Upload audio file
|
11 |
+
uploaded_file = st.file_uploader("Choose an audio file...", type=["wav"])
|
12 |
+
|
13 |
+
# Load the model and feature extractor
|
14 |
+
model = HubertForSequenceClassification.from_pretrained("superb/hubert-large-superb-er")
|
15 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("superb/hubert-large-superb-er")
|
16 |
+
classifier = pipeline("audio-classification", model="superb/hubert-large-superb-er")
|
17 |
+
|
18 |
+
if uploaded_file is not None:
|
19 |
+
# Load and preprocess audio file
|
20 |
+
speech, sr = librosa.load(uploaded_file, sr=16000, mono=True)
|
21 |
+
|
22 |
+
# Display audio player
|
23 |
+
st.audio(uploaded_file, format='audio/wav')
|
24 |
+
|
25 |
+
# Process the audio
|
26 |
+
inputs = feature_extractor(speech, sampling_rate=16000, padding=True, return_tensors="pt")
|
27 |
+
|
28 |
+
# Predict emotion
|
29 |
+
with torch.no_grad():
|
30 |
+
logits = model(**inputs).logits
|
31 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
32 |
+
labels = [model.config.id2label[_id] for _id in predicted_ids.tolist()]
|
33 |
+
|
34 |
+
# Display the result
|
35 |
+
st.write("Predicted Emotion:", labels[0])
|
36 |
+
|
37 |
+
# Alternatively using the pipeline
|
38 |
+
results = classifier(uploaded_file, top_k=5)
|
39 |
+
st.write("Top 5 Predicted Emotions:")
|
40 |
+
for result in results:
|
41 |
+
st.write(f"{result['label']}: {result['score']:.4f}")
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
streamlit
|
3 |
+
torch
|
4 |
+
librosa
|
5 |
+
transformers
|