papie lemmatization · tagging · any language

Sharing models

Export a model as a pickle-free folder, push it to the Hugging Face Hub, load it back.

Generated from the source by tools/make_api.py; do not edit by hand.

papie.hub

Share trained models: export to a self-contained folder, push it to the Hugging Face Hub, pull it back.

A published model is a folder of plain files, with no pickles (so loading one downloaded from the Hub cannot run code):

    config.json         architecture: tasks, model options, inference options, format version
    label_encoder.json  vocabularies (characters/bytes, words, one per task)
    model.safetensors   weights
    README.md           model card

    python -m papie.cli export-model --ckpt best.ckpt --out my-model --license cc-by-4.0 --language grc
    python -m papie.cli push-model my-model org/pie-thunder-grc-lemma-pos          # private by default
    >>> from papie.hub import from_pretrained
    >>> tagger = from_pretrained("org/pie-thunder-grc-lemma-pos", revision="<commit sha>")
    >>> tagger.annotate([["τὰ", "γὰρ", "πρὸ"]])

Models with a frozen external encoder (byt5 or soter) are not supported yet: their encoder weights are not part
of the checkpoint.

public_candidates

public_candidates(cfg)
Candidate-feature config safe to publish: only self-contained sources (Collatinus, importable callables) are
allowed, and local paths (package_path, cache_path) are removed.

export_model

export_model(ckpt, out_dir, *, license=None, languages=(), description='', metrics=None, dataset_note='', max_sent_len=None, keep_stats=False, name=None, base_model=None)
Write the publishable folder for a trained Lightning checkpoint (or a PaPie instance).

render_card

render_card(config, mle, n_params, *, license, description, metrics, dataset_note, name, base_model=None)
Model card. Only states what is known: the license is whatever the publisher passes (none is assumed).

update_manifest

update_manifest(root, variant, *, description='', requires=(), metrics=None, make_default=False, languages=(), dataset_note='', license=None)
Register variant (a sub-folder of root written by export_model) in root/papie.json and
refresh the repository card. A repository with a manifest holds several models that from_pretrained can pick
between with variant=; the first registered one is the default unless another is marked.

render_repo_card

render_repo_card(name, manifest, license=None)

available_variants

available_variants(repo_or_path, revision=None, token=None)
Names of the variants of a local folder or Hub repo ([] for a single-model repository).

class Annotator

Annotator(self, model, max_sent_len=None, device='cpu')
A loaded model that tags tokenised sentences.

Annotator.only

only(self, *tasks)
Keep only these heads (a head that is not run costs nothing; one that feeds another via inputs_from is
still computed inside it). Returns self.

Annotator.disable

disable(self, *tasks)
Switch these heads off (they are neither run nor returned). Returns self; enable_all() undoes it.

Annotator.enable_all

enable_all(self)

Annotator.quantize

quantize(self)
INT8 dynamic quantization (CPU only; about 1.5x faster, accuracy unchanged in our tests).

Annotator.annotate

annotate(self, sentences: Sequence[Sequence[str]], batch_size: int=64, workers: int=1, shard_size: int=512) -> List[List[Dict[str, str]]]
Tag tokenised sentences. Returns, per sentence, one dict per token: {"token": ..., "<task>": label}.
Sentences longer than max_sent_len are cut into chunks and re-joined.

workers > 1 (CPU) spreads the sentences over that many single-thread processes, each holding its own copy of
the model: these models are small, so this scales far better than giving one process many threads. The result is
the same as with workers=1 in fp32; with INT8 a few tokens can differ (dynamic quantization scales each batch). Needs an annotator loaded with load_model / from_pretrained.

load_model

load_model(path, device='cpu') -> Annotator
Load an exported folder.

from_pretrained

from_pretrained(repo_or_path, variant: Optional[str]=None, revision: Optional[str]=None, cache_dir=None, token=None, device='cpu', tasks=None, disable=None) -> Annotator
Load a model from a local folder or a Hugging Face repo id.

A repository may hold several variants (see available_variants); variant picks one, the default is used
otherwise. Pin revision (a commit sha) for reproducibility. Only the model files of the chosen variant are
downloaded. tasks keeps only those heads, disable switches heads off (see Annotator.only /
Annotator.disable).

class CombinedAnnotator

CombinedAnnotator(self, annotators, conflict='prefix')
Several models behind the Annotator interface: each one tags the sentences and the labels are merged token by
token into one record. Use it to put a lemmatizer from one model next to the POS of another, or to run all the
single-task networks of a legacy PaPie pack.

conflict says what happens when two models predict the same head: "prefix" (the later one is kept apart as
"<name>:<head>"), "first" / "last" (that model wins), "error". Heads can be switched off
(disable) or restricted (only) by their output name; a model whose heads are all off is not run. Each model
reads the raw tokens itself, so cost grows with the number of models that still have an active head.

CombinedAnnotator.tasks

tasks(self)

CombinedAnnotator.tasks

tasks(self, names)

CombinedAnnotator.only

only(self, *names)

CombinedAnnotator.disable

disable(self, *names)

CombinedAnnotator.enable_all

enable_all(self)

CombinedAnnotator.quantize

quantize(self)

CombinedAnnotator.annotate

annotate(self, sentences, **kw)

class PackAnnotator (CombinedAnnotator)

All the networks of one legacy pack (prefix on conflicts).

combine

combine(sources, conflict='error', device='cpu', token=None)
Build one tagger from several models.

sources is a list or a {name: source} dict; a source is an Annotator / CombinedAnnotator, a repo id or
folder (default variant), a (repo, variant) pair, or a dict {"model": repo, "variant": ..., "revision": ...,
"pack": "<legacy pack>", "tasks": [...], "disable": [...]} (tasks keeps only those heads, disable drops
heads of that model). Example::

    tagger = combine({"lat": ("biblissima-textes/pie-thunder-latin", "morph"),
                      "pos": {"model": "my-org/other-pos", "tasks": ["pos"]}}, conflict="first")

from_pretrained_pack

from_pretrained_pack(repo_or_path, pack: str, revision: Optional[str]=None, cache_dir=None, token=None, device='cpu', tasks=None, disable=None) -> PackAnnotator
Load every network of pack (the variants named "<pack>/<model>") from a local folder or a Hub repository
and return them as one tagger.

push_to_hub

push_to_hub(folder, repo_id, *, private=None, token=None, commit_message='Add papie model', revision=None, create_pr=False)
Upload an exported folder. A new repository is created **private**; an existing one keeps its visibility unless
private is given explicitly (True or False), so a routine push can never change it by accident.

export_main

export_main(argv=None)

push_main

push_main(argv=None)