Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -3,18 +3,21 @@ import os
|
|
| 3 |
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
| 4 |
|
| 5 |
import spaces # noqa: E402 — must precede torch / transformers
|
| 6 |
-
import
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import numpy as np
|
| 10 |
-
import
|
| 11 |
-
import
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
MODEL_ID = "BuzzASR/persian"
|
| 15 |
SAMPLE_RATE = 16_000
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
CSS = """
|
| 20 |
#col-container { max-width: 1100px; margin: 0 auto; }
|
|
@@ -38,28 +41,58 @@ model = (
|
|
| 38 |
print("Model ready.", flush=True)
|
| 39 |
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if sr != SAMPLE_RATE:
|
|
|
|
| 47 |
wav = torchaudio.functional.resample(wav, sr, SAMPLE_RATE)
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
-
@spaces.GPU(duration=
|
| 52 |
-
def transcribe(audio: str | None) -> str:
|
| 53 |
"""Transcribe Persian (Farsi) speech to text with BuzzASR/persian.
|
| 54 |
|
| 55 |
Args:
|
| 56 |
audio: Path to a recorded or uploaded audio file containing
|
| 57 |
Persian speech.
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
if not audio:
|
| 60 |
raise gr.Error("Record or upload Persian audio first.")
|
| 61 |
|
| 62 |
-
|
| 63 |
if duration < 0.2:
|
| 64 |
raise gr.Error("Audio is too short to transcribe.")
|
| 65 |
if duration > MAX_AUDIO_SECONDS:
|
|
@@ -69,34 +102,42 @@ def transcribe(audio: str | None) -> str:
|
|
| 69 |
)
|
| 70 |
|
| 71 |
t0 = time.perf_counter()
|
| 72 |
-
# Whisper
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
elapsed = time.perf_counter() - t0
|
| 91 |
print(
|
| 92 |
-
f"transcribed {duration:.1f}s audio
|
| 93 |
-
f"
|
| 94 |
flush=True,
|
| 95 |
)
|
| 96 |
return text
|
| 97 |
|
| 98 |
|
| 99 |
-
with gr.Blocks(
|
| 100 |
with gr.Column(elem_id="col-container"):
|
| 101 |
gr.Markdown(
|
| 102 |
"""
|
|
@@ -148,8 +189,9 @@ Record from the microphone or upload a clip, then transcribe.
|
|
| 148 |
gr.Markdown(
|
| 149 |
"""
|
| 150 |
---
|
| 151 |
-
Example clips are from the
|
| 152 |
-
`fa_ir` test set
|
|
|
|
| 153 |
[paper](https://arxiv.org/abs/2609.09554) ·
|
| 154 |
[project page](https://lemn-lab.github.io/buzz-asr/) ·
|
| 155 |
[all 102 language models](https://huggingface.co/BuzzASR).
|
|
@@ -157,4 +199,4 @@ Example clips are from the [FLEURS](https://huggingface.co/datasets/google/fleur
|
|
| 157 |
)
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
-
demo.launch(mcp_server=True)
|
|
|
|
| 3 |
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
| 4 |
|
| 5 |
import spaces # noqa: E402 — must precede torch / transformers
|
| 6 |
+
import subprocess # noqa: E402
|
| 7 |
+
import tempfile # noqa: E402
|
| 8 |
+
import time # noqa: E402
|
| 9 |
|
| 10 |
+
import gradio as gr # noqa: E402
|
| 11 |
+
import numpy as np # noqa: E402
|
| 12 |
+
import soundfile as sf # noqa: E402
|
| 13 |
+
import torch # noqa: E402
|
| 14 |
+
import torchaudio # noqa: E402
|
| 15 |
+
from transformers import WhisperForConditionalGeneration, WhisperProcessor # noqa: E402
|
| 16 |
|
| 17 |
MODEL_ID = "BuzzASR/persian"
|
| 18 |
SAMPLE_RATE = 16_000
|
| 19 |
+
CHUNK_SAMPLES = 30 * SAMPLE_RATE # Whisper's receptive field is 30 s
|
| 20 |
+
MAX_AUDIO_SECONDS = 300 # 5 minutes (transcribed in 30 s windows)
|
| 21 |
|
| 22 |
CSS = """
|
| 23 |
#col-container { max-width: 1100px; margin: 0 auto; }
|
|
|
|
| 41 |
print("Model ready.", flush=True)
|
| 42 |
|
| 43 |
|
| 44 |
+
def _read_audio(filepath: str) -> tuple[np.ndarray, int]:
|
| 45 |
+
"""Decode any audio file to a mono float32 numpy array plus its sample rate."""
|
| 46 |
+
try:
|
| 47 |
+
data, sr = sf.read(filepath, dtype="float32", always_2d=True)
|
| 48 |
+
except Exception:
|
| 49 |
+
# Fallback for containers libsndfile can't read (m4a/aac/…): let
|
| 50 |
+
# ffmpeg normalise it to 16 kHz mono WAV first.
|
| 51 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 52 |
+
out = tmp.name
|
| 53 |
+
try:
|
| 54 |
+
subprocess.run(
|
| 55 |
+
["ffmpeg", "-y", "-i", filepath, "-ac", "1", "-ar",
|
| 56 |
+
str(SAMPLE_RATE), "-f", "wav", out],
|
| 57 |
+
check=True,
|
| 58 |
+
capture_output=True,
|
| 59 |
+
)
|
| 60 |
+
data, sr = sf.read(out, dtype="float32", always_2d=True)
|
| 61 |
+
finally:
|
| 62 |
+
if os.path.exists(out):
|
| 63 |
+
os.unlink(out)
|
| 64 |
+
if data.ndim > 1 and data.shape[1] > 1: # stereo -> mono
|
| 65 |
+
data = data.mean(axis=1)
|
| 66 |
+
else:
|
| 67 |
+
data = data.reshape(-1)
|
| 68 |
+
return np.ascontiguousarray(data, dtype=np.float32), int(sr)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _prepare_audio(filepath: str) -> tuple[np.ndarray, float]:
|
| 72 |
+
"""Load a file to mono 16 kHz float32 and return it with its duration."""
|
| 73 |
+
data, sr = _read_audio(filepath)
|
| 74 |
if sr != SAMPLE_RATE:
|
| 75 |
+
wav = torch.from_numpy(data)
|
| 76 |
wav = torchaudio.functional.resample(wav, sr, SAMPLE_RATE)
|
| 77 |
+
data = wav.numpy().astype(np.float32)
|
| 78 |
+
return data, len(data) / SAMPLE_RATE
|
| 79 |
|
| 80 |
|
| 81 |
+
@spaces.GPU(duration=45)
|
| 82 |
+
def transcribe(audio: str | None = None, progress=gr.Progress()) -> str:
|
| 83 |
"""Transcribe Persian (Farsi) speech to text with BuzzASR/persian.
|
| 84 |
|
| 85 |
Args:
|
| 86 |
audio: Path to a recorded or uploaded audio file containing
|
| 87 |
Persian speech.
|
| 88 |
+
|
| 89 |
+
Returns:
|
| 90 |
+
The Persian transcript as plain text.
|
| 91 |
"""
|
| 92 |
if not audio:
|
| 93 |
raise gr.Error("Record or upload Persian audio first.")
|
| 94 |
|
| 95 |
+
data, duration = _prepare_audio(audio)
|
| 96 |
if duration < 0.2:
|
| 97 |
raise gr.Error("Audio is too short to transcribe.")
|
| 98 |
if duration > MAX_AUDIO_SECONDS:
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
t0 = time.perf_counter()
|
| 105 |
+
# Whisper only sees 30 s at a time, so anything longer is transcribed in
|
| 106 |
+
# consecutive 30 s windows and the pieces are concatenated.
|
| 107 |
+
windows = [
|
| 108 |
+
data[i : i + CHUNK_SAMPLES] for i in range(0, len(data), CHUNK_SAMPLES)
|
| 109 |
+
]
|
| 110 |
+
pieces = []
|
| 111 |
+
for k, window in enumerate(windows):
|
| 112 |
+
if len(windows) > 1:
|
| 113 |
+
progress((k, len(windows)), desc=f"Transcribing {k + 1}/{len(windows)}…")
|
| 114 |
+
feats = processor(
|
| 115 |
+
window, sampling_rate=SAMPLE_RATE, return_tensors="pt"
|
| 116 |
+
).input_features.to("cuda").half()
|
| 117 |
+
with torch.no_grad():
|
| 118 |
+
# Decoding params from the model card: greedy decoding with light
|
| 119 |
+
# anti-repetition shaping.
|
| 120 |
+
ids = model.generate(
|
| 121 |
+
feats,
|
| 122 |
+
num_beams=1,
|
| 123 |
+
no_repeat_ngram_size=3,
|
| 124 |
+
repetition_penalty=1.2,
|
| 125 |
+
)
|
| 126 |
+
piece = processor.batch_decode(ids, skip_special_tokens=True)[0].strip()
|
| 127 |
+
if piece:
|
| 128 |
+
pieces.append(piece)
|
| 129 |
+
|
| 130 |
+
text = " ".join(pieces)
|
| 131 |
elapsed = time.perf_counter() - t0
|
| 132 |
print(
|
| 133 |
+
f"transcribed {duration:.1f}s audio ({len(windows)} window(s)) in "
|
| 134 |
+
f"{elapsed:.1f}s chars={len(text)}",
|
| 135 |
flush=True,
|
| 136 |
)
|
| 137 |
return text
|
| 138 |
|
| 139 |
|
| 140 |
+
with gr.Blocks(title="BuzzASR — Persian ASR") as demo:
|
| 141 |
with gr.Column(elem_id="col-container"):
|
| 142 |
gr.Markdown(
|
| 143 |
"""
|
|
|
|
| 189 |
gr.Markdown(
|
| 190 |
"""
|
| 191 |
---
|
| 192 |
+
Example clips are short utterances from the
|
| 193 |
+
[FLEURS](https://huggingface.co/datasets/google/fleurs) `fa_ir` test set
|
| 194 |
+
(CC-BY). Model by the BuzzASR team —
|
| 195 |
[paper](https://arxiv.org/abs/2609.09554) ·
|
| 196 |
[project page](https://lemn-lab.github.io/buzz-asr/) ·
|
| 197 |
[all 102 language models](https://huggingface.co/BuzzASR).
|
|
|
|
| 199 |
)
|
| 200 |
|
| 201 |
if __name__ == "__main__":
|
| 202 |
+
demo.launch(mcp_server=True, theme=gr.themes.Citrus(), css=CSS)
|