Spaces:
Sleeping
Sleeping
Commit
·
028bb6f
1
Parent(s):
14704a2
full working app with mp3 uploads.
Browse files- .gitignore +95 -0
- .gradio/flagged/dataset1.csv +2 -0
- app.py +132 -4
- requirements.txt +9 -0
- templates/index.html +14 -0
.gitignore
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
env/
|
| 12 |
+
venv/
|
| 13 |
+
ENV/
|
| 14 |
+
build/
|
| 15 |
+
develop-eggs/
|
| 16 |
+
dist/
|
| 17 |
+
downloads/
|
| 18 |
+
eggs/
|
| 19 |
+
.eggs/
|
| 20 |
+
lib/
|
| 21 |
+
lib64/
|
| 22 |
+
parts/
|
| 23 |
+
sdist/
|
| 24 |
+
var/
|
| 25 |
+
*.egg-info/
|
| 26 |
+
.installed.cfg
|
| 27 |
+
*.egg
|
| 28 |
+
|
| 29 |
+
# Virtual environments
|
| 30 |
+
.venv/
|
| 31 |
+
venv/
|
| 32 |
+
ENV/
|
| 33 |
+
|
| 34 |
+
# PyInstaller
|
| 35 |
+
*.manifest
|
| 36 |
+
*.spec
|
| 37 |
+
|
| 38 |
+
# Installer logs
|
| 39 |
+
pip-log.txt
|
| 40 |
+
pip-delete-this-directory.txt
|
| 41 |
+
|
| 42 |
+
# Unit test / coverage reports
|
| 43 |
+
htmlcov/
|
| 44 |
+
.tox/
|
| 45 |
+
.nox/
|
| 46 |
+
.coverage
|
| 47 |
+
.coverage.*
|
| 48 |
+
.cache
|
| 49 |
+
nosetests.xml
|
| 50 |
+
coverage.xml
|
| 51 |
+
*.cover
|
| 52 |
+
.hypothesis/
|
| 53 |
+
.pytest_cache/
|
| 54 |
+
|
| 55 |
+
# Translations
|
| 56 |
+
*.mo
|
| 57 |
+
*.pot
|
| 58 |
+
|
| 59 |
+
# Django stuff
|
| 60 |
+
*.log
|
| 61 |
+
|
| 62 |
+
# Flask stuff
|
| 63 |
+
instance/
|
| 64 |
+
.webassets-cache
|
| 65 |
+
|
| 66 |
+
# Scrapy stuff
|
| 67 |
+
.scrapy
|
| 68 |
+
|
| 69 |
+
# Sphinx documentation
|
| 70 |
+
docs/_build/
|
| 71 |
+
|
| 72 |
+
# PyBuilder
|
| 73 |
+
target/
|
| 74 |
+
|
| 75 |
+
# IPython
|
| 76 |
+
profile_default/
|
| 77 |
+
ipython_config.py
|
| 78 |
+
|
| 79 |
+
# mypy
|
| 80 |
+
.mypy_cache/
|
| 81 |
+
.dmypy.json
|
| 82 |
+
dmypy.json
|
| 83 |
+
|
| 84 |
+
# Pyre
|
| 85 |
+
.pyre/
|
| 86 |
+
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import shutil
|
| 5 |
+
import requests
|
| 6 |
+
import subprocess
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
from scipy.signal import resample
|
| 9 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 10 |
+
from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2FeatureExtractor
|
| 11 |
|
| 12 |
+
# === Constants ===
|
| 13 |
+
TEMP_VIDEO = "temp_video.mp4"
|
| 14 |
+
RAW_AUDIO = "raw_audio_input"
|
| 15 |
+
CONVERTED_AUDIO = "converted_audio.wav"
|
| 16 |
+
MODEL_DIR = "model"
|
| 17 |
|
| 18 |
+
# === Load model and feature extractor ===
|
| 19 |
+
MODEL_REPO = "ylacombe/accent-classifier"
|
| 20 |
+
model = Wav2Vec2ForSequenceClassification.from_pretrained(MODEL_REPO, cache_dir="hf_model_cache")
|
| 21 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(MODEL_REPO)
|
| 22 |
+
|
| 23 |
+
model.eval()
|
| 24 |
+
|
| 25 |
+
# === Dynamic label list from model config ===
|
| 26 |
+
LABELS = [model.config.id2label[i] for i in range(len(model.config.id2label))]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# === Download video from URL ===
|
| 30 |
+
def download_video(url, filename=TEMP_VIDEO):
|
| 31 |
+
r = requests.get(url, stream=True)
|
| 32 |
+
r.raise_for_status()
|
| 33 |
+
with open(filename, 'wb') as f:
|
| 34 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 35 |
+
f.write(chunk)
|
| 36 |
+
return filename
|
| 37 |
+
|
| 38 |
+
# === Extract audio from video ===
|
| 39 |
+
def extract_audio_from_video(video_path, output_path=RAW_AUDIO + ".mp4"):
|
| 40 |
+
clip = VideoFileClip(video_path)
|
| 41 |
+
if clip.audio is None:
|
| 42 |
+
raise ValueError("No audio stream found in video.")
|
| 43 |
+
clip.audio.write_audiofile(output_path)
|
| 44 |
+
return output_path
|
| 45 |
+
|
| 46 |
+
# === Convert any input audio to WAV using ffmpeg ===
|
| 47 |
+
def convert_to_wav(input_path, output_path=CONVERTED_AUDIO):
|
| 48 |
+
command = ["ffmpeg", "-y", "-i", input_path, output_path]
|
| 49 |
+
subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 50 |
+
return output_path
|
| 51 |
+
|
| 52 |
+
# === Run accent classification ===
|
| 53 |
+
def classify_accent(audio_path):
|
| 54 |
+
waveform, sr = sf.read(audio_path)
|
| 55 |
+
|
| 56 |
+
if len(waveform.shape) > 1:
|
| 57 |
+
waveform = waveform.mean(axis=1) # Convert stereo to mono
|
| 58 |
+
|
| 59 |
+
target_sr = 16000
|
| 60 |
+
if sr != target_sr:
|
| 61 |
+
num_samples = int(len(waveform) * target_sr / sr)
|
| 62 |
+
waveform = resample(waveform, num_samples)
|
| 63 |
+
sr = target_sr
|
| 64 |
+
|
| 65 |
+
inputs = feature_extractor(
|
| 66 |
+
waveform,
|
| 67 |
+
sampling_rate=sr,
|
| 68 |
+
return_tensors="pt",
|
| 69 |
+
padding=True
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
with torch.no_grad():
|
| 73 |
+
outputs = model(**inputs)
|
| 74 |
+
logits = outputs.logits[0]
|
| 75 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 76 |
+
|
| 77 |
+
top_idx = torch.argmax(probs).item()
|
| 78 |
+
top_label = LABELS[top_idx]
|
| 79 |
+
top_conf = round(probs[top_idx].item(), 4)
|
| 80 |
+
|
| 81 |
+
top5 = torch.topk(probs, k=5)
|
| 82 |
+
top5_labels = [LABELS[i] for i in top5.indices.tolist()]
|
| 83 |
+
top5_scores = [round(p, 4) for p in top5.values.tolist()]
|
| 84 |
+
top5_text = "\n".join([f"{label}: {score}" for label, score in zip(top5_labels, top5_scores)])
|
| 85 |
+
|
| 86 |
+
return f"Top prediction: {top_label}", top_conf, top_label, audio_path, top5_text
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# === Main Gradio handler ===
|
| 90 |
+
def process_input(video_url, uploaded_audio):
|
| 91 |
+
try:
|
| 92 |
+
audio_path = None
|
| 93 |
+
|
| 94 |
+
if uploaded_audio:
|
| 95 |
+
shutil.copy(uploaded_audio, RAW_AUDIO)
|
| 96 |
+
audio_path = convert_to_wav(RAW_AUDIO)
|
| 97 |
+
|
| 98 |
+
elif video_url and video_url.strip():
|
| 99 |
+
download_video(video_url)
|
| 100 |
+
extracted = extract_audio_from_video(TEMP_VIDEO)
|
| 101 |
+
audio_path = convert_to_wav(extracted)
|
| 102 |
+
|
| 103 |
+
else:
|
| 104 |
+
return "Please provide a video URL or upload an audio file.", None, None, None, None
|
| 105 |
+
|
| 106 |
+
return classify_accent(audio_path)
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return f"Error: {str(e)}", None, None, None, None
|
| 110 |
+
|
| 111 |
+
finally:
|
| 112 |
+
for f in [TEMP_VIDEO, RAW_AUDIO, CONVERTED_AUDIO, RAW_AUDIO + ".mp4"]:
|
| 113 |
+
if os.path.exists(f):
|
| 114 |
+
os.remove(f)
|
| 115 |
+
|
| 116 |
+
# === Gradio UI ===
|
| 117 |
+
interface = gr.Interface(
|
| 118 |
+
fn=process_input,
|
| 119 |
+
inputs=[
|
| 120 |
+
gr.Textbox(label="Enter Loom or MP4 Video URL (optional)"),
|
| 121 |
+
gr.Audio(label="Upload MP3 or WAV (optional)", type="filepath")
|
| 122 |
+
],
|
| 123 |
+
outputs=[
|
| 124 |
+
gr.Text(label="Prediction"),
|
| 125 |
+
gr.Number(label="Confidence Score"),
|
| 126 |
+
gr.Text(label="Accent"),
|
| 127 |
+
gr.Audio(label="Processed Audio", type="filepath"),
|
| 128 |
+
gr.Text(label="Top 5 Predictions")
|
| 129 |
+
],
|
| 130 |
+
title="Accent Classifier",
|
| 131 |
+
description="Upload an audio file or Loom/MP4 link to detect speaker's accent with top-5 prediction breakdown."
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
torchaudio
|
| 4 |
+
gradio
|
| 5 |
+
moviepy==1.0.3
|
| 6 |
+
requests
|
| 7 |
+
safetensors
|
| 8 |
+
soundfile
|
| 9 |
+
scipy
|
templates/index.html
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<title>Video Audio Extractor</title>
|
| 5 |
+
</head>
|
| 6 |
+
<body>
|
| 7 |
+
<h1>Extract Audio from Loom or MP4 URL</h1>
|
| 8 |
+
<form method="POST">
|
| 9 |
+
<label for="video_url">Video URL:</label><br>
|
| 10 |
+
<input type="url" name="video_url" id="video_url" required style="width: 400px;"><br><br>
|
| 11 |
+
<button type="submit">Extract Audio</button>
|
| 12 |
+
</form>
|
| 13 |
+
</body>
|
| 14 |
+
</html>
|