|
| 1 | +""" |
| 2 | +Copyright 2024 Tecnotree, Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | +---- |
| 17 | +Module to interact with the Sensa model registry |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +from typing import Optional, Dict, Any |
| 22 | +from cortex.serviceconnector import _Client |
| 23 | +try: |
| 24 | + import mlflow |
| 25 | +except ImportError: |
| 26 | + mlflow = None |
| 27 | + |
| 28 | + |
| 29 | +def check_installed(): |
| 30 | + if mlflow is None: |
| 31 | + raise NotImplementedError( |
| 32 | + 'Models SDK extra not installed, please run `pip install cortex-python[models_sdk]` to install') |
| 33 | + |
| 34 | +class ModelClient(_Client): |
| 35 | + """ |
| 36 | + Client for model registry, this class requires the `models_sdk` extras to be installed |
| 37 | + """ |
| 38 | + def __init__(self, *args, **kwargs): |
| 39 | + super().__init__(*args, **kwargs) |
| 40 | + # Setup model registry by default |
| 41 | + self._setup_model_client() |
| 42 | + |
| 43 | + def _setup_model_client(self): |
| 44 | + # Generate a JWT, this call stores the JWT in `_serviceconnector.jwt` ( meh ) |
| 45 | + self._serviceconnector._construct_headers({}) # pylint: disable=protected-access |
| 46 | + |
| 47 | + mlflow.set_tracking_uri(self._serviceconnector.url) |
| 48 | + os.environ['MLFLOW_TRACKING_URI'] = self._serviceconnector.url |
| 49 | + os.environ['MLFLOW_TRACKING_TOKEN'] = self._serviceconnector.token |
| 50 | + # detect cortex client setting to avoid invalid SSL cert errors |
| 51 | + # os.environ['MLFLOW_TRACKING_TOKEN']='true' |
| 52 | + # os.environ['MLFLOW_TRACKING_CLIENT_CERT_PATH']= |
| 53 | + # Need api to fetch serverside userid.. |
| 54 | + # os.environ['MLFLOW_TRACKING_USERNAME']=_Client.??? |
| 55 | + |
| 56 | + def login(self): |
| 57 | + """ |
| 58 | + Configure connection settings for model registry. |
| 59 | + This also setup by `Cortex.client()` |
| 60 | + """ |
| 61 | + check_installed() |
| 62 | + self._setup_model_client() |
| 63 | + print("Configuring connection for model registry") |
| 64 | + |
| 65 | + def create_experiment(self, |
| 66 | + name: str, |
| 67 | + tags: Optional[Dict[str, Any]] = None, |
| 68 | + ) -> str: |
| 69 | + """ |
| 70 | + Create an MLFlow experiment with default tags |
| 71 | + :param name: experiment name, must be unique |
| 72 | + :param tags: optional experiment tags |
| 73 | + """ |
| 74 | + check_installed() |
| 75 | + if tags is None: |
| 76 | + tags = {} |
| 77 | + # default to client project if project tag isn't specified |
| 78 | + if tags.get('project') is None: |
| 79 | + tags['project'] = self._project() |
| 80 | + return mlflow.create_experiment(name, tags=tags) |
0 commit comments