-
Notifications
You must be signed in to change notification settings - Fork 12
Content for /docs #43
Changes from all commits
6585bc2
c879e62
45d27ec
f7f9e02
be5a6a4
e8f6d1f
e007489
14bdc98
436c3a0
b6d0c20
6881860
13c4708
9dc8af1
fa11158
7588deb
9d849e8
3ba3813
37bb027
3c12118
b917932
de04871
d4649e0
5ad0166
98b1d1f
e131bdc
4a2ad81
3fe8683
d93db74
40f197d
d208f42
122b46f
cb66733
c7a6d99
3080ecd
08d2c5e
ee1f253
70119f2
9869cdc
21979f7
1443e3d
f8daae4
9bc1e2c
f69abc2
7d398b7
8fdee21
b1308c1
fd51131
831c310
d08e59e
46fc659
9299573
962a470
a7fdd10
110cbaf
3ed3a86
1811edc
02866ec
4c4023b
bb1b45b
43e7506
819600b
55859a6
ba26f71
23367b7
f0b509b
551246a
5ae6d8e
450cde7
e33b054
1346c77
7ac71e3
83b434b
bfa9d9e
a94b446
f010c3f
d13ed3f
5fe10aa
2eae931
d78bd59
18bd383
810ab3c
ad3d579
7258aad
db7e0ab
95a23ef
774d646
444935a
17bd0c8
f1331d2
1ffb064
5b71896
09b4335
ed6164d
3cca714
e7fe56c
ea3a92a
3125e2c
b45684c
c5de607
001a46b
4ec72a2
91fa7ba
f39dde1
2a14a27
13bcd12
dcab49b
620f9e9
f5abbb2
7bd5ed5
6e123a0
815e7fb
1394792
c6474ad
e6644e7
4ab62c6
e3b2ed8
5e6dc73
4e9c956
5da57d1
1b70da0
c8c602b
24742d8
c8776ff
8f4d29c
8b4229e
0d212a3
1aee3c7
997ea66
91a15c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# mlem.api.apply() | ||
|
||
Apply provided model against provided data | ||
|
||
```py | ||
def apply( | ||
model: Union[str, MlemModel], | ||
*data: Union[str, MlemDataset, Any], | ||
method: str = None, | ||
output: str = None, | ||
target_repo: str = None, | ||
index: bool = None, | ||
external: bool = None, | ||
batch_size: Optional[int] = None, | ||
) -> Optional[Any] | ||
``` | ||
|
||
### Usage: | ||
|
||
```py | ||
from mlem.api import apply | ||
|
||
y_pred = apply("rf", "data", method="predict_proba") | ||
``` | ||
|
||
## Description | ||
|
||
This API is the underlying mechanism for the | ||
[mlem apply](/doc/command-reference/apply) command and facilitates running | ||
inferences on entire datasets. The API applies i.e. calls a model's method (eg: | ||
`predict`) and returns the output (as a MLEM object) while also saving it if | ||
required. | ||
|
||
## Parameters | ||
|
||
- **`model`** (required) - MLEM model (a MlemModel object). | ||
- **`data`** (required) - Input to the model. | ||
- `method` (optional) - Which model method to use. If None, use the only method | ||
model has. If more than one is available, will fail. | ||
- `output` (optional) - If value is provided, assume its path and save output | ||
there. | ||
- `target_repo` (optional) - The path to repo to save the results to. | ||
- `index` (optional) - Whether to index saved output in MLEM root folder. | ||
- `external` (optional) - Whether to save result outside mlem dir. | ||
- `batch_size` (optional) - If data is to be loaded and applied in batches. | ||
|
||
## Exceptions | ||
|
||
- `WrongMethodError` - Thrown if wrong method name for model is provided | ||
- `NotImplementedError` - Saving several input data objects is not implemented | ||
yet | ||
|
||
## Examples | ||
|
||
```py | ||
from numpy import ndarray | ||
from sklearn.datasets import load_iris | ||
from sklearn.tree import DecisionTreeClassifier | ||
from mlem.core.objects import MlemDataset, MlemModel | ||
from mlem.api import apply | ||
|
||
train, target = load_iris(return_X_y=True) | ||
model = DecisionTreeClassifier().fit(train, target) | ||
d = MlemDataset.from_data(train) | ||
m = MlemModel.from_obj(model) | ||
res = apply(m, d, method="predict") | ||
assert isinstance(res, ndarray) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# mlem.api.apply_remote() | ||
|
||
Apply deployed model (possibly remote) against provided data. | ||
|
||
```py | ||
def apply_remote( | ||
client: Union[str, BaseClient], | ||
*data: Union[str, MlemDataset, Any], | ||
method: str = None, | ||
output: str = None, | ||
target_repo: str = None, | ||
index: bool = False, | ||
**client_kwargs, | ||
) -> Optional[Any] | ||
``` | ||
|
||
### Usage: | ||
|
||
```py | ||
from mlem.api import apply_remote | ||
|
||
res = apply_remote(client_obj, data, method="predict") | ||
``` | ||
|
||
## Description | ||
|
||
This API is the underlying mechanism for the | ||
[mlem apply-remote](/doc/command-reference/apply-remote) command and facilitates | ||
running inferences on entire datasets for models which are deployed remotely or | ||
are being served locally. The API requires an explicit client object, which | ||
knows how to make requests to the deployed model. | ||
|
||
## Parameters | ||
|
||
- **`client`** (required) - The client to access methods of deployed model. | ||
- **`data`** (required) - Input to the model. | ||
- `method` (optional) - Which model method to use. If None, use the only method | ||
model has. If more than one is available, will fail. | ||
- `output` (optional) - If value is provided, assume it's path and save output | ||
there. | ||
- `target_repo` (optional) - The path to repo to save the results to. | ||
- `index` (optional) - Whether to index saved output in MLEM root folder. | ||
- `client_kwargs` (optional) - Keyword arguments for the underlying client | ||
implementation being used. | ||
|
||
## Exceptions | ||
|
||
- `WrongMethodError` - Thrown if wrong method name for model is provided | ||
- `InvalidArgumentError` - Thrown if arguments are invalid, when method cannot | ||
be None | ||
- `NotImplementedError` - Saving several input data objects is not implemented | ||
yet | ||
|
||
## Examples | ||
|
||
```py | ||
from numpy import ndarray | ||
from sklearn.datasets import load_iris | ||
from mlem.api import apply_remote | ||
from mlem.runtime.client.base import HTTPClient | ||
|
||
train, _ = load_iris(return_X_y=True) | ||
client = HTTPClient(host="0.0.0.0", port=8080) | ||
|
||
res = apply_remote(client, train, method="predict") | ||
assert isinstance(res, ndarray) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# mlem.api.clone() | ||
|
||
Clones MLEM object from `path` to `target` and returns Python representation for | ||
the created object. | ||
|
||
```py | ||
def clone( | ||
path: str, | ||
target: str, | ||
repo: Optional[str] = None, | ||
rev: Optional[str] = None, | ||
fs: Optional[AbstractFileSystem] = None, | ||
target_repo: Optional[str] = None, | ||
target_fs: Optional[str] = None, | ||
follow_links: bool = True, | ||
load_value: bool = False, | ||
index: bool = None, | ||
external: bool = None, | ||
) -> MlemObject | ||
``` | ||
|
||
### Usage: | ||
|
||
```py | ||
from mlem.api import clone | ||
|
||
cloned_obj = clone(path="rf", target="mymodel". repo="https://github.com/iterative/example-mlem-get-started", rev="main") | ||
``` | ||
|
||
## Description | ||
|
||
This API is the underlying mechanism for the | ||
[mlem clone](/doc/command-reference/clone) command and facilitates copying of a | ||
[MLEM Object](/doc/user-guide/basic-concepts#mlem-objects) from source to | ||
target. | ||
|
||
## Parameters | ||
|
||
- **`path`** (required) - Path to the object. Could be local path or path inside | ||
a Git repo. | ||
- **`target`** (required) - Path to save the copy of initial object to. | ||
- `repo` (optional) - URL to repo if object is located there. | ||
- `rev` (optional) - revision, could be Git commit SHA, branch name or tag. | ||
- `fs` (optional) - filesystem to load object from | ||
- `target_repo` (optional) - path to repo to save cloned object to | ||
- `target_fs` (optional) - target filesystem | ||
- `follow_links` (optional) - If object we read is a MLEM link, whether to load | ||
the actual object link points to. Defaults to True. | ||
- `load_value` (optional) - Load actual python object incorporated in MlemMeta | ||
object. Defaults to False. | ||
- `index` (optional) - Whether to index output in .mlem directory | ||
- `external` (optional) - whether to put object inside mlem dir in target repo | ||
|
||
## Exceptions | ||
|
||
None | ||
|
||
## Example: Clone a remote model to a remote repo | ||
|
||
```py | ||
from mlem.api import clone | ||
|
||
cloned_obj = clone(path="rf", target="mymodel". repo="https://github.com/iterative/example-mlem-get-started", rev="main", target_repo="s3://mybucket/mymodel", load_value=True) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# mlem.api.deploy() | ||
|
||
Deploy a model to target environment. Can use existing deployment declaration or | ||
create a new one on-the-fly. | ||
|
||
```py | ||
def deploy( | ||
deploy_meta_or_path: Union[MlemDeploy, str], | ||
model: Union[MlemModel, str] = None, | ||
env: Union[MlemEnv, str] = None, | ||
repo: Optional[str] = None, | ||
fs: Optional[AbstractFileSystem] = None, | ||
external: bool = None, | ||
index: bool = None, | ||
**deploy_kwargs, | ||
) -> MlemDeploy | ||
``` | ||
|
||
### Usage: | ||
|
||
```py | ||
from mlem.api import deploy | ||
|
||
#TODO | ||
``` | ||
|
||
## Description | ||
|
||
This API is the underlying mechanism for the | ||
[mlem deploy create](/doc/command-reference/deploy/create) command and provides | ||
a programmatic way to create deployments for a target environment. | ||
|
||
## Parameters | ||
|
||
- **`deploy_meta_or_path`** (required) - Path to deployment meta (will be | ||
created if it does not exist) | ||
- `model` (optional) - Path to model | ||
- `env` (optional) - Path to target environment | ||
- `repo` (optional) - Path to MLEM repo | ||
- `fs` (optional) - filesystem to load deploy meta from. If not provided, will | ||
be inferred from `deploy_meta_or_path` | ||
- `external` (optional) - Save result not in mlem dir, but directly in repo | ||
- `index` (optional) - Whether to index output in .mlem directory | ||
- `deploy_kwargs` (optional) - Configuration for new deployment meta if it does | ||
not exist | ||
|
||
## Exceptions | ||
|
||
- `MlemObjectNotFound` - Thrown if we can't find MLEM object | ||
- `ValueError` - Please provide model and env args for new deployment | ||
|
||
## Examples | ||
|
||
```py | ||
from mlem.api import deploy | ||
|
||
#TODO | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# mlem.api.import_object() | ||
|
||
Try to load an object as MLEM model (or dataset) and return it, optionally | ||
saving to the specified target location. | ||
|
||
```py | ||
def import_object( | ||
path: str, | ||
repo: Optional[str] = None, | ||
rev: Optional[str] = None, | ||
fs: Optional[AbstractFileSystem] = None, | ||
target: Optional[str] = None, | ||
target_repo: Optional[str] = None, | ||
target_fs: Optional[AbstractFileSystem] = None, | ||
type_: Optional[str] = None, | ||
copy_data: bool = True, | ||
external: bool = None, | ||
index: bool = None, | ||
) | ||
``` | ||
|
||
### Usage: | ||
|
||
```py | ||
import os | ||
from mlem.api import import_object | ||
from mlem.core.objects import MlemDataset | ||
from mlem.contrib.pandas import DataFrameType | ||
|
||
path = os.path.join(os.getcwd(), "data.csv") | ||
target_path = os.path.join(os.getcwd(), "imported_data") | ||
meta = import_object(path, target=target_path, type_="pandas[csv]", copy_data=True) | ||
|
||
assert isinstance(meta, MlemDataset) | ||
dt = meta.dataset | ||
assert isinstance(dt, DataFrameType) | ||
``` | ||
|
||
## Description | ||
|
||
Existing datasets and model files are imported as | ||
[MLEM Objects](/doc/user-guide/basic-concepts#mlem-objects). Specifically, they | ||
are tried to be loaded as `MlemModel` or `MlemDataset`. The function also | ||
supports saving these objects for future use within the MLEM context. This API | ||
is the underlying mechanism for the [mlem import](/doc/command-reference/import) | ||
command. | ||
|
||
## Parameters | ||
|
||
- **`path`** (required) - Path of file to import. | ||
- `repo` (optional) - Path to MLEM repo. | ||
- `rev` (optional) - revision, could be Git commit SHA, branch name or tag. | ||
- `fs` (optional) - FileSystem for the `path` argument | ||
- `target` (optional) - Path to save MLEM object into. | ||
- `target_repo` (optional) - Path to MLEM repo for `target`. | ||
- `target_fs` (optional) - FileSystem for the `target` argument | ||
- `type_` (optional) - Specify how to read file. Available types: ['pickle', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to mention import modifiers (like |
||
'pandas']. Defaults to auto-infer. | ||
- `copy_data` (optional) - Whether to create a copy of file in target location | ||
or just link existing file. Defaults to True. | ||
- `external` (optional) - Save result not in `.mlem`, but directly in repo | ||
- `index` (optional) - Whether to index output in `.mlem` directory | ||
|
||
## Exceptions | ||
|
||
None | ||
|
||
## Example: Import a saved model as MlemModel | ||
|
||
```py | ||
import os | ||
from mlem.core.objects import MlemModel | ||
from mlem.api import import_object | ||
|
||
path = os.path.join(os.getcwd(), "mymodel") | ||
target_path = os.path.join(os.getcwd(), "mlem_model") | ||
meta = import_object(path, target=target_path, type_="pickle", copy_data=True) | ||
assert isinstance(meta, MlemModel) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Python API | ||
|
||
MLEM can be used as a python library, simply [install](/doc/install) with `pip` | ||
or `conda`. This reference provides the details about the functions in the API | ||
module `mlem.api`, which can be imported in any regular way, for example: | ||
|
||
```py | ||
import mlem.api | ||
``` | ||
|
||
The purpose of this API is to provide programmatic access to operate on models | ||
and datasets from Python code. | ||
|
||
Please choose a function from the navigation sidebar to the left, or click the | ||
`Next` button below to jump into the first one ↘ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# mlem.api.init() | ||
|
||
Creates `.mlem/` directory in `path` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to use term "MLEM project" somwhere here. Also, since a lot of our CLI are just wrappers around API, we should probably use the same descriptions for both. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same goes for their arguments/options (if they are the same of course from cli/api) |
||
|
||
```py | ||
def init(path: str = ".") -> None | ||
``` | ||
|
||
### Usage: | ||
|
||
```py | ||
from mlem.api import init | ||
|
||
init(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add multiple examples with explanations like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this looks like examples actually. Let me check dvc docs to see what is the difference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jorgeorpinel What is the purpose of usage section? We can see signature above and some examples in the end. For DVC python API usage has the same content as one of the examples in most cases |
||
``` | ||
|
||
## Description | ||
|
||
Initializes a MLEM repository by creating a `.mlem/` directory inside the given | ||
path. A new and empty `config.yaml` is also created inside it. | ||
|
||
## Parameters | ||
|
||
- **`path`** (required) - location of the target where a MLEM repository has to | ||
be initialized i.e. a `.mlem/` folder has to be created. `.` by default | ||
|
||
## Exceptions | ||
|
||
None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
,