"""DubLab on Hugging Face — Gradio ZeroGPU Space version. Same pipeline as the Flask site (Spain), rebuilt as a Gradio app: upload a short video (or paste a link) -> dubbed video back. Differences from the Flask version: - Gradio UI instead of Flask + custom HTML/JS (no admin panel, no reviews, no blur-box picker — the core dub feature only). - No @spaces.GPU anywhere: the whole pipeline is CPU-bound (faster-whisper CPU, Nara ONNX CPU, ffmpeg, Gemini API), so it runs on the Space's CPU and consumes ZERO daily GPU quota. - Luna translation is skipped (no OpenCode agent on HF) — Gemini translates directly, with the local NLLB fallback only if its model is present (it is not bundled here). - Limits are tighter because the Space CPU is small: 3 min / 100 MB (the Spain site takes 10 min / 250 MB). Secrets (Space Settings -> Variables and secrets): GEMINI_API_KEY (required) — translation + Gemini TTS fallback GEMINI_TTS_API_KEYS (optional) — comma-separated rotation keys """ import os import re import shutil import subprocess import sys import time import uuid from pathlib import Path import gradio as gr # No OpenCode agent on HF: Luna is never imported here at all. os.environ.setdefault("DUBLAB_NO_LUNA", "1") os.environ.setdefault("NARA_THREADS", "2") from alerts import quota_alert, quota_ok from dub import (build_dub_track, cleanup_segments, configure_tts, extract_audio, has_audio_stream, has_video_stream, merge_segments, mux, probe_duration, probe_size, set_engine, set_nara_voice, transcribe, valid_nara_voice, write_ass, write_srt) from gemini import translate_lines from gemini_tts import QuotaExhausted from linkutil import (MAX_DURATION, MAX_MB, check_link, download_link, fetch_link_info) BASE = Path(__file__).parent JOBS = BASE / "jobs" JOBS.mkdir(exist_ok=True) TTL = 24 * 3600 VOICES = { "en2mm": {"female": "my-MM-NilarNeural", "male": "my-MM-ThihaNeural"}, "mm2en": {"female": "en-US-AvaNeural", "male": "en-US-AndrewNeural"}, } STAGE_LABEL = { "queue": "တန်းစီနေတယ်", "download": "ဗီဒီယို ဆွဲချနေတယ်", "audio": "အသံထုတ်ယူနေတယ်", "transcribe": "စကားပြောမှတ်နေတယ်", "translate": "ဘာသာပြန်နေတယ်", "voice": "အသံဖော်နေတယ်", "mux": "ဗီဒီယိုပေါင်းနေတယ်", "done": "ပြီးပြီ ✅", } # ---- startup checks ------------------------------------------------------- def _check_ffmpeg(): """ffmpeg/ffprobe come from packages.txt. imageio-ffmpeg ships an ffmpeg binary only (no ffprobe), so it is a fallback for ffmpeg alone; without ffprobe the job cannot probe files and must fail with a clear message instead of a cryptic one.""" if shutil.which("ffmpeg") and shutil.which("ffprobe"): return try: import imageio_ffmpeg exe = Path(imageio_ffmpeg.get_ffmpeg_exe()) os.environ["PATH"] = (str(exe.parent) + os.pathsep + os.environ.get("PATH", "")) except Exception: # noqa: BLE001 - fall through to the loud check pass if not (shutil.which("ffmpeg") and shutil.which("ffprobe")): raise RuntimeError("ffmpeg/ffprobe not found — packages.txt " "did not install them; check the Space build log") def ensure_nara(): """Download the Nara Mini voices (Shoon + Ko Banyar, ~365 MB) from the public model repo YeeNyunn/nara-mini-v1 on first start. Gradio Spaces have no Dockerfile build step, so this runs at app startup instead. The files land in ./nara-mini, the same layout nara_tts.py expects (NARA_MODEL_DIR default). """ target = BASE / "nara-mini" / "onnx" / "shoon" / "encoder.onnx" if target.exists(): print("[dublab] nara-mini voices already present") return print("[dublab] downloading Nara Mini voices (~365 MB)…") from huggingface_hub import snapshot_download snapshot_download( repo_id="YeeNyunn/nara-mini-v1", local_dir=str(BASE / "nara-mini"), allow_patterns=["config.json", "frontend/*", "onnx/shoon/*", "onnx/ko-banyar/*"]) print("[dublab] nara-mini voices ready") def _sweep_old(): now = time.time() for d in JOBS.iterdir(): try: if d.is_dir() and now - d.stat().st_mtime > TTL: shutil.rmtree(d, ignore_errors=True) except OSError: pass _check_ffmpeg() ensure_nara() _sweep_old() _tts_keys = [k.strip() for k in os.environ.get("GEMINI_TTS_API_KEYS", os.environ.get("GEMINI_API_KEY", "")).split(",") if k.strip()] configure_tts(_tts_keys) KEY_OK = bool(os.environ.get("GEMINI_API_KEY")) # ---- helpers -------------------------------------------------------------- def _is_quota(e): if isinstance(e, QuotaExhausted): return True s = str(e).lower() return "resource_exhausted" in s or "quota" in s or "429" in s def clean_err(e): """User-facing error text — never leak server paths.""" s = str(e) if ("speech.platform.bing.com" in s or "WSServerHandshakeError" in s or "Invalid response status" in s): return ("အသံထုတ်တဲ့ service (Microsoft) ခေတ္တ ပျက်နေတယ် — " "ပြန်ကောင်းအောင် ပြင်နေပါတယ်။ ခဏနေမှ ပြန်စမ်းပါ 🙏") return s.replace(str(BASE), "[app]").replace(str(JOBS), "[jobs]")[:300] # ---- the dub job (generator: yields status updates as it runs) ----------- def dub_video(video_file, link, direction, voice, engine, subs, sub_bg, progress=gr.Progress(track_tqdm=True)): log = [] def say(stage, msg=""): label = STAGE_LABEL.get(stage, stage) line = f"[{time.strftime('%H:%M:%S')}] {label}" + (f" — {msg}" if msg else "") log.append(line) return line def out(status, video=None, srt=None): return status, video, srt, "\n".join(log) jid = uuid.uuid4().hex[:12] d = JOBS / jid d.mkdir(parents=True) try: if not KEY_OK: raise RuntimeError( "server key မရှိသေးဘူး — Space owner က GEMINI_API_KEY ကို " "Space Settings > Secrets မှာ ထည့်ရမယ်") direction = direction or "en2mm" voice = voice or "female" engine = engine or "nara" if engine not in ("edge", "gemini", "nara"): engine = "nara" nara_voice = valid_nara_voice("shoon" if voice == "female" else "ko-banyar") api_key = os.environ.get("GEMINI_API_KEY") # ---- input: pasted link wins over an uploaded file ---- url = (link or "").strip() ext = ".mp4" if url: say("download", "link စစ်နေတယ်…") yield out(say("download", "link စစ်နေတယ်…")) url = check_link(url) fetch_link_info(url) # fail fast on bad/private/too-long links say("download", "ဗီဒီယို ဆွဲချနေတယ်…") yield out(say("download", "ဗီဒီယို ဆွဲချနေတယ်…")) ext = download_link(url, d) else: if not video_file: raise ValueError("ဗီဒီယိုဖိုင် ရွေးပေးပါ — ဒါမှမဟုတ် link paste လုပ်ပါ") src_in = Path(video_file) ext = src_in.suffix.lower() if ext not in (".mp4", ".mov", ".mkv", ".webm", ".m4v"): raise ValueError("MP4/MOV ဖိုင်ပဲ လက်ခံတယ်") if src_in.stat().st_size > MAX_MB * 1024 * 1024: raise ValueError(f"{MAX_MB} MB ထက် ကြီးတဲ့ ဖိုင် လက်မခံဘူး") shutil.copyfile(src_in, d / ("input" + ext)) src = d / ("input" + ext) total = probe_duration(src) if total > MAX_DURATION: raise ValueError("၃ မိနစ်ထက် ရှည်တဲ့ ဗီဒီယို လက်မခံဘူး (Space version)") if not has_video_stream(src): raise ValueError("ဗီဒီယိုဖိုင် မဟုတ်ဘူး") if not has_audio_stream(src): raise ValueError("ဒီဗီဒီယိုမှာ အသံမပါဘူး — အသံပါတဲ့ ဗီဒီယိုမှ dub လို့ရမယ်") say("audio", f"video {total:.1f}s") progress(0.05, desc="အသံထုတ်ယူနေတယ်…") yield out(say("audio", "အသံထုတ်ယူနေတယ်…")) wav = d / "audio.wav" extract_audio(src, wav) progress(0.08, desc="စကားပြောမှတ်နေတယ်…") yield out(say("transcribe", "Whisper နဲ့ စကားပြောမှတ်နေတယ်…")) lang_hint = "en" if direction == "en2mm" else "my" segs = transcribe(wav, lang_hint) if not segs: raise ValueError("အသံထဲမှာ စကားပြောသံ မတွေ့ဘူး") utter = merge_segments(segs) say("transcribe", f"{len(segs)} segments → {len(utter)} utterances") progress(0.45, desc="ဘာသာပြန်နေတယ်…") yield out(say("translate", "Gemini နဲ့ ဘာသာပြန်နေတယ်…")) try: texts = translate_lines([u["text"] for u in utter], direction, api_key) say("translate", "Gemini နဲ့ ပြီးပြီ") except Exception as e2: # noqa: BLE001 - Gemini down/quota: local fallback say("translate", f"Gemini ခဏမရလို့ ({clean_err(e2)}) — NLLB (local) နဲ့ ဆက်ဘာသာပြန်တယ်") try: from nllb_translate import translate_lines as nllb_translate_lines texts = nllb_translate_lines([u["text"] for u in utter], direction) say("translate", "NLLB (local) နဲ့ ပြီးပြီ") except Exception: # NLLB model is not bundled on HF: surface the Gemini # error instead of masking it with a missing-model one. raise e2 for u, t in zip(utter, texts): u["text"] = t or u["text"] voice_name = VOICES[direction][voice] set_engine(engine) set_nara_voice(nara_voice) progress(0.58, desc="အသံဖော်နေတယ်…") yield out(say("voice", f"{len(utter)} လိုင်း အသံဖော်နေတယ် ({voice_name})…")) def tts_prog(i, n): progress(0.58 + 0.32 * i / max(n, 1), desc=f"အသံဖော်နေတယ်… {i}/{n}") dub_wav, warp, cues = build_dub_track(utter, voice_name, d, total, tts_prog) slowed = [f for (_a, _b, f) in warp if f > 1.001] if slowed: say("voice", f"စကားပြောသံ သဘာဝကျအောင် scene {len(slowed)} ခု နှေးထားတယ်") progress(0.93, desc="ဗီဒီယိုပေါင်းနေတယ်…") yield out(say("mux", "ဗီဒီယိုပေါင်းနေတယ်…")) ass_path = None srt_path = None if subs: vw, vh = probe_size(src) write_ass(cues, d / "subs.ass", vw, vh, sub_bg or "box") write_srt(cues, d / "subs.srt") ass_path = d / "subs.ass" srt_path = str(d / "subs.srt") say("mux", f"စာတန်း {len(cues)} လိုင်း ထည့်နေတယ်…") mux(src, dub_wav, d / "dubbed.mp4", warp, ass_path) try: cleanup_segments(d) except OSError: pass progress(1.0, desc="ပြီးပြီ ✅") say("done", "ပြီးပြီ ✅ — အောက်က video ကို ကြည့်နိုင် / download ဆွဲနိုင်ပါတယ်") quota_ok() yield out("✅ **ပြီးပြီ!** — အောက်က video ကို ကြည့်နိုင် / download ဆွဲနိုင်ပါတယ်", str(d / "dubbed.mp4"), srt_path) except Exception as e: # noqa: BLE001 - surfaced to UI err = clean_err(e) log.append(f"[{time.strftime('%H:%M:%S')}] FAILED: {type(e).__name__}: {err}") if _is_quota(e): quota_alert("gradio space", err) yield out(f"❌ **{err}**", None, None) finally: # keep the result files; the job dir itself is swept after a day pass # ---- UI ------------------------------------------------------------------- KEY_BANNER = "" if KEY_OK else ( "\n\n> ⚠️ **Server key မရှိသေးဘူး** — Space owner က `GEMINI_API_KEY` ကို " "Space Settings → Secrets မှာ ထည့်ရမယ်။ Key မရှိရင် dub job တွေ အလုပ်မလုပ်ဘူး။\n") with gr.Blocks(title="DubLab — EN↔MM Video Dubbing") as demo: gr.Markdown( "# 🎬 DubLab — English ↔ Burmese Video Dubbing\n" "ဗီဒီယိုအတိုတင်လိုက် → အခြားဘာသာစကားနဲ့ dub လုပ်ပြီးသား video ပြန်ရမယ်။\n" "Free Hugging Face Space — Whisper (local) → Gemini translate → " "Nara Mini အသံ (Shoon / Ko Banyar, local, free).\n\n" "**Limits (Space version):** video ≤ 3 min, ≤ 100 MB. " "ပိုရှည်တဲ့ video တွေအတွက် [Spain site](https://dublab.kbko.qzz.io) " "(10 min / 250 MB) သုံးပါ." + KEY_BANNER) with gr.Row(): with gr.Column(): # Gradio 6: gr.Video has no `type` param — an uploaded video # arrives as a filepath string, which is what we want. video_in = gr.Video(label="🎥 Video file (MP4 / MOV / MKV / WebM)") link_in = gr.Textbox(label="🔗 ဒါမှမဟုတ် video link (YouTube / TikTok / RedNote / Instagram / Facebook / X / Vimeo / Bilibili)", placeholder="https://… — link ထည့်ရင် file မလိုဘူး") direction = gr.Radio(["en2mm", "mm2en"], value="en2mm", label="ဘာသာပြန်လမ်း", info="en2mm = English video → မြန်မာအသံ · mm2en = မြန်မာစကား video → English အသံ") voice = gr.Radio(["female", "male"], value="female", label="အသံ", info="female = Shoon · male = Ko Banyar (Nara Mini local voices)") engine = gr.Radio(["nara", "edge", "gemini"], value="nara", label="အသံထုတ်စက် (engine)", info="nara = local, free (default) · edge = Microsoft · gemini = Gemini TTS") with gr.Row(): subs = gr.Checkbox(label="စာတန်း burn-in လုပ်မလား", value=False) sub_bg = gr.Radio(["box", "none"], value="box", label="စာတန်း နောက်ခံ") submit = gr.Button("🎙️ Dub လုပ်ပါ", variant="primary") with gr.Column(): status = gr.Markdown("အသင့်ဖြစ်ပြီ — video တင် (သို့) link paste လုပ်ပြီး **Dub လုပ်ပါ** ကို နှိပ်ပါ။") video_out = gr.Video(label="✅ Dub လုပ်ပြီးသား video", autoplay=False) srt_out = gr.File(label="📄 Subtitle (.srt) — စာတန်း burn-in ရွေးထားရင် ရမယ်") log_box = gr.Textbox(label="Log", lines=10, interactive=False, autoscroll=True) submit.click( dub_video, inputs=[video_in, link_in, direction, voice, engine, subs, sub_bg], outputs=[status, video_out, srt_out, log_box], ) gr.Markdown( "---\n" "DubLab by [Khant Bhone Ko](https://pro.kxant.org) · MIT license · " "[Source (Docker version)](https://huggingface.co/khantbhko/dublab) · " "[GitHub](https://github.com/khantbhoneko36-commits/dublab) · " "Made with ❤️ for Myanmar creators") demo.queue(max_size=8, default_concurrency_limit=1) if __name__ == "__main__": # Explicit bind: HF Spaces serve on $PORT (7860); local runs use 7860 too. demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")))