def upload(params): """Handles the 'upload' operation for loading files into a dataset. The "<file_paths>" from 'params' is passed to the library. """ dsid = params.get("--dsid", "missing_id") expectedArgs = {'--metadata': 'user-metadata', '--labels': 'labels'} kwargs = translate_flags(expectedArgs, params) rsp = server.files.upload(dsid, params["<file_paths>"], **kwargs) if rsp is None: try: results = server.json()["resultList"] total = len(results) success = sum([1 for x in results if x["result"] == "success"]) fail = sum([1 for x in results if x["result"] == "fail"]) except: total = "?" success = "?" fail = "?" reportApiError( server, f"Failure uploading files to dataset {dsid}; total={total}, successes={success}, fails={fail}" ) else: try: results = server.json()["resultList"] total = len(results) except: total = "?" reportSuccess( server, f"Successfully uploaded {total} files to dataset {dsid}")
def infer(params): """Handles the 'infer' operation to a deployed model""" modelid = params.get("--modelid", "missing_id") filepath = params.get("<path-to-file>", None) annotateFile = params.get("--annotatefile") expectedArgs = { '--minconfidence': 'confthre', '--heatmap': 'containHeatMap', '--rle': 'containrle', '--polygons': 'containPolygon', 'maxclasses': 'clsnum', 'caption': 'genCaption', 'wait': 'waitForResults' } kwargs = translate_flags(expectedArgs, params) rsp = server.deployed_models.infer(modelid, filepath, **kwargs) if rsp is None: reportApiError(server, f"Failure inferring to model id '{modelid}'") else: if annotateFile is not None: drawAnnotationsOnFile(params, filepath, server.json(), annotateFile) reportSuccess(server)
def report(params): """Handles the 'list' operation. 'params' flags are translated into query-parameter variable names.""" summaryFields = None if params["--summary"]: summaryFields = ["_id", "original_file_name", "file_type"] dsid = params.get("--dsid", "missing_id") expectedArgs = { '--catid': 'category_id', '--parentid': 'parent_id', '--query': 'query', '--sort': 'sortby', '--limit': 'limit', '--skip': 'skip' } kwargs = translate_flags(expectedArgs, params) rsp = server.files.report(dsid, **kwargs) if rsp is None: reportApiError(server, "Failure attempting to list files") else: reportSuccess(server, None, summaryFields=summaryFields)
def change(params): """Handles the 'change' operation for the indicated model""" model = params.get("--modelid", "missing_id") expectedArgs = {'--pgid': 'pg_id', '--prodstatus': 'production_status'} kwargs = translate_flags(expectedArgs, params) rsp = server.trained_models.update(model, **kwargs) if rsp is None: reportApiError(server, f"Failure attempting to change model id '{model}'") else: reportSuccess(server)
def report(params): """Handles the 'list' operation. 'params' flags are translated into query-parameter variable names.""" summaryFields = None if params["--summary"]: summaryFields = ["_id", "name", "total_file_count"] expectedArgs = {'--pgid': 'pg_id', '--sort': 'sortby'} kwargs = translate_flags(expectedArgs, params) rsp = server.datasets.report(**kwargs) if rsp is None: reportApiError(server, "Failure attempting to list datasets") else: reportSuccess(server, None, summaryFields=summaryFields)
def report(params): """Handles the 'list' operation. 'params' flags are translated into query-parameter variable names.""" summaryFields = None if params["--summary"]: summaryFields = ["_id", "usage", "status", "nn_arch", "name"] expectedArgs = {'--sort': 'sortby'} kwargs = translate_flags(expectedArgs, params) rsp = server.deployed_models.report(**kwargs) if rsp is None: reportApiError(server, "Failure attempting to list deployed-models") else: reportSuccess(server, None, summaryFields=summaryFields)
def deploy(params): """Handles the 'deploy' operation to deploy an inferencing endpoint""" model = params.get("--modelid", "missing_id") name = params.get("--name", None) expectedArgs = { '--accel': 'accel_type', '--userdnnid': 'userdnn_id', '--dnnscriptid': 'dnnscript_id' } kwargs = translate_flags(expectedArgs, params) rsp = server.deployed_models.create(model, name, **kwargs) if rsp is None: reportApiError(server, f"Failure attempting to get model id '{model}'") else: reportSuccess(server)
def train(params): """Handles the 'train' operation to train a model from a dataset""" dsid = params.get("--dsid", "missing_id") name = params.get("--name", None) model_type = params.get("--type", None) strat_str = params.get("--hyper", None) expectedArgs = { '--assets': 'generateassets', '--modelid': 'pretrained_model', '--subtype': 'nn_arch', '--userdnn': 'userdnn_id', '--hyper': 'strategy' } kwargs = translate_flags(expectedArgs, params) # If --hyper given, must turn it into a json object before adding to kwargs if strat_str is not None: try: strat = json.loads(strat_str) kwargs["strategy"] = strat except json.JSONDecodeError as e: print(f"ERROR: invalid json found in '--hyper' ({strat_str})", file=sys.stderr) print(train_usage, file=sys.stderr) exit(1) if model_type == "cod": kwargs["action"] = "create-cod-task" if kwargs.get("nn_arch", None) is None: kwargs["nn_arch"] = "frcnn" elif model_type == "act": kwargs["action"] = "create-act-task" else: kwargs["action"] = "create" rsp = server.dl_tasks.create(name, dsid, model_type, **kwargs) if rsp is None: reportApiError(server, f"Failure attempting to train dataset '{dsid}'") else: try: taskid = server.json()["task_id"] except: taskid = "???" reportSuccess(server, f"Started training task with id {taskid}")
def update(params): """Handles the 'change' operation for modifying a dataset. Expected flags in 'params' are translated to Json Field names to identify modifications to be made """ dsid = params.get("--dsid", "missing_id") expectedArgs = {'--pgid': 'project_group_id'} kwargs = translate_flags(expectedArgs, params) rsp = server.datasets.update(dsid, **kwargs) if rsp is None: reportApiError(server, f"Failure attempting to change dataset id {dsid}") else: reportSuccess(server, f"Changed dataset id {dsid}")
def report(params): """Handles the 'list' operation.""" summaryFields = None if params["--summary"]: summaryFields = ["_id", "name"] dsid = params.get("--dsid", "missing_id") expectedArgs = {'--sort': 'sortby'} kwargs = translate_flags(expectedArgs, params) rsp = server.categories.report(dsid, **kwargs) if rsp is None: reportApiError(server, "Failure attempting to list categories") else: reportSuccess(server, None, summaryFields=summaryFields)
def update(params): """Handles the 'change' operation for modifying an object tag. """ dsid = params.get("--dsid", "missingid") tagid = params.get("--tagid", "missing_id") expected_args = {'--name': 'name'} kwargs = translate_flags(expected_args, params) rsp = server.object_tags.update(dsid, tagid, **kwargs) if rsp is None: reportApiError( server, f"Failure attempting to change tag id '{tagid}' in dataset '{dsid}'" ) else: reportSuccess(server, f"Changed tag id '{tagid}' in dataset '{dsid}'")
def create(params): """Handles the 'create' operation for adding an object tag to a dataset. """ dsid = params.get("--dsid", "missing_id") expected_args = {"--name": "name"} kwargs = translate_flags(expected_args, params) rsp = server.object_tags.create(dsid, **kwargs) if rsp is None: reportApiError(server, f"Failed to create object tag in dataset {dsid}") else: tagid = rsp.get("dataset_tag_id", "???") reportSuccess( server, f"Successfully created object tag with id {tagid} in dataset {dsid}" )
def create(params): """Handles the 'create' operation to create a dataset. Expected flags in 'params' are translated to Json Field names for creation content """ expectedArgs = {'--name': 'name'} kwargs = translate_flags(expectedArgs, params) rsp = server.datasets.create(**kwargs) if rsp is None: reportApiError(server, "Failure while creating a dataset") else: try: dsid = server.json()["dataset_id"] except: dsid = "???" reportSuccess(server, f"Successfully created dataset with id {dsid}")
def update(params): """Handles the 'change' operation for modifying a category. """ dsid = params.get("--dsid", "missing_id") catid = params.get("--catid", "missing_id") expected_args = {'--name': 'name'} kwargs = translate_flags(expected_args, params) kwargs["action"] = "rename" rsp = server.categories.action(dsid, catid, **kwargs) if rsp is None: reportApiError( server, f"Failure attempting to change category id '{catid}' in dataset '{dsid}'" ) else: reportSuccess(server, f"Changed category id '{catid}' in dataset '{dsid}'")
def create(params): """Handles the 'create' operation to create a project group. Expected flags in 'params' are translated to Json Field names for creation content """ expectedArgs = {'--name': 'name', '--description': 'description'} kwargs = translate_flags(expectedArgs, params) rsp = server.projects.create(**kwargs) if rsp is None: reportApiError(server, "Failure creating a project group.") else: try: pgid = server.json()["project_group_id"] except: pgid = "???" reportSuccess(server, f"Successfully created a project group with id {pgid}")
def report(params): """Handles the 'list' operation. 'params' flags are translated into query-parameter variable names.""" summaryFields = None if params["--summary"]: summaryFields = [ "_id", "name", "dataset_count", "model_count", "enforce_pwf", "auto_deploy" ] expectedArgs = {'--sort': 'sortby'} kwargs = translate_flags(expectedArgs, params) rsp = server.projects.report(**kwargs) if rsp is None: reportApiError(server, "Failure attempting to list project groups") else: reportSuccess(server, None, summaryFields=summaryFields)
def update(params): """Handles the 'change' operation for modifying a file. Expected flags in 'params' are translated to Json Field names to identify modifications to be made""" dsid = params.get("--dsid", "missing_id") fileid = params.get("--fileid", "missing_id") expectedArgs = {'--catid': 'target_category_id'} kwargs = translate_flags(expectedArgs, params) kwargs["action"] = "change_category" rsp = server.files.action(dsid, fileid, **kwargs) if rsp is None: reportApiError( server, f"Failure attempting to change file id '{fileid}' in dataset '{dsid}'" ) else: reportSuccess(server, f"Changed file id '{fileid}' in dataset '{dsid}'")
def update(params): """Handles the changing attributes of a specific file user metadata key. Expected flags in 'params' are translated to Json Field names to identify modifications to be made""" dsid = params.get("--dsid", "missing_id") name = params.get("--name", "missing_id") expectedArgs = {'--newname': 'name', '--description': 'description'} kwargs = translate_flags(expectedArgs, params) rsp = server.file_keys.update(dsid, name, **kwargs) if rsp is None: reportApiError( server, f"Failure attempting to change file user metadata key '{name}' in dataset '{dsid}'" ) else: reportSuccess( server, f"Changed file user metadata key'{name}' in dataset '{dsid}'")
def update(params): """Handles the 'change' operation for modifying a project group. Expected flags in 'params' are translated to Json Field names to identify modifications to be made """ pgid = params.get("--pgid", "missing_id") expectedArgs = { '--name': 'project_group_name', '--description': 'description', '--enforce-pwf': 'enforce_pwf', '--auto-deploy': 'auto_deploy' } kwargs = translate_flags(expectedArgs, params) rsp = server.projects.update(pgid, **kwargs) if rsp is None: reportApiError( server, f"Failure attempting to modify project group id {pgid}") else: reportSuccess(server, f"Changed project group id {pgid}")