Path: blob/main/singlestoredb/fusion/handlers/models.py
469 views
#!/usr/bin/env python31import os2from typing import Any3from typing import Dict4from typing import Optional56from ..handler import SQLHandler7from ..result import FusionSQLResult8from .files import ShowFilesHandler9from .utils import get_file_space101112class ShowModelsHandler(ShowFilesHandler):13"""14SHOW MODELS15[ at_path ] [ <like> ]16[ <order-by> ]17[ <limit> ] [ recursive ] [ extended ];1819# File path to list20at_path = AT '<path>'2122# Should the listing be recursive?23recursive = RECURSIVE2425# Should extended attributes be shown?26extended = EXTENDED2728Description29-----------30Displays the list of models in models space.3132Arguments33---------34* ``<path>``: A path in the models space.35* ``<pattern>``: A pattern similar to SQL LIKE clause.36Uses ``%`` as the wildcard character.3738Remarks39-------40* Use the ``LIKE`` clause to specify a pattern and return only the41files that match the specified pattern.42* The ``LIMIT`` clause limits the number of results to the43specified number.44* Use the ``ORDER BY`` clause to sort the results by the specified45key. By default, the results are sorted in the ascending order.46* The ``AT`` clause specifies the path in the models47space to list the files from.48* To return more information about the files, use the ``EXTENDED``49clause.5051Examples52--------53The following command lists the models::5455SHOW MODELS;5657The following command lists the models with additional information::5859SHOW MODELS EXTENDED;6061See Also62--------63* ``UPLOAD MODEL model_name FROM path``64* ``DOWNLOAD MODEL model_name``656667""" # noqa: E5016869def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:70params['file_location'] = 'MODELS'7172return super().run(params)737475ShowModelsHandler.register(overwrite=True)767778class UploadModelHandler(SQLHandler):79"""80UPLOAD MODEL model_name81FROM local_path [ overwrite ];8283# Model Name84model_name = '<model-name>'8586# Path to local file or directory87local_path = '<local-path>'8889# Should an existing file be overwritten?90overwrite = OVERWRITE9192Description93-----------94Uploads a file or folder to models space.9596Arguments97---------98* ``<model-name>``: Model name.99* ``<local-path>``: The path to the file or folder to upload in the local100directory.101102Remarks103-------104* If the ``OVERWRITE`` clause is specified, any existing file at the105specified path in the models space is overwritten.106107Examples108--------109The following command uploads a file to models space and overwrite any110existing files at the specified path::111112UPLOAD MODEL model_name113FROM 'llama3/' OVERWRITE;114115See Also116--------117* ``DOWNLOAD MODEL model_name``118119""" # noqa: E501120121def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:122params['file_location'] = 'MODELS'123124model_name = params['model_name']125local_path = params['local_path']126127file_space = get_file_space(params)128129if os.path.isdir(local_path):130file_space.upload_folder(131local_path=local_path,132path=os.path.join(model_name, ''),133overwrite=params['overwrite'],134)135else:136file_space.upload_file(137local_path=local_path,138path=os.path.join(model_name, local_path),139overwrite=params['overwrite'],140)141142return None143144145UploadModelHandler.register(overwrite=True)146147148class DownloadModelHandler(SQLHandler):149"""150DOWNLOAD MODEL model_name151[ local_path ]152[ overwrite ];153154# Model Name155model_name = '<model-name>'156157# Path to local directory158local_path = TO '<local-path>'159160# Should an existing directory be overwritten?161overwrite = OVERWRITE162163Description164-----------165Download a model from models space.166167Arguments168---------169* ``<model-name>``: Model name to download in models space.170* ``<local-path>``: Specifies the path in the local directory171where the model is downloaded.172173Remarks174-------175* If the ``OVERWRITE`` clause is specified, any existing file or folder at176the download location is overwritten.177* If ``<local-path>`` is not specified, the model is downloaded to the current location.178179Examples180--------181The following command displays the contents of the file on the182standard output::183184DOWNLOAD MODEL llama3;185186The following command downloads a model to a specific location and187overwrites any existing models folder with the name ``local_llama3`` on the local storage::188189DOWNLOAD MODEL llama3190TO 'local_llama3' OVERWRITE;191192See Also193--------194* ``UPLOAD MODEL model_name FROM local_path``195196""" # noqa: E501197198def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:199params['file_location'] = 'MODELS'200201file_space = get_file_space(params)202203model_name = params['model_name']204file_space.download_folder(205path=os.path.join(model_name, ''),206local_path=params['local_path'] or model_name,207overwrite=params['overwrite'],208)209210return None211212213DownloadModelHandler.register(overwrite=True)214215216class DropModelsHandler(SQLHandler):217"""218DROP MODEL model_name;219220# Model Name221model_name = '<model-name>'222223Description224-----------225Deletes a model from models space.226227Arguments228---------229* ``<model-name>``: Model name to delete in models space.230231Example232--------233The following commands deletes a model from a model space::234235DROP MODEL llama3;236237""" # noqa: E501238239def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:240params['file_location'] = 'MODELS'241path = os.path.join(params['model_name'], '')242243file_space = get_file_space(params)244file_space.removedirs(path=path)245246return None247248249DropModelsHandler.register(overwrite=True)250251252