fix(mac-worker): handle MLX segments as list/tuple in addition to dict
lightning-whisper-mlx returns segments as [start, end, text] lists (not dicts) in some versions, which broke transcribe(). Normalize all three observed shapes: str output, list/tuple segments, dict segments. Also fall back to joining segment texts when top-level text is empty.
This commit is contained in:
parent
27f2f91ec6
commit
cb60172b51
@ -53,19 +53,34 @@ class WhisperEngine:
|
||||
|
||||
if self.engine == "mlx":
|
||||
out = self._impl.transcribe(audio_path=str(audio_path), language=lang)
|
||||
# MLX-Output: {"text": "...", "segments": [...]} oder String. Robust mappen.
|
||||
# MLX-Output schwankt zwischen Versionen:
|
||||
# - String
|
||||
# - {"text": "...", "segments": [...]}
|
||||
# - {"text": "...", "segments": [[start, end, text], ...]}
|
||||
# - {"text": "...", "segments": [{"start":..,"end":..,"text":..}, ...]}
|
||||
if isinstance(out, str):
|
||||
return {"text": out, "language": lang, "segments": []}
|
||||
segments = out.get("segments") or []
|
||||
normalized = [
|
||||
{
|
||||
"start": float(s.get("start", 0.0)),
|
||||
"end": float(s.get("end", 0.0)),
|
||||
"text": (s.get("text") or "").strip(),
|
||||
}
|
||||
for s in segments
|
||||
]
|
||||
return {"text": out.get("text", "").strip(), "language": lang, "segments": normalized}
|
||||
raw_segments = out.get("segments") or []
|
||||
normalized = []
|
||||
for s in raw_segments:
|
||||
if isinstance(s, dict):
|
||||
normalized.append({
|
||||
"start": float(s.get("start", 0.0)),
|
||||
"end": float(s.get("end", 0.0)),
|
||||
"text": (s.get("text") or "").strip(),
|
||||
})
|
||||
elif isinstance(s, (list, tuple)) and len(s) >= 3:
|
||||
normalized.append({
|
||||
"start": float(s[0] or 0.0),
|
||||
"end": float(s[1] or 0.0),
|
||||
"text": str(s[2] or "").strip(),
|
||||
})
|
||||
else:
|
||||
normalized.append({"start": 0.0, "end": 0.0, "text": str(s).strip()})
|
||||
text = (out.get("text") or "").strip()
|
||||
if not text and normalized:
|
||||
text = " ".join(seg["text"] for seg in normalized).strip()
|
||||
return {"text": text, "language": lang, "segments": normalized}
|
||||
|
||||
if self.engine == "faster":
|
||||
segments_iter, info = self._impl.transcribe(str(audio_path), language=lang, vad_filter=True)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user