Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import librosa
|
3 |
+
from speechbrain.inference.classifiers import EncoderClassifier
|
4 |
+
from pydub import AudioSegment
|
5 |
+
import gradio as gr
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Load model only once
|
9 |
+
classifier = EncoderClassifier.from_hparams(
|
10 |
+
source="Jzuluaga/accent-id-commonaccent_ecapa",
|
11 |
+
savedir="pretrained_models/accent-id-commonaccent_ecapa"
|
12 |
+
)
|
13 |
+
|
14 |
+
def classify_accent(video):
|
15 |
+
# 'video' will already be a path to the uploaded file
|
16 |
+
audio = AudioSegment.from_file(video, format="mp4")
|
17 |
+
audio.export("output.wav", format="wav")
|
18 |
+
|
19 |
+
waveform, sr = librosa.load("output.wav", sr=16000, mono=True)
|
20 |
+
waveform_tensor = torch.tensor(waveform).unsqueeze(0)
|
21 |
+
|
22 |
+
prediction = classifier.classify_batch(waveform_tensor)
|
23 |
+
_, score, _, text_lab = prediction
|
24 |
+
|
25 |
+
return f"Accent: {text_lab[0]} (Confidence: {score.item():.2f})"
|
26 |
+
|
27 |
+
|
28 |
+
iface = gr.Interface(fn=classify_accent,
|
29 |
+
inputs=gr.Video(),
|
30 |
+
outputs="text")
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
iface.launch()
|