"""rumblex demo: transcription with word timestamps and subtitles from Reson8/Rumble-2-Flash.""" from __future__ import annotations import html import json import os import tempfile import time import wave from pathlib import Path from urllib.parse import quote import gradio as gr import numpy as np import torch import rumblex from rumblex.audio import load_audio from rumblex.writers import WriterOptions ZERO_GPU = os.environ.get("SPACES_ZERO_GPU", "").lower() in ("1", "true") ON_GPU = ZERO_GPU or torch.cuda.is_available() MAX_SECONDS = float(os.environ.get("MAX_SECONDS", "600" if ON_GPU else "120")) SR = 16000 PX_PER_SECOND = 110 # width of one second on the word timeline LANGUAGES = ["Auto-detect", "English", "Dutch", "German", "French", "Spanish", "Italian", "Portuguese"] SPEAKER_COLOURS = ["#6366f1", "#d97706", "#059669", "#db2777"] OUT_ROOT = Path(tempfile.gettempdir()) / "rumblex_demo" # served to the browser (allowed_paths below) OUT_ROOT.mkdir(exist_ok=True) def gpu_seconds(audio_path, language=None, line_width=None, progress=None) -> int: """GPU time to reserve on ZeroGPU. Each call uses up this much of the visitor's daily quota, so short clips ask for little (logged-out visitors only get a few minutes).""" try: seconds = min(len(load_audio(audio_path)) / SR, MAX_SECONDS) except Exception: return 60 return int(min(120, 20 + seconds / 10)) try: # Hugging Face's ZeroGPU helper; a no-op outside ZeroGPU hardware import spaces gpu = spaces.GPU(duration=gpu_seconds) except ImportError: def gpu(fn): return fn # On ZeroGPU the model is placed on cuda at import time (the GPU is attached inside @gpu calls). The dtype is # given explicitly so nothing queries the device before a GPU is attached. DEVICE = "cuda" if ON_GPU else "cpu" MODEL = rumblex.load_model( device=DEVICE, compute_type="bfloat16" if ON_GPU else "float32", batch_size=16 if ON_GPU else 4 ) # Player, word timeline and karaoke transcript. Event delegation on the document, so it works for every # result Gradio renders. A requestAnimationFrame loop repaints while playing (timeupdate is only ~4 Hz). HEAD = """ """ HERO = """

rumblex

Word-level timestamps from Reson8/Rumble-2-Flash, aligned with the model's own CTC head. Upload a file or record yourself, then press play and watch each word light up as it is spoken.

""" EXAMPLE = str(Path(__file__).with_name("librispeech_mr_quilter.wav")) EMPTY = ( '
Your transcript, word timeline and player will appear here.
' "No audio at hand? Click Try an example clip on the left.
" ) FOOTER = """ The demo transcribes the first {limit} of each file. Speaker labels (pyannote) are in the package, not in this demo. rumblex is an independent project and is not affiliated with Reson8. """.replace("{limit}", f"{MAX_SECONDS / 60:.0f} minutes") def fmt(t: float) -> str: return f"{int(t // 60)}:{int(t % 60):02d}" def write_wav(path: Path, audio: np.ndarray) -> None: pcm = (np.clip(audio, -1, 1) * 32767).astype(" str: """Player, word timeline and transcript; every word carries its times and lights up while spoken.""" speakers = sorted({s.speaker for s in result.segments if s.speaker}) colour = {s: SPEAKER_COLOURS[i % len(SPEAKER_COLOURS)] for i, s in enumerate(speakers)} rows, boxes = [], [] for seg in result.segments: words = seg.words or [] spans = " ".join( f'{html.escape(w.word.strip())}' for w in words ) or html.escape(seg.text) label = ( f'{html.escape(seg.speaker)} ' if seg.speaker else "" ) rows.append(f'
{fmt(seg.start)}{label}{spans}
') for w in words: left, width = w.start * PX_PER_SECOND, max((w.end - w.start) * PX_PER_SECOND, 6) boxes.append( f'' f"{html.escape(w.word.strip())}" ) chips = "".join(f"{html.escape(s)}" for s in stats) width = max(duration * PX_PER_SECOND, 1) return ( f'
' f'
{chips}
' f'' f'
' f'0:00 / {fmt(duration)}' f'
' f'
{"".join(boxes)}' f'
' f'
{"".join(rows)}
' ) @gpu def run(audio_path, language, line_width, progress=gr.Progress()): if not audio_path: raise gr.Error("Upload or record some audio first.") wav = load_audio(audio_path) note = "" if len(wav) > MAX_SECONDS * SR: wav = wav[: int(MAX_SECONDS * SR)] where = "a shared GPU" if ON_GPU else "a shared CPU" note = f"Only the first {MAX_SECONDS / 60:.0f} min were transcribed (this demo runs on {where})." kwargs = {} if language == "Auto-detect" else {"language": language} progress(0, desc="Transcribing") t0 = time.perf_counter() result = rumblex.transcribe( np.asarray(wav, dtype=np.float32), MODEL, progress=lambda done, total: progress(done / max(total, 1), desc="Transcribing"), **kwargs, ) took = time.perf_counter() - t0 audio_s = len(wav) / SR # On short clips the fixed start-up cost dominates, so a real-time factor would mislead. speed = f"done in {took:.1f} s" if audio_s >= 30: speed += f" ({audio_s / max(took, 1e-6):.0f}× real time)" return show(result, wav, Path(audio_path).stem or "audio", line_width, speed, note) def show(result, wav, stem: str, line_width, speed: str, note: str = ""): """Write the subtitle files and build the player, word table and status for a transcript.""" out_dir = Path(tempfile.mkdtemp(dir=OUT_ROOT)) opts = WriterOptions(max_line_width=int(line_width) if line_width else None) files = rumblex.write_result(result, f"{stem}.wav", out_dir, ["srt", "vtt", "json"], opts) played = out_dir / "audio.wav" write_wav(played, wav) words = [ [w.word.strip(), round(w.start, 2), round(w.end, 2)] for s in result.segments for w in (s.words or []) ] audio_s = len(wav) / SR stats = [f"{audio_s:.1f} s of audio", speed, f"language: {result.language}", f"{len(words)} words"] status = note or "Press ▶: each word lights up as it is spoken. Click any word to jump to it." view = result_html(result, f"/gradio_api/file={quote(str(played))}", audio_s, stats) return status, view, words, [str(p) for p in files] # The example's transcript ships with the Space (made with make_example.py on CPU), so trying the example # needs no GPU: it works for every visitor and uses none of their ZeroGPU quota. EXAMPLE_RESULT = Path(__file__).with_name("example_result.json") def run_example(line_width): result = rumblex.TranscriptResult.from_dict(json.loads(EXAMPLE_RESULT.read_text(encoding="utf-8"))) wav = load_audio(EXAMPLE) return show(result, wav, Path(EXAMPLE).stem, line_width, "precomputed, no GPU needed") THEME = gr.themes.Soft( primary_hue="indigo", font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"] ) with gr.Blocks(title="rumblex: word timestamps for Rumble-2-Flash") as demo: gr.HTML(HERO) with gr.Row(equal_height=False): with gr.Column(scale=2, min_width=300): audio = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio") language = gr.Dropdown(LANGUAGES, value="Auto-detect", label="Language", allow_custom_value=True) example = gr.Button("▶ No audio handy? Try an example clip", variant="primary", size="lg") go = gr.Button("Transcribe again (e.g. after changing the language)", variant="secondary", size="sm") gr.Markdown("The example is a 6 s LibriSpeech recording (CC BY 4.0).") with gr.Column(scale=3, min_width=360): status = gr.Markdown() transcript = gr.HTML(EMPTY) with gr.Accordion("Download subtitles and JSON", open=False): width = gr.Slider( 0, 80, value=42, step=1, label="Subtitle line width (0 = one cue per segment)" ) files = gr.File(label="SRT, VTT and JSON", file_count="multiple") table = gr.Dataframe(headers=["word", "start (s)", "end (s)"], label="Word timestamps") gr.Markdown(FOOTER) outputs = [status, transcript, table, files] go.click(run, [audio, language, width], outputs) # Uploads and microphone recordings are transcribed straight away, without pressing a button. audio.upload(run, [audio, language, width], outputs) audio.stop_recording(run, [audio, language, width], outputs) # One click: load the example clip into the audio box and transcribe it. example.click(lambda: (EXAMPLE, "Auto-detect"), None, [audio, language]).then( run_example, [width], outputs ) if __name__ == "__main__": demo.queue(default_concurrency_limit=1).launch(head=HEAD, allowed_paths=[str(OUT_ROOT)], theme=THEME)