def redeploy_api() -> str: api_token = connexion.request.form["api_token"] if auth_token(api_token): # Service Status Management tensorflow_model_server_down = True for proc in psutil.process_iter(): pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time']) if pinfo['name'] == "tensorflow_model_server": log("[INFO] TensorFlow model server is up", api_token) tensorflow_model_server_down = False if tensorflow_model_server_down: log("[INFO] Starting a new tensorflow_model_server", api_token) tf_server = subprocess.Popen([ "tensorflow_model_server " "--rest_api_port=$REST_API_PORT " "--model_config_file_poll_wait_seconds=$CONFIG_FILE_POLL_SECONDS " "--model_config_file=$MODEL_CONFIG_FILE > $TENSORFLOW_SERVER_LOGS_FILE 2>&1" ], stdout=subprocess.DEVNULL, shell=True, preexec_fn=os.setsid) try: model_version = int(connexion.request.form["model_version"]) except: model_version = None pass model_name = connexion.request.form["model_name"] append = connexion.request.form["append"] model_download_path = os.environ['MODEL_SAVE_PATH'] + "/" + model_name # Model Versioning Management # if model_version was not specified, it will be set by default as "the latest model_version number + 1" if model_version is None: if not os.path.exists(model_download_path): deployment_status = "[ERROR] There is no model stored with this name" + model_name + ". please choose one of the saved models" else: listOfFile = os.listdir(model_download_path) model_versions = [] for file in listOfFile: file_casted = file try: file_casted = int(file) model_versions.append(file_casted) except: pass # check if the model directory is empty or not if not model_versions: model_version = 1 else: model_versions.sort() model_version = model_versions[-1] version_path = model_download_path + "/" + str( model_version) log( "[INFO] the version that will be redeployed is : " + str(model_version), api_token) else: version_path = model_download_path + "/" + str(model_version) if not os.path.isdir(version_path): deployment_status = "[ERROR] This model version path doesn't exist: " + version_path + ". Please choose an already uploaded model version." return log(deployment_status, api_token) # Model Deployment log( "[INFO] Redeploying the version " + str(model_version) + " of " + model_name, api_token) if append == "true": deployment_status = "[INFO] " + utils.append_version_model_service_config( model_name, version_path, model_version) else: deployment_status = "[INFO] " + utils.add_version_model_service_config( model_name, version_path, model_version) return log(deployment_status, api_token) else: return log("[ERROR] Invalid token ", api_token)
def deploy_api(model_name, model_file) -> str: model_zip_path = os.environ['MODEL_SAVE_PATH'] + str(uuid.uuid4()) + ".zip" api_token = connexion.request.form["api_token"] model_name = connexion.request.form["model_name"] append = connexion.request.form["append"] model_file = connexion.request.files["model_file"] model_download_path = os.environ['MODEL_SAVE_PATH'] + "/" + model_name download_model = True # if model_version is empty, model_version will be set on None try: model_version = int(connexion.request.form["model_version"]) except: model_version = None pass log("[INFO] Calling deploy_api", api_token) if auth_token(api_token): # Service Status Management tensorflow_model_server_down = True for proc in psutil.process_iter(): pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time']) if pinfo['name'] == "tensorflow_model_server": log("[INFO] TensorFlow model server is already up", api_token) tensorflow_model_server_down = False if tensorflow_model_server_down: log("[INFO] Starting a new tensorflow_model_server", api_token) tf_server = subprocess.Popen([ "tensorflow_model_server " "--rest_api_port=$REST_API_PORT " "--model_config_file_poll_wait_seconds=$CONFIG_FILE_POLL_SECONDS " "--model_config_file=$MODEL_CONFIG_FILE > $TENSORFLOW_SERVER_LOGS_FILE 2>&1" ], stdout=subprocess.DEVNULL, shell=True, preexec_fn=os.setsid) # Model Versioning Management # if model_version was not specified, it will be set by default as "the latest model_version number + 1" if model_version is None: if not os.path.exists(model_download_path): os.makedirs(model_download_path) model_version = 1 else: listOfFile = os.listdir(model_download_path) model_versions = [] for file in listOfFile: file_casted = file try: file_casted = int(file) model_versions.append(file_casted) except: pass # check if the model directory is empty or not if not model_versions: model_version = 1 else: model_versions.sort() model_version = model_versions[-1] + 1 log("[INFO] new version to be deployed : " + str(model_version), api_token) else: version_path = model_download_path + "/" + str(model_version) if os.path.isdir(version_path): download_model = False log( "[WARN] This model version already exists. \ The uploaded model version will be ignored. The existing version will be deployed.", api_token) # Model Downloading # if the specified model version doesn't exist in the directory, # the zip file uploaded by the user will be downloaded if download_model: version_path = model_download_path + "/" + str(model_version) log("[INFO] Downloading the new model in " + str(version_path), api_token) model_file.save(model_zip_path) with zipfile.ZipFile(model_zip_path, "r") as zip_ref: zip_ref.extractall(version_path) os.remove(model_zip_path) # Model Deployment log( "[INFO] Deploying the version " + str(model_version) + " of " + model_name, api_token) if append == "true": deployment_status = utils.append_version_model_service_config( model_name, model_download_path, model_version) else: deployment_status = utils.add_version_model_service_config( model_name, model_download_path, model_version) # print("The new tensorflow model file was deployed successfully at: ",os.environ['MODELS_PATH'], model_version) return log("[INFO]" + deployment_status, api_token) else: return log("[INFO] Invalid token", api_token)