Metadata-Version: 2.5
Name: pyconverters-mineru
Version: 1.6.89
Summary: Convert PDF and office documents to structured text using a MinerU 4 server
Author-email: Olivier Terrier <olivier.terrier@kairntech.com>
License: Proprietary
License-File: AUTHORS.md
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: License :: Other/Proprietary License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.12
Requires-Dist: beautifulsoup4
Requires-Dist: docvortex<0.7,>=0.6.16
Requires-Dist: markdownify
Requires-Dist: pylatexenc
Requires-Dist: pymultirole-plugins<1.7.0,>=1.6.0
Requires-Dist: pypdf>=5.6.0
Requires-Dist: requests
Provides-Extra: dev
Requires-Dist: pre-commit; extra == 'dev'
Provides-Extra: sbom
Requires-Dist: cyclonedx-bom; extra == 'sbom'
Requires-Dist: pip-audit; extra == 'sbom'
Provides-Extra: test
Requires-Dist: pytest; extra == 'test'
Requires-Dist: pytest-cov; extra == 'test'
Requires-Dist: ruff; extra == 'test'
Description-Content-Type: text/markdown

# pyconverters_mineru

[![license](https://img.shields.io/badge/license-proprietary-red)](LICENSE)
[![version](https://img.shields.io/pypi/v/pyconverters-mineru)](https://pypi.org/project/pyconverters-mineru/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyconverters-mineru)](https://pypi.org/project/pyconverters-mineru/)

Convert PDF, images and office documents to structured text using a
[MinerU 4](https://github.com/opendatalab/MinerU) server

## Installation

You can simply `pip install pyconverters_mineru`.

## MinerU service on CPU

The converter calls the V1 tasks API (`/v1/tasks`) of a MinerU 4 server: MinerU 3 servers
and their `/file_parse` API are not supported any more. `deploy/mineru-cpu` builds an image
serving the two tiers that run without a GPU:

- `flash` reads the text layer of the PDF, with no model: about a second per document, but
  nothing from the text drawn in images, and tables come out as preformatted text;
- `basic` adds the small ONNX models (layout, OCR, tables, formulas): about 1.6 second per
  page with 24 threads, close to the GPU results except for charts and very dense tables.

Office, HTML and EPUB files are parsed natively, at `flash`, whatever the tier.
`deploy/mineru-cpu/results` holds the output of both tiers on the test PDFs.

### Choosing between flash and basic

The tier is a parameter of the converter: choose it per project, after the documents.

|                                 | `flash`                          | `basic`                          |
|---------------------------------|----------------------------------|----------------------------------|
| Speed (24 threads)              | ~1 s per document                | ~1.6 s per page                  |
| Text of a born-digital PDF      | complete                         | complete                         |
| Scanned PDF                     | almost nothing                   | OCR                              |
| Text drawn in images            | missing                          | extracted                        |
| Tables                          | preformatted text, no cells      | Markdown or HTML tables          |
| Titles                          | often plain bold text            | `#`, `##` headings               |

- `flash` fits born-digital documents with a simple layout, when only the text matters
  (reports, contracts, articles), and the indexing of large volumes: if the text can be
  selected in the PDF viewer, `flash` reads it as it is.
- `basic` is needed as soon as there are scans, text inside pictures (brochures, slides
  exported to PDF) or tables whose structure matters. It loses nothing over `flash`, it is
  only slower: when in doubt, take `basic`.

To decide on a corpus, compare the `flash` and `basic` outputs in `deploy/mineru-cpu/results`
for a PDF that looks like it, or convert a few of its documents at both tiers.

### Build and publish the image

```bash
cd deploy/mineru-cpu
docker compose build         # image mineru-api-cpu:4.1.7
docker tag mineru-api-cpu:4.1.7 kairntech/mineru-api-cpu:4.1.7
docker push kairntech/mineru-api-cpu:4.1.7
```

The models (~800 MB) are downloaded at build time and kept in the image (~3.2 GB): no
Internet access is needed at start. `docker compose up -d` in the same directory runs the
service locally on port 9094. The service needs about 3.5 GB of memory.

### Add it to the Sherpa docker-compose

`deploy/mineru-cpu/docker-compose.sherpa.yml` holds the fragment: copy its service under
`services:` of the Sherpa `docker-compose.yml`, at the 4-space indentation of that file.

No port is published: in Sherpa, set the `base_url` of the converter to
`http://sherpa-mineru-cpu:8080`, and its `tier` to `basic` or `flash` (empty: `basic`, the
best tier this server offers). Tune `cpus` to the machine.

## Development

The build is driven by [Task](https://taskfile.dev) and [uv](https://docs.astral.sh/uv/),
with the shared stages coming from the `python-archetype` submodule.

### Getting started

The stages live in a Git submodule, so **clone with `--recurse-submodules`**:

```bash
git clone --recurse-submodules git@bitbucket.org:kairntech/pyconverters_mineru.git
cd pyconverters_mineru
sh -c "$(curl -sSL https://taskfile.dev/install.sh)" -- -d -b ~/.local/bin
task
```

Already cloned without it? The submodule directory is empty, and `task` fails on:

```
task: No Taskfile found at ".../submodules/python-archetype/resources/Taskfile.yml"
```

which means exactly that, and nothing worse:

```bash
git submodule update --init
```

**Task is the only manual prerequisite.** An archetype cannot bootstrap itself: uv and the
Python interpreter install themselves on demand (every task that runs uv depends on an
internal `install-python` task), but the thing that runs them does not. Make sure
`~/.local/bin` is on your `PATH` — that is where `task` and `uv` both land.

### Running the pipeline

```bash
task stages          # print the pipeline stages, in order
task                 # run the pipeline up to (but excluding) py:publish
task -- --skip-tests # same, without the test stage
task up-to -- py:lint # run the pipeline up to and including one stage
task jenkins         # run every stage, exactly what Jenkins runs
```

`task` with no argument is safe by construction: it runs every stage but the last, and that
bound is computed from the `STAGES` list rather than written down. The last stage is the
only one with an effect outside your machine.

`STAGES`, declared once in `Taskfile.yml`, is the single definition of the pipeline order —
so what you run locally is what Jenkins runs.

### Individual stages

| Task                            | Description                                           |
|---------------------------------|-------------------------------------------------------|
| `task py:sync`                  | Install the project and its dependencies (uv sync)    |
| `task py:lint`                  | `ruff check` and `ruff format --check`                |
| `task py:format`                | Reformat the code with ruff                           |
| `task py:test`                  | Run the test suite                                    |
| `task py:test-marker -- <m>`    | Run the tests carrying one pytest marker              |
| `task py:sbom`                  | Generate a CycloneDX SBOM of the resolved environment |
| `task py:check-vulnerabilities` | Check for known CVEs                                  |
| `task py:check-updates`         | Check for dependency updates                          |
| `task py:build`                 | Build the wheel and sdist (uv build)                  |
| `task py:publish`               | Publish the distributions (uv publish)                |
| `task py:version-file`          | Print the path of the file carrying `__version__`     |
| `task py:set-version VERSION=x` | Write that version into it                            |

`uv.lock` is not versioned here, so `py:sync` always resolves from scratch (`--upgrade`):
a stale lock lying around on a machine would otherwise make you test and audit versions the
CI never sees.

### SBOM & vulnerability check

`task py:sbom` and `task py:check-vulnerabilities` wrap the underlying tools. To run them
by hand:

```bash
uv sync --extra test --extra sbom
uv run cyclonedx-py environment -o sbom.cdx.json --output-format json
uv run pip-audit --skip-editable --format json --output audit-report.json
```

Ask for **all** the extras, not just `sbom`: `uv sync` synchronises rather than adds, so
whatever the requested extras do not pull in gets removed. `--extra sbom` on its own
uninstalls `ruff` and `pytest`, leaving an environment that can no longer lint or test.

`--skip-editable` skips the project itself, which no advisory database can know about. The
tasks do the same, and pip-audit still exits non-zero when it finds a real vulnerability —
which is the contract the CI relies on. Avoid `--strict`: it turns "dependency not found on
PyPI" into a fatal error, so it breaks as soon as the local version is not a published one.
