Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
-
|
2 |
-
import streamlit as st
|
3 |
from moviepy.editor import VideoFileClip
|
4 |
from speechbrain.pretrained import EncoderClassifier
|
5 |
import torchaudio
|
6 |
-
import os
|
7 |
from pytube import YouTube
|
|
|
8 |
|
9 |
CLASSIFIER = "Jzuluaga/accent-id-commonaccent_xlsr-en-english"
|
10 |
|
@@ -13,39 +12,62 @@ def download_video(url):
|
|
13 |
if "youtube.com" in url:
|
14 |
yt = YouTube(url)
|
15 |
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def extract_audio(video_path):
|
22 |
clip = VideoFileClip(video_path)
|
23 |
audio_path = "temp_audio.wav"
|
24 |
-
clip.audio.write_audiofile(audio_path)
|
|
|
25 |
return audio_path
|
26 |
|
27 |
def classify_accent(audio_path):
|
28 |
classifier = EncoderClassifier.from_hparams(
|
29 |
source=CLASSIFIER,
|
30 |
-
savedir="pretrained_models/accent_classifier"
|
|
|
31 |
)
|
32 |
waveform, sample_rate = torchaudio.load(audio_path)
|
33 |
prediction = classifier.classify_batch(waveform)
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
st.metric("Detected Accent", prediction[3][0])
|
48 |
-
st.metric("Confidence Score", f"{prediction[1].exp().max().item()*100:.2f}%")
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
-
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
from moviepy.editor import VideoFileClip
|
3 |
from speechbrain.pretrained import EncoderClassifier
|
4 |
import torchaudio
|
|
|
5 |
from pytube import YouTube
|
6 |
+
import os
|
7 |
|
8 |
CLASSIFIER = "Jzuluaga/accent-id-commonaccent_xlsr-en-english"
|
9 |
|
|
|
12 |
if "youtube.com" in url:
|
13 |
yt = YouTube(url)
|
14 |
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
|
15 |
+
video_path = stream.download()
|
16 |
+
return video_path
|
17 |
+
else: # Direct download (assumes URL is direct mp4 link)
|
18 |
+
# Download file locally
|
19 |
+
import requests
|
20 |
+
local_filename = "temp_video.mp4"
|
21 |
+
with requests.get(url, stream=True) as r:
|
22 |
+
r.raise_for_status()
|
23 |
+
with open(local_filename, 'wb') as f:
|
24 |
+
for chunk in r.iter_content(chunk_size=8192):
|
25 |
+
f.write(chunk)
|
26 |
+
return local_filename
|
27 |
|
28 |
def extract_audio(video_path):
|
29 |
clip = VideoFileClip(video_path)
|
30 |
audio_path = "temp_audio.wav"
|
31 |
+
clip.audio.write_audiofile(audio_path, logger=None)
|
32 |
+
clip.close()
|
33 |
return audio_path
|
34 |
|
35 |
def classify_accent(audio_path):
|
36 |
classifier = EncoderClassifier.from_hparams(
|
37 |
source=CLASSIFIER,
|
38 |
+
savedir="pretrained_models/accent_classifier",
|
39 |
+
run_opts={"device":"cpu"} # or "cuda" if GPU available
|
40 |
)
|
41 |
waveform, sample_rate = torchaudio.load(audio_path)
|
42 |
prediction = classifier.classify_batch(waveform)
|
43 |
+
# prediction format: (scores, probabilities, embeddings, predicted_labels)
|
44 |
+
predicted_accent = prediction[3][0]
|
45 |
+
confidence = prediction[1].exp().max().item() * 100
|
46 |
+
return predicted_accent, f"{confidence:.2f}%"
|
47 |
+
|
48 |
+
def process_video(url):
|
49 |
+
try:
|
50 |
+
video_path = download_video(url)
|
51 |
+
audio_path = extract_audio(video_path)
|
52 |
+
accent, confidence = classify_accent(audio_path)
|
53 |
+
finally:
|
54 |
+
# Cleanup temp files if they exist
|
55 |
+
for f in [video_path, audio_path]:
|
56 |
+
if os.path.exists(f):
|
57 |
+
os.remove(f)
|
58 |
+
return accent, confidence
|
59 |
|
60 |
+
# Gradio interface
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=process_video,
|
63 |
+
inputs=gr.Textbox(label="Enter Public Video URL (YouTube, Loom, direct MP4)"),
|
64 |
+
outputs=[
|
65 |
+
gr.Textbox(label="Detected Accent"),
|
66 |
+
gr.Textbox(label="Confidence Score")
|
67 |
+
],
|
68 |
+
title="English Accent Classifier",
|
69 |
+
description="Paste a public video URL to detect the English accent and confidence score."
|
70 |
+
)
|
|
|
|
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
+
iface.launch()
|