feat(pipeline): extraction OGC via Qwen2.5-VL-3B

Pipeline modulaire remplaçant le monolithe extract_ogc.py (conservé
en legacy pour comparaison).

Modules :
- ingest.py      : PDF → PNG 300dpi avec cache par SHA256
- ocr_qwen.py    : wrapper singleton Qwen2.5-VL-3B (bfloat16, ~7 Go VRAM)
- ocr_glm.py     : wrapper GLM-OCR 0.9B (alternatif, conservé)
- classify.py    : détection type de page + routing par index standard
                   (ordre des 6 pages OGC → -50% d'appels OCR)
- prompts.py     : JSON schemas par type (recueil, concertation 1/2/2/2,
                   preuves) + mots-clés de classification
- checkboxes.py  : détection Accord/Désaccord par densité de pixels
                   (inner-frac 0.35, 17/17 corrects sur échantillon vérifié ;
                   GLM-OCR et Qwen échouent sur les checkboxes, cf.
                   scratch/test_prompt_crop_v2.py)
- extract.py     : orchestration 1 dossier (ingest → classify → OCR →
                   parse JSON tolérant aux boucles + validation ATIH)
- persist.py     : sauvegarde JSON + metadata (pipeline_version,
                   ocr_model, timestamp)
- cli.py         : `python -m pipeline.cli <pdf|dir>`

Temps mesuré : ~35s/dossier (6 pages) sur RTX 5070.

Qwen2.5-VL-3B retenu après comparaison avec GLM-OCR 0.9B, GOT-OCR2.0,
Surya, PaddleOCR (cf. scratch/). Il extrait correctement dp_libelle,
praticien_conseil et les 4 GHM/GHS là où les autres échouent.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dom
2026-04-24 15:05:40 +02:00
parent ddebd8dfbf
commit ed4d9bd765
10 changed files with 704 additions and 0 deletions

45
pipeline/ingest.py Normal file
View File

@@ -0,0 +1,45 @@
"""PDF → images PNG 300 dpi avec cache par hash SHA256."""
import hashlib
import os
from pathlib import Path
from pdf2image import convert_from_path
from PIL import Image
DEFAULT_DPI = 300
CACHE_ROOT = Path(".cache/images")
def pdf_hash(pdf_path: str) -> str:
"""Hash SHA256 court du contenu PDF."""
h = hashlib.sha256()
with open(pdf_path, "rb") as f:
for chunk in iter(lambda: f.read(65536), b""):
h.update(chunk)
return h.hexdigest()[:16]
def pdf_to_images(pdf_path: str, dpi: int = DEFAULT_DPI, cache_root: Path = CACHE_ROOT) -> list[Path]:
"""Convertit un PDF en PNG 300 dpi. Retourne la liste des chemins (1 par page).
Le cache est indexé par hash du PDF : un PDF inchangé n'est jamais reconverti.
"""
cache_root = Path(cache_root)
h = pdf_hash(pdf_path)
out_dir = cache_root / h
out_dir.mkdir(parents=True, exist_ok=True)
existing = sorted(out_dir.glob("page_*.png"))
if existing:
return existing
pages = convert_from_path(pdf_path, dpi)
paths = []
for i, img in enumerate(pages, 1):
p = out_dir / f"page_{i:02d}.png"
img.save(p, "PNG", optimize=True)
paths.append(p)
return paths
def load_image(path: Path) -> Image.Image:
return Image.open(path)