Add self-contained omniASR-LLM-7B server
Browse files- serve_omniasr.py +73 -0
serve_omniasr.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Minimal self-contained ASR server for facebook/omniASR-LLM-7B (Meta's
|
| 2 |
+
Omnilingual ASR), using the model's own documented ASRInferencePipeline API.
|
| 3 |
+
This is fairseq2-native, not `transformers` - the standard Endpoint ASR task
|
| 4 |
+
container can't load it at all.
|
| 5 |
+
|
| 6 |
+
Contract: POST raw audio bytes to "/", get back {"text": "..."}. GET "/health" -> 200.
|
| 7 |
+
Defaults to Arabic (arb_Arab, FLORES-200/NLLB code) - pass ?lang=eng_Latn etc.
|
| 8 |
+
as a query string to override per-request.
|
| 9 |
+
"""
|
| 10 |
+
import http.server
|
| 11 |
+
import json
|
| 12 |
+
import socketserver
|
| 13 |
+
import tempfile
|
| 14 |
+
from urllib.parse import urlparse, parse_qs
|
| 15 |
+
|
| 16 |
+
from omnilingual_asr.models.inference.pipeline import ASRInferencePipeline
|
| 17 |
+
|
| 18 |
+
MODEL_CARD = "omniASR_LLM_7B"
|
| 19 |
+
DEFAULT_LANG = "arb_Arab"
|
| 20 |
+
|
| 21 |
+
print("Loading omniASR_LLM_7B pipeline (downloads ~31GB on first run, can take a while)...", flush=True)
|
| 22 |
+
pipeline = ASRInferencePipeline(model_card=MODEL_CARD)
|
| 23 |
+
print("Model loaded, ready to serve.", flush=True)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def transcribe(raw_bytes: bytes, lang: str) -> str:
|
| 27 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
| 28 |
+
f.write(raw_bytes)
|
| 29 |
+
path = f.name
|
| 30 |
+
result = pipeline.transcribe([path], lang=[lang], batch_size=1)
|
| 31 |
+
return result[0] if isinstance(result, list) else result
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class Handler(http.server.BaseHTTPRequestHandler):
|
| 35 |
+
def do_GET(self):
|
| 36 |
+
if self.path == "/health":
|
| 37 |
+
self.send_response(200)
|
| 38 |
+
self.end_headers()
|
| 39 |
+
self.wfile.write(b"ok")
|
| 40 |
+
else:
|
| 41 |
+
self.send_response(404)
|
| 42 |
+
self.end_headers()
|
| 43 |
+
|
| 44 |
+
def do_POST(self):
|
| 45 |
+
query = parse_qs(urlparse(self.path).query)
|
| 46 |
+
lang = query.get("lang", [DEFAULT_LANG])[0]
|
| 47 |
+
length = int(self.headers.get("Content-Length", 0))
|
| 48 |
+
raw = self.rfile.read(length)
|
| 49 |
+
try:
|
| 50 |
+
text = transcribe(raw, lang)
|
| 51 |
+
body = json.dumps({"text": text}, ensure_ascii=False).encode("utf-8")
|
| 52 |
+
status = 200
|
| 53 |
+
except Exception as e: # noqa: BLE001 - surfaced to the caller, not swallowed
|
| 54 |
+
body = json.dumps({"error": str(e)}).encode("utf-8")
|
| 55 |
+
status = 500
|
| 56 |
+
self.send_response(status)
|
| 57 |
+
self.send_header("Content-Type", "application/json; charset=utf-8")
|
| 58 |
+
self.send_header("Content-Length", str(len(body)))
|
| 59 |
+
self.end_headers()
|
| 60 |
+
self.wfile.write(body)
|
| 61 |
+
|
| 62 |
+
def log_message(self, fmt, *args):
|
| 63 |
+
print(f"[serve] {self.address_string()} - {fmt % args}", flush=True)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class ThreadingServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
|
| 67 |
+
daemon_threads = True
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
port = 8000
|
| 72 |
+
print(f"Serving on 0.0.0.0:{port}", flush=True)
|
| 73 |
+
ThreadingServer(("0.0.0.0", port), Handler).serve_forever()
|