hetchyy's picture
deploy qua@2c778176
64de8e2 verified
Raw History Blame Contribute Delete
2.36 kB
"""Build the alignment response body shared by /api/v1 and the legacy adapters.
The shape is contract-critical: production clients parse it, and the schema
parity test compares it against production byte-structure. Add keys only as
optional additions — never rename or remove one.
Batch review checks are projected to the response and excluded from session storage.
The rows go out through ``build_segment_export``'s public projection, the same
one the download uses. They did not, once: this builder handed the pipeline's
internal rows straight to the caller, so the API shipped every field the
matcher happened to set while the download shipped the public shape.
"""
from __future__ import annotations
from config import OUTPUT_LICENSE
from src.api.services import sessions
def build(audio_id, json_output, warning=None, device=None) -> dict:
"""Return the public alignment rows, and keep the lossless ones.
The session keeps the full rows — later edit and MFA calls need the
matcher's working state — while the caller gets the public projection.
``device`` is echoed so a caller can tell where the work actually ran —
a GPU request whose quota was exhausted finishes on the CPU, and only
the response says so.
"""
from src.pipeline.exports import build_segment_export
rows = [dict(seg) for seg in json_output.get("segments", [])]
for segment in rows:
segment.setdefault("has_missing_words", False)
segment.setdefault("has_repeated_words", False)
stored = [{k: v for k, v in row.items() if k != "review_checks"} for row in rows]
sessions.save_segments(audio_id, stored)
public = build_segment_export({**json_output, "segments": rows},
include_words=True)
resp = {"audio_id": audio_id, "segments": public["segments"] if public else []}
if device:
resp["device"] = device
# The edition identity is stamped once here and nowhere else. It used to be
# copied onto the response root as well, on the belief that production
# clients read it there — production emits none of these keys at all.
meta = dict((public or {}).get("_meta") or json_output.get("_meta") or {})
if meta:
resp["_meta"] = meta
if warning:
resp["warning"] = warning
resp["license"] = OUTPUT_LICENSE
return resp