Quanchan123abc commited on
Commit
888d183
·
verified ·
1 Parent(s): f17e680

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import librosa
4
+ from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2FeatureExtractor
5
+
6
+ # Load the model and feature extractor
7
+ model_name = "r-f/wav2vec-english-speech-emotion-recognition"
8
+ model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
9
+ feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name)
10
+
11
+ # Define the emotion labels
12
+ labels = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise']
13
+
14
+ def predict_emotion(audio):
15
+ # Load and preprocess the audio
16
+ audio, rate = librosa.load(audio, sr=16000)
17
+ inputs = feature_extractor(audio, sampling_rate=rate, return_tensors="pt", padding=True)
18
+ with torch.no_grad():
19
+ logits = model(**inputs).logits
20
+ predicted_class_id = torch.argmax(logits).item()
21
+ return labels[predicted_class_id]
22
+
23
+ # Create the Gradio interface
24
+ interface = gr.Interface(fn=predict_emotion, inputs=gr.Audio(type="filepath"), outputs="text")
25
+ interface.launch()