# coding=utf-8 # Copyright 2026 The Alibaba Qwen team. # SPDX-License-Identifier: Apache-2.0 """ Qwen3-ASR Demo for Huggingface Spaces with ZeroGPU support. Showcases the 1.7B model with timestamp visualization. """ import base64 import io import os from typing import Any, Dict, List, Optional, Tuple, Union import gradio as gr import numpy as np import spaces import torch from scipy.io.wavfile import write as wav_write from qwen_asr.inference.utils import ( MAX_ASR_INPUT_SECONDS, MAX_FORCE_ALIGN_INPUT_SECONDS, SAMPLE_RATE, normalize_audio_input, split_audio_into_chunks, ) """ No explicit Hugging Face auth is required here: - `ozlabs/qwen3-asr-hebrew` is a public model - Spaces automatically provide an anonymous token for public access If you ever gate the repo, you can reintroduce `huggingface_hub.login` with HF_TOKEN. """ def _title_case_display(s: str) -> str: s = (s or "").strip() s = s.replace("_", " ") return " ".join([w[:1].upper() + w[1:] if w else "" for w in s.split()]) def _build_choices_and_map(items: Optional[List[str]]) -> Tuple[List[str], Dict[str, str]]: if not items: return [], {} display = [_title_case_display(x) for x in items] mapping = {d: r for d, r in zip(display, items)} return display, mapping def _normalize_audio(wav, eps=1e-12, clip=True): x = np.asarray(wav) if np.issubdtype(x.dtype, np.integer): info = np.iinfo(x.dtype) if info.min < 0: y = x.astype(np.float32) / max(abs(info.min), info.max) else: mid = (info.max + 1) / 2.0 y = (x.astype(np.float32) - mid) / mid elif np.issubdtype(x.dtype, np.floating): y = x.astype(np.float32) m = np.max(np.abs(y)) if y.size else 0.0 if m > 1.0 + 1e-6: y = y / (m + eps) else: raise TypeError(f"Unsupported dtype: {x.dtype}") if clip: y = np.clip(y, -1.0, 1.0) if y.ndim > 1: y = np.mean(y, axis=-1).astype(np.float32) return y def _audio_to_tuple(audio: Any) -> Optional[Tuple[np.ndarray, int]]: """ Accept gradio audio: - {"sampling_rate": int, "data": np.ndarray} - (sr, np.ndarray) [some gradio versions] Return: (wav_float32_mono, sr) """ if audio is None: return None if isinstance(audio, dict) and "sampling_rate" in audio and "data" in audio: sr = int(audio["sampling_rate"]) wav = _normalize_audio(audio["data"]) return wav, sr if isinstance(audio, tuple) and len(audio) == 2: a0, a1 = audio if isinstance(a0, int): sr = int(a0) wav = _normalize_audio(a1) return wav, sr if isinstance(a1, int): wav = _normalize_audio(a0) sr = int(a1) return wav, sr return None def _parse_audio_any(audio: Any) -> Union[str, Tuple[np.ndarray, int]]: if audio is None: raise ValueError("Audio is required.") at = _audio_to_tuple(audio) if at is not None: return at raise ValueError("Unsupported audio input format.") def _make_timestamp_html(audio_upload: Any, timestamps: Any) -> str: """ Build HTML with per-token audio slices, using base64 data URLs. """ at = _audio_to_tuple(audio_upload) if at is None: return "
No audio available for visualization.
" audio, sr = at if not timestamps: return "
No timestamps to visualize.
" if not isinstance(timestamps, list): return "
Invalid timestamp format.
" html_content = """ """ html_content += """
Timestamps Visualization (click each word to hear the audio segment)
""" for item in timestamps: if not isinstance(item, dict): continue word = str(item.get("text", "") or "") start = item.get("start_time", None) end = item.get("end_time", None) if start is None or end is None: continue start = float(start) end = float(end) if end <= start: continue start_sample = max(0, int(start * sr)) end_sample = min(len(audio), int(end * sr)) if end_sample <= start_sample: continue seg = audio[start_sample:end_sample] seg_i16 = (np.clip(seg, -1.0, 1.0) * 32767.0).astype(np.int16) mem = io.BytesIO() wav_write(mem, sr, seg_i16) mem.seek(0) b64 = base64.b64encode(mem.read()).decode("utf-8") audio_src = f"data:audio/wav;base64,{b64}" html_content += f"""
{word}
{start:.3f}s - {end:.3f}s
""" html_content += "
" return html_content from qwen_asr import Qwen3ASRModel # Instantiate Hebrew-finetuned ASR model (still multilingual, but optimized for Hebrew) asr = Qwen3ASRModel.from_pretrained( "ozlabs/qwen3-asr-hebrew", dtype=torch.bfloat16, device_map="cuda", forced_aligner="Qwen/Qwen3-ForcedAligner-0.6B", forced_aligner_kwargs=dict( dtype=torch.bfloat16, device_map="cuda", ), max_inference_batch_size=16, attn_implementation="kernels-community/flash-attn3", ) # UI language choices – Hebrew should be a first-class option. SUPPORTED_LANGUAGES = [ "Hebrew", # surfaced first in the dropdown (after Auto) "Chinese", "Cantonese", "English", "Arabic", "German", "French", "Spanish", "Portuguese", "Indonesian", "Italian", "Korean", "Russian", "Thai", "Vietnamese", "Japanese", "Turkish", "Hindi", "Malay", "Dutch", "Swedish", "Danish", "Finnish", "Polish", "Czech", "Filipino", "Persian", "Greek", "Romanian", "Hungarian", "Macedonian", ] lang_choices_disp, lang_map = _build_choices_and_map(SUPPORTED_LANGUAGES) lang_choices = ["Auto"] + lang_choices_disp # Derived limits for display / streaming behavior MAX_LONG_AUDIO_MIN = MAX_ASR_INPUT_SECONDS // 60 MAX_ALIGN_SEC = MAX_FORCE_ALIGN_INPUT_SECONDS # Target streaming chunk length in seconds (must be <= MAX_ASR_INPUT_SECONDS) STREAM_CHUNK_SEC = 60.0 @spaces.GPU def transcribe(audio_upload: Any, lang_disp: str, return_ts: bool, progress=gr.Progress(track_tqdm=True)): """ Streaming transcription with explicit chunking. - Streams partial text back as audio chunks are processed. - Optionally runs forced alignment once at the end to produce timestamps. """ if audio_upload is None: yield "", "", None, "
Please upload an audio file first.
" return # Convert Gradio audio to (wav, sr) at = _audio_to_tuple(audio_upload) if at is None: yield "", "", None, "
Unsupported audio input format.
" return wav_raw, sr_raw = at # Normalize + resample to 16k mono in [-1, 1] using library utilities wav_16k = normalize_audio_input((wav_raw, int(sr_raw))) # Decide language hint language = None if lang_disp and lang_disp != "Auto": language = lang_map.get(lang_disp, lang_disp) # Split long audio into streaming chunks (using same splitter as core library) parts = split_audio_into_chunks( wav=wav_16k, sr=SAMPLE_RATE, max_chunk_sec=min(STREAM_CHUNK_SEC, float(MAX_ASR_INPUT_SECONDS)), ) # Ensure list for progress / multiple passes chunks = list(parts) if not chunks: yield "", "", None, "
Empty audio after preprocessing.
" return full_text: str = "" langs_seen: List[str] = [] # Stream chunk-level ASR (no timestamps here) for (chunk_wav, _offset_sec) in progress.tqdm(chunks, desc="Transcribing chunks"): results = asr.transcribe( audio=(chunk_wav, SAMPLE_RATE), language=language, return_time_stamps=False, ) if not isinstance(results, list) or len(results) != 1: continue r = results[0] chunk_text = getattr(r, "text", "") or "" chunk_lang = getattr(r, "language", "") or "" full_text += chunk_text if chunk_lang: langs_seen.append(chunk_lang) # Simple ordered unique merge of languages merged_lang = ",".join(dict.fromkeys([x for x in langs_seen if x])) # Stream text only; timestamps (if requested) are resolved in a final pass yield merged_lang, full_text, None, "" # Optional final forced alignment over the full audio and merged transcript ts_payload = None merged_lang = ",".join(dict.fromkeys([x for x in langs_seen if x])) final_lang = merged_lang or (language or "") if return_ts and getattr(asr, "forced_aligner", None) is not None and full_text.strip(): try: align_results = asr.forced_aligner.align( audio=[(wav_16k, SAMPLE_RATE)], text=[full_text], language=[final_lang or "Hebrew"], ) if align_results: ar = align_results[0] ts_payload = [ dict( text=getattr(t, "text", ""), start_time=getattr(t, "start_time", 0), end_time=getattr(t, "end_time", 0), ) for t in getattr(ar, "items", []) or [] ] except Exception as e: # Alignment failure should not kill the transcript print(f"[WARN] Forced alignment failed: {e}") # Final yield with transcript + (optional) timestamps yield final_lang, full_text, ts_payload, "" def visualize_timestamps(audio_upload: Any, timestamps_json: Any): """Generate timestamp visualization from existing results.""" if timestamps_json is None: return "
No timestamps available. Please run transcription with timestamps enabled first.
" return _make_timestamp_html(audio_upload, timestamps_json) # Build Gradio interface theme = gr.themes.Soft(primary_hue="orange", neutral_hue="slate") css = """ :root { --oz-bg: #faf9f4; --oz-fg: #111827; --oz-muted: #6b7280; --oz-primary: lab(54.5081% 39.1499 38.3555); --oz-accent: #0f172a; --oz-card: #ffffff; --oz-border: #e5e7eb; } .gradio-container { max-width: none !important; background-color: var(--oz-bg) !important; color: var(--oz-fg) !important; font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif !important; } .gradio-container h1, .gradio-container h2, .gradio-container h3 { color: var(--oz-fg) !important; letter-spacing: 0.04em; } .gradio-container p, .gradio-container label { color: var(--oz-muted) !important; } .oz-topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; padding: 8px 4px; } .oz-logo { font-weight: 600; font-size: 0.95rem; letter-spacing: 0.18em; text-transform: uppercase; color: var(--oz-accent); } .oz-nav-link { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.18em; text-align: right; } .long-audio-note { font-size: 0.9rem; color: var(--oz-muted); } .oz-main { max-width: 1100px; margin: 0 auto 40px auto; gap: 16px; } .oz-card { background-color: var(--oz-card); border-radius: 18px; border: 1px solid var(--oz-border); padding: 16px 20px; box-shadow: 0 14px 40px rgba(15, 23, 42, 0.06); } .oz-card h3 { margin-top: 0; } .oz-pill-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; } .oz-pill { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.16em; padding: 4px 10px; border-radius: 999px; border: 1px solid rgba(148, 163, 184, 0.6); color: var(--oz-muted); } .oz-json-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; } .gradio-container .gr-button-primary, .gradio-container button.primary { background: var(--oz-primary) !important; border-radius: 999px !important; border: 1px solid var(--oz-primary) !important; color: #ffffff !important; font-weight: 500; } .gradio-container .gr-button-secondary, .gradio-container button.secondary { background: transparent !important; border-radius: 999px !important; border: 1px solid var(--oz-border) !important; color: var(--oz-accent) !important; } .gradio-container .gr-markdown a { color: var(--oz-accent) !important; text-decoration: underline; text-underline-offset: 0.2em; } /* Kill leftover Gradio purple/violet accents inside the app */ .gradio-container .text-purple-500, .gradio-container .text-violet-500, .gradio-container .text-indigo-500 { color: var(--oz-accent) !important; } .gradio-container .border-purple-500, .gradio-container .border-violet-500, .gradio-container .border-indigo-500 { border-color: var(--oz-primary) !important; } .gradio-container .bg-purple-500, .gradio-container .bg-violet-500, .gradio-container .bg-indigo-500, .gradio-container .from-purple-500, .gradio-container .from-violet-500, .gradio-container .from-indigo-500, .gradio-container .to-purple-500, .gradio-container .to-violet-500, .gradio-container .to-indigo-500 { background-color: var(--oz-primary) !important; background-image: none !important; } """ with gr.Blocks(theme=theme, css=css, title="Qwen3-ASR Hebrew Demo") as demo: with gr.Row(elem_classes=["oz-topbar"]): gr.Markdown("**OZ LABS** · Sovereign AI", elem_classes=["oz-logo"]) gr.Markdown("[BACK TO OZLABS.AI](https://ozlabs.ai)", elem_classes=["oz-nav-link"]) with gr.Column(elem_classes=["oz-main"]): gr.Markdown( f""" # Qwen3-ASR Hebrew Demo Hebrew-optimised Qwen3 ASR, running entirely inside this Space. Upload long Hebrew audio (up to ~{MAX_LONG_AUDIO_MIN} minutes) and get full-text transcripts and word-level timestamps. """ ) with gr.Row(): with gr.Column(scale=2, elem_classes=["oz-card"]): gr.Markdown( """ ### Input Use a single long file or record directly from the browser. """ ) audio_in = gr.Audio( label="Audio", type="numpy", sources=["upload", "microphone"], ) gr.Markdown( f"**Long audio:** up to ~{MAX_LONG_AUDIO_MIN} minutes per file. " f"We automatically chunk, transcribe, and stitch results.", elem_classes=["long-audio-note"], ) lang_in = gr.Dropdown( label="Language (keep 'Auto' for best results in Hebrew)", choices=lang_choices, value="Hebrew", interactive=True, ) ts_in = gr.Checkbox( label=f"Return timestamps (alignment chunks are ~{MAX_ALIGN_SEC}s)", value=True, ) btn = gr.Button("Transcribe", variant="primary", size="lg") with gr.Column(scale=3, elem_classes=["oz-card"]): gr.Markdown("### Transcript") with gr.Row(elem_classes=["oz-pill-row"]): out_lang = gr.Textbox( label="Detected Language", lines=1, interactive=False, ) out_text = gr.Textbox( label="Transcription", lines=10, interactive=False, ) with gr.Row(): with gr.Column(scale=2, elem_classes=["oz-card"]): with gr.Row(elem_classes=["oz-json-header"]): gr.Markdown("### Timestamps JSON") viz_btn = gr.Button("Visualize", variant="secondary") out_ts = gr.JSON(label="", value=None) with gr.Column(scale=3, elem_classes=["oz-card"]): gr.Markdown("### Timeline") out_ts_html = gr.HTML() # Event handlers btn.click( transcribe, inputs=[audio_in, lang_in, ts_in], outputs=[out_lang, out_text, out_ts, out_ts_html], ) viz_btn.click( visualize_timestamps, inputs=[audio_in, out_ts], outputs=[out_ts_html], ) gr.Markdown( """ --- **Links:** [ozlabs Hebrew fine-tuned model](https://huggingface.co/ozlabs/qwen3-asr-hebrew) | [Qwen3-ASR collection](https://huggingface.co/collections/Qwen/qwen3-asr) | [Upstream GitHub](https://github.com/QwenLM/Qwen3-ASR) """ ) if __name__ == "__main__": demo.launch()