def predict(params): pgid = getattr(params, "pgid", None) modelid = getattr(params, "modelid", "latest") method = getattr(params, "method", "post") try: expectedArgs = [ "confthre", "containHeatMap", "containRle", "containPolygon", "attrthre", "waitForResults", "genCaption" ] infParams = dict( filter( lambda item: (item[0] in expectedArgs) and (item[1] is not None), vars(params).items())) # print(infParams) files = {'files': (params.files, open(params.files, 'rb'))} rsp = server.projects.predict(pgid, modelid, infParams) if rsp is None: reportApiError( server, f"Failed trying to predict with model '{modelid}' for project group {pgid}" ) else: reportSuccess(server) except IOError as ioe: reportUsageError( f"IO Error accessing file {params.files}; errno={ioe.errno}; msg={str(ioe)}\n" )
def savelabels(params): """Handles the 'savelabels' operation""" dsid = params.get("--dsid", "missing_id") fileid = params.get("--fileid", "missing_id") file_name = params.get("--label_file") if file_name is not None: try: with open(file_name) as json_file: data = json.load(json_file) except Exception as e: print( f"ERROR: failed to read json data from file '{file_name}'; {e}", file=sys.stderr) return -1 else: try: data = json.loads(params.get("<json_string>", "")) except Exception as e: print(f"ERROR: Failed to convert label input to json; {e}", file=sys.stderr) return -1 rsp = server.object_labels.create(dsid, fileid, data) if rsp is None: reportApiError( server, f"Failed to save labels for file {fileid} in dataset {dsid}") else: reportSuccess( server, f"Successfully created labels for file {fileid} in dataset {dsid}." )
def move(params): """Handles the 'move' operation for loading files into a dataset. The "<file_paths>" from 'params' is passed to the library. """ fromDs = params.get("--from", "missing-id") toDs = params.get("--to", "") rsp = server.files.copymove("move", fromDs, toDs, params["<file_ids>"]) if rsp is None: try: results = server.json()["result_list"] total = len(results) success = sum([1 for x in results if "new_file_id" in x]) fail = total - success except: total = "?" success = "?" fail = "?" reportApiError( server, f"Failure copying files from dataset {fromDs} to dataset {toDs}; total={total}, successes={success}, fails={fail}" ) else: try: results = server.json()["result_list"] total = len(results) except: total = "?" reportSuccess(server, f"Successfully moved {total} files to dataset {toDs}")
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 show(params): pgid = getattr(params, "pgid", None) rsp = server.projects.show(pgid) if rsp is None: reportApiError(server, "Failure attempting to get project group id '{}'".format(pgid)) else: reportSuccess(server)
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 remove(params): pgid = params.get("--pgid", "missing_id") dsid = params.get("--dsid", None) modelid = params.get("--modelid", None) if dsid is None and modelid is None: print("No dataset id or model id provided during 'remove' operation.", file=sys.stderr) return -1 if dsid is not None and modelid is not None: print( "Only one of '--dsid' or '--modelid' can be specified at a time.", file=sys.stderr) return -1 if dsid is not None: rsp = server.datasets.update(dsid, project_group_id="") id = dsid resource = "dataset" else: rsp = server.trained_models.update(modelid, project_group_id="") id = modelid resource = "trained model" if rsp is None: reportApiError( server, f"Failure attempting to remove {resource} id {id} from project group id '{pgid}'" ) else: reportSuccess( server, f"Successfully removed {resource} id {id} from project group 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", "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 show(params): """Handles the 'show' operation to show details of a single dltask""" dltask = params.get("--taskid", "missing_id") rsp = server.dl_tasks.show(dltask) if rsp is None: reportApiError(server, f"Failure attempting to get dltask id '{dltask}'") else: reportSuccess(server)
def delete(params): """Undeploys the model identified by the --modelid parameter.""" modelid = params.get("--modelid", "missing_id") rsp = server.deployed_models.delete(modelid) if rsp is None: reportApiError(server, f"Failure attempting to undeploy model id '{modelid}'") else: reportSuccess(server, f"Undeployed model id '{modelid}'")
def deploy(params): pgid = getattr(params, "pgid", None) modelid = getattr(params, "modelid", "latest") rsp = server.projects.deploy(pgid, modelid=modelid) if rsp is None: reportApiError(server, f"Failed trying to deploy '{modelid}' model for project group {pgid}") else: reportSuccess(server, f"Successfully deployed '{modelid}' model for project group {pgid}")
def show(params): """Handles the 'show' operation to show details of a single model""" model = params.get("--modelid", "missing_id") rsp = server.deployed_models.show(model) if rsp is None: reportApiError(server, f"Failure attempting to get model id '{model}'") else: reportSuccess(server)
def status(params): """Handles the 'status' operation to show training status messages of a dltask""" dltask = params.get("--taskid", "missing_id") rsp = server.dl_tasks.status(dltask) if rsp is None: reportApiError(server, f"Failure attempting to get dltask id '{dltask}'") else: reportSuccess(server)
def create(params): expectedArgs = ["name", "description", "enforce_pwf", "auto_deploy"] kwargs = dict(filter(lambda elem: (elem[0] in expectedArgs) and (elem[1] is not None), vars(params).items())) rsp = server.projects.create(**kwargs) if rsp is None: reportApiError(server, "Failure while creating a project group") else: reportSuccess(server, "Successfully created project group")
def token(params): """ Handles getting an authentication token for a specific user""" user = params.get("--user", None) pw = params.get("--password", None) rsp = server.users.get_token(user, pw) if rsp is None or rsp.get("result", "fail") == "fail": reportApiError(server, f"Failed to get token for user '{user}'") else: reportSuccess(server, rsp["token"])
def show(params): """Handles the 'show' operation to show details of a single project-group""" pgid = params.get("--pgid", "missing_id") rsp = server.projects.show(pgid) if rsp is None: reportApiError(server, f"Failure attempting to get project group id '{pgid}'") else: reportSuccess(server)
def show(params): """Handles the 'show' operation to show details of a single dataset""" dsid = params.get("--dsid", "missing_id") rsp = server.datasets.show(dsid) if rsp is None: reportApiError(server, f"Failure attempting to get dataset id '{dsid}'") else: reportSuccess(server)
def export(params): """Exports file metadata for all files in a dataset in CSV format """ dsid = params.get("--dsid", "missing_id") server.file_metadata.export(dsid) if server.rsp_ok(): # Must strip the trailing new line. reportSuccess(server, server.raw_http_response().text[:-1]) else: reportApiError(server, f"Failure attempting to export all files' user metadata in dataset id '{dsid}'")
def change(params): pgid = getattr(params, "pgid", None) expectedArgs = ["name", "description", "enforce_pwf", "auto_deploy"] kwargs = dict(filter(lambda elem: (elem[0] in expectedArgs) and (elem[1] is not None), vars(params).items())) rsp = server.projects.update(pgid, **kwargs) if rsp is None: reportApiError(server, "Failure attempting to change project group id '{}'".format(pgid)) else: reportSuccess(server, "Changed project group id '{}'".format(pgid))
def delete(params): """Deletes one dltask identified by the --taskid parameter. Future work should allow a list of dltasks.""" taskid = params.get("--taskid", "missing_id") rsp = server.dl_tasks.delete(taskid) if rsp is None: reportApiError(server, f"Failure attempting to delete dltask id '{taskid}'") else: reportSuccess(server, f"Deleted dltask id '{taskid}'")
def delete(params): """Deletes one model identified by the --modelid parameter. Future work should allow a list of trained-models.""" modelid = params.get("--modelid", "missing_id") rsp = server.trained_models.delete(modelid) if rsp is None: reportApiError(server, f"Failure attempting to delete model id '{modelid}'") else: reportSuccess(server, f"Deleted model id '{modelid}'")
def delete(params): """Deletes one project group identified by the --pgid parameter. Future work should allow a list of project groups.""" pgid = params.get("--pgid", "missing_id") rsp = server.projects.delete(pgid) if rsp is None: reportApiError( server, f"Failure attempting to delete project group id {pgid}") else: reportSuccess(server, f"Deleted project group id '{pgid}'")
def report(params): """ Lists all user metadata on the specified file.""" dsid = params.get("--dsid", "no_ds_id") fileid = params.get("--fileid", "no_file_id") server.file_metadata.report(dsid, fileid) if server.rsp_ok(): reportSuccess(server, None) else: reportApiError(server, f"Failure attempting to list metadata on file {fileid} in dataset {dsid}")
def delete(params): """Deletes one dataset identified by the --dsid parameter. Future work should allow a list of datasets.""" dsid = params.get("--dsid", "missing_id") rsp = server.datasets.delete(dsid) if rsp is None: reportApiError(server, f"Failure attempting to delete dataset id {dsid}") else: reportSuccess(server, f"Deleted dataset id '{dsid}'")
def getlabels(params): """Handles the 'getlabels' operation""" dsid = params.get("--dsid", "missing_id") fileid = params.get("--fileid", "missing_id") rsp = server.object_labels.getlabels(dsid, fileid) if rsp is None: reportApiError( server, f"Failed to get labels for dataset {dsid} and file {fileid}") else: reportSuccess(server, None)
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 delete(params): """Deletes one user file metadata key as specified. Future work should allow a list of files.""" dsid = params.get("--dsid", "no_ds_id") fileid = params.get("--fileid", "no_file_id") keys = params.get("<key-name>", []) deletedKeys = server.file_metadata.delete(dsid, fileid, keys) if server.rsp_ok() is False: reportApiError(server, f"Failure attempting to delete user metadata from file '{fileid}'") else: reportSuccess(server, f"Deleted metadata with keys {deletedKeys} ({len(deletedKeys)} keys) from file '{fileid}'")
def export(params): """Handles the 'export' operation for the indicated model""" model = params.get("--modelid", "missing_id") filename = params.get("--filename", None) filepath = server.trained_models.export(model, filename, status_callback=None) if filepath is None: reportApiError(server, f"Failure attempting to export model id '{model}'") else: reportSuccess(server, filepath)
def download_asset(params): """Handles downloading the indicated asset from the indicated model""" model = params.get("--modelid", "missing_id") asset = params.get("--assettype", "unknown_asset") filename = params.get("--filename", None) rsp = server.trained_models.download_asset(model, asset, filename) if rsp is None: reportApiError( server, f"Failure attempting to asset '{asset}' for model id '{model}'") else: reportSuccess(server)
def show(params): """Shows details of a specific file user metadata key""" dsid = params.get("--dsid", "missing_id") name = params.get("--name", "missing_id") rsp = server.file_keys.show(dsid, name) if rsp is None: reportApiError( server, f"Failure attempting to get file user metadata key '{name}' in dataset id '{dsid}'" ) else: reportSuccess(server)