Engines and wrappers

PaPie 1.0

Tagging with a model of PaPie 1.0 (https://github.com/DEFI-COLaF/pie-thunder), from the Hugging Face Hub.

PaPie 1.0 is Pie under PyTorch-Lightning (it was developed as pie-thunder), and it imports as papie. Its models are plain files (no pickle, they are safe to download), several can live in one repository (the variants), and it is imported only when the model is loaded: pip install “pie-extended[papie]”. It replaces PaPie 0.x (import pie, extra pie): the two cannot be installed together, as they are the same package of PyPI.

A model is named papie:REPOSITORY#VARIANT@REVISION (the variant and the revision, a commit, are optional):

>>> parse("papie:biblissima-textes/papie-latin#morph@4a5b6c7")
('biblissima-textes/papie-latin', 'morph', '4a5b6c7')
>>> parse("papie:biblissima-textes/papie-french")
('biblissima-textes/papie-french', None, None)
pie_extended.papie.parse(name)[source]

The repository, the variant and the revision of a name of model

Parameters:

name (str)

Return type:

Tuple[str, str | None, str | None]

class pie_extended.papie.PapieTagger(repo, variant=None, revision=None, tasks=None, device='cpu', batch_size=64, quantize=True, num_threads=None)[source]

Bases: object

An engine of the CompositeTagger: the heads of a model of PaPie 1.0 (lemma, pos, and the morphology for some)

Parameters:
  • tasks (Sequence[str] | None) – What is given back (all the tasks of the model by default)

  • quantize (bool) – INT8 on CPU (about 1.5 times faster, same accuracy according to PaPie)

  • repo (str)

  • variant (str | None)

  • revision (str | None)

  • device (str)

  • batch_size (int)

  • num_threads (int | None)

load()[source]

Loads the model: it is downloaded the first time (only the files of the variant)

Return type:

PapieTagger

tag(sents, lengths=None, **kwargs)[source]

Same contract as the taggers: tagged sentences ([(token, (a value for each task))]) and the tasks

Parameters:
  • sents (Sequence[Sequence[str]])

  • lengths (Sequence[int] | None)

Return type:

Tuple[List[List[Tuple[str, Tuple[str, …]]]], List[str]]

pie_extended.papie.papie_models(repo, variant, tasks, revision=None)[source]

The Models of a package that runs a model of PaPie 1.0: <papie:REPO#VARIANT,task,…>. A variant that ends with /* is a pack: all the networks of a repository whose names start with it (one for each task)

>>> papie_models("org/repo", "morph", ["lemma", "pos"])
'<papie:org/repo#morph,lemma,pos>'
>>> papie_models("org/repo", "morph", ["lemma"], "abc")
'<papie:org/repo#morph@abc,lemma>'
Parameters:
  • repo (str)

  • variant (str | None)

  • tasks (Sequence[str])

  • revision (str | None)

Return type:

str

Sôtêr

Lemmatization of Ancient Greek with Sôtêr (https://huggingface.co/Zual/soter-mini).

Sôtêr mini is a very small (1.9 M parameters) T5 model that works on the bytes of the text, and gives the lemma of one token at a time, from the token and its context. It is a lemmatizer: it gives the task lemma and nothing else, and is meant to be combined with another engine for the other tasks (see CompositeTagger).

It needs transformers (pip install “pie-extended[soter]”), imported only when the model is loaded.

pie_extended.soter.build_input(forms, index, context=8)[source]

What the model is given for the token index of a sentence: the token spelled letter by letter, then its context (context words on each side) in which the token is marked

>>> build_input("Ἀλέξανδρος ἐγένετο βασιλεύς".split(), 1)
'ἐ γ έ ν ε τ ο <sep> Ἀλέξανδρος <tgt> ἐγένετο </tgt> βασιλεύς'
>>> build_input("a b c d e".split(), 0, context=1)
'a <sep>  <tgt> a </tgt> b'
Parameters:
  • forms (Sequence[str])

  • index (int)

  • context (int)

Return type:

str

class pie_extended.soter.SoterLemmatizer(repo='Zual/soter-mini', convention='perseus', context=8, batch_size=64, device='cpu', max_length=48)[source]

Bases: object

Gives the lemma of each token of sentences, with Sôtêr

The contract is the one of a tagger (tag), so that it can be an engine of the CompositeTagger.

Parameters:
  • repo (str) – Repository of the model on Hugging Face (Zual/soter-mini), or a folder

  • convention (str) – perseus (what the model gives), proiel or ptnk (restored from the tables of the model)

  • context (int) – Words on each side of the token that the model sees (8: what it was trained with)

  • batch_size (int) – Tokens lemmatized together

  • device (str) – cpu or cuda

  • max_length (int) – Longest lemma, in bytes

TASKS: Tuple[str, ...] = ('lemma',)
load()[source]

Loads the model (and the table of the convention): it is downloaded the first time

Return type:

SoterLemmatizer

static normalize(form)[source]

What the model reads: NFC, and the apostrophe of an elision as in the treebanks

Parameters:

form (str)

Return type:

str

lemmas(sentences)[source]

The lemma of every token of every sentence

The model works on NFC text, and on the apostrophe of the treebanks: the forms are normalized for it. The tokens are lemmatized by batches of similar length (less padding), and given back in their order.

>>> SoterLemmatizer.normalize("διʼ")
'δι’'
Parameters:

sentences (Sequence[Sequence[str]])

Return type:

List[List[str]]

tag(sents, lengths=None, **kwargs)[source]

Same contract as the taggers: tagged sentences ([(token, (lemma,))]) and the tasks ([“lemma”])

Parameters:
  • sents (Sequence[Sequence[str]])

  • lengths (Sequence[int] | None)

Return type:

Tuple[List[List[Tuple[str, Tuple[str, …]]]], List[str]]

Models of Hugging Face and their wrappers

Models as wrappers around the models of Hugging Face.

A model of Pie Extended is a model of PaPie 1.0, named by its repository and its variant (biblissima-textes/papie-latin#morph). What Pie Extended adds around it is a wrapper: the known processes that go with the model (tokenizer, processor of the tagset, exclusions, disambiguator), and the short name (lasla) that it has had. The processes are code, in pie_extended/processes; the wrapper is the mapping from the Hugging Face model to them (the table of pie_extended.models).

>>> wrapper_of("biblissima-textes/papie-latin#morph")
'lasla'
>>> wrapper_of("biblissima-textes/papie-latin#morph@abc123")   # a revision does not change the wrapper
'lasla'
>>> wrapper_of("someone/other-model#v") is None
True
>>> hub_key("papie:biblissima-textes/papie-latin#morph@abc123")
'biblissima-textes/papie-latin#morph'
pie_extended.wrappers.hub_key(name)[source]

repository#variant of a name of model: the prefix papie: and the revision are not part of it

Parameters:

name (str)

Return type:

str

pie_extended.wrappers.wrappers()[source]

{repository#variant: name of the package that wraps it}, for the packages that run one model of PaPie 1.0

Return type:

Dict[str, str]

pie_extended.wrappers.wrapper_of(name)[source]

The package that wraps a model of Hugging Face, if there is one

Parameters:

name (str)

Return type:

str | None

pie_extended.wrappers.is_hub_name(name)[source]
Parameters:

name (str)

Return type:

bool

pie_extended.wrappers.hub_tasks(repo, variant, revision=None)[source]

The heads of a model of Hugging Face, from its config.json (a few KB): nothing else is downloaded

Parameters:
  • repo (str)

  • variant (str | None)

  • revision (str | None)

Return type:

List[str]

pie_extended.wrappers.wrap_as(name, package)[source]

Runs a model of Hugging Face that has no wrapper with the processes of a package (lasla: its tokenizer, tagset and disambiguator), instead of the generic ones

Raises:

ValueError – If the package does not exist

Parameters:
  • name (str)

  • package (str)

Return type:

None

pie_extended.wrappers.generic(name)[source]

A model of Hugging Face that has no wrapper: simple tokenizer, no post-processing, the tasks of the model, whatever they are. It is a package that is not in the list of the models

Parameters:

name (str)

pie_extended.wrappers.resolve(name)[source]

(package, None) for a name of a package or a model of Hugging Face that is wrapped by one, (None, generic) otherwise

Parameters:

name (str)

Return type:

Tuple[str | None, object]

Helpers of PaPie that do not need it

Small helpers that PaPie provides, kept here so that importing pie_extended does not need PaPie (0.x or 1.0). They behave as the ones of PaPie (pie.utils), whose syntax is the one of the models of –model_path.

pie_extended.spec.model_spec(inp)[source]

Reads <file,task,…><file,task>: the models and what each one gives

>>> model_spec('model-pos-2018:03:05.tar')
[('model-pos-2018:03:05.tar', [])]
>>> model_spec('<model-pos-2018:03:05.tar,pos><model-pos-2018:03:05.tar,lemma>')
[('model-pos-2018:03:05.tar', ['pos']), ('model-pos-2018:03:05.tar', ['lemma'])]
Parameters:

inp (str)

Return type:

List[Tuple[str, List[str]]]

pie_extended.spec.chunks(it, size)[source]

Cuts an iterable in lists of size (the last one may be smaller)

>>> list(chunks(range(5), 2))
[[0, 1], [2, 3], [4]]
Parameters:
  • it (Iterable)

  • size (int)

Return type:

Iterator[list]

pie_extended.spec.ensure_ext(path, ext, infix=None)[source]

Path with an infix and an extension

>>> ensure_ext("model.test", "test", infix="pie")
'model-pie.test'
>>> ensure_ext("model.test", "pt", infix="0.87")
'model-0.87.test.pt'
Parameters:
  • path (str)

  • ext (str)

  • infix (str | None)

Return type:

str

pie_extended.spec.regexsplitter(regex)[source]

Function that cuts a text at a regular expression, and gives the non-empty stripped parts

Parameters:

regex (str)