File size: 1,230 Bytes
40cd02e
 
 
 
 
f93f37c
 
40cd02e
612763b
40cd02e
 
fa06bfc
40cd02e
 
 
f93f37c
 
 
 
40cd02e
f93f37c
40cd02e
f93f37c
 
40cd02e
 
f93f37c
40cd02e
 
 
f93f37c
 
 
40cd02e
 
612763b
 
 
 
 
 
6923d3e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import torch
import librosa
from speechbrain.inference.classifiers import EncoderClassifier
from pydub import AudioSegment
import gradio as gr
import uuid
import os

# Load model once
classifier = EncoderClassifier.from_hparams(
    source="Jzuluaga/accent-id-commonaccent_ecapa",
    savedir="pretrained_models/accent-id-commonaccent_ecapa"
)

def classify_accent(video):
    # Generate unique filename
    temp_wav = f"/tmp/{uuid.uuid4().hex}.wav"

    # Convert to .wav
    audio = AudioSegment.from_file(video, format="mp4")
    audio.export(temp_wav, format="wav")

    # Load waveform
    waveform, sr = librosa.load(temp_wav, sr=16000, mono=True)
    waveform_tensor = torch.tensor(waveform).unsqueeze(0)

    # Predict
    prediction = classifier.classify_batch(waveform_tensor)
    _, score, _, text_lab = prediction

    # Cleanup
    os.remove(temp_wav)

    return f"Accent: {text_lab[0]} (Confidence: {score.item():.2f})"

app = gr.Interface(
    fn=classify_accent,
    inputs=gr.Video(label="Upload an MP4"),
    outputs=gr.Text(label="Prediction"),
    title="English Accent Classifier",
    description="Upload a short MP4 video of spoken English to detect accent."
)

if __name__ == "__main__":
    app.launch()