Skip to content

ModelKitManager class

You can import the ModelKitManager class from kitops.modelkit.manager:

from kitops.modelkit.manager import ModelKitManager

A class to represent a modelkit manager. This class manages the user credentials and modelkit reference. Attributes: user_credentials (UserCredentials): The user credentials. modelkit_reference (ModelKitReference): The modelkit reference. Methods: init(): Initializes the ModelKitManager instance. working_directory: Gets or sets the working directory. user_credentials: Gets or sets the user credentials. modelkit_reference: Gets or sets the modelkit reference.

Source code in kitops/modelkit/manager.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
148
149
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
class ModelKitManager:
    """
    A class to represent a modelkit manager.
    This class manages the user credentials and modelkit reference.
    Attributes:
        user_credentials (UserCredentials): The user credentials.
        modelkit_reference (ModelKitReference): The modelkit reference.
        Methods:
        __init__():
            Initializes the ModelKitManager instance.
        working_directory:
            Gets or sets the working directory.
        user_credentials:
            Gets or sets the user credentials.
        modelkit_reference:
            Gets or sets the modelkit reference.     
    """
    def __init__(self,
                 working_directory: str = os.getcwd(),
                 user_credentials: Optional[UserCredentials] = None, 
                 modelkit_reference: Optional[ModelKitReference] = None,
                 modelkit_tag: Optional[str] = None):
        """
        Initializes the ModelKitManager instance.

        Args:
            working_directory (str): The working directory. 
                Defaults to os.getcwd(). This is the directory used by
                the ModelKitManager to work with the given ModelKit or 
                used to create a new ModelKit. The ModelKit's Kitfile
                is also read from and saved to this directory. If the 
                given directory does not exist, it will be created, if possible;
                otherwise, an error will be raised.
            user_credentials (Optional[UserCredentials]): The user credentials. Defaults to None.
                If None, the user credentials are loaded from environment variables.
            modelkit_reference (Optional[ModelKitReference]): The modelkit reference. Defaults to None.
                If None, the modelkit reference is created from the modelkit tag.
            modelkit_tag (Optional[str]): The modelkit tag to parse into a ModelKitReference. Defaults to None.
                If None, an empty ModelKitReference is created.

        Examples:
            >>> manager = ModelKitManager()
            >>> manager.working_directory
            <WorkingDirectory>
            >>> manager.user_credentials
            <UserCredentials>
            >>> manager.modelkit_reference
            <ModelKitReference>
        """
        self.working_directory = working_directory

        if user_credentials is not None:
            self.user_credentials = user_credentials
        else:
            self.user_credentials = UserCredentials()

        if modelkit_reference is not None:
            self.modelkit_reference = modelkit_reference
        else:
            # try to build the modelkit reference from the tag.
            # if modelkit_tag is None then an empty ModelkitReference 
            # will be created.
            self.modelkit_reference = ModelKitReference(modelkit_tag)

    @property
    def working_directory(self) -> str:
        """
        Gets the working directory.

        Returns:
            str: The working directory.
        """
        return self._working_directory    

    @working_directory.setter
    def working_directory(self, value: str):
        """
        Sets the working directory.

        Args:
            value (str): The working directory to set.
        """
        self._working_directory = get_or_create_directory(value)

    @property
    def user_credentials(self):
        """
        Gets the user credentials.

        Returns:

        """
        return self._user_credentials

    @user_credentials.setter
    def user_credentials(self, value: UserCredentials):
        """
        Sets the user credentials.

        Args:
            value (UserCredentials): The user credentials to set.
        """
        self._user_credentials = value

    @property
    def modelkit_reference(self) -> ModelKitReference:
        """
        Gets the modelkit reference.

        Returns:
            ModelKitReference: The modelkit reference.
        """
        return self._modelkit_reference

    @modelkit_reference.setter
    def modelkit_reference(self, value: ModelKitReference):
        """
        Sets the modelkit reference.

        Args:
            value (ModelKitReference): The modelkit reference to set.
        """
        self._modelkit_reference = value

    @property
    def kitfile(self) -> Kitfile:
        """
        Gets the Kitfile.

        Returns:
            Kitfile: The Kitfile.
        """
        return self._kitfile

    @kitfile.setter
    def kitfile(self, value: Kitfile) -> None:
        """
        Sets the Kitfile.

        Args:
            value (Kitfile): The Kitfile to set.
        """
        self._kitfile = value

    def pull_and_unpack_modelkit(self, load_kitfile: bool = False,
                                 filters: Optional[list[str]] = None) -> None:
        """
        Unpacks the ModelKit into the working directory.

        Args:
            filters (Optional[list[str]]): The filters to apply when 
                unpacking the ModelKit.  Defaults to None.  The filters
                are used to specify the Kitfile parts to unpack from the
                ModelKit (e.g. ["model", "data"]). If None, all parts
                are unpacked.
            load_kitfile (bool): If True, the Kitfile will be loaded 
                from the working directory afer the ModelKit has been
                unpacked. Defaults to False.

        Returns:
            None
        """
        kit.login(user = self.user_credentials.username, 
                  passwd = self.user_credentials.password,
                  registry = self.modelkit_reference.registry)

        if not filters:
            # If no filters are provided, go ahead and issue a pull request
            # before unpacking; otherwise, issue the unpack request only.
            kit.pull(self.modelkit_reference.modelkit_tag)
        kit.unpack(self.modelkit_reference.modelkit_tag, 
                   dir = self.working_directory)
        kit.logout(registry = self.modelkit_reference.registry)

        if load_kitfile:
            kitfile_path = self.working_directory + "/Kitfile"
            self.kitfile = Kitfile(kitfile_path)

    def pack_and_push_modelkit(self, save_kitfile: bool = False) -> None:
        """
        Packs the ModelKit from the working directory and pushes it 
        to the registry.

        Args:
            save_kitfile (bool): If True, the Kitfile will be saved to 
                the working directory before the Kitfile is packed and
                pushed. Defaults to False.

        Returns:
            None
        """
        # save the current directory so we can return to it later
        current_directory = os.getcwd()
        os.chdir(self.working_directory)

        if save_kitfile:
            self.kitfile.save()

        kit.login(user = self.user_credentials.username, 
                  passwd = self.user_credentials.password,
                  registry = self.modelkit_reference.registry)
        kit.pack(self.modelkit_reference.modelkit_tag)
        kit.push(self.modelkit_reference.modelkit_tag)
        kit.logout(registry = self.modelkit_reference.registry)

        # return to the original directory
        os.chdir(current_directory)

    def remove_modelkit(self, local: Optional[bool] = False,
                        remote: Optional[bool] = False) -> None:
        """
        Removes the ModelKit from the registry.

        Args:
            local (Optional[bool]): If True, the ModelKit will be removed
                from the local registry. Defaults to True.
            remote (Optional[bool]): If True, the ModelKit will be removed 
                from the remote registry.

        Returns:
            None

        Examples:
            >>> modelkit_tag = "jozu.ml/brett/titanic-survivability:latest"
            >>> manager = ModelKitManager(working_directory = "temp/titanic-full",
            ...                           modelkit_tag = modelkit_tag)
            >>> manager.remove_modelkit(local = True, remote = True)
        """
        kit.login(user = self.user_credentials.username, 
                  passwd = self.user_credentials.password,
                  registry = self.modelkit_reference.registry)
        if local:
            kit.remove(self.modelkit_reference.modelkit_tag, remote = False)

        if remote:
            kit.remove(self.modelkit_reference.modelkit_tag, remote = True)

        kit.logout(registry = self.modelkit_reference.registry)

kitfile: Kitfile property writable

Gets the Kitfile.

Returns:

Name Type Description
Kitfile Kitfile

The Kitfile.

modelkit_reference: ModelKitReference property writable

Gets the modelkit reference.

Returns:

Name Type Description
ModelKitReference ModelKitReference

The modelkit reference.

user_credentials property writable

Gets the user credentials.

Returns:

working_directory: str property writable

Gets the working directory.

Returns:

Name Type Description
str str

The working directory.

__init__(working_directory=os.getcwd(), user_credentials=None, modelkit_reference=None, modelkit_tag=None)

Initializes the ModelKitManager instance.

Parameters:

Name Type Description Default
working_directory str

The working directory. Defaults to os.getcwd(). This is the directory used by the ModelKitManager to work with the given ModelKit or used to create a new ModelKit. The ModelKit's Kitfile is also read from and saved to this directory. If the given directory does not exist, it will be created, if possible; otherwise, an error will be raised.

getcwd()
user_credentials Optional[UserCredentials]

The user credentials. Defaults to None. If None, the user credentials are loaded from environment variables.

None
modelkit_reference Optional[ModelKitReference]

The modelkit reference. Defaults to None. If None, the modelkit reference is created from the modelkit tag.

None
modelkit_tag Optional[str]

The modelkit tag to parse into a ModelKitReference. Defaults to None. If None, an empty ModelKitReference is created.

None

Examples:

>>> manager = ModelKitManager()
>>> manager.working_directory
<WorkingDirectory>
>>> manager.user_credentials
<UserCredentials>
>>> manager.modelkit_reference
<ModelKitReference>
Source code in kitops/modelkit/manager.py
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
67
68
69
70
71
72
73
def __init__(self,
             working_directory: str = os.getcwd(),
             user_credentials: Optional[UserCredentials] = None, 
             modelkit_reference: Optional[ModelKitReference] = None,
             modelkit_tag: Optional[str] = None):
    """
    Initializes the ModelKitManager instance.

    Args:
        working_directory (str): The working directory. 
            Defaults to os.getcwd(). This is the directory used by
            the ModelKitManager to work with the given ModelKit or 
            used to create a new ModelKit. The ModelKit's Kitfile
            is also read from and saved to this directory. If the 
            given directory does not exist, it will be created, if possible;
            otherwise, an error will be raised.
        user_credentials (Optional[UserCredentials]): The user credentials. Defaults to None.
            If None, the user credentials are loaded from environment variables.
        modelkit_reference (Optional[ModelKitReference]): The modelkit reference. Defaults to None.
            If None, the modelkit reference is created from the modelkit tag.
        modelkit_tag (Optional[str]): The modelkit tag to parse into a ModelKitReference. Defaults to None.
            If None, an empty ModelKitReference is created.

    Examples:
        >>> manager = ModelKitManager()
        >>> manager.working_directory
        <WorkingDirectory>
        >>> manager.user_credentials
        <UserCredentials>
        >>> manager.modelkit_reference
        <ModelKitReference>
    """
    self.working_directory = working_directory

    if user_credentials is not None:
        self.user_credentials = user_credentials
    else:
        self.user_credentials = UserCredentials()

    if modelkit_reference is not None:
        self.modelkit_reference = modelkit_reference
    else:
        # try to build the modelkit reference from the tag.
        # if modelkit_tag is None then an empty ModelkitReference 
        # will be created.
        self.modelkit_reference = ModelKitReference(modelkit_tag)

pack_and_push_modelkit(save_kitfile=False)

Packs the ModelKit from the working directory and pushes it to the registry.

Parameters:

Name Type Description Default
save_kitfile bool

If True, the Kitfile will be saved to the working directory before the Kitfile is packed and pushed. Defaults to False.

False

Returns:

Type Description
None

None

Source code in kitops/modelkit/manager.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def pack_and_push_modelkit(self, save_kitfile: bool = False) -> None:
    """
    Packs the ModelKit from the working directory and pushes it 
    to the registry.

    Args:
        save_kitfile (bool): If True, the Kitfile will be saved to 
            the working directory before the Kitfile is packed and
            pushed. Defaults to False.

    Returns:
        None
    """
    # save the current directory so we can return to it later
    current_directory = os.getcwd()
    os.chdir(self.working_directory)

    if save_kitfile:
        self.kitfile.save()

    kit.login(user = self.user_credentials.username, 
              passwd = self.user_credentials.password,
              registry = self.modelkit_reference.registry)
    kit.pack(self.modelkit_reference.modelkit_tag)
    kit.push(self.modelkit_reference.modelkit_tag)
    kit.logout(registry = self.modelkit_reference.registry)

    # return to the original directory
    os.chdir(current_directory)

pull_and_unpack_modelkit(load_kitfile=False, filters=None)

Unpacks the ModelKit into the working directory.

Parameters:

Name Type Description Default
filters Optional[list[str]]

The filters to apply when unpacking the ModelKit. Defaults to None. The filters are used to specify the Kitfile parts to unpack from the ModelKit (e.g. ["model", "data"]). If None, all parts are unpacked.

None
load_kitfile bool

If True, the Kitfile will be loaded from the working directory afer the ModelKit has been unpacked. Defaults to False.

False

Returns:

Type Description
None

None

Source code in kitops/modelkit/manager.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def pull_and_unpack_modelkit(self, load_kitfile: bool = False,
                             filters: Optional[list[str]] = None) -> None:
    """
    Unpacks the ModelKit into the working directory.

    Args:
        filters (Optional[list[str]]): The filters to apply when 
            unpacking the ModelKit.  Defaults to None.  The filters
            are used to specify the Kitfile parts to unpack from the
            ModelKit (e.g. ["model", "data"]). If None, all parts
            are unpacked.
        load_kitfile (bool): If True, the Kitfile will be loaded 
            from the working directory afer the ModelKit has been
            unpacked. Defaults to False.

    Returns:
        None
    """
    kit.login(user = self.user_credentials.username, 
              passwd = self.user_credentials.password,
              registry = self.modelkit_reference.registry)

    if not filters:
        # If no filters are provided, go ahead and issue a pull request
        # before unpacking; otherwise, issue the unpack request only.
        kit.pull(self.modelkit_reference.modelkit_tag)
    kit.unpack(self.modelkit_reference.modelkit_tag, 
               dir = self.working_directory)
    kit.logout(registry = self.modelkit_reference.registry)

    if load_kitfile:
        kitfile_path = self.working_directory + "/Kitfile"
        self.kitfile = Kitfile(kitfile_path)

remove_modelkit(local=False, remote=False)

Removes the ModelKit from the registry.

Parameters:

Name Type Description Default
local Optional[bool]

If True, the ModelKit will be removed from the local registry. Defaults to True.

False
remote Optional[bool]

If True, the ModelKit will be removed from the remote registry.

False

Returns:

Type Description
None

None

Examples:

>>> modelkit_tag = "jozu.ml/brett/titanic-survivability:latest"
>>> manager = ModelKitManager(working_directory = "temp/titanic-full",
...                           modelkit_tag = modelkit_tag)
>>> manager.remove_modelkit(local = True, remote = True)
Source code in kitops/modelkit/manager.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def remove_modelkit(self, local: Optional[bool] = False,
                    remote: Optional[bool] = False) -> None:
    """
    Removes the ModelKit from the registry.

    Args:
        local (Optional[bool]): If True, the ModelKit will be removed
            from the local registry. Defaults to True.
        remote (Optional[bool]): If True, the ModelKit will be removed 
            from the remote registry.

    Returns:
        None

    Examples:
        >>> modelkit_tag = "jozu.ml/brett/titanic-survivability:latest"
        >>> manager = ModelKitManager(working_directory = "temp/titanic-full",
        ...                           modelkit_tag = modelkit_tag)
        >>> manager.remove_modelkit(local = True, remote = True)
    """
    kit.login(user = self.user_credentials.username, 
              passwd = self.user_credentials.password,
              registry = self.modelkit_reference.registry)
    if local:
        kit.remove(self.modelkit_reference.modelkit_tag, remote = False)

    if remote:
        kit.remove(self.modelkit_reference.modelkit_tag, remote = True)

    kit.logout(registry = self.modelkit_reference.registry)