Stylesaniswi commited on
Commit
4500af5
1 Parent(s): cf1630c

Initial commit

Browse files
Files changed (2) hide show
  1. app.py +42 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import HubertForSequenceClassification, HubertConfig, Wav2Vec2FeatureExtractor
3
+ import torch
4
+ import soundfile as sf
5
+
6
+ # Load model and tokenizer
7
+ model_name = "model_hubert_finetuned_nopeft.pth" # Replace with your model path or Hugging Face model hub path
8
+ config = HubertConfig.from_pretrained(model_name)
9
+ config.id2label = {0: 'neu', 1: 'hap', 2: 'ang', 3: 'sad', 4: 'dis', 5: 'sur', 6: 'fea', 7: 'cal'}
10
+ config.label2id = {"neu": 0, "hap": 1, "ang": 2, "sad": 3, "dis": 4, "sur": 5, "fea": 6, "cal": 7}
11
+ config.num_labels = 8 # Set it to the number of classes in your SER task
12
+
13
+ # Load the pre-trained model with the modified configuration
14
+ model = HubertForSequenceClassification.from_pretrained(model_name, config=config, ignore_mismatched_sizes=True)
15
+ model.to('cuda' if torch.cuda.is_available() else 'cpu')
16
+ model.eval()
17
+
18
+ # Load feature extractor
19
+ feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("superb/hubert-large-superb-er")
20
+
21
+ st.title("Speech Emotion Recognition Model")
22
+
23
+ uploaded_file = st.file_uploader("Upload an audio file", type=["wav"])
24
+
25
+ if uploaded_file is not None:
26
+ # Load audio file
27
+ audio_input, sampling_rate = sf.read(uploaded_file)
28
+
29
+ # Preprocess audio input
30
+ inputs = feature_extractor(audio_input, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
31
+ inputs = {key: value.to('cuda' if torch.cuda.is_available() else 'cpu') for key, value in inputs.items()}
32
+
33
+ # Get prediction
34
+ with torch.no_grad():
35
+ outputs = model(**inputs)
36
+ logits = outputs.logits
37
+ probabilities = torch.softmax(logits, dim=-1)
38
+ predicted_class = torch.argmax(probabilities, dim=1).item()
39
+
40
+ # Display prediction
41
+ st.write(f"Predicted class: {config.id2label[predicted_class]}")
42
+ st.write(f"Class probabilities: {probabilities}")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ soundfile
4
+ streamlit