Spaces:
Running on T4
Running on T4
File size: 4,136 Bytes
7bf5a7e 6963661 a8788e5 be69d1e 6963661 2756f0f 6963661 2756f0f be69d1e 2756f0f be69d1e 2756f0f be69d1e 2756f0f 6963661 2756f0f 6963661 2756f0f be69d1e 2756f0f be69d1e 19d2582 f9655f6 19d2582 f9655f6 19d2582 2756f0f be69d1e 2756f0f be69d1e 2756f0f be69d1e 7a3215f be69d1e 6963661 be69d1e 6963661 be69d1e 6963661 be69d1e 6963661 be69d1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | """H1: POST /v1/sessions/{session_id}/swing-report (contract 1.0/1.1) and /pitch-report (pitch-1.1).
The body is the ML API's payload, unchanged. Pitch-1.1 returns the pitching report extension.
By default the report is built in the background: the response is 202 with the session status, and the owner gets
a "result" notification when the report is saved (or when the build failed). ?queue=false builds it in the request
and returns the report (200), for a caller that waits on screen.
"""
import uuid
from typing import Annotated, Any
from uuid import UUID
from ai_coach.pitch_report.v1_1.schemas import PitchReport
from fastapi import APIRouter, Body, Depends, Request
from fastapi.responses import JSONResponse
from app.common.schemas import ResponseModel
from app.core.dependencies import CurrentAccount, Db
from app.core.errors import TooLarge
from app.modules.swing_reports import queue as report_queue
from app.modules.swing_reports.schemas import SwingReportOut
from app.modules.swing_reports.service import SwingReportService
router = APIRouter(prefix="/v1", tags=["swing reports"])
pitch_router = APIRouter(prefix="/v1", tags=["pitch reports"])
MAX_PAYLOAD_BYTES = 256 * 1024 # a contract payload is a few kilobytes
class ReportQueued(ResponseModel):
session_id: UUID
status: str # "queued", or "done" when this revision already has its report
QUEUED = {202: {"model": ReportQueued, "description": "Queued (the default): the report is built in the background"}}
def get_swing_report_service(db: Db) -> SwingReportService:
return SwingReportService(db)
async def _respond(db: Db, session_id: UUID, report: dict | None, queue: bool):
if not queue:
return report
if report is None:
await db.commit() # the worker must see the queued payload before it is woken
report_queue.kick()
return JSONResponse(ReportQueued(session_id=session_id, status="queued" if report is None else "done")
.model_dump(mode="json"), status_code=202)
@pitch_router.post("/sessions/{session_id}/pitch-report/retry-coaching", response_model=PitchReport,
response_model_exclude_unset=True)
async def retry_pitch_coaching(
session_id: UUID, request: Request, account: CurrentAccount,
service: Annotated[SwingReportService, Depends(get_swing_report_service)],
refresh: bool = False,
) -> dict:
request_id = getattr(request.state, "request_id", None) or uuid.uuid4().hex
return await service.retry_pitch_coaching(account, session_id, request_id, refresh=refresh)
async def payload_size_ok(request: Request) -> None:
length = request.headers.get("content-length")
if length and length.isdigit() and int(length) > MAX_PAYLOAD_BYTES:
raise TooLarge("The payload is too large.")
@router.post("/sessions/{session_id}/swing-report", response_model=SwingReportOut, responses=QUEUED,
dependencies=[Depends(payload_size_ok)])
async def create_swing_report(
session_id: UUID, request: Request, account: CurrentAccount, db: Db,
service: Annotated[SwingReportService, Depends(get_swing_report_service)],
payload: Annotated[Any, Body()], queue: bool = True,
):
request_id = getattr(request.state, "request_id", None) or uuid.uuid4().hex
return await _respond(db, session_id, await service.create(account, session_id, payload, request_id, queue=queue),
queue)
@pitch_router.post("/sessions/{session_id}/pitch-report", response_model=PitchReport | SwingReportOut,
response_model_exclude_unset=True, responses=QUEUED,
dependencies=[Depends(payload_size_ok)])
async def create_pitch_report(
session_id: UUID, request: Request, account: CurrentAccount, db: Db,
service: Annotated[SwingReportService, Depends(get_swing_report_service)],
payload: Annotated[Any, Body()], queue: bool = True,
):
request_id = getattr(request.state, "request_id", None) or uuid.uuid4().hex
return await _respond(
db, session_id, await service.create_pitch(account, session_id, payload, request_id, queue=queue), queue)
|