Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cce46305c9 | ||
|
|
6c73174829 | ||
|
|
5434a5bf21 | ||
|
|
44ccba2151 | ||
|
|
e83873e822 |
@@ -67,11 +67,13 @@ def derive_bands(result, doc=None, rapid_glob=None):
|
|||||||
topnum = _topnumber_boxes(docs)
|
topnum = _topnumber_boxes(docs)
|
||||||
# gather parts with geometry, grouped by page
|
# gather parts with geometry, grouped by page
|
||||||
by_page = defaultdict(list) # page -> [(q, label, t, b)]
|
by_page = defaultdict(list) # page -> [(q, label, t, b)]
|
||||||
|
part_marks = {} # (question, part label) -> parsed marks (born-digital grammar)
|
||||||
for q in result.get("questions", []):
|
for q in result.get("questions", []):
|
||||||
for p in q["parts"]:
|
for p in q["parts"]:
|
||||||
bb, pg = p.get("bbox"), p.get("page")
|
bb, pg = p.get("bbox"), p.get("page")
|
||||||
if bb and pg:
|
if bb and pg:
|
||||||
by_page[pg].append((q["question"], p["label"], bb["t"], bb["b"]))
|
by_page[pg].append((q["question"], p["label"], bb["t"], bb["b"]))
|
||||||
|
part_marks[(q["question"], p["label"])] = p.get("marks")
|
||||||
|
|
||||||
# global first page each question appears on (to mark the true start vs continuation pages)
|
# global first page each question appears on (to mark the true start vs continuation pages)
|
||||||
q_first_page = {}
|
q_first_page = {}
|
||||||
@@ -104,7 +106,8 @@ def derive_bands(result, doc=None, rapid_glob=None):
|
|||||||
for (q, lab), st, en, _ in _ends(part_items):
|
for (q, lab), st, en, _ in _ends(part_items):
|
||||||
qen = main_band.get(q, (st, 0))[1] # don't run past the question end
|
qen = main_band.get(q, (st, 0))[1] # don't run past the question end
|
||||||
part.append({"label": lab, "question": q,
|
part.append({"label": lab, "question": q,
|
||||||
"y_start": round(st, 1), "y_end": round(max(en, qen), 1)})
|
"y_start": round(st, 1), "y_end": round(max(en, qen), 1),
|
||||||
|
"marks": part_marks.get((q, lab))})
|
||||||
pages[pg] = {"main": main, "part": part}
|
pages[pg] = {"main": main, "part": part}
|
||||||
|
|
||||||
return {"board": result.get("board"), "paper_code": result.get("paper_code"),
|
return {"board": result.get("board"), "paper_code": result.get("paper_code"),
|
||||||
|
|||||||
@@ -159,6 +159,7 @@ def build(structured, bands, furniture, pdf=None, page_roles=None):
|
|||||||
"y_start": p["y_start"], "y_end": p["y_end"],
|
"y_start": p["y_start"], "y_end": p["y_end"],
|
||||||
"label_box": part_bbox.get(p["label"]), # anchor, not the part extent
|
"label_box": part_bbox.get(p["label"]), # anchor, not the part extent
|
||||||
"box": synthesize_part_box(p, xband),
|
"box": synthesize_part_box(p, xband),
|
||||||
|
"marks": p.get("marks"), # parsed per-part marks (born-digital)
|
||||||
"source": "auto", "confirmed": False,
|
"source": "auto", "confirmed": False,
|
||||||
})
|
})
|
||||||
pr = page_roles.get(pgs) or page_roles.get(pg) or {}
|
pr = page_roles.get(pgs) or page_roles.get(pg) or {}
|
||||||
|
|||||||
+78
-13
@@ -13,6 +13,7 @@ join keys (spec §2).
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import math
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
@@ -342,6 +343,13 @@ def _pdf_has_text_layer(pdf_bytes: bytes) -> bool:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Canvas page width the frontend renders each PDF page at (app src/utils/exam-canvas/model.ts
|
||||||
|
# PAGE_WIDTH). All auto-map canvas coords are emitted in this 780-wide, proportional-height space.
|
||||||
|
CANVAS_PAGE_WIDTH = 780.0
|
||||||
|
# Response/answer-region detector (api/services/docling/regions.py) renders at 144 DPI = 2 px / PDF point.
|
||||||
|
REGIONS_PX_PER_PT = 2.0
|
||||||
|
|
||||||
|
|
||||||
def _pdf_page_geometry(pdf_bytes: bytes) -> List[Dict[str, float]]:
|
def _pdf_page_geometry(pdf_bytes: bytes) -> List[Dict[str, float]]:
|
||||||
with tempfile.NamedTemporaryFile(prefix="cc-auto-map-geom-", suffix=".pdf", delete=False) as fh:
|
with tempfile.NamedTemporaryFile(prefix="cc-auto-map-geom-", suffix=".pdf", delete=False) as fh:
|
||||||
fh.write(pdf_bytes)
|
fh.write(pdf_bytes)
|
||||||
@@ -355,14 +363,23 @@ def _pdf_page_geometry(pdf_bytes: bytes) -> List[Dict[str, float]]:
|
|||||||
for page in doc:
|
for page in doc:
|
||||||
media = page.mediabox
|
media = page.mediabox
|
||||||
crop = page.cropbox
|
crop = page.cropbox
|
||||||
rendered_w = float(crop.width or page.rect.width or 595.0)
|
page_pt_w = float(crop.width or page.rect.width or 1.0)
|
||||||
rendered_h = float(crop.height or page.rect.height or 842.0)
|
page_pt_h = float(crop.height or page.rect.height or 1.0)
|
||||||
|
# Emit canvas coords in the FRONTEND render space: the app draws each page at
|
||||||
|
# CANVAS_PAGE_WIDTH (app model.ts PAGE_WIDTH=780) with proportional height and stacks
|
||||||
|
# pages by those heights. Previously rendered_w/h were left in PDF points (~595x842),
|
||||||
|
# so every shape landed shrunk (~0.76x) and shifted up-left on the 780-wide canvas.
|
||||||
|
rendered_w = CANVAS_PAGE_WIDTH
|
||||||
|
# Mirror the app's canvas.height = Math.ceil(viewport.height) EXACTLY (pdfLoader.ts),
|
||||||
|
# so page_top accumulates identically. Using the raw float drifts ~1px/page, compounding
|
||||||
|
# to a visible upward shift on later pages of long papers (~36px over 40 pages).
|
||||||
|
rendered_h = float(math.ceil(CANVAS_PAGE_WIDTH * page_pt_h / page_pt_w))
|
||||||
pages.append({
|
pages.append({
|
||||||
"media_x0": float(media.x0),
|
"media_x0": float(media.x0),
|
||||||
"crop_x0": float(crop.x0),
|
"crop_x0": float(crop.x0),
|
||||||
"crop_y0": float(crop.y0),
|
"crop_y0": float(crop.y0),
|
||||||
"page_pt_w": float(crop.width or page.rect.width or 1),
|
"page_pt_w": page_pt_w,
|
||||||
"page_pt_h": float(crop.height or page.rect.height or 1),
|
"page_pt_h": page_pt_h,
|
||||||
"rendered_w": rendered_w,
|
"rendered_w": rendered_w,
|
||||||
"rendered_h": rendered_h,
|
"rendered_h": rendered_h,
|
||||||
"page_top": page_top,
|
"page_top": page_top,
|
||||||
@@ -384,11 +401,12 @@ def _pdf_page_geometry(pdf_bytes: bytes) -> List[Dict[str, float]]:
|
|||||||
def _page_geom(pages: List[Dict[str, float]], page_number: int) -> Dict[str, float]:
|
def _page_geom(pages: List[Dict[str, float]], page_number: int) -> Dict[str, float]:
|
||||||
if 1 <= page_number <= len(pages):
|
if 1 <= page_number <= len(pages):
|
||||||
return pages[page_number - 1]
|
return pages[page_number - 1]
|
||||||
|
_fallback_h = float(math.ceil(CANVAS_PAGE_WIDTH * 842.0 / 595.0))
|
||||||
return {
|
return {
|
||||||
"media_x0": 0.0, "crop_x0": 0.0, "crop_y0": 0.0,
|
"media_x0": 0.0, "crop_x0": 0.0, "crop_y0": 0.0,
|
||||||
"page_pt_w": 595.0, "page_pt_h": 842.0,
|
"page_pt_w": 595.0, "page_pt_h": 842.0,
|
||||||
"rendered_w": 595.0, "rendered_h": 842.0,
|
"rendered_w": CANVAS_PAGE_WIDTH, "rendered_h": _fallback_h,
|
||||||
"page_top": (page_number - 1) * 842.0,
|
"page_top": (page_number - 1) * _fallback_h,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -397,12 +415,16 @@ def _box_to_canvas(box: Optional[Dict[str, Any]], page_number: int, pages: List[
|
|||||||
return None
|
return None
|
||||||
g = _page_geom(pages, page_number)
|
g = _page_geom(pages, page_number)
|
||||||
if box.get("coord_origin") == "TOPLEFT" and {"x", "y", "w", "h"}.issubset(box):
|
if box.get("coord_origin") == "TOPLEFT" and {"x", "y", "w", "h"}.issubset(box):
|
||||||
scale = 0.5 if box.get("unit") == "px" else 1.0
|
# Scale the box into the 780-wide canvas space. px boxes (opencv/gemma regions) are in
|
||||||
|
# rendered-image px at REGIONS_PX_PER_PT px/point; TOPLEFT point boxes are 1 px/point.
|
||||||
|
px_per_pt = REGIONS_PX_PER_PT if box.get("unit") == "px" else 1.0
|
||||||
|
sx = g["rendered_w"] / (g["page_pt_w"] * px_per_pt)
|
||||||
|
sy = g["rendered_h"] / (g["page_pt_h"] * px_per_pt)
|
||||||
return {
|
return {
|
||||||
"x": round(float(box["x"]) * scale, 2),
|
"x": round(float(box["x"]) * sx, 2),
|
||||||
"y": round(g["page_top"] + float(box["y"]) * scale, 2),
|
"y": round(g["page_top"] + float(box["y"]) * sy, 2),
|
||||||
"w": round(float(box["w"]) * scale, 2),
|
"w": round(float(box["w"]) * sx, 2),
|
||||||
"h": round(float(box["h"]) * scale, 2),
|
"h": round(float(box["h"]) * sy, 2),
|
||||||
}
|
}
|
||||||
if not {"l", "t", "r", "b"}.issubset(box):
|
if not {"l", "t", "r", "b"}.issubset(box):
|
||||||
return None
|
return None
|
||||||
@@ -443,6 +465,17 @@ def _safe_confidence(value: Any = None) -> float:
|
|||||||
return 0.75
|
return 0.75
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_marks(value: Any = None) -> int:
|
||||||
|
"""Parsed per-part marks → a non-negative int; unknown/None → 0 (image-only OCR has no marks yet)."""
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return 0
|
||||||
|
if isinstance(value, (int, float)):
|
||||||
|
return max(0, int(value))
|
||||||
|
if isinstance(value, str) and value.strip().isdigit():
|
||||||
|
return int(value.strip())
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def _margin_values(first_pass: Dict[str, Any], page_number: int) -> Dict[str, Optional[float]]:
|
def _margin_values(first_pass: Dict[str, Any], page_number: int) -> Dict[str, Optional[float]]:
|
||||||
vals: Dict[str, Optional[float]] = {"left": None, "right": None, "top": None, "bottom": None}
|
vals: Dict[str, Optional[float]] = {"left": None, "right": None, "top": None, "bottom": None}
|
||||||
for m in first_pass.get("margins") or []:
|
for m in first_pass.get("margins") or []:
|
||||||
@@ -519,7 +552,7 @@ def _map_first_pass_to_rows(template_id: str, first_pass: Dict[str, Any], pdf_by
|
|||||||
top = max(float(y1), float(y2)); bottom = min(float(y1), float(y2))
|
top = max(float(y1), float(y2)); bottom = min(float(y1), float(y2))
|
||||||
bounds = _box_to_canvas({"l": margins["left"], "r": margins["right"], "t": top, "b": bottom, "coord_origin": "BOTTOMLEFT"}, page_number, pages_geom)
|
bounds = _box_to_canvas({"l": margins["left"], "r": margins["right"], "t": top, "b": bottom, "coord_origin": "BOTTOMLEFT"}, page_number, pages_geom)
|
||||||
bounds = bounds or _box_to_canvas(band.get("label_box"), page_number, pages_geom)
|
bounds = bounds or _box_to_canvas(band.get("label_box"), page_number, pages_geom)
|
||||||
questions.append({"id": pid, "template_id": template_id, "parent_id": parent_id, "label": label, "order": len(questions), "max_marks": 0, "is_container": False, "bounds": bounds, "page": page_number, "source": "ai", "confirmed": False, "confidence": _safe_confidence(band.get("confidence")), "derivation": "docling-part-band-x-margins"})
|
questions.append({"id": pid, "template_id": template_id, "parent_id": parent_id, "label": label, "order": len(questions), "max_marks": _safe_marks(band.get("marks")), "is_container": False, "bounds": bounds, "page": page_number, "source": "ai", "confirmed": False, "confidence": _safe_confidence(band.get("confidence")), "derivation": "docling-part-band-x-margins"})
|
||||||
|
|
||||||
default_qid = questions[0]["id"] if questions else _ai_id(template_id, "question", "auto")
|
default_qid = questions[0]["id"] if questions else _ai_id(template_id, "question", "auto")
|
||||||
for page_key in sorted(pages_obj, key=lambda k: int(k)):
|
for page_key in sorted(pages_obj, key=lambda k: int(k)):
|
||||||
@@ -540,15 +573,47 @@ def _map_first_pass_to_rows(template_id: str, first_pass: Dict[str, Any], pdf_by
|
|||||||
response_form = _response_form_from_region_type(region.get("region_type"))
|
response_form = _response_form_from_region_type(region.get("region_type"))
|
||||||
if response_form:
|
if response_form:
|
||||||
response_areas.append({"id": _ai_id(template_id, "region", page_index, idx), "template_id": template_id, "question_id": first_part_by_page.get(page_index, default_qid), "page": page_index + 1, "bounds": bounds, "kind": "response", "response_form": response_form, "source": "ai", "confirmed": False, "confidence": _safe_confidence(region.get("confidence")), "derivation": region.get("detection_method") or "opencv-response-region"})
|
response_areas.append({"id": _ai_id(template_id, "region", page_index, idx), "template_id": template_id, "question_id": first_part_by_page.get(page_index, default_qid), "page": page_index + 1, "bounds": bounds, "kind": "response", "response_form": response_form, "source": "ai", "confirmed": False, "confidence": _safe_confidence(region.get("confidence")), "derivation": region.get("detection_method") or "opencv-response-region"})
|
||||||
|
# Integrity guard: every response_area/boundary question_id must reference an inserted question
|
||||||
|
# (FK exam_response_areas/exam_boundaries -> exam_questions). On papers where band detection yields
|
||||||
|
# few/no questions but opencv/gemma still emit regions, those regions point at the synthetic
|
||||||
|
# default_qid which was never inserted. Ensure that fallback container question exists and reattach
|
||||||
|
# any orphan child rows to it, so persistence can't violate the FK.
|
||||||
|
qid_set = {q["id"] for q in questions}
|
||||||
|
orphans = [r for r in (response_areas + boundaries) if r.get("question_id") not in qid_set]
|
||||||
|
if orphans:
|
||||||
|
if default_qid not in qid_set:
|
||||||
|
questions.insert(0, {"id": default_qid, "template_id": template_id, "label": "Unassigned",
|
||||||
|
"order": 0, "max_marks": 0, "is_container": True, "source": "ai",
|
||||||
|
"confirmed": False, "confidence": 0.5,
|
||||||
|
"derivation": "auto-map-fallback-container"})
|
||||||
|
qid_set.add(default_qid)
|
||||||
|
for r in orphans:
|
||||||
|
r["question_id"] = default_qid
|
||||||
|
|
||||||
return {"questions": questions, "response_areas": response_areas, "boundaries": boundaries, "layout": layout}
|
return {"questions": questions, "response_areas": response_areas, "boundaries": boundaries, "layout": layout}
|
||||||
|
|
||||||
|
|
||||||
|
def _dedupe_rows_by_id(rows: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||||
|
"""Preserve first occurrence of stable AI row ids emitted by noisy OCR detectors."""
|
||||||
|
out: List[Dict[str, Any]] = []
|
||||||
|
seen: set[str] = set()
|
||||||
|
for row in rows:
|
||||||
|
row_id = row.get("id")
|
||||||
|
if row_id:
|
||||||
|
key = str(row_id)
|
||||||
|
if key in seen:
|
||||||
|
continue
|
||||||
|
seen.add(key)
|
||||||
|
out.append(row)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def _refresh_ai_rows(ctx: ExamContext, template_id: str, rows: Dict[str, List[Dict[str, Any]]]) -> None:
|
def _refresh_ai_rows(ctx: ExamContext, template_id: str, rows: Dict[str, List[Dict[str, Any]]]) -> None:
|
||||||
sb = ctx.supabase
|
sb = ctx.supabase
|
||||||
for table in ("exam_response_areas", "exam_boundaries", "exam_template_layout", "exam_questions"):
|
for table in ("exam_response_areas", "exam_boundaries", "exam_template_layout", "exam_questions"):
|
||||||
sb.table(table).delete().eq("template_id", template_id).eq("source", "ai").eq("confirmed", False).execute()
|
sb.table(table).delete().eq("template_id", template_id).eq("source", "ai").eq("confirmed", False).execute()
|
||||||
for table, key in (("exam_questions", "questions"), ("exam_response_areas", "response_areas"), ("exam_boundaries", "boundaries"), ("exam_template_layout", "layout")):
|
for table, key in (("exam_questions", "questions"), ("exam_response_areas", "response_areas"), ("exam_boundaries", "boundaries"), ("exam_template_layout", "layout")):
|
||||||
payload = rows.get(key) or []
|
payload = _dedupe_rows_by_id(rows.get(key) or [])
|
||||||
if payload:
|
if payload:
|
||||||
sb.table(table).insert(payload).execute()
|
sb.table(table).insert(payload).execute()
|
||||||
|
|
||||||
|
|||||||
@@ -642,6 +642,35 @@ def test_auto_map_fast_path_merges_ai_rows_and_returns_detail(monkeypatch):
|
|||||||
assert store["exam_boundaries"] and store["exam_boundaries"][0]["derivation"] == "docling-main-band"
|
assert store["exam_boundaries"] and store["exam_boundaries"][0]["derivation"] == "docling-main-band"
|
||||||
|
|
||||||
|
|
||||||
|
def test_auto_map_surfaces_born_digital_part_marks(monkeypatch):
|
||||||
|
# Regression: the born-digital grammar parses per-part marks, but the row mapper hardcoded
|
||||||
|
# max_marks=0. A part band carrying `marks` must flow through to the question row's max_marks.
|
||||||
|
store = _template_with_source()
|
||||||
|
store.update({"exam_questions": [], "exam_response_areas": [], "exam_boundaries": [], "exam_template_layout": []})
|
||||||
|
client, store = make_client(store=store)
|
||||||
|
fp = _first_pass_template()
|
||||||
|
fp["pages"]["1"]["part_bands"][0]["marks"] = 4
|
||||||
|
_patch_auto_map(monkeypatch, store, fast=True)
|
||||||
|
monkeypatch.setattr(templates_mod, "auto_map", lambda *_a, **_k: fp) # override with the marked part band
|
||||||
|
assert client.post("/api/exam/templates/t1/auto-map").status_code == 200
|
||||||
|
part = next(q for q in store["exam_questions"] if q.get("label") == "01.1")
|
||||||
|
assert part["max_marks"] == 4
|
||||||
|
|
||||||
|
|
||||||
|
def test_auto_map_deduplicates_repeated_response_area_ids(monkeypatch):
|
||||||
|
store = _template_with_source()
|
||||||
|
client, store = make_client(store=store)
|
||||||
|
_patch_auto_map(monkeypatch, store, fast=True)
|
||||||
|
dup = {"page_index": 0, "bbox": {"l": 50, "t": 700, "r": 100, "b": 680, "coord_origin": "BOTTOMLEFT"}, "region_type": "answer_lines", "confidence": 0.9}
|
||||||
|
monkeypatch.setattr(templates_mod, "detect_response_regions_from_pdf", lambda *_a, **_k: [dup, dict(dup)])
|
||||||
|
|
||||||
|
resp = client.post("/api/exam/templates/t1/auto-map")
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
response_area_ids = [r["id"] for r in store["exam_response_areas"]]
|
||||||
|
assert len(response_area_ids) == len(set(response_area_ids))
|
||||||
|
|
||||||
|
|
||||||
def test_auto_map_preserves_manual_and_confirmed_rows_on_rerun(monkeypatch):
|
def test_auto_map_preserves_manual_and_confirmed_rows_on_rerun(monkeypatch):
|
||||||
store = _template_with_source()
|
store = _template_with_source()
|
||||||
store.update({
|
store.update({
|
||||||
|
|||||||
Reference in New Issue
Block a user