multimodalart HF Staff commited on
Commit
4fe52c8
·
verified ·
1 Parent(s): 14585c9

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +83 -41
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 time
 
 
7
 
8
- import gradio as gr
9
- import numpy as np
10
- import torch
11
- import torchaudio
12
- from transformers import WhisperForConditionalGeneration, WhisperProcessor
 
13
 
14
  MODEL_ID = "BuzzASR/persian"
15
  SAMPLE_RATE = 16_000
16
- MAX_AUDIO_SECONDS = 300 # 5 minutes, plenty for a demo clip
17
- CHUNK_SECONDS = 30
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 _load_audio_16k(filepath: str) -> tuple[torch.Tensor, float]:
42
- """Load any audio file to a mono 16 kHz float32 tensor and its duration."""
43
- wav, sr = torchaudio.load(filepath)
44
- if wav.dim() > 1 and wav.shape[0] > 1:
45
- wav = wav.mean(dim=0, keepdim=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  if sr != SAMPLE_RATE:
 
47
  wav = torchaudio.functional.resample(wav, sr, SAMPLE_RATE)
48
- return wav[0], float(wav.shape[-1]) / SAMPLE_RATE
 
49
 
50
 
51
- @spaces.GPU(duration=90)
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
- wav, duration = _load_audio_16k(audio)
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's feature extractor expects raw sample arrays.
73
- feats = processor(
74
- wav.numpy(),
75
- sampling_rate=SAMPLE_RATE,
76
- return_tensors="pt",
77
- ).input_features.to("cuda").half()
78
-
79
- with torch.no_grad():
80
- # Decoding params from the model card: greedy with light
81
- # anti-repetition shaping. Language/task prompt comes from
82
- # generation_config (baked in).
83
- ids = model.generate(
84
- feats,
85
- num_beams=1,
86
- no_repeat_ngram_size=3,
87
- repetition_penalty=1.2,
88
- )
89
- text = processor.batch_decode(ids, skip_special_tokens=True)[0].strip()
 
 
 
 
 
 
 
 
90
  elapsed = time.perf_counter() - t0
91
  print(
92
- f"transcribed {duration:.1f}s audio in {elapsed:.1f}s "
93
- f"(rtf={elapsed / max(duration, 1e-6):.2f}) chars={len(text)}",
94
  flush=True,
95
  )
96
  return text
97
 
98
 
99
- with gr.Blocks(theme=gr.themes.Citrus(), css=CSS, title="BuzzASR — Persian ASR") as demo:
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 [FLEURS](https://huggingface.co/datasets/google/fleurs)
152
- `fa_ir` test set (CC-BY). Model by the BuzzASR team —
 
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)