File size: 1,629 Bytes
3859913
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
import asyncio
import gradio as gr
import tempfile
from pathlib import Path

from app.utils import download_video, extract_audio, trim_silence
from app.accent_classifier import get_classifier

clf = get_classifier()

async def _url_pipeline(url: str):
    with tempfile.TemporaryDirectory() as td:
        tdir = Path(td)
        video = await download_video(url, tdir)
        wav = tdir / "aud.wav"
        await extract_audio(video, wav)
        wav = trim_silence(wav)
        return clf.classify(str(wav))

def analyze_url(url: str):
    return asyncio.run(_url_pipeline(url))


def analyze_file(file):
    path = Path(file.name)
    if path.suffix.lower() in {".mp4", ".mov", ".mkv"}:
        wav = path.with_suffix(".wav")
        asyncio.run(extract_audio(path, wav))
    else:
        wav = path
    wav = trim_silence(wav)
    return clf.classify(str(wav))


def fmt(res):
    if not res:
        return "Analysis failed."
    return f"**Accent:** {res['accent']}\n\n**Confidence:** {res['confidence']}%"

with gr.Blocks(title="English Accent Detector") as demo:
    gr.Markdown("## REM Waste – Accent Screening Tool")
    with gr.Tab("From URL"):
        url_in = gr.Text(label="Public video URL (Loom, MP4, YouTube, …)")
        btn = gr.Button("Analyze")
        out = gr.Markdown()
        btn.click(lambda u: fmt(analyze_url(u)), inputs=url_in, outputs=out)
    with gr.Tab("Upload File"):
        file_in = gr.File()
        btn2 = gr.Button("Analyze")
        out2 = gr.Markdown()
        btn2.click(lambda f: fmt(analyze_file(f)), inputs=file_in, outputs=out2)

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