Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,38 +3,38 @@ import os
|
|
| 3 |
import torchaudio
|
| 4 |
from speechbrain.pretrained import EncoderClassifier
|
| 5 |
|
| 6 |
-
def accent_detect(
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
else:
|
| 11 |
-
video_path = "uploaded_input.mp4"
|
| 12 |
-
with open(video_path, "wb") as f:
|
| 13 |
-
f.write(video_file.read())
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
os.system(f"ffmpeg -y -i {video_path} -ar 16000 -ac 1 -vn audio.wav")
|
| 17 |
if not os.path.exists("audio.wav") or os.path.getsize("audio.wav") < 1000:
|
| 18 |
return "Audio extraction failed. Please check your file."
|
| 19 |
|
| 20 |
-
#
|
| 21 |
accent_model = EncoderClassifier.from_hparams(
|
| 22 |
source="speechbrain/lang-id-commonlanguage_ecapa",
|
| 23 |
savedir="tmp_accent_model"
|
| 24 |
)
|
|
|
|
| 25 |
signal, fs = torchaudio.load("audio.wav")
|
| 26 |
if signal.shape[0] > 1:
|
| 27 |
signal = signal[0].unsqueeze(0)
|
|
|
|
| 28 |
prediction = accent_model.classify_batch(signal)
|
| 29 |
pred_label = prediction[3][0]
|
| 30 |
pred_scores = prediction[1][0]
|
| 31 |
confidence = float(pred_scores.max()) * 100
|
| 32 |
-
explanation =
|
|
|
|
|
|
|
|
|
|
| 33 |
return explanation
|
| 34 |
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=accent_detect,
|
| 37 |
-
inputs=gr.Video(
|
| 38 |
outputs="text",
|
| 39 |
title="🗣️ English Accent Classifier (Gradio Demo)",
|
| 40 |
description="Upload a short video clip of English speech. This tool predicts the English accent and confidence."
|
|
|
|
| 3 |
import torchaudio
|
| 4 |
from speechbrain.pretrained import EncoderClassifier
|
| 5 |
|
| 6 |
+
def accent_detect(video_path):
|
| 7 |
+
# video_path is a string file path provided by Gradio
|
| 8 |
+
# Extract audio from the video using ffmpeg
|
| 9 |
+
os.system(f"ffmpeg -y -i '{video_path}' -ar 16000 -ac 1 -vn audio.wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Check if audio.wav was created and is of reasonable size
|
|
|
|
| 12 |
if not os.path.exists("audio.wav") or os.path.getsize("audio.wav") < 1000:
|
| 13 |
return "Audio extraction failed. Please check your file."
|
| 14 |
|
| 15 |
+
# Load the accent classification model
|
| 16 |
accent_model = EncoderClassifier.from_hparams(
|
| 17 |
source="speechbrain/lang-id-commonlanguage_ecapa",
|
| 18 |
savedir="tmp_accent_model"
|
| 19 |
)
|
| 20 |
+
# Load audio
|
| 21 |
signal, fs = torchaudio.load("audio.wav")
|
| 22 |
if signal.shape[0] > 1:
|
| 23 |
signal = signal[0].unsqueeze(0)
|
| 24 |
+
# Predict accent
|
| 25 |
prediction = accent_model.classify_batch(signal)
|
| 26 |
pred_label = prediction[3][0]
|
| 27 |
pred_scores = prediction[1][0]
|
| 28 |
confidence = float(pred_scores.max()) * 100
|
| 29 |
+
explanation = (
|
| 30 |
+
f"Predicted Accent: {pred_label} ({confidence:.1f}%)\n"
|
| 31 |
+
f"The model is {confidence:.0f}% confident this is a {pred_label} English accent."
|
| 32 |
+
)
|
| 33 |
return explanation
|
| 34 |
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=accent_detect,
|
| 37 |
+
inputs=gr.Video(label="Upload a Video File (MP4, WEBM, etc.)"),
|
| 38 |
outputs="text",
|
| 39 |
title="🗣️ English Accent Classifier (Gradio Demo)",
|
| 40 |
description="Upload a short video clip of English speech. This tool predicts the English accent and confidence."
|