Spaces:
Runtime error
Runtime error
File size: 6,245 Bytes
c2264a9 8b349e6 c2264a9 8b349e6 c2264a9 8b349e6 c2264a9 8b349e6 c2264a9 5f4bc28 | 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 | """Milo-ASR: Danish Speech Recognition - Hugging Face Space."""
import base64
import io
import tempfile
import time
import gradio as gr
import numpy as np
from scipy.io.wavfile import write as wav_write
MODEL_ID = "pluttodk/Milo-ASR"
ALIGNER_ID = "Qwen/Qwen3-ForcedAligner-0.6B"
_model = None
_model_ts = None
def _load_model(with_timestamps: bool):
global _model, _model_ts
from qwen_asr import Qwen3ASRModel
if with_timestamps:
if _model_ts is None:
_model_ts = Qwen3ASRModel.from_pretrained(
MODEL_ID,
dtype="float32",
device_map="cpu",
forced_aligner=ALIGNER_ID,
forced_aligner_kwargs=dict(
dtype="float32",
device_map="cpu",
),
)
return _model_ts
else:
if _model is None:
_model = Qwen3ASRModel.from_pretrained(
MODEL_ID,
dtype="float32",
device_map="cpu",
)
return _model
def _normalize_audio(wav):
x = np.asarray(wav, dtype=np.float32)
if x.ndim > 1:
x = np.mean(x, axis=-1)
m = np.max(np.abs(x)) if x.size else 0.0
if m > 1.0 + 1e-6:
x = x / m
return np.clip(x, -1.0, 1.0)
def _make_timestamp_html(sr, audio, timestamps):
if not timestamps:
return ""
html = """
<style>
.ts-container { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.ts-box {
border: 1px solid #ddd; border-radius: 8px; padding: 8px 12px;
background: #f9f9f9; box-shadow: 0 1px 3px rgba(0,0,0,0.06);
text-align: center;
}
.ts-word { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
.ts-time { font-size: 11px; color: #666; margin-bottom: 6px; }
.ts-audio audio { width: 120px; height: 28px; }
</style>
<details open>
<summary style="font-weight: 700; cursor: pointer; margin-bottom: 8px;">
Word-level Timestamps (click to play each segment)
</summary>
<div class="ts-container">
"""
for item in timestamps:
word = item["text"]
start = float(item["start_time"])
end = float(item["end_time"])
if end <= start:
continue
s_idx = max(0, int(start * sr))
e_idx = min(len(audio), int(end * sr))
if e_idx <= s_idx:
continue
seg = (np.clip(audio[s_idx:e_idx], -1.0, 1.0) * 32767).astype(np.int16)
buf = io.BytesIO()
wav_write(buf, sr, seg)
b64 = base64.b64encode(buf.getvalue()).decode()
html += f"""
<div class="ts-box">
<div class="ts-word">{word}</div>
<div class="ts-time">{start:.2f}s - {end:.2f}s</div>
<div class="ts-audio">
<audio controls preload="none" src="data:audio/wav;base64,{b64}"></audio>
</div>
</div>
"""
html += "</div></details>"
return html
def transcribe(audio, use_timestamps):
if audio is None:
return "Please upload or record an audio file.", "", ""
sr, raw = audio
normalized = _normalize_audio(raw)
# Write to temp WAV file for the model
int16_data = (normalized * 32767).astype(np.int16)
tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
wav_write(tmp.name, sr, int16_data)
tmp.close()
t0 = time.perf_counter()
model = _load_model(with_timestamps=use_timestamps)
load_time = time.perf_counter() - t0
t1 = time.perf_counter()
results = model.transcribe(
audio=tmp.name,
language="Danish",
return_time_stamps=use_timestamps,
)
inference_time = time.perf_counter() - t1
r = results[0]
text = getattr(r, "text", "") or ""
info = f"Inference: {inference_time:.1f}s"
if load_time > 1.0:
info += f" (model load: {load_time:.1f}s)"
ts_html = ""
if use_timestamps and hasattr(r, "time_stamps") and r.time_stamps:
ts_data = [
{
"text": getattr(t, "text", ""),
"start_time": getattr(t, "start_time", 0),
"end_time": getattr(t, "end_time", 0),
}
for t in r.time_stamps.items
]
ts_html = _make_timestamp_html(sr, normalized, ts_data)
return text, info, ts_html
theme = gr.themes.Soft(
font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"],
)
with gr.Blocks(theme=theme, title="Milo-ASR") as demo:
gr.Markdown(
"""
# Milo-ASR - Danish Speech Recognition
**Model:** [`pluttodk/Milo-ASR`](https://huggingface.co/pluttodk/Milo-ASR) (finetuned Qwen3-ASR-1.7B)
Upload an audio file or record with your microphone to transcribe Danish speech.
Running on CPU -- the first request will be slow while the model loads, and inference takes longer than on GPU.
"""
)
with gr.Row():
with gr.Column(scale=1):
audio_in = gr.Audio(
label="Audio",
sources=["upload", "microphone"],
type="numpy",
)
ts_checkbox = gr.Checkbox(
label="Word-level timestamps",
value=False,
info="Uses Qwen3-ForcedAligner for word alignment",
)
btn = gr.Button("Transcribe", variant="primary", size="lg")
with gr.Column(scale=1):
out_text = gr.Textbox(
label="Transcription",
lines=6,
show_copy_button=True,
interactive=False,
)
out_info = gr.Textbox(
label="Info",
lines=1,
interactive=False,
)
out_ts = gr.HTML()
btn.click(
fn=transcribe,
inputs=[audio_in, ts_checkbox],
outputs=[out_text, out_info, out_ts],
)
gr.Markdown(
"""
---
**Links:** [Model Card](https://huggingface.co/pluttodk/Milo-ASR) |
Based on [Qwen3-ASR-1.7B](https://huggingface.co/Qwen/Qwen3-ASR-1.7B) finetuned on CoRal v2 Danish speech data.
"""
)
if __name__ == "__main__":
demo.launch(ssr_mode=False)
|