When EXAM_EXTRACT_URL is set, POST /templates/{id}/auto-map now runs the spike's
full recognition pipeline (via the P1 service) instead of the thin first-pass, as
an async job. New modules/services/exam_extract.py (HTTP client: POST paper, poll,
return the analyse contract). templates.py: _map_service_contract_to_rows adapts the
page-fraction analyse contract onto the app's 780-wide stacked canvas (fraction ×
rendered dims + page_top), re-namespaces the service's uuid5s per template (stable
re-map, no cross-template collision), and is FK-safe (drops orphan regions, de-parents
dangling parents). Rich meta (OMR boxes, select_n, unit/quantity, n_options) persists
to the new exam_response_areas.meta column (migration 75). Typed response_form + mcq
answer_type + kind:context reference links map natively (schema already allows them).
The thin path is preserved as fallback when EXAM_EXTRACT_URL is unset. Unit test
covers the mapping (coord conversion, id remap, FK-safety, meta passthrough).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
"""Client for the exam extraction SERVICE (docling-exam-spike, P1).
|
|
|
|
The service runs the spike's FULL recognition pipeline (textlayer → question tree → OMR/figure/table
|
|
sidecars → structure fusion → analyse) and returns the ghost-region contract the app already consumes.
|
|
This module is a thin HTTP client: POST the paper, poll, return the `analyse` suggestions. The app's
|
|
auto-map merges them (coordinate-adapted, id-remapped) in routers/exam/templates.py.
|
|
|
|
Config: EXAM_EXTRACT_URL (e.g. http://192.168.0.203:8899). If unset, the app keeps its thin first-pass.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import os
|
|
import time
|
|
from typing import Any, Dict, Optional
|
|
|
|
import requests
|
|
|
|
from modules.logger_tool import initialise_logger
|
|
|
|
logger = initialise_logger(__name__, os.getenv("LOG_LEVEL"), os.getenv("LOG_PATH"), "default", True)
|
|
|
|
|
|
class ExtractError(RuntimeError):
|
|
pass
|
|
|
|
|
|
def service_url() -> Optional[str]:
|
|
url = os.getenv("EXAM_EXTRACT_URL")
|
|
return url.rstrip("/") if url else None
|
|
|
|
|
|
def is_enabled() -> bool:
|
|
return bool(service_url())
|
|
|
|
|
|
def _get(base: str, slug: str, timeout: int = 30) -> Dict[str, Any]:
|
|
r = requests.get(f"{base}/api/extract/{slug}", timeout=timeout)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
|
|
def extract_suggestions(slug: str, pdf_bytes: bytes, *, force: bool = False,
|
|
poll_timeout: int = 1500, poll_interval: int = 5) -> Dict[str, Any]:
|
|
"""POST the paper to the service and poll until the analyse contract is ready.
|
|
|
|
Returns the full analyse payload: {status, coordinate_space:'page_fraction', margins,
|
|
suggestions:{questions[], response_areas[], boundaries[]}, meta}. Raises ExtractError on
|
|
failure/timeout. A cold paper is ~15 min (Docling per masked page); cached papers return instantly.
|
|
"""
|
|
base = service_url()
|
|
if not base:
|
|
raise ExtractError("EXAM_EXTRACT_URL not configured")
|
|
payload = {"slug": slug, "pdf_b64": base64.b64encode(pdf_bytes).decode(), "force": force}
|
|
r = requests.post(f"{base}/api/extract", json=payload, timeout=120)
|
|
r.raise_for_status()
|
|
started = r.json()
|
|
if not started.get("ok", True):
|
|
raise ExtractError(started.get("error") or "service rejected the request")
|
|
# cached → fetch the contract straight away; otherwise poll the running job
|
|
deadline = time.time() + poll_timeout
|
|
while True:
|
|
d = _get(base, slug)
|
|
status = d.get("status")
|
|
if status == "complete":
|
|
if not (d.get("suggestions") or {}):
|
|
raise ExtractError("service returned complete with no suggestions")
|
|
return d
|
|
if status == "error":
|
|
raise ExtractError(f"extraction failed: {d.get('error')}")
|
|
if time.time() >= deadline:
|
|
raise ExtractError(f"extraction timed out after {poll_timeout}s (slug={slug})")
|
|
time.sleep(poll_interval)
|