259 lines
7.5 KiB
Python
259 lines
7.5 KiB
Python
"""
|
|
Parakeet Sidecar — self-hosted HTTP wrapper for NVIDIA Parakeet ASR.
|
|
|
|
Endpoints
|
|
---------
|
|
GET /health
|
|
POST /transcribe (multipart/form-data)
|
|
fields:
|
|
- file: audio file
|
|
- model: model id (default: parakeet-1.1b-rnnt-multilingual-asr)
|
|
- language: optional language hint (BCP-47 or ISO code)
|
|
- task: transcribe (only)
|
|
- initial_prompt: optional (currently ignored)
|
|
- temperature: optional (currently ignored)
|
|
|
|
Response shape
|
|
--------------
|
|
{
|
|
"text": "...",
|
|
"language": "en",
|
|
"segments": [
|
|
{
|
|
"start": 0.0,
|
|
"end": 12.34,
|
|
"text": "...",
|
|
"words": []
|
|
}
|
|
]
|
|
}
|
|
|
|
Notes
|
|
-----
|
|
- This sidecar intentionally returns a conservative response shape compatible
|
|
with backend/services/lyric_transcriber_service.py normalization.
|
|
- Word timestamps are not guaranteed from this initial integration and are
|
|
returned as an empty list.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
import threading
|
|
from typing import Callable
|
|
import wave
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
|
|
|
log = logging.getLogger("parakeet-sidecar")
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
|
|
DEFAULT_MODEL = os.environ.get("DEFAULT_PARAKEET_MODEL", "parakeet-1.1b-rnnt-multilingual-asr").strip()
|
|
MODEL_CACHE_DIR = Path(os.environ.get("PARAKEET_MODEL_CACHE_DIR", "/root/.cache/parakeet"))
|
|
SUPPORTED_MODELS = {
|
|
"parakeet-1.1b-rnnt-multilingual-asr",
|
|
"parakeet-1_1b-rnnt-multilingual-asr",
|
|
"parakeet-tdt_ctc-110m",
|
|
}
|
|
|
|
# The currently shipped NeMo runtime image exposes `parakeet-tdt_ctc-110m`
|
|
# via `ASRModel.list_available_models()`. Keep API-level compatibility with
|
|
# previously selected 1.1B model ids by aliasing them to the available runtime
|
|
# model until the exact 1.1B artifact is accessible in this runtime.
|
|
MODEL_ALIASES = {
|
|
"parakeet-1.1b-rnnt-multilingual-asr": "parakeet-tdt_ctc-110m",
|
|
"parakeet-1_1b-rnnt-multilingual-asr": "parakeet-tdt_ctc-110m",
|
|
}
|
|
|
|
MODEL_CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
_app = FastAPI(title="Parakeet Sidecar", version="0.1")
|
|
_model_lock = threading.Lock()
|
|
_model_cache: dict[str, object] = {}
|
|
|
|
|
|
def _import_nemo_asr():
|
|
try:
|
|
import nemo.collections.asr as nemo_asr # type: ignore
|
|
return nemo_asr
|
|
except Exception as exc:
|
|
raise RuntimeError(
|
|
"NeMo ASR is not available in parakeet-sidecar. "
|
|
"Install nemo_toolkit[asr] and runtime dependencies."
|
|
) from exc
|
|
|
|
|
|
def _load_model(model_id: str):
|
|
resolved_model_id = MODEL_ALIASES.get(model_id, model_id)
|
|
with _model_lock:
|
|
cached = _model_cache.get(resolved_model_id)
|
|
if cached is not None:
|
|
return cached
|
|
|
|
nemo_asr = _import_nemo_asr()
|
|
if resolved_model_id != model_id:
|
|
log.info("Aliasing requested model %s -> %s", model_id, resolved_model_id)
|
|
log.info("Loading Parakeet model: %s", resolved_model_id)
|
|
model_obj = nemo_asr.models.ASRModel.from_pretrained(model_name=resolved_model_id)
|
|
_model_cache[resolved_model_id] = model_obj
|
|
return model_obj
|
|
|
|
|
|
def _duration_seconds(wav_path: Path) -> float:
|
|
with wave.open(str(wav_path), "rb") as wf:
|
|
frames = wf.getnframes()
|
|
rate = wf.getframerate() or 16000
|
|
return float(frames) / float(rate)
|
|
|
|
|
|
def _to_wav(src: Path, dst: Path) -> None:
|
|
cmd = [
|
|
"ffmpeg",
|
|
"-y",
|
|
"-i",
|
|
str(src),
|
|
"-ac",
|
|
"1",
|
|
"-ar",
|
|
"16000",
|
|
str(dst),
|
|
]
|
|
proc = subprocess.run(cmd, capture_output=True, text=True)
|
|
if proc.returncode != 0:
|
|
raise RuntimeError(proc.stderr.strip() or proc.stdout.strip() or "ffmpeg conversion failed")
|
|
|
|
|
|
def _transcribe_with_fallbacks(model_obj, wav_path: Path, language: str):
|
|
audio_path = str(wav_path)
|
|
calls: list[Callable[[], object]] = []
|
|
|
|
if language:
|
|
calls.extend(
|
|
[
|
|
lambda: model_obj.transcribe(paths2audio_files=[audio_path], batch_size=1, target_lang=language),
|
|
lambda: model_obj.transcribe(audio=[audio_path], batch_size=1, target_lang=language),
|
|
lambda: model_obj.transcribe(audio_path, target_lang=language),
|
|
]
|
|
)
|
|
|
|
calls.extend(
|
|
[
|
|
lambda: model_obj.transcribe(paths2audio_files=[audio_path], batch_size=1),
|
|
lambda: model_obj.transcribe(audio=[audio_path], batch_size=1),
|
|
lambda: model_obj.transcribe([audio_path]),
|
|
lambda: model_obj.transcribe(audio_path),
|
|
]
|
|
)
|
|
|
|
last_exc: Exception | None = None
|
|
for call in calls:
|
|
try:
|
|
return call()
|
|
except TypeError as exc:
|
|
last_exc = exc
|
|
continue
|
|
|
|
if last_exc is not None:
|
|
raise last_exc
|
|
raise RuntimeError("No supported transcribe() call signature found")
|
|
|
|
|
|
@_app.get("/health")
|
|
def health():
|
|
nemo_ok = True
|
|
detail = "ok"
|
|
try:
|
|
_import_nemo_asr()
|
|
except Exception as exc:
|
|
nemo_ok = False
|
|
detail = str(exc)
|
|
|
|
return {
|
|
"ok": nemo_ok,
|
|
"engine": "parakeet",
|
|
"default_model": DEFAULT_MODEL,
|
|
"supported_models": sorted(SUPPORTED_MODELS),
|
|
"model_aliases": MODEL_ALIASES,
|
|
"detail": detail,
|
|
}
|
|
|
|
|
|
@_app.post("/transcribe")
|
|
async def transcribe(
|
|
file: UploadFile = File(...),
|
|
model: str = Form(DEFAULT_MODEL),
|
|
language: str = Form(""),
|
|
task: str = Form("transcribe"),
|
|
initial_prompt: str = Form(""),
|
|
temperature: str = Form("0"),
|
|
):
|
|
model_id = (model or DEFAULT_MODEL).strip()
|
|
if model_id not in SUPPORTED_MODELS:
|
|
raise HTTPException(status_code=400, detail=f"Unsupported model: {model_id}")
|
|
|
|
if task != "transcribe":
|
|
raise HTTPException(status_code=400, detail="Only task='transcribe' is supported")
|
|
|
|
if not file.filename:
|
|
raise HTTPException(status_code=400, detail="Missing file name")
|
|
|
|
_ = initial_prompt
|
|
_ = temperature
|
|
|
|
try:
|
|
model_obj = _load_model(model_id)
|
|
except Exception as exc:
|
|
raise HTTPException(status_code=503, detail=f"Failed to load model '{model_id}': {exc}") from exc
|
|
|
|
with tempfile.TemporaryDirectory(prefix="parakeet-") as td:
|
|
tmp = Path(td)
|
|
in_path = tmp / Path(file.filename).name
|
|
wav_path = tmp / "input.wav"
|
|
|
|
content = await file.read()
|
|
in_path.write_bytes(content)
|
|
|
|
try:
|
|
_to_wav(in_path, wav_path)
|
|
duration = _duration_seconds(wav_path)
|
|
except Exception as exc:
|
|
raise HTTPException(status_code=400, detail=f"Audio conversion failed: {exc}") from exc
|
|
|
|
try:
|
|
texts = _transcribe_with_fallbacks(model_obj, wav_path, language)
|
|
|
|
text = ""
|
|
if isinstance(texts, list) and texts:
|
|
text = str(texts[0])
|
|
else:
|
|
text = str(texts or "")
|
|
except Exception as exc:
|
|
raise HTTPException(status_code=500, detail=f"Parakeet transcription failed: {exc}") from exc
|
|
|
|
resolved_language = language.strip() or "unknown"
|
|
return {
|
|
"text": text.strip(),
|
|
"language": resolved_language,
|
|
"segments": [
|
|
{
|
|
"start": 0.0,
|
|
"end": max(duration, 0.0),
|
|
"text": text.strip(),
|
|
"words": [],
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
app = _app
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", "8777")))
|