Spaces:
Running on Zero

File size: 3,627 Bytes
2a9b9fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Developer guide

## Project layout

- `streamlit_app.py` implements the interface and ZIP assembly.
- `docflow/conversion.py` renders pages, invokes parsing, postprocesses results, and saves artifacts. `docflow/backends.py` selects the inference transport.
- `runtime/` contains the local GPU worker, its probe, and Dockerfile.
- `scripts/` contains worker startup and the synthetic hosted-service probe.
- `tests/` contains pytest tests. `docs/` contains guides and research.
- `.models/` holds the downloaded model; `conversion_results/` holds outputs. Both are ignored by Git and may contain large or sensitive files.

## Conversion flow

The UI prepares input bytes and page selection, then calls `convert`. PDF pages are rendered with PDFium; image frames are loaded with Pillow. Each page is sent to `parse_page` as PNG and processed with NVIDIA's downloaded `postprocessing.py`. Results include page text, blocks, bounding boxes, raw output, completion state, and available usage. Annotated PNGs draw the returned pixel bounding boxes.

Offline requests go to the worker at `127.0.0.1:8473`. Its `/health` endpoint reports readiness and `/v1/parse` accepts one uploaded page. The worker loads the pinned model locally and serializes inference with a lock. Online requests go to NVIDIA's hosted parse endpoint with the process `NVIDIA_API_KEY`. Never log the key or automatically switch modes after an error.

Results are stored in `conversion_results/<hash>/document.json`, alongside page images and generated files. The hash includes source bytes, mode, configured model revision, page range, and a format version. The hosted service's actual model version is not part of this cache key. Complete results are reused; incomplete results are processed again.

## Checks

### UI previews

The Space layout lives in `gradio_ui.py`, separate from the GPU model initialization in `app.py`. The local app uses Streamlit's native components and the light/dark theme in `.streamlit/config.toml`.

Preview the actual Gradio layout without loading the model:

```powershell

uv run --with gradio==6.31.0 python scripts/preview_ui.py

uv run --with gradio==6.31.0 python scripts/preview_ui.py --stress

```

The first command serves a UI-only preview at `http://127.0.0.1:7860`; parsing is disconnected. The second serves the real upload panel with synthetic files at `http://127.0.0.1:7861`, covering empty, single-file, five-file, long-filename and multilingual cases in narrow and wide containers. These are local development tools, not inference backends. See [UI verification](ui-verification.md) for checks and limits.

### Backend checks

Run from the project root:

```powershell

uv run python -m pytest -q

uv run ty check docflow

uv run ruff check --select E4,E7,E9,F,I docflow scripts tests streamlit_app.py

```

The optional hosted probe sends a generated synthetic image to NVIDIA and requires a configured key:

```powershell

uv run python scripts/probe_online.py

```

Check the worker with `docker logs --tail 20 docflow-nim-worker` and its health endpoint at `http://127.0.0.1:8473/health`. For setup and normal use, see the [user guide](user-guide.md).

## Boundaries

The model snapshot is pinned to `4035ffb1d8f8bafcdb867dfaf6f7514f101464f9`. Preserve NVIDIA's model and tokenizer notices when distributing their files. The Docker build resolves its dependencies during construction and has no separate runtime lockfile. The app is unfinished: see the [README](../README.md) for current scope. The neighboring DocLayout repository is reference material only.