Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EAGLE-3887] model uploadv2 cli #269

Merged
merged 9 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions clarifai/models/model_serving/cli/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import subprocess
from typing import Dict, Union

from ..constants import (CLARIFAI_EXAMPLES_REPO, CLARIFAI_EXAMPLES_REPO_PATH,
MODEL_UPLOAD_EXAMPLE_FOLDER)


def download_examples_repo(forced_download: bool = False):
if not os.path.isdir(CLARIFAI_EXAMPLES_REPO_PATH):
print(f"Download examples to {CLARIFAI_EXAMPLES_REPO_PATH}")
subprocess.run(f"git clone {CLARIFAI_EXAMPLES_REPO} {CLARIFAI_EXAMPLES_REPO_PATH}")
else:
if forced_download:
os.chdir(CLARIFAI_EXAMPLES_REPO_PATH)
subprocess.run("git pull")


def list_model_upload_examples(
forced_download: bool = False) -> Dict[str, tuple[str, Union[str, None]]]:
download_examples_repo(forced_download)
model_upload_folder = MODEL_UPLOAD_EXAMPLE_FOLDER
model_upload_path = os.path.join(CLARIFAI_EXAMPLES_REPO_PATH, model_upload_folder)
examples = {}
for model_type_ex in os.listdir(model_upload_path):
_folder = os.path.join(model_upload_path, model_type_ex)
if os.path.isdir(_folder):
_walk = list(os.walk(_folder))
if len(_walk) > 0:
_, model_names, _files = _walk[0]
readme = [item for item in _files if "readme" in item.lower()]
for name in model_names:
examples.update({
f"{model_type_ex}/{name}": [
os.path.join(_folder, name),
os.path.join(_folder, readme[0]) or None
]
})

return examples
14 changes: 14 additions & 0 deletions clarifai/models/model_serving/cli/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from abc import ABC, abstractmethod
from argparse import _SubParsersAction


class BaseClarifaiCli(ABC):

@staticmethod
@abstractmethod
def register(parser: _SubParsersAction):
raise NotImplementedError()

@abstractmethod
def run(self):
raise NotImplementedError()
31 changes: 31 additions & 0 deletions clarifai/models/model_serving/cli/clarifai_clis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from argparse import ArgumentParser

from .create import CreateCli
from .example_cli import ExampleCli
from .login import LoginCli
from .upload import UploadCli


def main():

parser = ArgumentParser("clarifai")
cmd_parser = parser.add_subparsers(help="Clarifai cli helpers")

UploadCli.register(cmd_parser)
CreateCli.register(cmd_parser)
LoginCli.register(cmd_parser)
ExampleCli.register(cmd_parser)

args = parser.parse_args()

if not hasattr(args, "func"):
parser.print_help()
exit(1)

# Run
service = args.func(args)
service.run()


if __name__ == "__main__":
main()
180 changes: 180 additions & 0 deletions clarifai/models/model_serving/cli/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import os
import shutil
from argparse import Namespace, _SubParsersAction
from typing import List

from InquirerPy import prompt

from ..constants import MAX_HW_DIM
from ..model_config import MODEL_TYPES, get_model_config
from ..pb_model_repository import TritonModelRepository
from ._utils import list_model_upload_examples
from .base import BaseClarifaiCli


class CreateCli(BaseClarifaiCli):

@staticmethod
def register(parser: _SubParsersAction):
creator_parser = parser.add_parser("create", help="Create component of Clarifai platform")
sub_creator_parser = creator_parser.add_subparsers()

SubCreateModelCli.register(sub_creator_parser)

creator_parser.set_defaults(func=CreateCli)


class SubCreateModelCli(BaseClarifaiCli):

@staticmethod
def register(parser: _SubParsersAction):
model_parser = parser.add_parser("model")
model_parser.add_argument(
"--working-dir",
type=str,
required=True,
help="Path to your working dir. Create new dir if it does not exist")
model_parser.add_argument(
"--from-example",
required=False,
action="store_true",
help="Create repository from example")
model_parser.add_argument(
"--example-id",
required=False,
type=str,
choices=list_model_upload_examples(),
help="Example id, run `clarifai example list` to list of examples")

model_parser.add_argument(
"--type",
type=str,
choices=MODEL_TYPES,
required=False,
help="Clarifai supported model types.")
model_parser.add_argument(
"--image-shape",
nargs='+',
type=int,
required=False,
help="H W dims for models with an image input type. H and W each have a max value of 1024",
default=[-1, -1])
model_parser.add_argument(
"--max-bs", type=int, default=1, required=False, help="Max batch size")

model_parser.add_argument(
"--overwrite", action="store_true", help="Overwrite working-dir if exists")

model_parser.set_defaults(func=SubCreateModelCli)

def __init__(self, args: Namespace) -> None:
self.working_dir: str = args.working_dir
self.from_example = args.from_example
self.example_id = args.example_id
self.overwrite = args.overwrite

if os.path.exists(self.working_dir):
if self.overwrite:
print(f"Overwrite {self.working_dir}")
else:
raise FileExistsError(
f"{self.working_dir} exists. If you want to overwrite it, please set `--overwrite` flag"
)

# prevent wrong args when creating from example
if not self.from_example:
if len(args.image_shape) != 2:
raise ValueError(
f"image_shape takes 2 values, Height and Width. Got {len(args.image_shape)} values instead."
)
if args.image_shape[0] > MAX_HW_DIM or args.image_shape[1] > MAX_HW_DIM:
raise ValueError(
f"H and W each have a maximum value of {MAX_HW_DIM}. Got H: {args.image_shape[0]}, W: {args.image_shape[1]}"
)
self.image_shape: List[int] = args.image_shape

self.type: str = args.type
self.max_bs: int = args.max_bs

else:
if not self.example_id:
questions = [
{
"type": "list",
"message": "Select an example:",
"choices": list_model_upload_examples(),
},
]
result = prompt(questions)
self.example_id = result[0]

def run(self):
if self.from_example:
os.makedirs(self.working_dir, exist_ok=True)
model_repo, readme = list_model_upload_examples()[self.example_id]
shutil.copytree(model_repo, self.working_dir, dirs_exist_ok=True)
if readme:
shutil.copy(readme, os.path.join(self.working_dir, "readme.md"))

else:
model_config = get_model_config(self.type).make_triton_model_config(
model_name="",
model_version="1",
image_shape=self.image_shape,
max_batch_size=self.max_bs,
)

triton_repo = TritonModelRepository(model_config)
triton_repo.build_repository(self.working_dir)

from itertools import islice
from pathlib import Path

def tree(dir_path: Path,
level: int = -1,
limit_to_directories: bool = False,
length_limit: int = 1000):
# prefix components:
space = ' '
branch = '│ '
# pointers:
tee = '├── '
last = '└── '
"""Given a directory Path object print a visual tree structure"""
dir_path = Path(dir_path) # accept string coerceable to Path
files = 0
directories = 0

def inner(dir_path: Path, prefix: str = '', level=-1):
nonlocal files, directories
if not level:
return # 0, stop iterating
if limit_to_directories:
contents = [d for d in dir_path.iterdir() if d.is_dir()]
else:
contents = list(dir_path.iterdir())
pointers = [tee] * (len(contents) - 1) + [last]
for pointer, path in zip(pointers, contents):
if path.is_dir():
yield prefix + pointer + path.name
directories += 1
extension = branch if pointer == tee else space
yield from inner(path, prefix=prefix + extension, level=level - 1)
elif not limit_to_directories:
yield prefix + pointer + path.name
files += 1

print(dir_path.name)
iterator = inner(dir_path, level=level)
for line in islice(iterator, length_limit):
print(line)
if next(iterator, None):
print(f'... length_limit, {length_limit}, reached, counted:')
print(f'\n{directories} directories' + (f', {files} files' if files else ''))

print("-" * 75)
print(f"* Created repository at: {self.working_dir}")
tree(self.working_dir)
print()
print("* Please make sure your code is tested using `test.py` before uploading")
print("-" * 75)
123 changes: 0 additions & 123 deletions clarifai/models/model_serving/cli/deploy_cli.py

This file was deleted.

Loading
Loading