Major OCR infrastructure improvements: - Add persistent SQLite-based job queue for OCR tasks - Implement worker pool with process isolation and auto-restart - Add OCR engine selector dropdown (Tesseract/PaddleOCR) in upload zone - Optimize Tesseract preprocessing based on benchmark results (8x faster) - Add recognize_cif_optimized() with multi-strategy CIF extraction - Add Romanian CIF checksum validation - Increase Telegram long polling timeout from 10s to 30s Squashed commits: - feat(ocr): Implement persistent worker pool with SQLite job queue - feat(ocr): Add OCR engine selector dropdown to upload zone - perf(telegram): Increase long polling timeout from 10s to 30s - perf(ocr): Optimize Tesseract preprocessing based on benchmark results 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
112 lines
4.0 KiB
Markdown
112 lines
4.0 KiB
Markdown
# Implementation Prompt: Persistent OCR Worker + Engine Selection + Job Queue
|
|
|
|
## Context
|
|
|
|
Ești într-un git worktree separat (`feature/ocr-persistent-worker-queue`) pentru implementarea sistemului OCR îmbunătățit.
|
|
|
|
**Branch**: `feature/ocr-persistent-worker-queue`
|
|
**Bază**: `main` (commit 77d74a9)
|
|
|
|
## Task Principal
|
|
|
|
Implementează sistemul OCR cu:
|
|
1. **Worker Persistent** - PaddleOCR încărcat O DATĂ la startup (nu 30s per request)
|
|
2. **Engine Selection** - Parametru API pentru `paddleocr`, `tesseract`, sau `auto`
|
|
3. **Tesseract Optimizat** - Fix inversare imagine + OEM 1 + multi-PSM
|
|
4. **Windows IIS Compatible** - Funcționează cu NSSM service management
|
|
5. **SQLite Job Queue** - Procesare secvențială cu poziție în coadă și estimare timp
|
|
|
|
## Planul Detaliat
|
|
|
|
Citește planul complet în:
|
|
```
|
|
/home/marius/.claude/plans/serene-growing-newell.md
|
|
```
|
|
|
|
## Fișiere de Creat (Noi)
|
|
|
|
1. `backend/modules/data_entry/services/ocr/__init__.py`
|
|
2. `backend/modules/data_entry/services/ocr/ocr_worker_pool.py` - Manager ProcessPoolExecutor
|
|
3. `backend/modules/data_entry/services/ocr/ocr_worker_process.py` - Cod pentru worker process
|
|
4. `backend/modules/data_entry/services/ocr/tesseract_engine.py` - Tesseract optimizat
|
|
5. `backend/modules/data_entry/services/ocr/job_queue.py` - SQLite Job Queue Manager
|
|
6. `backend/modules/data_entry/services/ocr/job_worker.py` - Background worker pentru coadă
|
|
7. `data/ocr_queue/` - Director pentru fișiere în coadă
|
|
|
|
## Fișiere de Modificat
|
|
|
|
1. `backend/modules/data_entry/schemas/ocr.py` - Noi scheme OCRJobResponse
|
|
2. `backend/modules/data_entry/routers/ocr.py` - Endpoint-uri job queue + engine param
|
|
3. `backend/modules/data_entry/services/ocr_service.py` - Folosire worker pool
|
|
4. `backend/modules/data_entry/services/image_preprocessor.py` - Fix inversare Tesseract
|
|
5. `backend/main.py` - Startup/shutdown hooks pentru worker pool + job worker
|
|
|
|
## Ordine Implementare (Faze)
|
|
|
|
### Faza 1: Infrastructură Worker
|
|
1. Creare `services/ocr/__init__.py`
|
|
2. Creare `ocr_worker_pool.py`
|
|
3. Creare `ocr_worker_process.py`
|
|
4. Creare `tesseract_engine.py`
|
|
|
|
### Faza 2: SQLite Job Queue
|
|
5. Creare `job_queue.py` cu schema SQLite
|
|
6. Creare `job_worker.py` background task
|
|
7. Creare director `data/ocr_queue/`
|
|
|
|
### Faza 3: API Integration
|
|
8. Update `schemas/ocr.py` - adăugare OCRJobResponse, OCRJobStatus
|
|
9. Update `routers/ocr.py` - modificare /extract, adăugare /jobs/{id}
|
|
10. Update `main.py` - startup job worker
|
|
|
|
### Faza 4: Tesseract Fix
|
|
11. Fix inversare în `image_preprocessor.py`
|
|
|
|
### Faza 5: Frontend (opțional)
|
|
12. Update componenta OCR pentru polling
|
|
13. Afișare poziție coadă și estimare timp
|
|
|
|
## Criterii Succes
|
|
|
|
- [ ] Prima cerere OCR după restart: <5s (nu 30s)
|
|
- [ ] 10 cereri consecutive fără memory leak
|
|
- [ ] `?engine=tesseract` produce text lizibil
|
|
- [ ] `?engine=paddleocr` funcționează independent
|
|
- [ ] POST /extract returnează instant (<100ms) cu job_id
|
|
- [ ] GET /jobs/{id} returnează poziție corectă în coadă
|
|
- [ ] Estimare timp ±30% din realitate
|
|
- [ ] Jobs expiră și se șterg după 24h
|
|
- [ ] Windows: stop service → no orphan python.exe
|
|
|
|
## Comenzi Utile
|
|
|
|
```bash
|
|
# Start backend (development)
|
|
cd /mnt/e/proiecte/ab-worktrees/ocr-persistent-worker-queue
|
|
./start-test.sh
|
|
|
|
# Verificare OCR
|
|
curl -X POST "http://localhost:8001/api/data-entry/ocr/extract?engine=auto" \
|
|
-H "Authorization: Bearer $TOKEN" \
|
|
-F "file=@test-receipt.jpg"
|
|
|
|
# Poll job status
|
|
curl "http://localhost:8001/api/data-entry/ocr/jobs/{job_id}" \
|
|
-H "Authorization: Bearer $TOKEN"
|
|
|
|
# Queue status
|
|
curl "http://localhost:8001/api/data-entry/ocr/queue/status" \
|
|
-H "Authorization: Bearer $TOKEN"
|
|
```
|
|
|
|
## Notă Importantă
|
|
|
|
Acest worktree este izolat de main. După finalizare, SI DOAR DUPA TOATE TESTELE, OFERA POSIBILITATEA UTILIZATORULUI (NU FACE TU AUTOMAT):
|
|
1. Commit toate schimbările
|
|
2. Push branch-ul: `git push -u origin feature/ocr-persistent-worker-queue`
|
|
3. Creează PR către main
|
|
|
|
## Start
|
|
|
|
Începe cu Faza 1 - creează directorul `services/ocr/` și primul fișier `__init__.py`.
|