Rajeev Pandey commited on
Commit
e1242e7
·
1 Parent(s): 12a86e2

Improve document scrolling and interactive insights

Browse files
TASK_SHEET.md CHANGED
@@ -93,6 +93,7 @@ Every item below must pass before FalconScan is marked ready for GitHub or Huggi
93
  - [x] No more than three highest-confidence dots are shown on the page at once.
94
  - [x] Dots use the reduced 18px control size and can be dragged to uncover document text.
95
  - [x] A readable single-word OCR region can receive a contextual dot when it is not already represented by a verified term.
 
96
 
97
  ### Selection, highlight, and business insight
98
 
@@ -118,6 +119,8 @@ Every item below must pass before FalconScan is marked ready for GitHub or Huggi
118
  - [x] Files over 15 MB are rejected before analysis.
119
  - [x] Uploaded document images and files are not persisted by default.
120
  - [x] Uploaded document previews use a dedicated scroll container with visible native scrollbars.
 
 
121
 
122
  ### Camera and performance
123
 
@@ -129,6 +132,8 @@ Every item below must pass before FalconScan is marked ready for GitHub or Huggi
129
  - [x] Large uploaded images are resized to a maximum 1600px edge in the browser before transfer.
130
  - [x] Small images avoid unnecessary recompression.
131
  - [x] Portable CPU OCR is warmed in the background to reduce first-scan latency.
 
 
132
 
133
  ### Responsive UI and accessibility
134
 
@@ -151,10 +156,11 @@ Every item below must pass before FalconScan is marked ready for GitHub or Huggi
151
  - [x] SME-approved definitions override pending user corrections.
152
  - [x] Feedback and review actions preserve an audit history.
153
  - [x] AI-generated or unmatched explanations are marked unverified.
 
154
 
155
  ### Automated verification recorded
156
 
157
- - [x] Python/API suite: 12 tests passing.
158
  - [x] JavaScript syntax checks: camera, overlay, feedback, and insights scripts passing.
159
  - [x] DOM integration: two terms produced two dots.
160
  - [x] DOM integration: dot click expanded the inline definition.
 
93
  - [x] No more than three highest-confidence dots are shown on the page at once.
94
  - [x] Dots use the reduced 18px control size and can be dragged to uncover document text.
95
  - [x] A readable single-word OCR region can receive a contextual dot when it is not already represented by a verified term.
96
+ - [x] Clicking selectable document text creates a dot at the clicked location without exceeding the three-dot limit.
97
 
98
  ### Selection, highlight, and business insight
99
 
 
119
  - [x] Files over 15 MB are rejected before analysis.
120
  - [x] Uploaded document images and files are not persisted by default.
121
  - [x] Uploaded document previews use a dedicated scroll container with visible native scrollbars.
122
+ - [x] Multi-page PDFs render into one continuous scroll surface with positioned text and dots across pages.
123
+ - [x] DOCX rendering grows with document content rather than stopping at the earlier fixed preview height.
124
 
125
  ### Camera and performance
126
 
 
132
  - [x] Large uploaded images are resized to a maximum 1600px edge in the browser before transfer.
133
  - [x] Small images avoid unnecessary recompression.
134
  - [x] Portable CPU OCR is warmed in the background to reduce first-scan latency.
135
+ - [x] Uploaded images are preprocessed for contrast/sharpness at a bounded 1400px edge.
136
+ - [x] The last four document analyses are cached in memory for immediate repeat uploads.
137
 
138
  ### Responsive UI and accessibility
139
 
 
156
  - [x] SME-approved definitions override pending user corrections.
157
  - [x] Feedback and review actions preserve an audit history.
158
  - [x] AI-generated or unmatched explanations are marked unverified.
159
+ - [x] Confidence combines recognition and knowledge signals using calibrated weighting rather than artificially multiplying scores downward.
160
 
161
  ### Automated verification recorded
162
 
163
+ - [x] Python/API suite: 15 tests passing.
164
  - [x] JavaScript syntax checks: camera, overlay, feedback, and insights scripts passing.
165
  - [x] DOM integration: two terms produced two dots.
166
  - [x] DOM integration: dot click expanded the inline definition.
backend/document_service.py CHANGED
@@ -1,11 +1,13 @@
1
  import base64
 
2
  import io
3
  import textwrap
4
  import zipfile
 
5
  from pathlib import Path
6
  from xml.etree import ElementTree
7
 
8
- from PIL import Image, ImageDraw, ImageFont
9
 
10
  from backend.glossary_service import GlossaryService
11
  from backend.ocr_service import OCRService
@@ -13,10 +15,12 @@ from backend.ocr_service import OCRService
13
 
14
  class DocumentService:
15
  MAX_BYTES = 15 * 1024 * 1024
 
16
 
17
  def __init__(self, ocr: OCRService, glossary: GlossaryService):
18
  self.ocr = ocr
19
  self.glossary = glossary
 
20
 
21
  def analyze(self, encoded: str, filename: str, language: str) -> dict:
22
  try:
@@ -25,14 +29,23 @@ class DocumentService:
25
  raise ValueError("Invalid uploaded document") from exc
26
  if not raw or len(raw) > self.MAX_BYTES:
27
  raise ValueError("Document must be between 1 byte and 15 MB")
 
 
 
 
28
  extension = Path(filename).suffix.lower()
29
  if extension in {".jpg", ".jpeg", ".png", ".webp"}:
30
- return self._image(raw, language)
31
- if extension == ".pdf":
32
- return self._pdf(raw)
33
- if extension == ".docx":
34
- return self._docx(raw)
35
- raise ValueError("Supported formats are JPG, PNG, WebP, PDF, and DOCX")
 
 
 
 
 
36
 
37
  def _image(self, raw: bytes, language: str) -> dict:
38
  try:
@@ -40,7 +53,9 @@ class DocumentService:
40
  image.load()
41
  except Exception as exc:
42
  raise ValueError("The uploaded image could not be opened") from exc
43
- detections = self.ocr.extract(image, language)
 
 
44
  terms = self.glossary.match_regions(detections)
45
  if len(terms) < 3:
46
  terms.extend(self.glossary.contextual_fallbacks(detections, 3 - len(terms), terms))
@@ -56,28 +71,51 @@ class DocumentService:
56
  document = fitz.open(stream=raw, filetype="pdf")
57
  if document.page_count < 1:
58
  raise ValueError("The PDF has no pages")
59
- page = document[0]
60
- zoom = min(2.0, 1800 / max(page.rect.width, page.rect.height))
61
- matrix = fitz.Matrix(zoom, zoom)
62
- pixmap = page.get_pixmap(matrix=matrix, alpha=False)
63
- image = Image.frombytes("RGB", [pixmap.width, pixmap.height], pixmap.samples)
 
 
 
 
 
 
 
 
64
  regions = []
65
- for block in page.get_text("blocks"):
66
- x1, y1, x2, y2, text = block[:5]
67
- if text.strip():
68
- regions.append({"text": text, "bbox": [x1 * zoom, y1 * zoom, x2 * zoom, y2 * zoom],
69
- "confidence": 1.0, "language": self._language(text)})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  terms = self.glossary.match_regions(regions)
71
  if len(terms) < 3:
72
  terms.extend(self.glossary.contextual_fallbacks(regions, 3 - len(terms), terms))
73
- if not regions:
74
- detections = self.ocr.extract(image, "en")
75
- terms = self.glossary.match_regions(detections)
76
- if len(terms) < 3:
77
- terms.extend(self.glossary.contextual_fallbacks(detections, 3 - len(terms), terms))
78
- _, unknown = self.glossary.match_ocr(detections)
79
- return self._result(image, terms, detections, unknown, "scanned_pdf_ocr")
80
- return self._result(image, terms, regions, [], "pdf_text")
81
  except (ValueError, RuntimeError):
82
  raise
83
  except Exception as exc:
@@ -105,7 +143,7 @@ class DocumentService:
105
  for paragraph in paragraphs:
106
  wrapped.extend(textwrap.wrap(paragraph, width=85, break_long_words=False) or [""])
107
  wrapped.append("")
108
- height = min(2200, max(900, margin * 2 + len(wrapped) * line_height))
109
  image = Image.new("RGB", (width, height), "#ffffff")
110
  draw = ImageDraw.Draw(image)
111
  try:
@@ -134,7 +172,7 @@ class DocumentService:
134
  @staticmethod
135
  def _result(image: Image.Image, terms: list, detections: list, unknown: list, method: str) -> dict:
136
  output = io.BytesIO()
137
- image.save(output, format="JPEG", quality=88, optimize=True)
138
  return {
139
  "detected_terms": terms,
140
  "ocr_items": detections,
 
1
  import base64
2
+ import hashlib
3
  import io
4
  import textwrap
5
  import zipfile
6
+ from collections import OrderedDict
7
  from pathlib import Path
8
  from xml.etree import ElementTree
9
 
10
+ from PIL import Image, ImageDraw, ImageFilter, ImageFont, ImageOps
11
 
12
  from backend.glossary_service import GlossaryService
13
  from backend.ocr_service import OCRService
 
15
 
16
  class DocumentService:
17
  MAX_BYTES = 15 * 1024 * 1024
18
+ MAX_PDF_PAGES = 12
19
 
20
  def __init__(self, ocr: OCRService, glossary: GlossaryService):
21
  self.ocr = ocr
22
  self.glossary = glossary
23
+ self._cache: OrderedDict[str, dict] = OrderedDict()
24
 
25
  def analyze(self, encoded: str, filename: str, language: str) -> dict:
26
  try:
 
29
  raise ValueError("Invalid uploaded document") from exc
30
  if not raw or len(raw) > self.MAX_BYTES:
31
  raise ValueError("Document must be between 1 byte and 15 MB")
32
+ cache_key = hashlib.sha256(raw + filename.lower().encode() + language.encode()).hexdigest()
33
+ if cache_key in self._cache:
34
+ self._cache.move_to_end(cache_key)
35
+ return {**self._cache[cache_key], "cached": True}
36
  extension = Path(filename).suffix.lower()
37
  if extension in {".jpg", ".jpeg", ".png", ".webp"}:
38
+ result = self._image(raw, language)
39
+ elif extension == ".pdf":
40
+ result = self._pdf(raw)
41
+ elif extension == ".docx":
42
+ result = self._docx(raw)
43
+ else:
44
+ raise ValueError("Supported formats are JPG, PNG, WebP, PDF, and DOCX")
45
+ self._cache[cache_key] = result
46
+ while len(self._cache) > 4:
47
+ self._cache.popitem(last=False)
48
+ return {**result, "cached": False}
49
 
50
  def _image(self, raw: bytes, language: str) -> dict:
51
  try:
 
53
  image.load()
54
  except Exception as exc:
55
  raise ValueError("The uploaded image could not be opened") from exc
56
+ image.thumbnail((1400, 1400), Image.Resampling.LANCZOS)
57
+ prepared = ImageOps.autocontrast(image.convert("L")).filter(ImageFilter.SHARPEN).convert("RGB")
58
+ detections = self.ocr.extract(prepared, language)
59
  terms = self.glossary.match_regions(detections)
60
  if len(terms) < 3:
61
  terms.extend(self.glossary.contextual_fallbacks(detections, 3 - len(terms), terms))
 
71
  document = fitz.open(stream=raw, filetype="pdf")
72
  if document.page_count < 1:
73
  raise ValueError("The PDF has no pages")
74
+ page_count = min(document.page_count, self.MAX_PDF_PAGES)
75
+ rendered = []
76
+ canvas_width = 0
77
+ for index in range(page_count):
78
+ page = document[index]
79
+ zoom = min(1.5, 1100 / max(1, page.rect.width))
80
+ pixmap = page.get_pixmap(matrix=fitz.Matrix(zoom, zoom), alpha=False)
81
+ page_image = Image.frombytes("RGB", [pixmap.width, pixmap.height], pixmap.samples)
82
+ rendered.append((page, page_image, zoom))
83
+ canvas_width = max(canvas_width, page_image.width)
84
+ gap = 18
85
+ total_height = sum(item[1].height for item in rendered) + gap * max(0, page_count - 1)
86
+ image = Image.new("RGB", (canvas_width, total_height), "#dfe3df")
87
  regions = []
88
+ y_offset = 0
89
+ for page, page_image, zoom in rendered:
90
+ x_offset = (canvas_width - page_image.width) // 2
91
+ image.paste(page_image, (x_offset, y_offset))
92
+ page_regions = []
93
+ for block in page.get_text("blocks"):
94
+ x1, y1, x2, y2, text = block[:5]
95
+ if text.strip():
96
+ page_regions.append({
97
+ "text": text,
98
+ "bbox": [x_offset + x1 * zoom, y_offset + y1 * zoom,
99
+ x_offset + x2 * zoom, y_offset + y2 * zoom],
100
+ "confidence": 1.0,
101
+ "language": self._language(text),
102
+ })
103
+ if page_regions:
104
+ regions.extend(page_regions)
105
+ else:
106
+ page_detections = self.ocr.extract(page_image, "en")
107
+ for item in page_detections:
108
+ item["bbox"] = [item["bbox"][0] + x_offset, item["bbox"][1] + y_offset,
109
+ item["bbox"][2] + x_offset, item["bbox"][3] + y_offset]
110
+ regions.extend(page_detections)
111
+ y_offset += page_image.height + gap
112
  terms = self.glossary.match_regions(regions)
113
  if len(terms) < 3:
114
  terms.extend(self.glossary.contextual_fallbacks(regions, 3 - len(terms), terms))
115
+ result = self._result(image, terms, regions, [], "pdf_multpage")
116
+ result.update({"page_count": document.page_count, "pages_analyzed": page_count,
117
+ "truncated": document.page_count > page_count})
118
+ return result
 
 
 
 
119
  except (ValueError, RuntimeError):
120
  raise
121
  except Exception as exc:
 
143
  for paragraph in paragraphs:
144
  wrapped.extend(textwrap.wrap(paragraph, width=85, break_long_words=False) or [""])
145
  wrapped.append("")
146
+ height = max(900, margin * 2 + len(wrapped) * line_height)
147
  image = Image.new("RGB", (width, height), "#ffffff")
148
  draw = ImageDraw.Draw(image)
149
  try:
 
172
  @staticmethod
173
  def _result(image: Image.Image, terms: list, detections: list, unknown: list, method: str) -> dict:
174
  output = io.BytesIO()
175
+ image.save(output, format="JPEG", quality=80, optimize=True)
176
  return {
177
  "detected_terms": terms,
178
  "ocr_items": detections,
backend/glossary_service.py CHANGED
@@ -144,8 +144,12 @@ class GlossaryService:
144
  "ocr_confidence": region.get("confidence", 1.0),
145
  "language": region.get("language", "en"),
146
  })
 
 
 
 
147
  definition["confidence"] = round(
148
- definition["confidence"] * float(region.get("confidence", 1.0)), 3
149
  )
150
  found.append(definition)
151
  return found
 
144
  "ocr_confidence": region.get("confidence", 1.0),
145
  "language": region.get("language", "en"),
146
  })
147
+ knowledge_confidence = float(definition["confidence"])
148
+ recognition_confidence = float(region.get("confidence", 1.0))
149
+ # Calibrate two independent signals instead of multiplying them into an
150
+ # artificially low score. OCR remains the dominant factor.
151
  definition["confidence"] = round(
152
+ 0.72 * recognition_confidence + 0.28 * knowledge_confidence, 3
153
  )
154
  found.append(definition)
155
  return found
data/project_progress.json CHANGED
@@ -5,7 +5,7 @@
5
  "current_module": "Representative device validation",
6
  "pending_modules": ["Install production OCR models", "Configure optional hosted AI endpoint", "Configure optional VLM endpoint", "Persistent production database"],
7
  "open_issues": ["Local JSON storage is single-instance and ephemeral on default free Spaces", "PaddleOCR model download adds cold-start time", "Remote AI and VLM adapters require provider-specific endpoint contracts"],
8
- "last_completed_step": "Added scrollable document stage, three small draggable dots, full-page Info summary, faster multi-word selection, and removed right-panel selection history",
9
  "next_recommended_step": "Run the remaining physical iPhone/Android acceptance checks, then execute GitHub CI and Hugging Face smoke tests after remotes are configured",
10
  "last_updated": "2026-06-20T12:00:00Z"
11
  }
 
5
  "current_module": "Representative device validation",
6
  "pending_modules": ["Install production OCR models", "Configure optional hosted AI endpoint", "Configure optional VLM endpoint", "Persistent production database"],
7
  "open_issues": ["Local JSON storage is single-instance and ephemeral on default free Spaces", "PaddleOCR model download adds cold-start time", "Remote AI and VLM adapters require provider-specific endpoint contracts"],
8
+ "last_completed_step": "Added continuous multi-page scrolling, click-text-to-dot interaction, calibrated confidence, OCR preprocessing, and repeat-upload caching",
9
  "next_recommended_step": "Run the remaining physical iPhone/Android acceptance checks, then execute GitHub CI and Hugging Face smoke tests after remotes are configured",
10
  "last_updated": "2026-06-20T12:00:00Z"
11
  }
frontend/camera.js CHANGED
@@ -218,16 +218,16 @@
218
  const objectUrl = URL.createObjectURL(file);
219
  try {
220
  const image = await loadImage(objectUrl);
221
- const maxEdge = 1600;
222
  const scale = Math.min(1, maxEdge / Math.max(image.naturalWidth, image.naturalHeight));
223
- if (scale === 1 && file.size < 2.5 * 1024 * 1024) {
224
  return { base64: await readBase64(file), filename: file.name };
225
  }
226
  const canvas = document.createElement("canvas");
227
  canvas.width = Math.max(1, Math.round(image.naturalWidth * scale));
228
  canvas.height = Math.max(1, Math.round(image.naturalHeight * scale));
229
  canvas.getContext("2d").drawImage(image, 0, 0, canvas.width, canvas.height);
230
- const blob = await new Promise((resolve) => canvas.toBlob(resolve, "image/jpeg", 0.84));
231
  return { base64: await readBase64(blob), filename: "optimized-upload.jpg" };
232
  } finally {
233
  URL.revokeObjectURL(objectUrl);
 
218
  const objectUrl = URL.createObjectURL(file);
219
  try {
220
  const image = await loadImage(objectUrl);
221
+ const maxEdge = 1400;
222
  const scale = Math.min(1, maxEdge / Math.max(image.naturalWidth, image.naturalHeight));
223
+ if (scale === 1 && file.size < 2 * 1024 * 1024) {
224
  return { base64: await readBase64(file), filename: file.name };
225
  }
226
  const canvas = document.createElement("canvas");
227
  canvas.width = Math.max(1, Math.round(image.naturalWidth * scale));
228
  canvas.height = Math.max(1, Math.round(image.naturalHeight * scale));
229
  canvas.getContext("2d").drawImage(image, 0, 0, canvas.width, canvas.height);
230
+ const blob = await new Promise((resolve) => canvas.toBlob(resolve, "image/jpeg", 0.80));
231
  return { base64: await readBase64(blob), filename: "optimized-upload.jpg" };
232
  } finally {
233
  URL.revokeObjectURL(objectUrl);
frontend/overlay.js CHANGED
@@ -114,6 +114,49 @@ window.FalconOverlay = (() => {
114
  marker.addEventListener("pointercancel", finish);
115
  }
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  function openPopover(item, marker, layout) {
118
  closePopover();
119
  marker.classList.add("active");
@@ -160,6 +203,7 @@ window.FalconOverlay = (() => {
160
  span.style.width = `${Math.max(8, (x2 - x1) * layout.scale)}px`;
161
  span.style.height = `${Math.max(12, (y2 - y1) * layout.scale)}px`;
162
  span.style.fontSize = `${clamp((y2 - y1) * layout.scale * 0.72, 8, 24)}px`;
 
163
  layer.appendChild(span);
164
  });
165
  const explainSelection = () => {
 
114
  marker.addEventListener("pointercancel", finish);
115
  }
116
 
117
+ function triggerDotAtText(event, region, layout) {
118
+ const selection = window.getSelection();
119
+ if (selection && !selection.isCollapsed && selection.toString().trim()) return;
120
+ const existingMarkers = [...overlay.querySelectorAll(".term-marker")];
121
+ if (existingMarkers.length >= 3) {
122
+ (overlay.querySelector(".term-marker.user-triggered") || existingMarkers[existingMarkers.length - 1]).remove();
123
+ }
124
+ const match = terms.find((item) =>
125
+ item.bbox?.every((value, index) => Math.abs(value - region.bbox[index]) < 2)
126
+ || String(region.text).toLowerCase().includes(String(item.term).toLowerCase())
127
+ );
128
+ const item = match || {
129
+ term: String(region.text).trim().slice(0, 58),
130
+ definition: "Select this phrase for a contextual summary and business meaning.",
131
+ confidence: Math.min(.72, Number(region.confidence || .75) * .72),
132
+ source: "ai_generated_unverified",
133
+ source_label: "Document context · needs verification",
134
+ category: "Document Context",
135
+ related_terms: [],
136
+ document_types: [],
137
+ bbox: region.bbox,
138
+ };
139
+ const overlayRect = overlay.getBoundingClientRect();
140
+ const marker = document.createElement("button");
141
+ marker.className = "term-marker user-triggered";
142
+ marker.type = "button";
143
+ marker.style.left = `${clamp(event.clientX - overlayRect.left - 9, 5, layout.viewRect.width - 23)}px`;
144
+ marker.style.top = `${clamp(event.clientY - overlayRect.top - 9, 5, layout.viewRect.height - 23)}px`;
145
+ marker.setAttribute("aria-label", `Explain ${item.term}`);
146
+ marker.innerHTML = "<span></span>";
147
+ let dragged = false;
148
+ enableDrag(marker, layout, () => { dragged = true; });
149
+ marker.onclick = (clickEvent) => {
150
+ clickEvent.stopPropagation();
151
+ if (dragged) {
152
+ dragged = false;
153
+ return;
154
+ }
155
+ openPopover(item, marker, layout);
156
+ };
157
+ overlay.appendChild(marker);
158
+ }
159
+
160
  function openPopover(item, marker, layout) {
161
  closePopover();
162
  marker.classList.add("active");
 
203
  span.style.width = `${Math.max(8, (x2 - x1) * layout.scale)}px`;
204
  span.style.height = `${Math.max(12, (y2 - y1) * layout.scale)}px`;
205
  span.style.fontSize = `${clamp((y2 - y1) * layout.scale * 0.72, 8, 24)}px`;
206
+ span.addEventListener("click", (event) => triggerDotAtText(event, region, layout));
207
  layer.appendChild(span);
208
  });
209
  const explainSelection = () => {
frontend/styles.css CHANGED
@@ -50,7 +50,7 @@ main { width: 100%; max-width: 1320px; margin: auto; padding: 30px 14px calc(64p
50
  .scanner-shell { display: flex; flex-direction: column; min-height: 0; overflow: hidden; background: var(--white); border: 1px solid rgba(20,35,31,.06); border-radius: 20px; box-shadow: var(--shadow); transition: grid-template-columns .42s cubic-bezier(.22,1,.36,1); }
51
  .viewport { position: relative; min-height: min(62svh, 560px); background: radial-gradient(circle at center, #23423a, #071a18 72%); overflow: hidden; }
52
  .viewport video { position: absolute; width: 100%; height: 100%; object-fit: contain; }
53
- .document-scroller { position: absolute; inset: 0; z-index: 1; overflow: auto; overscroll-behavior: contain; scrollbar-gutter: stable; scrollbar-color: #71827c #162a25; scrollbar-width: auto; background: #152622; }
54
  .document-scroller[hidden] { display: none; }
55
  .document-scroller::-webkit-scrollbar { width: 11px; height: 11px; }
56
  .document-scroller::-webkit-scrollbar-track { background: #162a25; }
@@ -80,7 +80,9 @@ main { width: 100%; max-width: 1320px; margin: auto; padding: 30px 14px calc(64p
80
  .document-info span { display: grid; place-items: center; width: 25px; height: 25px; background: var(--lime); color: var(--dark); border-radius: 50%; font-family: Georgia, serif; font-weight: 800; }
81
  .document-info b { display: none; font-size: 10px; }
82
  .selection-layer { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
83
- .selectable-region { position: absolute; display: block; overflow: hidden; color: transparent; line-height: 1; white-space: pre-wrap; user-select: text; -webkit-user-select: text; pointer-events: auto; cursor: text; }
 
 
84
  .selectable-region::selection { background: rgba(201,242,87,.72); color: rgba(8,28,24,.9); }
85
  @keyframes pop { from { scale: .2; opacity: 0; } }
86
  .corner { position: absolute; width: 34px; height: 34px; border-color: var(--lime); border-style: solid; opacity: .8; }
 
50
  .scanner-shell { display: flex; flex-direction: column; min-height: 0; overflow: hidden; background: var(--white); border: 1px solid rgba(20,35,31,.06); border-radius: 20px; box-shadow: var(--shadow); transition: grid-template-columns .42s cubic-bezier(.22,1,.36,1); }
51
  .viewport { position: relative; min-height: min(62svh, 560px); background: radial-gradient(circle at center, #23423a, #071a18 72%); overflow: hidden; }
52
  .viewport video { position: absolute; width: 100%; height: 100%; object-fit: contain; }
53
+ .document-scroller { position: absolute; inset: 0; z-index: 1; overflow: auto; overscroll-behavior: contain; scrollbar-gutter: stable; scrollbar-color: #71827c #162a25; scrollbar-width: auto; background: #152622; -webkit-overflow-scrolling: touch; touch-action: pan-x pan-y; }
54
  .document-scroller[hidden] { display: none; }
55
  .document-scroller::-webkit-scrollbar { width: 11px; height: 11px; }
56
  .document-scroller::-webkit-scrollbar-track { background: #162a25; }
 
80
  .document-info span { display: grid; place-items: center; width: 25px; height: 25px; background: var(--lime); color: var(--dark); border-radius: 50%; font-family: Georgia, serif; font-weight: 800; }
81
  .document-info b { display: none; font-size: 10px; }
82
  .selection-layer { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
83
+ .selectable-region { position: absolute; display: block; overflow: hidden; color: transparent; line-height: 1; white-space: pre-wrap; user-select: text; -webkit-user-select: text; pointer-events: auto; cursor: text; touch-action: pan-x pan-y; }
84
+ .term-marker.user-triggered { animation: marker-pulse .28s cubic-bezier(.22,1,.36,1); }
85
+ @keyframes marker-pulse { from { opacity: 0; transform: scale(.2); } 70% { transform: scale(1.25); } }
86
  .selectable-region::selection { background: rgba(201,242,87,.72); color: rgba(8,28,24,.9); }
87
  @keyframes pop { from { scale: .2; opacity: 0; } }
88
  .corner { position: absolute; width: 34px; height: 34px; border-color: var(--lime); border-style: solid; opacity: .8; }
tests/test_services.py CHANGED
@@ -125,3 +125,41 @@ def test_scanner_serves_scroll_info_and_clean_status_controls():
125
  assert 'id="stabilityMetric"' not in html
126
  assert 'id="lightMetric"' not in html
127
  assert 'id="ocrMetric"' not in html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  assert 'id="stabilityMetric"' not in html
126
  assert 'id="lightMetric"' not in html
127
  assert 'id="ocrMetric"' not in html
128
+
129
+
130
+ def test_multipage_pdf_is_rendered_as_one_scroll_surface():
131
+ import fitz
132
+ document = fitz.open()
133
+ first = document.new_page()
134
+ first.insert_text((72, 100), "Commercial Invoice and HS Code", fontsize=16)
135
+ second = document.new_page()
136
+ second.insert_text((72, 100), "Packing List and Customs Duty", fontsize=16)
137
+ response = client.post("/analyze-document", json={
138
+ "file_base64": base64.b64encode(document.tobytes()).decode(),
139
+ "filename": "two-pages.pdf",
140
+ "language_preference": "en",
141
+ })
142
+ assert response.status_code == 200
143
+ data = response.json()
144
+ assert data["page_count"] == 2
145
+ assert data["pages_analyzed"] == 2
146
+ assert data["frame_height"] > data["frame_width"] * 2
147
+ assert {item["term"] for item in data["detected_terms"]} >= {"HS Code", "Commercial Invoice"}
148
+
149
+
150
+ def test_document_cache_makes_repeat_upload_fast_path():
151
+ xml = '''<?xml version="1.0"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>Delivery Order</w:t></w:r></w:p></w:body></w:document>'''
152
+ buffer = io.BytesIO()
153
+ with zipfile.ZipFile(buffer, "w") as archive:
154
+ archive.writestr("word/document.xml", xml)
155
+ payload = {"file_base64": base64.b64encode(buffer.getvalue()).decode(), "filename": "repeat.docx", "language_preference": "en"}
156
+ assert client.post("/analyze-document", json=payload).status_code == 200
157
+ second = client.post("/analyze-document", json=payload)
158
+ assert second.status_code == 200
159
+ assert second.json()["cached"] is True
160
+
161
+
162
+ def test_confidence_calibration_does_not_multiply_down_verified_match():
163
+ service = GlossaryService(ROOT / "data/glossary.json", ROOT / "data/user_corrections.json", ROOT / "data/sme_approved_definitions.json")
164
+ terms = service.match_regions([{"text": "HS Code", "bbox": [0, 0, 100, 20], "confidence": .8, "language": "en"}])
165
+ assert terms[0]["confidence"] > .8