ser_app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from transformers import AutoModelForAudioClassification
|
3 |
+
import gradio as gr
|
4 |
+
import librosa
|
5 |
+
import torch
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
def classify_audio(audio_file):
|
13 |
+
model = AutoModelForAudioClassification.from_pretrained("3loi/SER-Odyssey-Baseline-WavLM-Multi-Attributes", trust_remote_code=True)
|
14 |
+
print(audio_file)
|
15 |
+
mean, std = -8.278621631819787e-05, 0.08485510250851999
|
16 |
+
raw_wav, _ = librosa.load(audio_file, sr=16000)
|
17 |
+
norm_wav = (raw_wav - mean) / (std+0.000001)
|
18 |
+
|
19 |
+
mask = torch.ones(1, len(norm_wav))
|
20 |
+
wavs = torch.tensor(norm_wav).unsqueeze(0)
|
21 |
+
|
22 |
+
pred = model(wavs, mask).detach().numpy()
|
23 |
+
print(str(pred))
|
24 |
+
return str(pred)
|
25 |
+
|
26 |
+
|
27 |
+
def main():
|
28 |
+
audio_input = gr.inputs.Audio(source="upload", type="filepath")
|
29 |
+
output_text = gr.outputs.Textbox()
|
30 |
+
|
31 |
+
iface = gr.Interface(fn=classify_audio, inputs=audio_input,
|
32 |
+
outputs=output_text, title="Speech Emotion Recognition App",
|
33 |
+
description="Upload an audio file and hit the 'Submit'\
|
34 |
+
button")
|
35 |
+
|
36 |
+
iface.launch()
|
37 |
+
|
38 |
+
|
39 |
+
if __name__ == '__main__':
|
40 |
+
main()
|
41 |
+
|