Add some docstrings

This commit is contained in:
Andras Schmelczer 2022-07-10 13:21:08 +02:00
parent 63a45989f4
commit 2b378114aa
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
5 changed files with 126 additions and 0 deletions

View file

@ -9,6 +9,30 @@ from ..context import get_context
def save_model(
model: Union[Path, str, object], key: str, *, keep_last_n: Optional[int] = None
) -> str:
"""Save (and optionally serialise) a model in order to use by `use_model`.
The `model` can be a Path or string representing a path in which case the
local file/folder is read and saved using the current LargeFile implementation.
In case `model` is an object, it is serialised using `dill` before uploading it.
Examples:
>>> from great_ai import use_model
>>> save_model(3, 'my_number')
'my_number:...'
>>> @use_model('my_number')
... def my_function(a, model):
... return a + model
>>> my_function(4)
7
Args:
model: The object or path to be uploaded.
key: The model's name.
keep_last_n: If specified, remove old models and only keep the latest n. Directly passed to LargeFile.
Returns:
The key and version of the saved model separated by a colon. Example: "key:version"
"""
file = get_context().large_file_implementation(
name=key, mode="wb", keep_last_n=keep_last_n
)

View file

@ -30,6 +30,38 @@ def use_model(
version: Union[int, Literal["latest"]] = "latest",
model_kwarg_name: str = "model",
) -> Callable[[F], F]:
"""Inject a model into a function.
Load a model specified by `key` and `version` using the
currently active `LargeFile` implementation. If it's a
single object, it is deserialised using `dill`. If it's
a directory of files, a `pathlib.Path` instance is given.
By default, the function's `model` parameter is replaced
by the loaded model. This can be customised by changing
`model_kwarg_name`.
Multiple models can be loaded by decorating the same
function with `use_model` multiple times.
Examples:
>>> from great_ai import save_model
>>> save_model(3, 'my_number')
'my_number:...'
>>> @use_model('my_number')
... def my_function(a, model):
... return a + model
>>> my_function(4)
7
Args:
key: The model's name as stored by the LargeFile implementation.
version: The model's version as stored by the LargeFile implementation.
model_kwarg_name: the parameter to use for injecting the loaded model
Returns:
A decorator for model injection.
"""
assert (
isinstance(version, int) or version == "latest"
), "Only integers or the string literal `latest` is allowed as a version"