Source code for pie_extended.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'
"""
from typing import Dict, List, Optional, Tuple

from .papie import PAPIE, papie_models, parse
from .spec import model_spec


[docs] def hub_key(name: str) -> str: """ `repository#variant` of a name of model: the prefix `papie:` and the revision are not part of it """ repo, variant, _ = parse(name) return repo + ("#" + variant if variant else "")
[docs] def wrappers() -> Dict[str, str]: """ `{repository#variant: name of the package that wraps it}`, for the packages that run one model of PaPie 1.0 """ from . import models table = {} for model in models.MODELS: spec = model_spec(model.models) if len(spec) == 1 and spec[0][0].startswith(PAPIE): table[hub_key(spec[0][0])] = model.name return table
[docs] def wrapper_of(name: str) -> Optional[str]: """ The package that wraps a model of Hugging Face, if there is one """ return wrappers().get(hub_key(name)) if name.startswith(PAPIE) or "/" in name else None
[docs] def is_hub_name(name: str) -> bool: return name.startswith(PAPIE) or "/" in name
[docs] def hub_tasks(repo: str, variant: Optional[str], revision: Optional[str] = None) -> List[str]: """ The heads of a model of Hugging Face, from its `config.json` (a few KB): nothing else is downloaded """ import json from huggingface_hub import hf_hub_download if variant and variant.endswith("/*"): raise ValueError("The tasks of a pack cannot be known without loading it: give them with --model_path " "\"<papie:{}#{},task,task>\"".format(repo, variant)) config = hf_hub_download(repo, "{}/config.json".format(variant) if variant else "config.json", revision=revision) with open(config, encoding="utf-8") as f: return list(json.load(f)["tasks"])
# {repository#variant: package whose processes are used}, set by `wrap_as` WRAP_AS: Dict[str, str] = {}
[docs] def wrap_as(name: str, package: str) -> None: """ 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 """ from . import models if package not in models.BY_NAME: raise ValueError("Unknown wrapper {!r}. Available: {}".format(package, ", ".join(models.modules))) WRAP_AS[hub_key(name)] = package
[docs] def generic(name: str): """ 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 """ from . import models repo, variant, revision = parse(name) tasks = hub_tasks(repo, variant, revision) key = repo + ("#" + variant if variant else "") wrapper = models.BY_NAME[WRAP_AS[key]] if key in WRAP_AS else None # The processes of the model it is wrapped as, or the generic ones; the heads are the ones of this model process = wrapper.process if wrapper else "simple:generic" return models.Model( name=key, lang=wrapper.lang if wrapper else "", title=key, authors=[], link="https://huggingface.co/" + repo, description="A model of Hugging Face, with no wrapper (generic tokenizer and columns)" if not wrapper else wrapper.description, models=papie_models(repo, variant, tasks, revision), process=process, files=list(wrapper.files) if wrapper else [])
[docs] def resolve(name: str) -> Tuple[Optional[str], object]: """ (package, None) for a name of a package or a model of Hugging Face that is wrapped by one, (None, generic) otherwise """ from . import models if name in models.BY_NAME: return name, None if is_hub_name(name): package = wrapper_of(name) return (package, None) if package else (None, generic(name)) raise ValueError("Unknown model {!r}. Available: {}, or a model of Hugging Face (`organisation/repository#variant`)".format( name, ", ".join(models.modules)))