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 |
|---|---|
|
It brings PaPie 1.0 ( |
The files of a model were downloaded as |
The model is downloaded from Hugging Face when first used, into the cache of Hugging Face ( |
|
|
|
It is a |
|
|
|
|
|
|
A model was a package in |
A line of the table in |
|
|
|
Removed: no converted model |
|
|
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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 setHF_HOMEto a folder that you keep. WithHF_HUB_OFFLINE=1nothing 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¶
pip install -U pie-extended(this brings PaPie 1.0); remove what you installed for PaPie 0.x.Replace the imports of
pie_extended.models.<name>...(table above) and ofpie.utils.Replace the
.tarfiles by the name of the model of Hugging Face, in--model_pathand in your code.Check the columns:
grcanddumgive less than before, andfris gone.Run
pie-extended download MODELfor each model you use, and your tests.If you add models, read Implement things.