fihria commited on
Commit
9e259e6
·
verified ·
1 Parent(s): a3b63ee

Upload transcribe.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. transcribe.py +196 -0
transcribe.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Transcribe audio with Qwen3-ASR Darija GGUF via a running llama-server.
3
+
4
+ Start the server first:
5
+
6
+ llama-server \
7
+ -m qwen3-asr-darija-0.6b-Q4_K_M.gguf \
8
+ --mmproj mmproj-qwen3-asr-darija-0.6b-BF16.gguf \
9
+ --no-mmproj-offload -ngl 0 -c 8192 \
10
+ --host 127.0.0.1 --port 8080
11
+
12
+ Then:
13
+
14
+ python3 transcribe.py --audio sample.wav
15
+ python3 transcribe.py --input-jsonl clips.jsonl --output results.jsonl
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import argparse
21
+ import base64
22
+ import json
23
+ import re
24
+ import sys
25
+ import urllib.request
26
+ from pathlib import Path
27
+ from typing import Any
28
+
29
+ PROMPT = "Transcribe the speech in the original language."
30
+
31
+ # The model emits "language <Name>" followed by the transcript, sometimes with an
32
+ # <asr_text> tag and sometimes concatenated straight onto the Arabic text. The
33
+ # language name is ASCII, so match [A-Za-z]+ to avoid swallowing the first word.
34
+ ASR_OUTPUT_RE = re.compile(r"language\s+([A-Za-z]+)\s*(?:<asr_text>)?\s*(.*)", re.DOTALL)
35
+
36
+ try:
37
+ from asr_guards import has_speech, scrub_hallucination
38
+ except ImportError: # allow running from another cwd if asr_guards.py is beside this file
39
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
40
+ from asr_guards import has_speech, scrub_hallucination
41
+
42
+
43
+ def parse_output(raw: str) -> dict[str, Any]:
44
+ raw = raw.strip()
45
+ match = ASR_OUTPUT_RE.search(raw)
46
+ if match:
47
+ language, transcription = match.group(1), match.group(2).strip()
48
+ else:
49
+ language, transcription = None, raw
50
+ scrubbed = scrub_hallucination(transcription)
51
+ row = {"language": language, "transcription": scrubbed, "raw": raw}
52
+ if scrubbed != transcription:
53
+ row["suppressed"] = "known_hallucination"
54
+ return row
55
+
56
+
57
+ def default_model(base_url: str, timeout: int) -> str:
58
+ req = urllib.request.Request(base_url.rstrip("/") + "/v1/models")
59
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
60
+ payload = json.load(resp)
61
+ entries = payload.get("data") or payload.get("models") or []
62
+ if not entries:
63
+ raise RuntimeError("Server returned no models")
64
+ first = entries[0]
65
+ return first.get("id") if isinstance(first, dict) else str(first)
66
+
67
+
68
+ def transcribe(base_url: str, wav: Path, model: str, *, max_tokens: int, timeout: int) -> str:
69
+ audio_b64 = base64.standard_b64encode(wav.read_bytes()).decode("ascii")
70
+ body = json.dumps(
71
+ {
72
+ "model": model,
73
+ "messages": [
74
+ {
75
+ "role": "user",
76
+ "content": [
77
+ {
78
+ "type": "input_audio",
79
+ "input_audio": {"data": audio_b64, "format": "wav"},
80
+ },
81
+ {"type": "text", "text": PROMPT},
82
+ ],
83
+ }
84
+ ],
85
+ "temperature": 0.0,
86
+ "max_tokens": max_tokens,
87
+ }
88
+ ).encode("utf-8")
89
+
90
+ req = urllib.request.Request(
91
+ base_url.rstrip("/") + "/v1/chat/completions",
92
+ data=body,
93
+ method="POST",
94
+ headers={"Content-Type": "application/json"},
95
+ )
96
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
97
+ payload = json.load(resp)
98
+
99
+ choices = payload.get("choices") or []
100
+ if not choices:
101
+ raise RuntimeError(f"No choices in response: {payload!r}")
102
+ return (choices[0].get("message") or {}).get("content") or ""
103
+
104
+
105
+ def collect_inputs(args: argparse.Namespace) -> list[Path]:
106
+ if bool(args.audio) == bool(args.input_jsonl):
107
+ raise SystemExit("Pass exactly one of --audio or --input-jsonl")
108
+
109
+ if args.audio:
110
+ wav = args.audio.expanduser()
111
+ if not wav.is_file():
112
+ raise SystemExit(f"Audio not found: {wav}")
113
+ return [wav]
114
+
115
+ paths: list[Path] = []
116
+ with args.input_jsonl.expanduser().open(encoding="utf-8") as handle:
117
+ for number, line in enumerate(handle, start=1):
118
+ line = line.strip()
119
+ if not line:
120
+ continue
121
+ field = json.loads(line).get("audio")
122
+ if not field:
123
+ raise SystemExit(f"Line {number}: missing 'audio' field")
124
+ wav = Path(field).expanduser()
125
+ if not wav.is_file():
126
+ raise SystemExit(f"Line {number}: audio not found: {wav}")
127
+ paths.append(wav)
128
+ return paths
129
+
130
+
131
+ def main() -> int:
132
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
133
+ parser.add_argument("--audio", type=Path, help="Single audio file")
134
+ parser.add_argument("--input-jsonl", type=Path, help="JSONL with an 'audio' path per line")
135
+ parser.add_argument("--output", type=Path, help="Write JSONL results here instead of stdout")
136
+ parser.add_argument("--base-url", default="http://127.0.0.1:8080")
137
+ parser.add_argument("--model", default=None, help="Model id (default: first from /v1/models)")
138
+ parser.add_argument("--max-tokens", type=int, default=512)
139
+ parser.add_argument("--timeout", type=int, default=600)
140
+ parser.add_argument(
141
+ "--require-speech",
142
+ action="store_true",
143
+ help="Skip ASR when Silero VAD finds no speech (silence/music). Needs models/silero_vad.onnx",
144
+ )
145
+ args = parser.parse_args()
146
+
147
+ wavs = collect_inputs(args)
148
+
149
+ try:
150
+ model = args.model or default_model(args.base_url, timeout=10)
151
+ except Exception as exc:
152
+ raise SystemExit(f"Cannot reach llama-server at {args.base_url}: {exc}") from exc
153
+
154
+ results: list[dict[str, Any]] = []
155
+ failures = 0
156
+ for index, wav in enumerate(wavs, start=1):
157
+ try:
158
+ if args.require_speech and not has_speech(wav):
159
+ row = {
160
+ "audio": str(wav),
161
+ "language": None,
162
+ "transcription": "",
163
+ "raw": None,
164
+ "suppressed": "no_speech",
165
+ }
166
+ print(f"[{index}/{len(wavs)}] skip {wav.name} (no speech)", file=sys.stderr)
167
+ results.append(row)
168
+ continue
169
+ raw = transcribe(
170
+ args.base_url, wav, model, max_tokens=args.max_tokens, timeout=args.timeout
171
+ )
172
+ row = {"audio": str(wav), **parse_output(raw)}
173
+ note = f" suppressed={row['suppressed']}" if row.get("suppressed") else ""
174
+ print(f"[{index}/{len(wavs)}] ok {wav.name}{note}", file=sys.stderr)
175
+ except Exception as exc:
176
+ failures += 1
177
+ row = {"audio": str(wav), "language": None, "transcription": None, "error": str(exc)}
178
+ print(f"[{index}/{len(wavs)}] FAIL {wav.name}: {exc}", file=sys.stderr)
179
+ results.append(row)
180
+
181
+ if args.output:
182
+ with args.output.expanduser().open("w", encoding="utf-8") as handle:
183
+ for row in results:
184
+ handle.write(json.dumps(row, ensure_ascii=False) + "\n")
185
+ print(f"Wrote {args.output}", file=sys.stderr)
186
+ elif len(results) == 1:
187
+ print(results[0]["transcription"] or "")
188
+ else:
189
+ for row in results:
190
+ print(json.dumps(row, ensure_ascii=False))
191
+
192
+ return 1 if failures else 0
193
+
194
+
195
+ if __name__ == "__main__":
196
+ raise SystemExit(main())