Spaces:
Running on Zero
Running on Zero
Commit ·
8f291d6
1
Parent(s): 117b0a5
Major refactoring: Fast WhisperX, model caching, parallel translation
Browse files**Performance Improvements:**
- Use large-v3-turbo model (significantly faster than large-v3)
- Global model caching - models load once, not on every request
- Parallel translation with ThreadPoolExecutor
**Architecture Fixes:**
- Use detected language from WhisperX for alignment
- Proper error handling and cleanup
- Context managers for temp files
**New Features:**
- Progress bar during processing
- Speaker diarization support (optional, requires HF_TOKEN)
- Multiple outputs: video + SRT file + transcription text
- Automatic language detection
**Code Quality:**
- Type hints throughout
- Clean separation of concerns
- Proper logging with [DEBUG] prefixes
- app.py +349 -147
- requirements.txt +9 -8
app.py
CHANGED
|
@@ -4,24 +4,62 @@ import ffmpeg
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import whisperx
|
| 9 |
import spaces
|
| 10 |
-
from scipy.io import wavfile
|
| 11 |
import numpy as np
|
| 12 |
-
import gc
|
| 13 |
-
import tempfile
|
| 14 |
import soundfile as sf
|
| 15 |
-
from
|
| 16 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 17 |
|
| 18 |
# Load Google language codes
|
| 19 |
with open('google_lang_codes.json', 'r') as f:
|
| 20 |
google_lang_codes = json.load(f)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
def ffmpeg_read(input_data_bytes, sampling_rate):
|
|
|
|
| 25 |
process = (
|
| 26 |
ffmpeg.input('pipe:0')
|
| 27 |
.output('pipe:1', format='wav', acodec='pcm_s16le', ar=sampling_rate)
|
|
@@ -31,165 +69,329 @@ def ffmpeg_read(input_data_bytes, sampling_rate):
|
|
| 31 |
audio_array = np.frombuffer(out, np.int16)
|
| 32 |
return audio_array
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
def
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
result = whisper_model.transcribe(audio, batch_size=batch_size)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Align whisper output
|
| 64 |
-
result = whisperx.align(result["segments"], align_model, align_metadata, audio, device, return_char_alignments=False)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
gc.collect()
|
| 73 |
torch.cuda.empty_cache()
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def translate_text(text, target_language_code):
|
| 77 |
-
translated_text = translator.translate(text.strip(), dest=target_language_code).text
|
| 78 |
-
return translated_text
|
| 79 |
|
| 80 |
-
@spaces.GPU
|
| 81 |
-
def
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
print(f"
|
| 93 |
-
return
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
target_language_code = google_lang_codes.get(target_language, "en")
|
| 99 |
-
print(f"Target language
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
with open(audio_file, "rb") as f:
|
| 103 |
-
audio_bytes = f.read()
|
| 104 |
-
inputs = {"array": ffmpeg_read(audio_bytes, 16000), "sampling_rate": 16000}
|
| 105 |
-
|
| 106 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 107 |
-
compute_type = "float16" if
|
| 108 |
-
|
| 109 |
-
align_model, align_metadata = load_align_model(target_language_code, device)
|
| 110 |
-
|
| 111 |
-
transcription_result = transcribe_and_align(inputs, target_language_code, whisper_model, align_model, align_metadata)
|
| 112 |
-
|
| 113 |
-
if "aligned" not in transcription_result:
|
| 114 |
-
print("Error: Transcription result does not contain 'aligned'")
|
| 115 |
-
return
|
| 116 |
-
|
| 117 |
-
aligned_segments = transcription_result["aligned"]
|
| 118 |
-
word_segments = transcription_result["word_segments"]
|
| 119 |
-
|
| 120 |
-
print("Printing aligned segments for debugging:")
|
| 121 |
-
for segment in aligned_segments:
|
| 122 |
-
print(f"Segment start: {segment['start']}, end: {segment['end']}, text: {segment['text']}")
|
| 123 |
-
|
| 124 |
-
def format_timestamp(seconds):
|
| 125 |
-
millis = int((seconds - int(seconds)) * 1000)
|
| 126 |
-
hours, remainder = divmod(int(seconds), 3600)
|
| 127 |
-
minutes, seconds = divmod(remainder, 60)
|
| 128 |
-
return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"
|
| 129 |
-
|
| 130 |
-
with open(transcript_file, "w+", encoding="utf-8") as f:
|
| 131 |
-
counter = 1
|
| 132 |
-
for segment in aligned_segments:
|
| 133 |
-
start_time = format_timestamp(segment['start'])
|
| 134 |
-
end_time = format_timestamp(segment['end'])
|
| 135 |
-
f.write(f"{counter}\n")
|
| 136 |
-
f.write(f"{start_time} --> {end_time}\n")
|
| 137 |
-
f.write(f"{segment['text'].strip()}\n\n")
|
| 138 |
-
counter += 1
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
if translate_video:
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
try:
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
except ffmpeg.Error as e:
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
with gr.Blocks() as demo:
|
| 189 |
-
iface.render()
|
| 190 |
gr.Markdown("""
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
| 194 |
""")
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
+
import tempfile
|
| 8 |
+
import gc
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 11 |
+
from typing import Optional, Tuple
|
| 12 |
+
|
| 13 |
import whisperx
|
| 14 |
import spaces
|
|
|
|
| 15 |
import numpy as np
|
|
|
|
|
|
|
| 16 |
import soundfile as sf
|
| 17 |
+
from deep_translator import GoogleTranslator
|
|
|
|
| 18 |
|
| 19 |
# Load Google language codes
|
| 20 |
with open('google_lang_codes.json', 'r') as f:
|
| 21 |
google_lang_codes = json.load(f)
|
| 22 |
|
| 23 |
+
# ============================================================================
|
| 24 |
+
# GLOBAL MODEL CACHE - Load once, reuse forever
|
| 25 |
+
# ============================================================================
|
| 26 |
+
_whisper_model = None
|
| 27 |
+
_align_models = {} # Cache align models by language
|
| 28 |
+
_diarize_model = None
|
| 29 |
+
|
| 30 |
+
def get_whisper_model(device: str, compute_type: str):
|
| 31 |
+
"""Get cached WhisperX model (large-v3-turbo for speed)."""
|
| 32 |
+
global _whisper_model
|
| 33 |
+
if _whisper_model is None:
|
| 34 |
+
print("[DEBUG] Loading WhisperX model (large-v3-turbo)...")
|
| 35 |
+
_whisper_model = whisperx.load_model(
|
| 36 |
+
"large-v3-turbo", # Faster than large-v3 with similar quality
|
| 37 |
+
device,
|
| 38 |
+
compute_type=compute_type
|
| 39 |
+
)
|
| 40 |
+
print("[DEBUG] WhisperX model loaded successfully")
|
| 41 |
+
return _whisper_model
|
| 42 |
+
|
| 43 |
+
def get_align_model(language_code: str, device: str):
|
| 44 |
+
"""Get cached alignment model for a specific language."""
|
| 45 |
+
global _align_models
|
| 46 |
+
if language_code not in _align_models:
|
| 47 |
+
print(f"[DEBUG] Loading alignment model for language: {language_code}")
|
| 48 |
+
model, metadata = whisperx.load_align_model(
|
| 49 |
+
language_code=language_code,
|
| 50 |
+
device=device,
|
| 51 |
+
model_name="WAV2VEC2_ASR_LARGE_LV60K_960H"
|
| 52 |
+
)
|
| 53 |
+
_align_models[language_code] = (model, metadata)
|
| 54 |
+
print(f"[DEBUG] Alignment model for {language_code} loaded successfully")
|
| 55 |
+
return _align_models[language_code]
|
| 56 |
+
|
| 57 |
+
# ============================================================================
|
| 58 |
+
# Helper Functions
|
| 59 |
+
# ============================================================================
|
| 60 |
|
| 61 |
+
def ffmpeg_read(input_data_bytes: bytes, sampling_rate: int) -> np.ndarray:
|
| 62 |
+
"""Convert audio bytes to numpy array using ffmpeg."""
|
| 63 |
process = (
|
| 64 |
ffmpeg.input('pipe:0')
|
| 65 |
.output('pipe:1', format='wav', acodec='pcm_s16le', ar=sampling_rate)
|
|
|
|
| 69 |
audio_array = np.frombuffer(out, np.int16)
|
| 70 |
return audio_array
|
| 71 |
|
| 72 |
+
def format_timestamp(seconds: float) -> str:
|
| 73 |
+
"""Convert seconds to SRT timestamp format."""
|
| 74 |
+
millis = int((seconds - int(seconds)) * 1000)
|
| 75 |
+
hours, remainder = divmod(int(seconds), 3600)
|
| 76 |
+
minutes, seconds = divmod(remainder, 60)
|
| 77 |
+
return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"
|
| 78 |
|
| 79 |
+
def translate_segment_text(text: str, target_language_code: str) -> str:
|
| 80 |
+
"""Translate a single text segment."""
|
| 81 |
+
if not text.strip():
|
| 82 |
+
return text
|
| 83 |
+
try:
|
| 84 |
+
return GoogleTranslator(source='auto', target=target_language_code).translate(text.strip())
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"[WARNING] Translation failed for '{text[:50]}...': {e}")
|
| 87 |
+
return text
|
| 88 |
|
| 89 |
+
def translate_segments_parallel(segments: list, target_language_code: str) -> list:
|
| 90 |
+
"""Translate multiple segments in parallel using ThreadPoolExecutor."""
|
| 91 |
+
texts = [s['text'].strip() for s in segments]
|
| 92 |
+
|
| 93 |
+
print(f"[DEBUG] Translating {len(texts)} segments in parallel...")
|
| 94 |
+
|
| 95 |
+
with ThreadPoolExecutor(max_workers=8) as executor:
|
| 96 |
+
translated = list(executor.map(
|
| 97 |
+
lambda t: translate_segment_text(t, target_language_code),
|
| 98 |
+
texts
|
| 99 |
+
))
|
| 100 |
+
|
| 101 |
+
# Update segments with translated text
|
| 102 |
+
for i, segment in enumerate(segments):
|
| 103 |
+
segment['text'] = translated[i]
|
| 104 |
+
|
| 105 |
+
return segments
|
| 106 |
|
| 107 |
+
def generate_srt(segments: list, filepath: str):
|
| 108 |
+
"""Generate SRT file from segments."""
|
| 109 |
+
with open(filepath, "w", encoding="utf-8") as f:
|
| 110 |
+
for i, segment in enumerate(segments, 1):
|
| 111 |
+
start_time = format_timestamp(segment['start'])
|
| 112 |
+
end_time = format_timestamp(segment['end'])
|
| 113 |
+
f.write(f"{i}\n")
|
| 114 |
+
f.write(f"{start_time} --> {end_time}\n")
|
| 115 |
+
f.write(f"{segment['text'].strip()}\n\n")
|
| 116 |
|
| 117 |
+
# ============================================================================
|
| 118 |
+
# Main Processing Functions
|
| 119 |
+
# ============================================================================
|
| 120 |
|
| 121 |
+
@spaces.GPU(duration=300)
|
| 122 |
+
def transcribe_and_align(
|
| 123 |
+
audio_path: str,
|
| 124 |
+
device: str,
|
| 125 |
+
compute_type: str,
|
| 126 |
+
progress: gr.Progress
|
| 127 |
+
) -> Tuple[list, str]:
|
| 128 |
+
"""
|
| 129 |
+
Transcribe audio and align timestamps.
|
| 130 |
+
Returns (segments, detected_language).
|
| 131 |
+
"""
|
| 132 |
+
progress(0.3, desc="Transcribing audio...")
|
| 133 |
+
|
| 134 |
+
# Load audio
|
| 135 |
+
audio = whisperx.load_audio(audio_path)
|
| 136 |
+
|
| 137 |
+
# Get cached whisper model
|
| 138 |
+
whisper_model = get_whisper_model(device, compute_type)
|
| 139 |
+
|
| 140 |
+
# Transcribe (WhisperX detects language automatically)
|
| 141 |
+
batch_size = 16
|
| 142 |
result = whisper_model.transcribe(audio, batch_size=batch_size)
|
| 143 |
|
| 144 |
+
# Get detected language from transcription
|
| 145 |
+
detected_language = result.get("language", "en")
|
| 146 |
+
print(f"[DEBUG] Detected language: {detected_language}")
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
if not result.get("segments"):
|
| 149 |
+
raise ValueError("No segments found in transcription")
|
| 150 |
+
|
| 151 |
+
print(f"[DEBUG] Transcribed {len(result['segments'])} segments")
|
| 152 |
+
|
| 153 |
+
progress(0.5, desc="Aligning timestamps...")
|
| 154 |
+
|
| 155 |
+
# Get cached align model for detected language
|
| 156 |
+
align_model, align_metadata = get_align_model(detected_language, device)
|
| 157 |
+
|
| 158 |
+
# Align timestamps
|
| 159 |
+
result = whisperx.align(
|
| 160 |
+
result["segments"],
|
| 161 |
+
align_model,
|
| 162 |
+
align_metadata,
|
| 163 |
+
audio,
|
| 164 |
+
device,
|
| 165 |
+
return_char_alignments=False
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
print(f"[DEBUG] Aligned {len(result['segments'])} segments")
|
| 169 |
+
|
| 170 |
+
# Cleanup
|
| 171 |
+
del audio
|
| 172 |
gc.collect()
|
| 173 |
torch.cuda.empty_cache()
|
| 174 |
+
|
| 175 |
+
return result["segments"], detected_language
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
@spaces.GPU(duration=60)
|
| 178 |
+
def diarize_audio(
|
| 179 |
+
audio_path: str,
|
| 180 |
+
segments: list,
|
| 181 |
+
hf_token: Optional[str],
|
| 182 |
+
device: str,
|
| 183 |
+
progress: gr.Progress
|
| 184 |
+
) -> list:
|
| 185 |
+
"""Identify speakers in audio (optional feature)."""
|
| 186 |
+
if not hf_token:
|
| 187 |
+
print("[DEBUG] No HF token provided, skipping diarization")
|
| 188 |
+
return segments
|
| 189 |
+
|
| 190 |
+
progress(0.6, desc="Identifying speakers...")
|
| 191 |
+
|
| 192 |
+
global _diarize_model
|
| 193 |
+
if _diarize_model is None:
|
| 194 |
+
print("[DEBUG] Loading diarization model...")
|
| 195 |
+
_diarize_model = whisperx.DiarizationPipeline(
|
| 196 |
+
use_auth_token=hf_token,
|
| 197 |
+
device=device
|
| 198 |
+
)
|
| 199 |
|
| 200 |
try:
|
| 201 |
+
audio = whisperx.load_audio(audio_path)
|
| 202 |
+
diarize_segments = _diarize_model(audio)
|
| 203 |
+
result = whisperx.assign_word_speakers(diarize_segments, {"segments": segments})
|
| 204 |
+
print(f"[DEBUG] Diarization complete, found speakers")
|
| 205 |
+
return result["segments"]
|
| 206 |
+
except Exception as e:
|
| 207 |
+
print(f"[WARNING] Diarization failed: {e}")
|
| 208 |
+
return segments
|
| 209 |
|
| 210 |
+
# ============================================================================
|
| 211 |
+
# Main Video Processing Function
|
| 212 |
+
# ============================================================================
|
| 213 |
|
| 214 |
+
def process_video(
|
| 215 |
+
video_path: str,
|
| 216 |
+
target_language: str,
|
| 217 |
+
translate_video: bool,
|
| 218 |
+
enable_diarization: bool,
|
| 219 |
+
progress: gr.Progress = gr.Progress()
|
| 220 |
+
):
|
| 221 |
+
"""Main function to process video with transcription and optional translation."""
|
| 222 |
+
|
| 223 |
+
print("=" * 60)
|
| 224 |
+
print("VIDEO PROCESSING STARTED")
|
| 225 |
+
print("=" * 60)
|
| 226 |
+
|
| 227 |
+
if not video_path:
|
| 228 |
+
raise gr.Error("Please upload a video file")
|
| 229 |
+
|
| 230 |
+
# Get target language code
|
| 231 |
target_language_code = google_lang_codes.get(target_language, "en")
|
| 232 |
+
print(f"[DEBUG] Target language: {target_language} ({target_language_code})")
|
| 233 |
+
|
| 234 |
+
# Setup device
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 236 |
+
compute_type = "float16" if device == "cuda" else "int8"
|
| 237 |
+
print(f"[DEBUG] Device: {device}, Compute type: {compute_type}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
# Generate unique ID for this job
|
| 240 |
+
job_id = uuid.uuid4()
|
| 241 |
+
|
| 242 |
+
progress(0.1, desc="Extracting audio from video...")
|
| 243 |
+
|
| 244 |
+
# Extract audio using context manager
|
| 245 |
+
audio_file = f"/tmp/{job_id}_audio.wav"
|
| 246 |
+
try:
|
| 247 |
+
print(f"[DEBUG] Extracting audio to {audio_file}")
|
| 248 |
+
ffmpeg.input(video_path).output(audio_file, ac=1, ar=16000).run(
|
| 249 |
+
quiet=True,
|
| 250 |
+
overwrite_output=True
|
| 251 |
+
)
|
| 252 |
+
except ffmpeg.Error as e:
|
| 253 |
+
raise gr.Error(f"Failed to extract audio: {e.stderr.decode()}")
|
| 254 |
+
|
| 255 |
+
progress(0.2, desc="Loading audio...")
|
| 256 |
+
|
| 257 |
+
# Transcribe and align
|
| 258 |
+
segments, detected_language = transcribe_and_align(
|
| 259 |
+
audio_file,
|
| 260 |
+
device,
|
| 261 |
+
compute_type,
|
| 262 |
+
progress
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
# Optional: Diarization
|
| 266 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 267 |
+
if enable_diarization and hf_token:
|
| 268 |
+
segments = diarize_audio(audio_file, segments, hf_token, device, progress)
|
| 269 |
+
|
| 270 |
+
# Translate if requested
|
| 271 |
if translate_video:
|
| 272 |
+
progress(0.7, desc=f"Translating to {target_language}...")
|
| 273 |
+
print(f"[DEBUG] Translating {len(segments)} segments to {target_language_code}")
|
| 274 |
+
segments = translate_segments_parallel(segments, target_language_code)
|
| 275 |
+
|
| 276 |
+
progress(0.8, desc="Generating subtitles...")
|
| 277 |
+
|
| 278 |
+
# Generate SRT file
|
| 279 |
+
srt_file = f"/tmp/{job_id}_subtitles.srt"
|
| 280 |
+
generate_srt(segments, srt_file)
|
| 281 |
+
print(f"[DEBUG] Generated SRT file: {srt_file}")
|
| 282 |
+
|
| 283 |
+
# Generate plain text transcription
|
| 284 |
+
transcription_text = "\n".join([s['text'].strip() for s in segments])
|
| 285 |
+
|
| 286 |
+
progress(0.9, desc="Embedding subtitles into video...")
|
| 287 |
+
|
| 288 |
+
# Embed subtitles
|
| 289 |
+
output_video = f"/tmp/{job_id}_output.mp4"
|
| 290 |
+
|
| 291 |
+
# Choose subtitle style based on language
|
| 292 |
+
if target_language_code in ['ja', 'zh-cn', 'zh-tw', 'ko']:
|
| 293 |
+
subtitle_style = "FontName=Noto Sans CJK JP,PrimaryColour=&H00FFFFFF,OutlineColour=&H000000,BackColour=&H80000000,BorderStyle=3,Outline=2,Shadow=1"
|
| 294 |
+
else:
|
| 295 |
+
subtitle_style = "FontName=Arial,PrimaryColour=&H00FFFFFF,OutlineColour=&H000000,BackColour=&H80000000,BorderStyle=3,Outline=2,Shadow=1"
|
| 296 |
+
|
| 297 |
try:
|
| 298 |
+
(
|
| 299 |
+
ffmpeg
|
| 300 |
+
.input(video_path)
|
| 301 |
+
.output(
|
| 302 |
+
output_video,
|
| 303 |
+
vf=f"subtitles={srt_file}:force_style='{subtitle_style}'",
|
| 304 |
+
codec="libx264",
|
| 305 |
+
preset="fast"
|
| 306 |
+
)
|
| 307 |
+
.run(quiet=True, overwrite_output=True)
|
| 308 |
+
)
|
| 309 |
+
print(f"[DEBUG] Output video created: {output_video}")
|
| 310 |
except ffmpeg.Error as e:
|
| 311 |
+
raise gr.Error(f"Failed to embed subtitles: {e.stderr.decode()}")
|
| 312 |
+
|
| 313 |
+
# Cleanup temporary files
|
| 314 |
+
try:
|
| 315 |
+
os.unlink(audio_file)
|
| 316 |
+
os.unlink(srt_file)
|
| 317 |
+
except:
|
| 318 |
+
pass
|
| 319 |
+
|
| 320 |
+
progress(1.0, desc="Complete!")
|
| 321 |
+
|
| 322 |
+
print("=" * 60)
|
| 323 |
+
print("VIDEO PROCESSING COMPLETE")
|
| 324 |
+
print("=" * 60)
|
| 325 |
+
|
| 326 |
+
return output_video, srt_file, transcription_text
|
| 327 |
+
|
| 328 |
+
# ============================================================================
|
| 329 |
+
# Gradio Interface
|
| 330 |
+
# ============================================================================
|
| 331 |
+
|
| 332 |
+
with gr.Blocks(title="Video Transcription & Translation") as demo:
|
|
|
|
|
|
|
| 333 |
gr.Markdown("""
|
| 334 |
+
# 🎬 Video Transcription & Translation
|
| 335 |
+
|
| 336 |
+
Powered by **WhisperX (large-v3-turbo)** for fast, accurate transcription with word-level timestamps.
|
| 337 |
+
|
| 338 |
+
Developed by [@artificialguybr](https://twitter.com/artificialguybr) • [Video Dubbing](https://huggingface.co/spaces/artificialguybr/video-dubbing)
|
| 339 |
""")
|
| 340 |
+
|
| 341 |
+
with gr.Row():
|
| 342 |
+
with gr.Column(scale=2):
|
| 343 |
+
video_input = gr.Video(
|
| 344 |
+
label="Upload Video (max 15 min)",
|
| 345 |
+
include_audio=True
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
with gr.Row():
|
| 349 |
+
target_language = gr.Dropdown(
|
| 350 |
+
choices=list(google_lang_codes.keys()),
|
| 351 |
+
label="Target Language",
|
| 352 |
+
value="English"
|
| 353 |
+
)
|
| 354 |
+
translate_checkbox = gr.Checkbox(
|
| 355 |
+
label="Translate Subtitles",
|
| 356 |
+
value=True,
|
| 357 |
+
info="Translate to target language"
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
diarization_checkbox = gr.Checkbox(
|
| 361 |
+
label="Speaker Diarization",
|
| 362 |
+
value=False,
|
| 363 |
+
info="Identify different speakers (requires HF_TOKEN)"
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
process_btn = gr.Button("🚀 Process Video", variant="primary", size="lg")
|
| 367 |
+
|
| 368 |
+
with gr.Column(scale=2):
|
| 369 |
+
output_video = gr.Video(label="Output Video")
|
| 370 |
+
|
| 371 |
+
with gr.Row():
|
| 372 |
+
srt_file = gr.File(label="Download .SRT")
|
| 373 |
+
transcription_text = gr.Textbox(
|
| 374 |
+
label="Transcription",
|
| 375 |
+
lines=10,
|
| 376 |
+
max_lines=20,
|
| 377 |
+
interactive=False
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
gr.Markdown("""
|
| 381 |
+
---
|
| 382 |
+
**Notes:**
|
| 383 |
+
- Video limit: 15 minutes
|
| 384 |
+
- Uses WhisperX large-v3-turbo for fast transcription
|
| 385 |
+
- Automatic language detection
|
| 386 |
+
- Parallel translation for speed
|
| 387 |
+
- Speaker diarization optional (set HF_TOKEN secret)
|
| 388 |
+
""")
|
| 389 |
+
|
| 390 |
+
process_btn.click(
|
| 391 |
+
fn=process_video,
|
| 392 |
+
inputs=[video_input, target_language, translate_checkbox, diarization_checkbox],
|
| 393 |
+
outputs=[output_video, srt_file, transcription_text]
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
if __name__ == "__main__":
|
| 397 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
torchvision --index-url https://download.pytorch.org/whl/cu118
|
| 6 |
-
torchaudio --index-url https://download.pytorch.org/whl/cu118
|
| 7 |
-
setuptools
|
| 8 |
ffmpeg-python
|
| 9 |
scipy
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
torchaudio
|
|
|
|
|
|
|
|
|
|
| 5 |
ffmpeg-python
|
| 6 |
scipy
|
| 7 |
+
numpy
|
| 8 |
+
soundfile
|
| 9 |
+
deep-translator
|
| 10 |
+
git+https://github.com/m-bain/whisperx.git
|
| 11 |
+
pyannote.audio
|