Spaces:
Sleeping
Sleeping
File size: 22,912 Bytes
bad74fd | 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | """
PlotWeaver β Live Commentary Translation Platform
===================================================
Event management, multi-language dubbing, live streaming.
"""
import os
import time
import tempfile
import numpy as np
import re
import soundfile as sf
import gradio as gr
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger(__name__)
from languages import LANGUAGES, LANGUAGE_GROUPS, ALL_LANGUAGE_NAMES, QWEN_VOICES
from tts_engine import synthesize_chunked
from qwen_engine import dub_video_qwen, translate_chunk_qwen
from pipeline import (
load_models, transcribe, translate_text, translate_sentence,
split_into_sentences, extract_audio_from_video, get_media_duration,
stretch_audio_to_duration, mux_video_audio, tts_pipe_local,
)
import pipeline
# Load all models at startup
load_models()
# =============================================================================
# Helper functions
# =============================================================================
def get_voices_for_language(lang_name):
"""Get available voices for a language based on its engine."""
config = LANGUAGES.get(lang_name, {})
engine = config.get("tts_engine", "local")
if engine == "qwen":
return QWEN_VOICES
elif engine == "yourvoic" and config.get("yourvoic_voices"):
return config["yourvoic_voices"]
elif engine == "local":
return ["Default (local model)"]
return ["Peter"]
def full_pipeline_audio(audio_input, target_language):
"""Full pipeline: English audio β target language audio."""
if audio_input is None:
return None, "Please upload or record audio."
lang_config = LANGUAGES.get(target_language)
if not lang_config:
return None, f"Language '{target_language}' not configured."
sample_rate, audio_array = audio_input
audio_array = audio_array.astype(np.float32)
if audio_array.ndim > 1:
audio_array = audio_array.mean(axis=1)
if audio_array.max() > 1.0 or audio_array.min() < -1.0:
max_val = max(abs(audio_array.max()), abs(audio_array.min()))
if max_val > 0:
audio_array = audio_array / max_val
log = []
total_start = time.time()
# ASR
t0 = time.time()
english = transcribe(audio_array, sample_rate)
log.append(f"**ASR** ({time.time()-t0:.2f}s)\n{english}")
if not english:
return None, "ASR returned empty text."
# MT
t0 = time.time()
nllb_code = lang_config["nllb"]
translated, en_sents, tgt_sents = translate_text(english, nllb_code, fast=False)
log.append(f"\n**Translation** ({time.time()-t0:.2f}s)")
for e, t in zip(en_sents, tgt_sents):
log.append(f" EN: {e}\n {target_language.upper()}: {t}")
if not translated:
return None, "Translation returned empty."
# TTS
t0 = time.time()
audio_out, sr_out = synthesize_chunked(
translated, lang_config, tts_pipe=pipeline.tts_pipe_local
)
log.append(f"\n**TTS** ({time.time()-t0:.2f}s) = {len(audio_out)/sr_out:.1f}s audio")
total = time.time() - total_start
log.append(f"\n**Total: {total:.2f}s**")
return (sr_out, audio_out), "\n".join(log)
def full_pipeline_text(english_text, target_language, voice_name):
"""Text-only pipeline: English text β target language audio."""
if not english_text or not english_text.strip():
return None, "Please enter English text."
lang_config = LANGUAGES.get(target_language)
if not lang_config:
return None, f"Language '{target_language}' not configured."
log = []
total_start = time.time()
# MT
t0 = time.time()
nllb_code = lang_config["nllb"]
translated, en_sents, tgt_sents = translate_text(english_text.strip(), nllb_code, fast=False)
log.append(f"**Translation** ({time.time()-t0:.2f}s)")
for e, t in zip(en_sents, tgt_sents):
log.append(f" EN: {e}\n {target_language.upper()}: {t}")
if not translated:
return None, "Translation returned empty."
# TTS
t0 = time.time()
audio_out, sr_out = synthesize_chunked(
translated, lang_config, tts_pipe=pipeline.tts_pipe_local
)
log.append(f"\n**TTS** ({time.time()-t0:.2f}s) = {len(audio_out)/sr_out:.1f}s audio")
total = time.time() - total_start
log.append(f"\n**Total: {total:.2f}s**")
return (sr_out, audio_out), "\n".join(log)
def dub_video(video_path, target_languages, dub_voice, chunk_seconds, progress=gr.Progress()):
"""
Dub a video into one or more target languages.
Routes to Qwen Omni for global languages, local pipeline for African languages.
"""
if video_path is None:
return None, "Please upload a video."
if not target_languages:
return None, "Please select at least one target language."
results_log = []
output_videos = []
for lang_name in target_languages:
lang_config = LANGUAGES.get(lang_name)
if not lang_config:
results_log.append(f"**{lang_name}**: not configured, skipped")
continue
engine = lang_config.get("tts_engine", "local")
results_log.append(f"\n{'='*50}")
results_log.append(f"**Dubbing: {lang_name}** (engine: {engine})")
results_log.append(f"{'='*50}")
try:
if engine == "qwen":
# Qwen Omni: end-to-end speech-to-speech (best for global languages)
qwen_lang_name = lang_config.get("qwen_name", lang_name)
voice = dub_voice if dub_voice in QWEN_VOICES else "Ethan"
out_video, log_text = dub_video_qwen(
video_path, qwen_lang_name, voice=voice,
chunk_seconds=chunk_seconds, progress_fn=progress,
)
results_log.append(log_text)
if out_video:
output_videos.append(out_video)
else:
# Local/YourVoic pipeline: ASR β NLLB β TTS
work_dir = tempfile.mkdtemp(prefix=f"dub_{lang_name}_")
extracted_audio = os.path.join(work_dir, "audio.wav")
tgt_audio_raw = os.path.join(work_dir, "tgt_raw.wav")
tgt_audio_aligned = os.path.join(work_dir, "tgt_aligned.wav")
output_video = os.path.join(work_dir, f"dubbed_{lang_name}.mp4")
progress(0.05, desc=f"{lang_name}: extracting audio...")
extract_audio_from_video(video_path, extracted_audio)
video_duration = get_media_duration(video_path)
results_log.append(f"Video: {video_duration:.1f}s")
audio_array, sr = sf.read(extracted_audio, dtype="float32")
if audio_array.ndim > 1:
audio_array = audio_array.mean(axis=1)
progress(0.15, desc=f"{lang_name}: transcribing...")
t0 = time.time()
english = transcribe(audio_array, sr)
results_log.append(f"ASR: {time.time()-t0:.1f}s")
if not english:
results_log.append("ASR empty β skipped")
continue
progress(0.4, desc=f"{lang_name}: translating...")
t0 = time.time()
nllb_code = lang_config["nllb"]
translated, _, _ = translate_text(english, nllb_code, fast=True)
results_log.append(f"MT: {time.time()-t0:.1f}s")
if not translated:
results_log.append("Translation empty β skipped")
continue
progress(0.65, desc=f"{lang_name}: synthesizing...")
t0 = time.time()
tgt_audio, tgt_sr = synthesize_chunked(
translated, lang_config, tts_pipe=pipeline.tts_pipe_local
)
sf.write(tgt_audio_raw, tgt_audio, tgt_sr)
tgt_duration = len(tgt_audio) / tgt_sr
results_log.append(f"TTS: {time.time()-t0:.1f}s ({tgt_duration:.1f}s audio)")
progress(0.85, desc=f"{lang_name}: aligning...")
MAX_STRETCH = 1.2
stretch_ratio = tgt_duration / video_duration
if stretch_ratio <= MAX_STRETCH:
if abs(stretch_ratio - 1.0) > 0.02:
stretch_audio_to_duration(tgt_audio_raw, tgt_audio_aligned, video_duration)
else:
import shutil
shutil.copy(tgt_audio_raw, tgt_audio_aligned)
extend_video = False
final_duration = video_duration
else:
import shutil
shutil.copy(tgt_audio_raw, tgt_audio_aligned)
extend_video = True
final_duration = tgt_duration
results_log.append(f"Audio longer ({stretch_ratio:.1f}x) β extending video")
progress(0.95, desc=f"{lang_name}: combining...")
mux_video_audio(
video_path, tgt_audio_aligned, output_video,
extend_video=extend_video, target_duration=final_duration
)
output_videos.append(output_video)
except Exception as e:
logger.exception(f"Dubbing {lang_name} failed")
results_log.append(f"Error: {str(e)}")
progress(1.0, desc="Done!")
final_video = output_videos[0] if output_videos else None
return final_video, "\n".join(results_log)
def update_voices(language):
"""Update voice dropdown when language changes."""
voices = get_voices_for_language(language)
return gr.update(choices=voices, value=voices[0])
# =============================================================================
# Gradio UI
# =============================================================================
EXAMPLES = [
"And it's a brilliant goal from the striker!",
"The referee has shown a yellow card. Corner kick for the home team.",
"What a save by the goalkeeper! The match is heading into injury time.",
"He dribbles past two defenders and shoots! The ball hits the back of the net!",
]
CSS = """
.main-header { text-align: center; margin-bottom: 0.5rem; }
.main-header h1 { font-size: 1.8rem; font-weight: 700; margin: 0; }
.main-header p { color: #666; font-size: 0.95rem; }
.lang-group-label { font-weight: 600; font-size: 0.85rem; color: #888; text-transform: uppercase; letter-spacing: 0.05em; margin-top: 0.5rem; }
"""
with gr.Blocks(
title="PlotWeaver β Live Commentary Translation",
theme=gr.themes.Soft(),
css=CSS,
) as demo:
gr.HTML("""
<div class="main-header">
<h1>PlotWeaver</h1>
<p>Live commentary translation platform — English to 40+ languages</p>
<p style="font-size:0.8rem; color:#999">ASR (Whisper) → MT (NLLB-200) → TTS (YourVoic + local models)</p>
</div>
""")
with gr.Tabs():
# ====== TAB 1: EVENT MANAGEMENT ======
with gr.TabItem("Event Management"):
gr.Markdown("### Create new event")
gr.Markdown("Configure your live broadcast event with target languages and input source.")
with gr.Row():
with gr.Column(scale=2):
event_name = gr.Textbox(
label="Event name",
placeholder="e.g. Premier League: Arsenal vs. Chelsea",
)
with gr.Row():
start_time = gr.Textbox(label="Start time", placeholder="08:30 PM")
end_time = gr.Textbox(label="End time", placeholder="10:30 PM")
event_date = gr.Textbox(label="Date", placeholder="2026-06-06")
gr.Markdown("#### Input source")
input_method = gr.Radio(
choices=["RTMP Stream", "WebRTC (Browser)", "Direct Audio Feed"],
value="RTMP Stream",
label="Input method",
)
gr.Markdown("#### Target languages")
gr.Markdown("Select languages for simultaneous broadcast. Additional languages consume more stream minutes.")
# Language checkboxes grouped by category
target_langs = gr.CheckboxGroup(
choices=ALL_LANGUAGE_NAMES,
label="Languages",
value=["Yoruba"],
)
with gr.Column(scale=1):
gr.Markdown("#### Estimate summary")
estimate_display = gr.Markdown(
value="**Event:** Not configured\n\n**Languages:** 1 selected\n\n**Estimated duration:** --\n\n**Total estimate:** --"
)
create_event_btn = gr.Button("Create Event", variant="primary", size="lg")
event_status = gr.Markdown("")
def update_estimate(name, langs, start, end):
n_langs = len(langs) if langs else 0
lang_list = ", ".join(langs) if langs else "None"
return (
f"**Event:** {name or 'Not set'}\n\n"
f"**Languages:** {n_langs} selected\n\n"
f"{lang_list}\n\n"
f"**Input:** Configured\n\n"
f"**Rate:** 1x (Standard)"
)
for inp in [event_name, target_langs, start_time, end_time]:
inp.change(
fn=update_estimate,
inputs=[event_name, target_langs, start_time, end_time],
outputs=[estimate_display],
)
def create_event(name, langs):
if not name:
return "Please enter an event name."
if not langs:
return "Please select at least one language."
return f"Event **{name}** created with {len(langs)} languages: {', '.join(langs)}"
create_event_btn.click(
fn=create_event,
inputs=[event_name, target_langs],
outputs=[event_status],
)
# ====== TAB 2: LIVE STUDIO ======
with gr.TabItem("Live Studio"):
gr.Markdown("### Live streaming translation")
gr.Markdown("Record or stream English commentary and hear it translated in real-time.")
with gr.Row():
studio_language = gr.Dropdown(
choices=ALL_LANGUAGE_NAMES,
value="Yoruba",
label="Target language",
)
studio_voice = gr.Dropdown(
choices=get_voices_for_language("Yoruba"),
value=get_voices_for_language("Yoruba")[0],
label="Voice",
)
studio_language.change(
fn=update_voices,
inputs=[studio_language],
outputs=[studio_voice],
)
with gr.Row():
with gr.Column():
studio_audio_in = gr.Audio(
label="English commentary (upload or record)",
type="numpy",
sources=["upload", "microphone"],
)
studio_translate_btn = gr.Button("Translate", variant="primary", size="lg")
with gr.Column():
studio_audio_out = gr.Audio(label="Translated audio", type="numpy", autoplay=True)
studio_log = gr.Markdown(label="Pipeline log")
studio_translate_btn.click(
fn=full_pipeline_audio,
inputs=[studio_audio_in, studio_language],
outputs=[studio_audio_out, studio_log],
)
# ====== TAB 3: VIDEO DUBBING ======
with gr.TabItem("Video Dubbing"):
gr.Markdown("### Video dubbing (English β multi-language)")
gr.Markdown(
"Upload a video with English commentary and get back a dubbed version. "
"**Global languages** (Arabic, French, Spanish, etc.) use Qwen Omni for best quality. "
"**African languages** (Yoruba, Hausa, etc.) use the local Whisper β NLLB β MMS-TTS pipeline."
)
with gr.Row():
with gr.Column():
dub_video_in = gr.Video(label="Upload English video", sources=["upload"])
dub_languages = gr.CheckboxGroup(
choices=ALL_LANGUAGE_NAMES,
label="Target languages",
value=["Yoruba"],
)
with gr.Row():
dub_voice = gr.Dropdown(
choices=QWEN_VOICES,
value="Ethan",
label="Voice (for Qwen languages)",
info="Applies to Arabic, French, Spanish, etc. Local languages use default voice.",
)
dub_chunk_slider = gr.Slider(
minimum=30, maximum=300, value=120, step=10,
label="Chunk duration (seconds)",
info="Shorter = more API calls but less timeout risk.",
)
dub_btn = gr.Button("Dub Video", variant="primary", size="lg")
with gr.Column():
dub_video_out = gr.Video(label="Dubbed video (download from player)")
dub_log = gr.Markdown(
label="Processing log",
value="Upload a video and select languages to start."
)
dub_btn.click(
fn=dub_video,
inputs=[dub_video_in, dub_languages, dub_voice, dub_chunk_slider],
outputs=[dub_video_out, dub_log],
)
# ====== TAB 4: TEXT TRANSLATION ======
with gr.TabItem("Text \u2192 Audio"):
gr.Markdown("### Text to translated speech")
gr.Markdown("Type English text, choose a language, and hear the translated audio.")
with gr.Row():
text_language = gr.Dropdown(
choices=ALL_LANGUAGE_NAMES,
value="Yoruba",
label="Target language",
)
text_voice = gr.Dropdown(
choices=get_voices_for_language("Yoruba"),
value=get_voices_for_language("Yoruba")[0],
label="Voice",
)
text_language.change(
fn=update_voices,
inputs=[text_language],
outputs=[text_voice],
)
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="English text",
placeholder="Type English football commentary here...",
lines=4,
)
text_btn = gr.Button("Translate to speech", variant="primary", size="lg")
gr.Examples(
examples=[[e] for e in EXAMPLES],
inputs=[text_input],
label="Example commentary",
)
with gr.Column():
text_audio_out = gr.Audio(label="Translated audio", type="numpy", autoplay=True)
text_log = gr.Markdown(label="Pipeline log")
text_btn.click(
fn=full_pipeline_text,
inputs=[text_input, text_language, text_voice],
outputs=[text_audio_out, text_log],
)
# ====== TAB 5: RECORDINGS ======
with gr.TabItem("Recordings & Clips"):
gr.Markdown("### Recordings management")
gr.Markdown(
"Past dubbed recordings will appear here. "
"This feature is coming soon β for now, use Video Dubbing to create new recordings "
"and download them from the player."
)
# ====== TAB 6: VOICE MODELS ======
with gr.TabItem("Voice Models"):
gr.Markdown("### Voice model library")
gr.Markdown("Browse available voices for each language.")
voice_lang_select = gr.Dropdown(
choices=ALL_LANGUAGE_NAMES,
value="Yoruba",
label="Select language",
)
voice_info = gr.Markdown()
def show_voice_info(lang):
config = LANGUAGES.get(lang, {})
engine = config.get("tts_engine", "unknown")
voices = config.get("yourvoic_voices", [])
info = f"### {lang}\n\n"
if engine == "qwen":
info += f"**Engine:** Qwen 3.5 Omni (end-to-end speech-to-speech)\n\n"
info += f"This is the highest quality option. Qwen handles ASR + translation + TTS in a single API call, "
info += f"preserving tone, emotion, and pacing from the original speaker.\n\n"
info += f"**Available voices ({len(QWEN_VOICES)}):** {', '.join(QWEN_VOICES[:10])}... and {len(QWEN_VOICES)-10} more\n\n"
info += f"All voices support all Qwen languages."
elif engine == "yourvoic":
info += f"**Engine:** YourVoic API (TTS) + NLLB-200 (translation)\n\n"
info += f"**YourVoic language:** `{config.get('yourvoic_lang', 'N/A')}`\n\n"
info += f"**Available voices:** {', '.join(voices) if voices else 'Peter (default)'}"
else:
info += f"**Engine:** Local pipeline (Whisper ASR + NLLB MT + MMS-TTS)\n\n"
info += f"**NLLB code:** `{config.get('nllb', 'N/A')}`\n\n"
info += "Uses locally fine-tuned models on GPU. Voice selection not available."
return info
voice_lang_select.change(fn=show_voice_info, inputs=[voice_lang_select], outputs=[voice_info])
demo.load(fn=show_voice_info, inputs=[voice_lang_select], outputs=[voice_info])
gr.Markdown("""
---
**PlotWeaver** by PlotweaverAI | Models:
[ASR](https://huggingface.co/PlotweaverAI/whisper-small-de-en) |
[MT](https://huggingface.co/PlotweaverAI/nllb-200-distilled-600M-african-6lang) |
[TTS](https://huggingface.co/PlotweaverAI/yoruba-mms-tts-new) |
[YourVoic API](https://yourvoic.com)
""")
if __name__ == "__main__":
demo.launch()
|