Spaces:
Sleeping
Sleeping
File size: 2,387 Bytes
a68aef0 08b9568 a68aef0 08b9568 a68aef0 08b9568 326321c 08b9568 326321c 08b9568 | 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 | ---
title: Screener OCR API
emoji: 🔎
colorFrom: blue
colorTo: gray
sdk: docker
app_port: 7860
suggested_hardware: cpu-basic
---
# Screener OCR API
Public CPU-only OCR API for scanned PDFs/images. It returns the same `pages_text` style array expected by Screener's current PDF pipeline.
## API
```bash
curl -F "file=@annual-report.pdf" https://YOUR-SPACE.hf.space/ocr
```
Response:
```json
{
"pages_text": ["page 1 text", "page 2 text"],
"page_count": 2,
"processing_ms": 1234
}
```
The caller can read only `pages_text` if it wants to match the existing pipeline exactly.
## Local run
```bash
docker build -t screener-ocr .
docker run --rm -p 7860:7860 screener-ocr
./scripts/smoke_test.sh http://localhost:7860/ocr tilted_test.pdf
```
Without Docker:
```bash
python -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 7860
```
You also need the `tesseract` binary installed locally.
## Query options
- `dpi`: render DPI, default `220`. Higher is often more accurate but slower.
- `psm`: Tesseract page segmentation mode, default `6`. Try `4` or `11` for difficult layouts.
- `deskew`: default `true`.
- `workers`: parallel OCR workers, capped by `OCR_MAX_WORKERS` if set, otherwise by the container CPU quota/cpuset count.
- `first_page` / `last_page`: useful for debugging a subset of a large PDF.
## Environment variables
- `OCR_DPI=220`
- `OCR_PSM=6`
- `OCR_LANG=eng`
- `OCR_MAX_WORKERS`: optional override. Defaults to the container CPU quota/cpuset count, falling back to visible host CPU cores.
- `OCR_PAGE_TIMEOUT_SECONDS=120`
- `MAX_UPLOAD_MB=300`
## Notes
This service intentionally does OCR only. Regular embedded PDF text extraction should stay in the main Screener app.
## Deploy to Hugging Face Space
1. Create a new Space on Hugging Face.
2. Choose:
- **Visibility:** Public
- **SDK:** Docker
- **Hardware:** CPU Basic to start; CPU Upgrade if annual reports are too slow.
3. Push this repository to the Space repo:
```bash
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
git add .
git commit -m "Initial OCR API"
git push hf main
```
Once the Space is running, the OCR endpoint will be:
```text
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/ocr
```
FastAPI docs are available at:
```text
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/docs
```
|