Skip to content

context

mlte/backend/api/endpoints/context.py

Endpoints for artifact organization context.

create_model(*, model, current_user)

Create a MLTE model.

Parameters:

Name Type Description Default
model Model

The model create model

required

Returns:

Type Description
Model

The created model

Source code in mlte/backend/api/endpoints/context.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@router.post("")
def create_model(
    *,
    model: Model,
    current_user: AuthorizedUser,
) -> Model:
    """
    Create a MLTE model.
    :param model: The model create model
    :return: The created model
    """
    # First create model.
    created_model: Model
    with state_stores.artifact_store_session() as handle:
        try:
            created_model = handle.create_model(model)
        except errors.ErrorNotFound as e:
            raise HTTPException(
                status_code=codes.NOT_FOUND, detail=f"{e} not found."
            )
        except errors.ErrorAlreadyExists as e:
            raise HTTPException(
                status_code=codes.ALREADY_EXISTS, detail=f"{e} already exists."
            )
        except Exception as ex:
            raise_http_internal_error(ex)

    with state_stores.user_store_session() as handle:
        try:
            # Create permissions and groups to handle this model.
            policy = Policy(ResourceType.MODEL, created_model.identifier)
            policy.save_to_store(handle)

            # Also make user have access to CRUD for this model, since they are its creator.
            policy.assign_to_user(current_user)
            handle.user_mapper.edit(current_user)
        except Exception as ex:
            raise_http_internal_error(ex)

    return created_model

create_version(*, model_id, version, current_user)

Create a MLTE version.

Parameters:

Name Type Description Default
model_id str

The model identifier

required
version Version

The version create model

required

Returns:

Type Description
Version

The created version

Source code in mlte/backend/api/endpoints/context.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
@router.post("/{model_id}/version")
def create_version(
    *,
    model_id: str,
    version: Version,
    current_user: AuthorizedUser,
) -> Version:
    """
    Create a MLTE version.
    :param model_id: The model identifier
    :param version: The version create model
    :return: The created version
    """
    model_id = url_utils.revert_valid_url_part(model_id)
    with state_stores.artifact_store_session() as handle:
        try:
            return handle.create_version(model_id, version)
        except errors.ErrorNotFound as e:
            raise HTTPException(
                status_code=codes.NOT_FOUND, detail=f"{e} not found."
            )
        except errors.ErrorAlreadyExists as e:
            raise HTTPException(
                status_code=codes.ALREADY_EXISTS, detail=f"{e} already exists."
            )
        except Exception as ex:
            raise_http_internal_error(ex)

delete_model(*, model_id, current_user)

Delete a MLTE model.

Parameters:

Name Type Description Default
model_id str

The model identifier

required

Returns:

Type Description
Model

The deleted model

Source code in mlte/backend/api/endpoints/context.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
@router.delete("/{model_id}")
def delete_model(
    *,
    model_id: str,
    current_user: AuthorizedUser,
) -> Model:
    """
    Delete a MLTE model.
    :param model_id: The model identifier
    :return: The deleted model
    """
    model_id = url_utils.revert_valid_url_part(model_id)
    deleted_model: Model
    with state_stores.artifact_store_session() as handle:
        try:
            deleted_model = handle.delete_model(model_id)
        except errors.ErrorNotFound as e:
            raise HTTPException(
                status_code=codes.NOT_FOUND, detail=f"{e} not found."
            )
        except Exception:
            raise HTTPException(
                status_code=codes.INTERNAL_ERROR,
                detail="Internal server error.",
            )

    with state_stores.user_store_session() as handle:
        # Now delete related permissions and groups.
        try:
            policy = Policy(ResourceType.MODEL, resource_id=model_id)
            policy.remove_from_store(handle)
        except Exception as ex:
            raise_http_internal_error(ex)

    return deleted_model

delete_version(*, model_id, version_id, current_user)

Delete a MLTE version.

Parameters:

Name Type Description Default
model_id str

The model identifier

required
version_id

The version identifier

required

Returns:

Type Description
Version

The deleted version

Source code in mlte/backend/api/endpoints/context.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
@router.delete("/{model_id}/version/{version_id}")
def delete_version(
    *,
    model_id: str,
    version_id,
    current_user: AuthorizedUser,
) -> Version:
    """
    Delete a MLTE version.
    :param model_id: The model identifier
    :param version_id: The version identifier
    :return: The deleted version
    """
    model_id = url_utils.revert_valid_url_part(model_id)
    version_id = url_utils.revert_valid_url_part(version_id)
    with state_stores.artifact_store_session() as handle:
        try:
            return handle.delete_version(model_id, version_id)
        except errors.ErrorNotFound as e:
            raise HTTPException(
                status_code=codes.NOT_FOUND, detail=f"{e} not found."
            )
        except Exception as ex:
            raise_http_internal_error(ex)

list_models(current_user)

List MLTE models.

Returns:

Type Description
List[str]

A collection of model identifiers

Source code in mlte/backend/api/endpoints/context.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
@router.get("")
def list_models(
    current_user: AuthorizedUser,
) -> List[str]:
    """
    List MLTE models.
    :return: A collection of model identifiers
    """
    with state_stores.artifact_store_session() as handle:
        try:
            return handle.list_models()
        except errors.ErrorNotFound as e:
            raise HTTPException(
                status_code=codes.NOT_FOUND, detail=f"{e} not found."
            )
        except Exception as ex:
            raise_http_internal_error(ex)

list_versions(model_id, current_user)

List MLTE versions for the provided model.

Parameters:

Name Type Description Default
model_id str

The model identifier

required

Returns:

Type Description
List[str]

A collection of version identifiers

Source code in mlte/backend/api/endpoints/context.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
@router.get("/{model_id}/version")
def list_versions(
    model_id: str,
    current_user: AuthorizedUser,
) -> List[str]:
    """
    List MLTE versions for the provided model.
    :param model_id: The model identifier
    :return: A collection of version identifiers
    """
    model_id = url_utils.revert_valid_url_part(model_id)
    with state_stores.artifact_store_session() as handle:
        try:
            return handle.list_versions(model_id)
        except errors.ErrorNotFound as e:
            raise HTTPException(
                status_code=codes.NOT_FOUND, detail=f"{e} not found."
            )
        except Exception as ex:
            raise_http_internal_error(ex)

read_model(*, model_id, current_user)

Read a MLTE model.

Parameters:

Name Type Description Default
model_id str

The model identifier

required

Returns:

Type Description
Model

The read model

Source code in mlte/backend/api/endpoints/context.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@router.get("/{model_id}")
def read_model(
    *,
    model_id: str,
    current_user: AuthorizedUser,
) -> Model:
    """
    Read a MLTE model.
    :param model_id: The model identifier
    :return: The read model
    """
    model_id = url_utils.revert_valid_url_part(model_id)
    try:
        with state_stores.artifact_store_session() as handle:
            model = handle.read_model(model_id)

        return model
    except errors.ErrorNotFound as e:
        raise HTTPException(
            status_code=codes.NOT_FOUND, detail=f"{e} not found."
        )
    except Exception as ex:
        raise_http_internal_error(ex)

read_version(*, model_id, version_id, current_user)

Read a MLTE version.

Parameters:

Name Type Description Default
model_id str

The model identifier

required
version_id

The version identifier

required

Returns:

Type Description
Version

The read version

Source code in mlte/backend/api/endpoints/context.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
@router.get("/{model_id}/version/{version_id}")
def read_version(
    *,
    model_id: str,
    version_id,
    current_user: AuthorizedUser,
) -> Version:
    """
    Read a MLTE version.
    :param model_id: The model identifier
    :param version_id: The version identifier
    :return: The read version
    """
    model_id = url_utils.revert_valid_url_part(model_id)
    version_id = url_utils.revert_valid_url_part(version_id)
    with state_stores.artifact_store_session() as handle:
        try:
            return handle.read_version(model_id, version_id)
        except errors.ErrorNotFound as e:
            raise HTTPException(
                status_code=codes.NOT_FOUND, detail=f"{e} not found."
            )
        except Exception as ex:
            raise_http_internal_error(ex)