Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/fusion/handlers/models.py
801 views
1
#!/usr/bin/env python3
2
import os
3
from typing import Any
4
from typing import Dict
5
from typing import Optional
6
7
from .. import result
8
from ..handler import SQLHandler
9
from ..result import FusionSQLResult
10
from .files import ShowFilesHandler
11
from .utils import get_file_space
12
from .utils import get_inference_api
13
from .utils import get_inference_api_manager
14
15
16
class ShowCustomModelsHandler(ShowFilesHandler):
17
"""
18
SHOW CUSTOM MODELS
19
[ at_path ] [ <like> ]
20
[ <order-by> ]
21
[ <limit> ] [ recursive ] [ extended ];
22
23
# File path to list
24
at_path = AT '<path>'
25
26
# Should the listing be recursive?
27
recursive = RECURSIVE
28
29
# Should extended attributes be shown?
30
extended = EXTENDED
31
32
Description
33
-----------
34
Displays the list of models in models space.
35
36
Arguments
37
---------
38
* ``<path>``: A path in the models space.
39
* ``<pattern>``: A pattern similar to SQL LIKE clause.
40
Uses ``%`` as the wildcard character.
41
42
Remarks
43
-------
44
* Use the ``LIKE`` clause to specify a pattern and return only the
45
files that match the specified pattern.
46
* The ``LIMIT`` clause limits the number of results to the
47
specified number.
48
* Use the ``ORDER BY`` clause to sort the results by the specified
49
key. By default, the results are sorted in the ascending order.
50
* The ``AT`` clause specifies the path in the models
51
space to list the files from.
52
* To return more information about the files, use the ``EXTENDED``
53
clause.
54
55
Examples
56
--------
57
The following command lists the models::
58
59
SHOW CUSTOM MODELS;
60
61
The following command lists the models with additional information::
62
63
SHOW CUSTOM MODELS EXTENDED;
64
65
See Also
66
--------
67
* ``UPLOAD CUSTOM MODEL model_name FROM path``
68
* ``DOWNLOAD CUSTOM MODEL model_name``
69
70
71
""" # noqa: E501
72
73
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
74
params['file_location'] = 'MODELS'
75
76
return super().run(params)
77
78
79
ShowCustomModelsHandler.register(overwrite=True)
80
81
82
class UploadCustomModelHandler(SQLHandler):
83
"""
84
UPLOAD CUSTOM MODEL model_name
85
FROM local_path [ overwrite ];
86
87
# Model Name
88
model_name = '<model-name>'
89
90
# Path to local file or directory
91
local_path = '<local-path>'
92
93
# Should an existing file be overwritten?
94
overwrite = OVERWRITE
95
96
Description
97
-----------
98
Uploads a file or folder to models space.
99
100
Arguments
101
---------
102
* ``<model-name>``: Model name.
103
* ``<local-path>``: The path to the file or folder to upload in the local
104
directory.
105
106
Remarks
107
-------
108
* If the ``OVERWRITE`` clause is specified, any existing file at the
109
specified path in the models space is overwritten.
110
111
Examples
112
--------
113
The following command uploads a file to models space and overwrite any
114
existing files at the specified path::
115
116
UPLOAD CUSTOM MODEL model_name
117
FROM 'llama3/' OVERWRITE;
118
119
See Also
120
--------
121
* ``DOWNLOAD CUSTOM MODEL model_name``
122
123
""" # noqa: E501
124
125
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
126
params['file_location'] = 'MODELS'
127
128
model_name = params['model_name']
129
local_path = params['local_path']
130
131
file_space = get_file_space(params)
132
133
if os.path.isdir(local_path):
134
file_space.upload_folder(
135
local_path=local_path,
136
path=os.path.join(model_name, ''),
137
overwrite=params['overwrite'],
138
)
139
else:
140
file_space.upload_file(
141
local_path=local_path,
142
path=os.path.join(model_name, local_path),
143
overwrite=params['overwrite'],
144
)
145
146
return None
147
148
149
UploadCustomModelHandler.register(overwrite=True)
150
151
152
class DownloadCustomModelHandler(SQLHandler):
153
"""
154
DOWNLOAD CUSTOM MODEL model_name
155
[ local_path ]
156
[ overwrite ];
157
158
# Model Name
159
model_name = '<model-name>'
160
161
# Path to local directory
162
local_path = TO '<local-path>'
163
164
# Should an existing directory be overwritten?
165
overwrite = OVERWRITE
166
167
Description
168
-----------
169
Download a model from models space.
170
171
Arguments
172
---------
173
* ``<model-name>``: Model name to download in models space.
174
* ``<local-path>``: Specifies the path in the local directory
175
where the model is downloaded.
176
177
Remarks
178
-------
179
* If the ``OVERWRITE`` clause is specified, any existing file or folder at
180
the download location is overwritten.
181
* If ``<local-path>`` is not specified, the model is downloaded to the current location.
182
183
Examples
184
--------
185
The following command displays the contents of the file on the
186
standard output::
187
188
DOWNLOAD CUSTOM MODEL llama3;
189
190
The following command downloads a model to a specific location and
191
overwrites any existing models folder with the name ``local_llama3`` on the local storage::
192
193
DOWNLOAD CUSTOM MODEL llama3
194
TO 'local_llama3' OVERWRITE;
195
196
See Also
197
--------
198
* ``UPLOAD CUSTOM MODEL model_name FROM local_path``
199
200
""" # noqa: E501
201
202
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
203
params['file_location'] = 'MODELS'
204
205
file_space = get_file_space(params)
206
207
model_name = params['model_name']
208
file_space.download_folder(
209
path=os.path.join(model_name, ''),
210
local_path=params['local_path'] or model_name,
211
overwrite=params['overwrite'],
212
)
213
214
return None
215
216
217
DownloadCustomModelHandler.register(overwrite=True)
218
219
220
class DropCustomModelHandler(SQLHandler):
221
"""
222
DROP CUSTOM MODEL model_name;
223
224
# Model Name
225
model_name = '<model-name>'
226
227
Description
228
-----------
229
Deletes a model from models space.
230
231
Arguments
232
---------
233
* ``<model-name>``: Model name to delete in models space.
234
235
Example
236
--------
237
The following commands deletes a model from a model space::
238
239
DROP CUSTOM MODEL llama3;
240
241
""" # noqa: E501
242
243
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
244
params['file_location'] = 'MODELS'
245
path = os.path.join(params['model_name'], '')
246
247
file_space = get_file_space(params)
248
file_space.removedirs(path=path)
249
250
return None
251
252
253
DropCustomModelHandler.register(overwrite=True)
254
255
256
class StartModelHandler(SQLHandler):
257
"""
258
START MODEL model_name ;
259
260
# Model Name
261
model_name = '<model-name>'
262
263
Description
264
-----------
265
Starts an inference API model.
266
267
Arguments
268
---------
269
* ``<model-name>``: Name of the model to start.
270
271
Example
272
--------
273
The following command starts a model::
274
275
START MODEL my_model;
276
277
See Also
278
--------
279
* ``STOP MODEL model_name``
280
* ``SHOW MODELS``
281
282
""" # noqa: E501
283
284
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
285
inference_api = get_inference_api(params)
286
operation_result = inference_api.start()
287
288
res = FusionSQLResult()
289
res.add_field('Status', result.STRING)
290
res.add_field('Message', result.STRING)
291
res.set_rows([
292
(
293
operation_result.status,
294
operation_result.get_message(),
295
),
296
])
297
298
return res
299
300
301
StartModelHandler.register(overwrite=True)
302
303
304
class StopModelHandler(SQLHandler):
305
"""
306
STOP MODEL model_name ;
307
308
# Model Name
309
model_name = '<model-name>'
310
311
Description
312
-----------
313
Stops an inference API model.
314
315
Arguments
316
---------
317
* ``<model-name>``: Name of the model to stop.
318
319
Example
320
--------
321
The following command stops a model::
322
323
STOP MODEL my_model;
324
325
See Also
326
--------
327
* ``START MODEL model_name``
328
* ``SHOW MODELS``
329
330
""" # noqa: E501
331
332
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
333
inference_api = get_inference_api(params)
334
operation_result = inference_api.stop()
335
336
res = FusionSQLResult()
337
res.add_field('Status', result.STRING)
338
res.add_field('Message', result.STRING)
339
res.set_rows([
340
(
341
operation_result.status,
342
operation_result.get_message(),
343
),
344
])
345
346
return res
347
348
349
StopModelHandler.register(overwrite=True)
350
351
352
class ShowModelsHandler(SQLHandler):
353
"""
354
SHOW MODELS ;
355
356
Description
357
-----------
358
Displays the list of inference APIs in the current project.
359
360
Example
361
--------
362
The following command lists all inference APIs::
363
364
SHOW MODELS;
365
366
See Also
367
--------
368
* ``START MODEL model_name``
369
* ``STOP MODEL model_name``
370
* ``DROP MODEL model_name``
371
372
""" # noqa: E501
373
374
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
375
inference_api_manager = get_inference_api_manager()
376
models = inference_api_manager.show()
377
378
res = FusionSQLResult()
379
res.add_field('Model Name', result.STRING)
380
res.add_field('Status', result.STRING)
381
382
rows = []
383
for model in models:
384
rows.append((
385
model.name,
386
model.status,
387
))
388
389
res.set_rows(rows)
390
return res
391
392
393
ShowModelsHandler.register(overwrite=True)
394
395
396
class DropModelHandler(SQLHandler):
397
"""
398
DROP MODEL model_name ;
399
400
# Model Name
401
model_name = '<model-name>'
402
403
Description
404
-----------
405
Drops (deletes) an inference API model.
406
407
Arguments
408
---------
409
* ``<model-name>``: Name of the model to drop.
410
411
Example
412
--------
413
The following command drops an inference API::
414
415
DROP MODEL my_model;
416
417
See Also
418
--------
419
* ``START MODEL model_name``
420
* ``STOP MODEL model_name``
421
* ``SHOW MODELS``
422
423
""" # noqa: E501
424
425
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
426
inference_api = get_inference_api(params)
427
operation_result = inference_api.drop()
428
429
res = FusionSQLResult()
430
res.add_field('Model Name', result.STRING)
431
res.add_field('Status', result.STRING)
432
res.add_field('Message', result.STRING)
433
res.set_rows([
434
(
435
operation_result.name,
436
operation_result.status,
437
operation_result.get_message(),
438
),
439
])
440
441
return res
442
443
444
DropModelHandler.register(overwrite=True)
445
446