Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import soundfile as sf
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
try:
|
| 10 |
-
|
|
|
|
| 11 |
except Exception as e:
|
| 12 |
st.write(f"Error loading model: {e}")
|
| 13 |
|
|
@@ -21,6 +24,7 @@ uploaded_file = st.file_uploader("Choose an audio file...", type=["wav", "mp3",
|
|
| 21 |
if uploaded_file is not None:
|
| 22 |
# Load the audio file
|
| 23 |
audio_input, sample_rate = sf.read(uploaded_file)
|
|
|
|
| 24 |
|
| 25 |
# Display the audio player
|
| 26 |
st.audio(uploaded_file)
|
|
@@ -28,10 +32,34 @@ if uploaded_file is not None:
|
|
| 28 |
# Perform emotion classification
|
| 29 |
st.write("Classifying...")
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
st.write(f"Error during classification: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import soundfile as sf
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModel, AutoFeatureExtractor
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Get the Hugging Face API token from environment variables
|
| 8 |
+
token = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
+
# Load the model and feature extractor using your token
|
| 11 |
try:
|
| 12 |
+
model = AutoModel.from_pretrained("sami606713/emotion_classification", use_auth_token=token)
|
| 13 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("sami606713/emotion_classification", use_auth_token=token)
|
| 14 |
except Exception as e:
|
| 15 |
st.write(f"Error loading model: {e}")
|
| 16 |
|
|
|
|
| 24 |
if uploaded_file is not None:
|
| 25 |
# Load the audio file
|
| 26 |
audio_input, sample_rate = sf.read(uploaded_file)
|
| 27 |
+
sample_rate = 16000 # Ensure the sample rate is 16000
|
| 28 |
|
| 29 |
# Display the audio player
|
| 30 |
st.audio(uploaded_file)
|
|
|
|
| 32 |
# Perform emotion classification
|
| 33 |
st.write("Classifying...")
|
| 34 |
try:
|
| 35 |
+
inputs = feature_extractor(audio_input, sampling_rate=sample_rate, return_tensors="pt")
|
| 36 |
+
|
| 37 |
+
# Make prediction
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
outputs = model(**inputs)
|
| 40 |
+
|
| 41 |
+
embeddings = outputs.pooler_output
|
| 42 |
+
|
| 43 |
+
# Apply a classification head on top of the embeddings
|
| 44 |
+
id2label={
|
| 45 |
+
0:"angry",
|
| 46 |
+
1:'calm',
|
| 47 |
+
2:'disgust',
|
| 48 |
+
3:'fearful',
|
| 49 |
+
4:'happy',
|
| 50 |
+
5:'neutral',
|
| 51 |
+
6:'sad',
|
| 52 |
+
7:'surprised'
|
| 53 |
+
}
|
| 54 |
+
classifier = torch.nn.Linear(embeddings.shape[-1], len(id2label))
|
| 55 |
+
|
| 56 |
+
# Pass embeddings through the classifier
|
| 57 |
+
logits = classifier(embeddings)
|
| 58 |
+
|
| 59 |
+
# Get predicted class
|
| 60 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 61 |
+
predicted_class = id2label[predicted_class_idx]
|
| 62 |
+
|
| 63 |
+
st.write(f"Predicted Emotion: {predicted_class}")
|
| 64 |
except Exception as e:
|
| 65 |
st.write(f"Error during classification: {e}")
|