Spaces:
Running on Zero

Sanidhya Ravi commited on
Commit
5eb40dc
·
1 Parent(s): 9bed480

Turn the TrOCR demo into an agentic document pipeline

Browse files

The Space did not run. `from_pretrained(".")` loads weights from the repo
root, but no model files were ever committed -- git history shows only
.gitattributes, README.md, app.py and requirements.txt, and the live Space
holds the same four. It crashed on boot. TrOCR now loads from the Hub.

Beyond that fix, the demo becomes an agent: drop in any document, optionally
say what you want, and it works out what the document is, reads it with the
right model, and writes the structured output that suits it -- a spreadsheet
for tabular data, JSON for a form, a summary PDF for prose. The same tools are
published over MCP so external agents can call the Space directly.

Pipeline: ingest -> read -> classify -> tables -> route or plan -> export.

* ingest.py takes images, multi-frame TIFF, HEIC, PDF, DOCX, PPTX, XLSX, text
and ZIP. pypdfium2 bundles its own PDFium, so no poppler/packages.txt.
* Pages arriving with a text layer are used as-is and never reach the GPU.
* router.py settles unambiguous documents with no planner tokens at all;
planner.py handles mixed documents and free-text requests.
* export/ writes markdown, JSON, CSV, XLSX, DOCX, a PDF report and a
searchable PDF, bundled with a manifest recording per-page confidence.

Three things only showed up when run against the real models:

* Qwen3.5 emits tool calls as XML, not JSON. The parser returned zero calls
and the loop degenerated into echoing raw text, so the planner was entirely
broken. It now handles the Qwen3.5, Hermes and GLM formats. Adding a stop
string also cut a planner turn from 24.7s to 3.2s.
* GLM-OCR is prompt-limited, not a general instruction-following VLM. Reading
a table with "Text Recognition:" returns space-aligned prose with nothing to
export, so tabular pages get a "Table Recognition:" pass; and since it has
no classification mode, page types are inferred from the text on the CPU --
which is why reading now comes before classifying.
* Office loaders parsed tables into structure and left page text empty, so a
table-only slide exported an empty document.md. Tables are rendered back
into the text, verified to round-trip without duplicating.

Quota shapes the design: a ZeroGPU visitor gets five minutes a day. The whole
model phase is one GPU session sized by page count, export is deliberately
outside it, and the trace reports GPU work that actually happened rather than
work a tool might have done.

Also restores the sync workflow, which still had the literal placeholder
HF_USERNAME/SPACE_NAME.

135 tests, no network or weights required (DOCAGENT_DISABLE_MODELS=1).

47 files changed +5827 -56
Files changed (47)
.gitattributes+5
.github/workflows/sync-to-hub.ymlAdded+23
.gitignoreAdded+7
README.md+191-3
app.py+227-48
docagent/__init__.pyAdded+37
docagent/agent.pyAdded+124
docagent/backends/__init__.pyAdded+19
docagent/backends/base.pyAdded+70
docagent/backends/glm_ocr.pyAdded+222
docagent/backends/trocr.pyAdded+176
docagent/export/__init__.pyAdded+32
docagent/export/bundle.pyAdded+148
docagent/export/writers.pyAdded+520
docagent/ingest.pyAdded+351
docagent/llm.pyAdded+192
docagent/models.pyAdded+254
docagent/planner.pyAdded+161
docagent/preprocess.pyAdded+171
docagent/router.pyAdded+91
docagent/state.pyAdded+309
docagent/tables.pyAdded+231
docagent/tools.pyAdded+648
mcp_api.pyAdded+284
requirements.txt+30-5
tests/__init__.pyAdded
Empty file
tests/conftest.pyAdded+46
tests/fixtures/agreement.docxAdded
Binary file (35.6 kB). View file
tests/fixtures/batch.zipAdded
Binary file (25.8 kB). View file
tests/fixtures/deck.pptxAdded
Binary file (29.4 kB). View file
tests/fixtures/digital.pdfAdded
Binary file (2.41 kB). View file
tests/fixtures/handwriting.pngAdded
tests/fixtures/invoice.pngAdded
tests/fixtures/multipage.tiffAdded
tests/fixtures/photo.heicAdded
tests/fixtures/prose.pngAdded
tests/fixtures/ruled_table.pngAdded
tests/fixtures/scanned.pdfAdded
Binary file (66.3 kB). View file
tests/fixtures/table.pngAdded
tests/fixtures/workbook.xlsxAdded
Binary file (5.47 kB). View file
tests/make_fixtures.pyAdded+272
tests/test_agent.pyAdded+238
tests/test_backends.pyAdded+230
tests/test_export.pyAdded+159
tests/test_ingest.pyAdded+143
tests/test_mcp_api.pyAdded+90
tests/test_tables.pyAdded+126