File size: 6,611 Bytes
8c3e275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!--
SPDX-FileCopyrightText: 2026 Team Centurions
SPDX-License-Identifier: AGPL-3.0-or-later
-->

# Technical Specification Kit β€” PageParse

**Any handwritten page β†’ clean structured data. On CPU. Fully offline.**

PageParse is designed for **The CPU-First Hackathon** to process unstructured handwriting, clean the image using computer vision, perform OCR, structure the text using an offline small language model, and persist it to SQLite β€” completely offline.

---

## 1. High-Level Architecture

```
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚                  PageParse                    β”‚
                       β”‚            (100% on-device, CPU)              β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

  scan / photo       OpenCV             ONNX / Tesseract     llama.cpp + GBNF      SQLite
  JPG Β· PNG Β· PDF  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  ───────────────▢│ Ingestion│──────▢│  Preprocessing │──▢│  OCR (on CPU)  │──▢│ Transform│──▢│ Storage β”‚
                  │          │ clean │  deskew·binarize│   │ TrOCR / Surya /│   │ SLM→JSON │   │  + vec  │
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ denoiseβ””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚   Tesseract    β”‚   β”‚ validatedβ”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                                                        β”‚
                                                                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                                                              β”‚ query Β· search    β”‚
                                                                              β”‚ export Β· review   β”‚
                                                                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

The system operates strictly in an **offline-first/air-gapped** mode. All models run locally on the CPU.

---

## 2. Ingestion & Preprocessing Specification

### 2.1 Ingestion
- **Inputs:** Scan or photograph of a handwritten page (`.jpg`, `.jpeg`, `.png`, `.bmp`, `.tiff`, `.webp`, `.pdf`).
- **PWA/Web File Handler:** FastAPI multipart upload endpoint.
- **CLI File Handler:** Recursively globbing directory files for batch processing.

### 2.2 OpenCV Preprocessing
To improve OCR transcription accuracy, raw inputs undergo:
1. **Grayscale conversion:** Simplifies the image channels.
2. **Adaptive thresholding:** Binarizes the image using Gaussian adaptive thresholding, coping with uneven lighting.
3. **Denoising:** Median filter to eliminate speckles and camera noise.
4. **Deskewing:** Calculates orientation of written text line bounding boxes and rotates back to horizontal.

---

## 3. Model & Runtime Specification

No GPU or CUDA execution providers are loaded. Model formats are standardized on ONNX and GGUF.

| Stage | Task | Model | Runtime / Engine | Precision |
|---|---|---|---|---|
| **OCR (Handwritten)** | Local offline line recognition | `TrOCR-small-handwritten` | ONNX Runtime (CPU EP) | INT8 |
| **OCR (Printed)** | OCR for structured clean documents | `Tesseract LSTM` | Tesseract Binaries | N/A |
| **Transformation** | Schema Mapping & Structuring | `Qwen2.5-1.5B-Instruct` | llama.cpp (`llama-cpp-python`) | Q4_K_M |
| **Semantic Embeddings**| Embedding generation for index | `all-MiniLM-L6-v2` | ONNX Runtime (CPU EP) | INT8 |

### 3.1 GBNF Grammar-Constrained Decoding
To ensure 100% compliance with the JSON output schema, the small language model (SLM) is constrained by a GBNF grammar (`grammars/task.gbnf`). This eliminates model hallucination, syntax errors, and missing fields.

---

## 4. Schema & Data Model Specifications

### 4.1 Input-Output JSON Schema
Handwriting is parsed and mapped to the following schema structure:
```json
{
  "source_file": "string",
  "captured_date": "YYYY-MM-DD",
  "records": [
    {
      "type": "task | action_item | note | key_value | transcript_segment",
      "content": "string",
      "due_date": "YYYY-MM-DD | null",
      "priority": "high | medium | low | null",
      "category": "string | null",
      "speaker": "string | null",
      "timestamp": "string | null",
      "status": "todo | done | null",
      "confidence": 0.0
    }
  ]
}
```

### 4.2 Database Schema (SQLite)

```sql
CREATE TABLE IF NOT EXISTS sources (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    filename TEXT NOT NULL UNIQUE,
    source_type TEXT NOT NULL, -- image, audio, video, document
    image_path TEXT,
    cleaned_image_path TEXT,
    captured_date TEXT,
    raw_text TEXT NOT NULL,
    summary TEXT,
    created_at TEXT DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS records (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    source_id INTEGER NOT NULL,
    type TEXT NOT NULL,
    content TEXT NOT NULL,
    due_date TEXT,
    priority TEXT CHECK(priority IN ('high', 'medium', 'low') OR priority IS NULL),
    category TEXT,
    speaker TEXT,
    timestamp TEXT,
    status TEXT CHECK(status IN ('todo', 'done') OR status IS NULL),
    confidence REAL DEFAULT 1.0,
    user_edited INTEGER DEFAULT 0,
    FOREIGN KEY(source_id) REFERENCES sources(id) ON DELETE CASCADE
);
```

---

## 5. Telemetry & Observability Panel

- **Process Telemetry:** Tracks CPU percentage utilization and RAM usage (MB) during inference using `psutil`.
- **System Telemetry:** Visible on the web dashboard to demonstrate the exact physical load of the CPU-only model execution.

---

## 6. Offline Resiliency & Air-Gap Mode

- **Air-gap Toggle:** Command-line argument `--airgap` and Web UI switch that disables fallback external translate calls, proving total isolation.
- **Local Fallback Translation:** Includes static multi-language translation dictionaries for key hackathon demo texts (Hindi, Telugu, Tamil) so that translation features are testable offline.