Migrate from 1.x to 2.0

Pie Extended 2.0 runs its models with PaPie 1.0 (imported as papie, models from Hugging Face) instead of PaPie 0.x (imported as pie, models as .tar files). If you only use the command line, almost nothing changes. If you use the Python API, this page tells what to change, with the code before and after.

Note

You do not have to migrate today. The release 1.0.3 keeps the former .tar models and requires PaPie<1: pip install "pie-extended<2". The releases 1.0.0, 1.0.1 and 1.0.2 do not limit PaPie, and an installation of one of them can bring PaPie 1.0: use 1.0.3, or pin PaPie<1 yourself.

What stays the same

The functions that most people use have the same signature and the same output:

from pie_extended.cli.utils import get_tagger, get_pipeline

tagger = get_tagger("lasla", tasks=["lemma", "pos"])
iterator, processor = get_pipeline("lasla", tagger, space_after=False)
for token in tagger.tag_str("Arma uirumque cano", iterator=iterator, processor=processor):
    print(token)

get_tagger, get_pipeline, tag_file, tagger.tag_str, tagger.iter_tag, the columns of lasla, fro, frm, freem, occ_cont, osp, hy_hye, hy_hyw and hy_xcl, --tasks, SpaceAfter, the disambiguation of Latin: all of this is as it was.

What changes

In 1.x

In 2.0

pip install pie-extended brought PaPie 0.x

It brings PaPie 1.0 (PaPie>=1.0.0); the two are the same package of PyPI, so not both

The files of a model were downloaded as .tar into downloads/

The model is downloaded from Hugging Face when first used, into the cache of Hugging Face (~/.cache/huggingface, or HF_HOME)

--model_path "<lemma.tar,lemma><pos.tar,pos>"

--model_path "<papie:organisation/repository#variant,lemma,pos>"

get_model("lasla") was a Python module

It is a Model (same DESC, DOWNLOADS, Models)

pie_extended.models.lasla.imports, .tokenizer, .processor…

pie_extended.processes.latin (one module for a family of languages)

ExtensibleTagger().add_model("lemma.tar", "lemma")

CompositeTagger().add_model("papie:org/repo#variant", "lemma"), or get_tagger

from pie.utils import model_spec

from pie_extended.spec import model_spec (PaPie is no longer imported by Pie Extended itself)

A model was a package in pie_extended/models/foo/

A line of the table in pie_extended/models.py, and a Process if it has code

grc (ten tasks, its own tagset)

grc is biblissima-textes/papie-greek#gold: lemma (LSJ headwords) and the universal pos only

fr (Classical French)

Removed: no converted model

dum gave lemma and pos

dum gives lemma only (its pos model was not converted)

Use a tagger directly

Before, you made a Pie tagger and gave it the files:

from pie_extended.tagger import ExtensibleTagger

tagger = ExtensibleTagger(device="cpu", batch_size=16)
tagger.add_model("downloads/lasla/lemma.tar", "lemma")
tagger.add_model("downloads/lasla/pos.tar", "pos")

ExtensibleTagger is now the base of the taggers: it has the tokenization, the processors and the formatting, but no model of its own (add_model raises NotImplementedError, and says what to use). Use get_tagger, that does it for the model that you name:

from pie_extended.cli.utils import get_tagger

tagger = get_tagger("lasla", tasks=["lemma", "pos"], batch_size=16)

or build the tagger yourself, from the model of Hugging Face, with the engines of pie_extended.backends:

from pie_extended.backends import CompositeTagger

tagger = CompositeTagger(batch_size=16)
tagger.add_model("papie:biblissima-textes/papie-latin#morph", "lemma", "pos")

Only the heads that you name are run. A model of PaPie 1.0 can be mixed with Sôtêr or a spaCy pipeline in the same tagger: see Backends. quantize, num_threads and device are still taken (cache and cache_size were options of the embedding cache of PaPie 0.x: they are ignored).

The model that get_model returns

get_model(name) gave a module, and now gives a Model. What you read from it is the same, and it can now be asked for by the name of the model of Hugging Face as well:

from pie_extended.cli.utils import get_imports, get_model

model = get_model("lasla")
print(model.DESC.lang, [file.name for file in model.DOWNLOADS])
print(model.Models.split(",")[0])                                  # where the model is, then its tasks
print(get_model("biblissima-textes/papie-latin#morph") is model)   # the name of the model of Hugging Face wraps to the same
iterator, processor = get_imports(model).get_iterator_and_processor()
lat ['latin-straight.json', 'latin-pos.json', 'latin-needs.json']
<papie:biblissima-textes/papie-latin#morph
True

The files of DOWNLOADS are the ones that a model needs besides its network: the lexicons of the disambiguation of Latin. They still go in downloads/ (or PIE_EXTENDED_DOWNLOADS).

Import the pieces of a model

The tokenizers, processors and pipelines moved from pie_extended.models.<name> to pie_extended.processes.<family>:

1.x

2.0

pie_extended.models.lasla.tokenizer.LatMemorizingTokenizer

pie_extended.processes.latin.LatMemorizingTokenizer

pie_extended.models.lasla.processor.LatinGlueProcessor

pie_extended.processes.latin.LatinGlueProcessor

pie_extended.models.lasla.imports.get_iterator_and_processor

pie_extended.processes.latin.lasla_pipeline

pie_extended.models.fro.imports.get_iterator_and_processor

pie_extended.processes.french.fro_pipeline

pie_extended.models.freem..., pie_extended.models.fr...

pie_extended.processes.french (fr is gone, its tokenizer and processor are freem’s)

pie_extended.models.hy_hye.tokenizer.ArmenianTokenizer

pie_extended.processes.armenian.ArmenianTokenizer

pie_extended.models.hy_xcl.segmenter

pie_extended.processes.armenian (the method is pie_extended.pipeline.tokenizers.learned)

pie_extended.models.grc.tokenizer, grc_grecy.tokenizer

pie_extended.processes.greek

pie_extended.models.occ_cont, osp, dum

pie_extended.processes.occitan, castilian, simple

The portable way, that does not depend on where the code is, is to ask the model:

from pie_extended.cli.utils import get_model, get_imports

iterator, processor = get_imports(get_model("lasla")).get_iterator_and_processor()

Run a model by its files, or by its name

--model_path and the model_path of get_tagger take the model of Hugging Face and its tasks. The files of PaPie 0.x are not read by this version, and the error says what to do:

from pie_extended.cli.utils import get_tagger

try:
    get_tagger("lasla", model_path="<lemma.tar,lemma>")
except ValueError as error:
    print(str(error).split(": use")[0])
lemma.tar: the models of PaPie 0.x are not run by this version (PaPie 1.0)
tagger = get_tagger("lasla", model_path="<papie:my-org/my-latin#v1,lemma,pos>")

A model of PaPie 0.x can be converted and published as a model of PaPie 1.0 (see the tool scripts/legacy/port_legacy.py in the repository of PaPie, that made the models of papie-legacy), then used as above.

The model_spec of PaPie

If you parsed the specification of the models with PaPie:

from pie_extended.spec import model_spec

print(model_spec("<lemma.tar,lemma><pos.tar,pos>"))
[('lemma.tar', ['lemma']), ('pos.tar', ['pos'])]

Same function, same result, and importing it does not import PaPie (nor torch).

A model that you added

Before, a model was a package of pie_extended/models/ with an __init__.py (DESC, DOWNLOADS, Models) and an imports.py (get_iterator_and_processor, Disambiguator, addons), and a line in modules. Now:

# 1.x: pie_extended/models/foo/__init__.py
DESC = Metadata("Foo", "foo", ["Author"], "A description", "https://example.org")
DOWNLOADS = [File("https://example.org/lemma.tar", "lemma.tar"), File("https://example.org/pos.tar", "pos.tar")]
Models = "<{},lemma><{},pos>".format(get_path("foo", "lemma.tar"), get_path("foo", "pos.tar"))

# 1.x: pie_extended/models/foo/imports.py
def get_iterator_and_processor(max_tokens=256): ...
# 2.0: pie_extended/processes/foo.py  (the code, if the model has some)
def foo_pipeline(max_tokens=256): ...                    # the same function, renamed
PROCESSES = {"foo": Process(foo_pipeline)}               # + disambiguator=..., addons=... if it had them

# 2.0: pie_extended/models.py  (one line in MODELS)
Model(name="foo", lang="foo", title="Foo", authors=["Author"], description="A description", link="https://example.org",
      models=papie_models("my-org/papie-foo", "default", ["lemma", "pos"]), process="foo:foo"),

The networks have to be on Hugging Face as a model of PaPie 1.0 (see the end of Implement things). If they are not, they cannot be run by 2.0. Implement things has the complete guide, and the tokenizers can be a few lines on top of RuleTokenizer.

Offline, servers, and where things are kept

  • The model is downloaded the first time you use it, or in advance with pie-extended download MODEL. In a container, run that at build time, and set HF_HOME to a folder that you keep. With HF_HUB_OFFLINE=1 nothing is downloaded.

  • In a service, load the tagger once and make the pipeline (get_pipeline) for each request: they keep the state of the text being read. The tagger itself can be shared.

  • Pin the revision of a model that you depend on (papie:organisation/repository#variant@commit): a branch can change.

Checklist

  1. pip install -U pie-extended (this brings PaPie 1.0); remove what you installed for PaPie 0.x.

  2. Replace the imports of pie_extended.models.<name>... (table above) and of pie.utils.

  3. Replace the .tar files by the name of the model of Hugging Face, in --model_path and in your code.

  4. Check the columns: grc and dum give less than before, and fr is gone.

  5. Run pie-extended download MODEL for each model you use, and your tests.

  6. If you add models, read Implement things.