def get_web_service_uri(aml_interface): service = Webservice(name=DEPLOYMENT_SERVICE_NAME, workspace=aml_interface.workspace) return service.scoring_uri
runtime='python', entry_script='score.py', conda_file='env.yml') # Deploy model aci_config = AciWebservice.deploy_configuration( cpu_cores=2, memory_gb=4, tags={ 'model': 'RESNET', 'method': 'pytorch' }, description='CIFAR-Type object classifier') try: service = Webservice(workspace, name=service_name) if service: service.delete() except WebserviceException as e: print() service = Model.deploy(workspace, service_name, [model], inference_config, aci_config) service.wait_for_deployment(True) else: service = Webservice(workspace, name=service_name) # Output scoring url print(service.scoring_uri) with open(scoring_url, 'w+') as f: f.write(service.scoring_uri)
def main(): e = Env() # Get Azure machine learning workspace ws = Workspace.get(name=e.workspace_name, subscription_id=e.subscription_id, resource_group=e.resource_group) print(f"get_workspace: {ws}") # Parameters sources_directory_train = e.sources_directory_train # model_names = ["nyc_energy_model", "diabetes_model"] model_names = get_model_names( os.path.join(sources_directory_train, "pipeline_config.json")) models = [] for model_name in model_names: models.append(Model(ws, name=model_name)) # Conda environment myenv = Environment.from_conda_specification( "myenv", os.path.join(sources_directory_train, "conda_dependencies.yml")) # Enable Docker based environment myenv.docker.enabled = True # Deprecated: pass the model names string to score.py # score.py reads model names from pipeline_config.json directly. # model_names_str = '' # for name in model_names: # model_names_str = model_names_str + name + ',' # model_names_str = model_names_str[:-1] # myenv.environment_variables = {"MODEL_NAMES": model_names_str} inference_config = InferenceConfig( source_directory=sources_directory_train, entry_script="scoring/score.py", environment=myenv) deployment_config = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=2, tags={ 'area': "digits", 'type': aci_service_name }, description=aci_service_name) try: # Check if the service is existed service = Webservice(ws, name=aci_service_name) if service: print("Found existing service: %s .. delete it" % aci_service_name) service.delete() except WebserviceException as e: print(e) service = Model.deploy(ws, aci_service_name, models, inference_config, deployment_config) service.wait_for_deployment(True) print(service.state)
# COMMAND ---------- # MAGIC %md ### Deploy to the model's image to the specified AKS cluster # COMMAND ---------- from azureml.core.webservice import Webservice, AksWebservice from azureml.core.image import Image # Get Model model_image = Image(workspace, id=model_image_id) # Get Webservice prod_webservice_name = "wine-quality-aks" try: prod_webservice = Webservice(workspace, prod_webservice_name) print('updating existing webservice.') prod_webservice.update(image=model_image) prod_webservice.wait_for_deployment(show_output=True) except: print('creating new webservice.') # Set configuration and service name prod_webservice_deployment_config = AksWebservice.deploy_configuration() # Deploy from image prod_webservice = Webservice.deploy_from_image( workspace=workspace, name=prod_webservice_name, image=model_image, deployment_config=prod_webservice_deployment_config, deployment_target=aks_target) # Wait for the deployment to complete
inference_config=inference_config, deployment_config=deployment_config) service.wait_for_deployment(show_output=True) print(service.state) print(service.scoring_uri) print(service.get_logs()) else: print(" ") print("**************************************") print("Existing Service, updating the service") print(" ") print("**************************************") service = Webservice(name=DEPLOYMENT_SERVICE_NAME, workspace=ws) service.update(models=[model], inference_config=inference_config) print(service.state) print(service.scoring_uri) print(service.get_logs()) # AKS deployment **************************************************************************************** from azureml.core.webservice import AksWebservice, Webservice from azureml.core.webservice import AksEndpoint from azureml.core.compute import AksCompute from azureml.core.compute import ComputeTarget print("AKS deployement ") AKS_DELOYMENT_SERVICE_NAME = "aks-dgraphics1" AML_AKS_COMPUTE_NAME = "aks-amlcompute" aks_target = ComputeTarget(ws, AML_AKS_COMPUTE_NAME)
resource_group=resource_group, auth=cli_auth) # Get workspace #ws = Workspace.from_config(auth=cli_auth) # Get the ACI Details try: with open("./configuration/aci_webservice.json") as f: config = json.load(f) except: print("No new model, thus no deployment on ACI") # raise Exception('No new model to register as production model perform better') sys.exit(0) service_name = config["aci_name"] # Get the hosted web service service = Webservice(name=service_name, workspace=ws) # Input for Model with all features step_size = [3] test_sample = json.dumps({"data": step_size}) test_sample = bytes(test_sample, encoding="utf8") print(step_size) try: prediction = service.run(input_data=test_sample) print(prediction) except Exception as e: result = str(e) print(result) raise Exception("ACI service is not working as expected")
from azureml.core import Workspace from azureml.core.webservice import Webservice ws = Workspace.from_config() aci_service = Webservice(ws, "bank-mark-vote-ensemble-model") aci_service.update(enable_app_insights=True)
# MAGIC %md if you receive an error deploying your image as a webservice, you can use the following code to get information from the image creation logs # COMMAND ---------- # MAGIC %md # MAGIC # MAGIC ## Connect to an existing Web Service in Azure ML # COMMAND ---------- from azureml.core.webservice import Webservice webservice_name = "<webservice_name>" #connect to an existing web service webservice = Webservice(name=webservice_name, workspace=workspace) # COMMAND ---------- # MAGIC %md # MAGIC With the webservice succesfully deployed, a test call to the API can be executed. # MAGIC # MAGIC Further integration with web and mobile apps is now possible using standard web technolgies. Because the model is hosted in Azure Kubernetes Services, scale is not an issue. The AKS cluster will automatically react to increased or decreased demand. # COMMAND ---------- import requests import json #this function calls an Azure ML hosted webservice and returns the predicted results
print('..7.completed') print('') print('') print('8. Check for and delete any existing web service instance') aksName = args.aks_name aksRegion = args.aks_region aksServiceName = args.service_name print('aksName=', aksName) print('aksRegion=', aksRegion) print('aksServiceName=', aksServiceName) try: mlRestService = Webservice(name=aksServiceName, workspace=amlWs) print(".... Deleting AKS service {}".format(aksServiceName)) mlRestService.delete() except: print(".... No existing webservice found: ", aksServiceName) print('..8.completed') print('') print('') print('9. AKS inference cluster creation') computeList = amlWs.compute_targets aksTarget = None if aksName in computeList: aksTarget = computeList[aksName]
return img_str.decode('utf-8') # Define arguments parser = argparse.ArgumentParser(description='Test script parser') parser.add_argument( '--image_url', type=str, help='URL of the image to score', default= 'https://compote.slate.com/images/222e0b84-f164-4fb1-90e7-d20bc27acd8c.jpg' ) image_url = parser.parse_args().image_url # get scoring url aci_service_name = 'object-recognition-service' workspace = Workspace.from_config() aci_service = Webservice(workspace, name=aci_service_name) scoring_url = aci_service.scoring_uri # Download image and convert to base 64 with urllib.request.urlopen(image_url) as url: test_img = io.BytesIO(url.read()) base64Img = imgToBase64(Image.open(test_img)) # Get prediciton through endpoint input_data = '{\"data\": \"' + base64Img + '\"}' headers = {'Content-Type': 'application/json'} response = requests.post(scoring_url, input_data, headers=headers) print(json.loads(response.text))
image.wait_for_creation(show_output=True) ## ---------------------------- If you have free Azure credit (Start) ------------------------------- ## This section, we deploy the image as a WebService on Azure Container Instance. This is light way to host the image ## if you have free credit or you don't want to host your model on Kubernetes cluster aciconfig = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags={ 'area': "MNIST", 'type': "classification" }, description='Predict digits from MNIST dataset') try: aci_service = Webservice(name=aks_service_name, workspace=ws) print('Found the webservice, deleting the service to add a new one') aci_service.delete() print('Old webservice is deleted') except Exception: print("This webservice doesn't exist") finally: print('Deploying the new web service') aci_service = Webservice.deploy_from_image(deployment_config=aciconfig, image=image, name=aks_service_name, workspace=ws) aci_service.wait_for_deployment(show_output=True) print('This webservice is deployed')
model_image = Image(aksml_workspace, id=model_image_id) # Get Webservice prod_webservice_name = "drinks-quality-aks" # COMMAND ---------- from azureml.core.webservice import Webservice, AksWebservice from azureml.core.image import Image # Get Model model_image = Image(aksml_workspace, id=model_image_id) # Get Webservice prod_webservice_name = "drinks-quality-aks" try: prod_webservice = Webservice(aksml_workspace, prod_webservice_name) print('updating existing webservice.') prod_webservice.update(image=model_image) prod_webservice.wait_for_deployment(show_output=True) except: print('creating new webservice.') # Set configuration and service name prod_webservice_deployment_config = AksWebservice.deploy_configuration() # Deploy from image prod_webservice = Webservice.deploy_from_image( workspace=aksml_workspace, name=prod_webservice_name, image=model_image, deployment_config=prod_webservice_deployment_config, deployment_target=aks_target) # Wait for the deployment to complete
workspace="<Name of your workspace>" subscription_id="<Subscription id>" resource_grp="<Name of your resource group where aml service is created>" experiment_name='<Name of your experiment you defined in dataprep.py>' print("Starting trigger engine") # Start creating # Point file to conf directory containing details for the aml service spn = ServicePrincipalAuthentication(tenant_id,app_id,app_key) ws = Workspace(auth = spn, workspace_name = workspace, subscription_id = subscription_id, resource_group = resource_grp) print(ws.name, ws._workspace_name, ws.resource_group, ws.location, sep = '\t') service= Webservice(ws,'sklearn-mnist-svc') #Function to load and parse images def load_data(filename, label=False): with gzip.open(filename) as gz: struct.unpack('I', gz.read(4)) n_items = struct.unpack('>I', gz.read(4)) if not label: n_rows = struct.unpack('>I', gz.read(4))[0] n_cols = struct.unpack('>I', gz.read(4))[0] res = np.frombuffer(gz.read(n_items[0] * n_rows * n_cols), dtype=np.uint8) res = res.reshape(n_items[0], n_rows * n_cols) else: res = np.frombuffer(gz.read(n_items[0]), dtype=np.uint8)
#from azureml.core.model import Model #from azureml.core.webservice import AciWebservice import os #import matplotlib import matplotlib.pyplot as plt # find 30 random samples from test set n = 30 myws = ws.get( name='mnist1', subscription_id='a383ccde-05bc-45bf-b3ae-f51bd72644df', resource_group='mnist4', ) #model = Model(myws, 'sklearn-mnist') #web = 'https://mlworkspace.azure.ai/portal/subscriptions/bcbc4e01-e5d6-42b0-95af-06286341e6ca/resourceGroups/mnist3/providers/Microsoft.MachineLearningServices/workspaces/mnist1/deployments/mnist' print(Webservice.list(myws)[0].name) service = Webservice(myws, 'sklearn-mnist-image') #print(type(model)) data_folder = os.path.join(os.getcwd(), 'data') X_test = load_data(os.path.join(data_folder, 'test-images.gz'), False) / 255.0 y_test = load_data(os.path.join(data_folder, 'test-labels.gz'), True).reshape(-1) sample_indices = np.random.permutation(X_test.shape[0])[0:n] test_samples = json.dumps({"data": X_test[sample_indices].tolist()}) test_samples = bytes(test_samples, encoding='utf8') #result = service.run(input_data=test_samples) # predict using the deployed model result = service.run(input_data=test_samples) # compare actual value vs. the predicted values: i = 0
def deploy(self, workspace_name, resource_group, subscription_id, model_name, service_name, inference_config_file, deployment_config_file, signing_certificate, overwrite, verbose): ws = Workspace.get(workspace_name, auth=self.auth, resource_group=resource_group, subscription_id=subscription_id) inference_config = file_to_inference_config(ws, inference_config_file, description='') # Package the model so we have an immutable image url (using hash) to validate later models = Model.list(ws, model_name, latest=True) if len(models) == 0: raise OMLException("Model is not registered") if verbose: print("Packaging model.") model_package = Model.package(ws, [models[0].id], inference_config) model_package.wait_for_creation() image_uri = model_package.location if verbose: print("Model package generated at {}.".format(image_uri)) # Generate a signature containing the immutable model image url if verbose: print("Generating deployment signature.") signature_payload = "imageUrl:{}".format(image_uri) signature_bytes = rsa.sign(signature_payload.encode('utf-8'), signing_certificate, 'SHA-384') signature = base64.b64encode(signature_bytes).decode('utf-8') # Set up base-image-only deploy configuration if verbose: print("Creating byoc inference config and deployment config.") # "AzureML-" is a reserved environment name prefix. Uniquify the environment name if using # a curated environment for this model. if inference_config.environment.name.startswith("AzureML-"): deploy_environment_name = "Office-{}-deploy".format(inference_config.environment.name[8:]) else: deploy_environment_name = "{}-deploy".format(inference_config.environment.name) byoc_config = InferenceConfig(entry_script=inference_config.entry_script, environment=Environment(deploy_environment_name)) byoc_config.environment.docker.base_image = image_uri byoc_config.environment.python.user_managed_dependencies = True properties = { "isByoc": "true", "requestSignaturePayload": signature_payload, "requestSignature": signature } deployment_config = file_to_deploy_config(deployment_config_file, properties) # MirWebservice doesn't support service updates. # Make sure you remove any existing service under the same name before calling deploy. if overwrite: if verbose: print("Deleting webservice if it exists.") try: Webservice(ws, service_name).delete() if verbose: print("Previous webservice deleted.") except WebserviceException: pass if verbose: print("Deploying model.") service = Model.deploy(ws, service_name, models, byoc_config, deployment_config) try: service.wait_for_deployment() except WebserviceException: print('Failed service operation id: {}'.format(Webservice._get(ws, service_name)['operationId'])) raise print("ScoringUri:{}\n".format(service.scoring_uri)) return service
]) #showing how to add libs as an eg. - not needed for this model. with open("mydeployenv.yml", "w") as f: f.write(myacienv.serialize_to_string()) # COMMAND ---------- from azureml.core.webservice import AciWebservice, Webservice from azureml.exceptions import WebserviceException from azureml.core.model import InferenceConfig from azureml.core.environment import Environment from azureml.core.conda_dependencies import CondaDependencies # Remove any existing service under the same name. try: Webservice(ws, webservice_name).delete() except WebserviceException: pass env = Environment.get(ws, name='AzureML-PySpark-MmlSpark-0.15') inference_config = InferenceConfig(entry_script="score_sparkml.py", environment=env) deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, auth_enabled=True) #image_config = ContainerImage.image_configuration(execution_script = "score_sparkml.py", # runtime = "spark-py", # conda_file = "mydeployenv.yml")
if variables.get("MODEL_DESCRIPTION") is not None: MODEL_DESCRIPTION = variables.get("MODEL_DESCRIPTION") if variables.get("SERVICE_NAME") is not None: SERVICE_NAME = variables.get("SERVICE_NAME") if variables.get("EXECUTION_SCRIPT_URL") is not None: EXECUTION_SCRIPT_URL = variables.get("EXECUTION_SCRIPT_URL") wget.download(EXECUTION_SCRIPT_URL,EXECUTION_SCRIPT_PATH) if variables.get("CONDA_FILE_URL") is not None: CONDA_FILE_URL = variables.get("CONDA_FILE_URL") #Set the interactive authentification interactive_auth = InteractiveLoginAuthentication() #Set the interactive authentification ws = Workspace.get(name=AZURE_WORKSPACE_NAME, auth=interactive_auth, subscription_id=AZURE_SUBSCRIPTION_ID,resource_group=AZURE_RESOURCE_GROUP) service = Webservice(workspace=ws, name=SERVICE_NAME) #Get model MODEL_PATH = os.path.join(os.getcwd(),MODEL_PATH) wget.download(MODEL_URL,MODEL_PATH) #Register a new model new_model = Model.register(model_path = MODEL_PATH, model_name = MODEL_NAME, description = MODEL_DESCRIPTION, workspace = ws) #Create a new image CONDA_FILE_PATH = os.path.join(os.getcwd(),CONDA_FILE_PATH) wget.download(CONDA_FILE_URL,CONDA_FILE_PATH) inference_config = InferenceConfig(entry_script=EXECUTION_SCRIPT_PATH, runtime="python", conda_file=CONDA_FILE_PATH)
# Prepare input for forecasting time_column_name = 'dtime' freq = granularity[0].upper() X_test = pd.date_range( start=from_datetime, periods=horizon, freq=freq).to_frame(index=False).rename(columns={0: time_column_name}) y_test = np.full(len(X_test), np.nan, dtype=np.float) test_sample = json.dumps({ 'X': X_test.to_json(date_format='iso'), 'y': y_test.tolist() }) print('input json:{}'.format(test_sample)) # Find the web service service = Webservice(ws, model_name) # Call the web service response = service.run(test_sample) print('output json:{}'.format(response)) # Parse results res_dict = json.loads(response) y_fcst_all = pd.read_json(res_dict['index']) y_fcst_all[time_column_name] = pd.to_datetime(y_fcst_all[time_column_name], unit='ms') y_fcst_all['forecast'] = res_dict['forecast'] y_fcst_all.index = y_fcst_all[time_column_name] y_fcst_all.drop(time_column_name, axis=1, inplace=True) y_fcst_all.sort_index(inplace=True) print(y_fcst_all)
# Note that the service creation can take few minutes. # In[ ]: from azureml.core.webservice import AciWebservice, Webservice from azureml.exceptions import WebserviceException deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1) aci_service_name = 'aciservice1' try: # if you want to get existing service below is the command # since aci name needs to be unique in subscription deleting existing aci if any # we use aci_service_name to create azure aci service = Webservice(ws, name=aci_service_name) if service: service.delete() except WebserviceException as e: print() service = Model.deploy(ws, aci_service_name, [model], inference_config, deployment_config) service.wait_for_deployment(True) print(service.state) # #### Test web service # In[ ]:
conda_dep = CondaDependencies("turbofan.yml") #conda_dep.add_conda_package("numpy") #conda_dep.add_conda_package("scikit-learn") # You must list azureml-defaults as a pip dependency #conda_dep.add_pip_package("azureml-defaults") myenv.python.conda_dependencies = conda_dep inference_config = InferenceConfig(entry_script="score.py", environment=myenv) #image_config = ContainerImage.image_configuration(execution_script = "score.py", # runtime = "python", #conda_file = "turbofan.yml") try: service = Webservice(ws, webservice_name) service.delete() except Exception as e: print("No Existing Service") service = Model.deploy(ws, webservice_name, [model], inference_config, deployment_config) #service.get_logs() #service = Webservice.deploy_from_model(workspace=ws, # name = webservice_name, # deployment_config = aci_config, #models = [model], #image_config = image_config) service.wait_for_deployment(show_output=True)
# Create Container Instance from azureml.core.webservice import AciWebservice aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={ "data": "fdc", "method": WhatModel }, description='fdc') from azureml.core.webservice import Webservice aci_service_name = 'fdc-oneclass-prediction1' print(aci_service_name) aci_service = Webservice.deploy_from_image(deployment_config=aciconfig, image=image, name=aci_service_name, workspace=ws) aci_service.wait_for_deployment(True) print(aci_service.state) # retrive service from azureml.core.webservice import Webservice aci_service = Webservice(ws, aci_service_name) print(aci_service.scoring_uri) print('model created complete...')
workspace_name=workspace, subscription_id=subscription_id, resource_group=resource_grp) image_name = config["image_name"] image_version = config["image_version"] images = Image.list(workspace=ws) image, = (m for m in images if m.version == image_version and m.name == image_name) print( 'From image.json, Image used to deploy webservice on ACI: {}\nImage Version: {}\nImage Location = {}' .format(image.name, image.version, image.image_location)) #delete old service first before new one try: oldservice = Webservice(workspace=ws, name=service_name) print("delete " + service_name + " before creating new one") oldservice.delete() except: print(service_name + " does not exist, create new one") aciconfig = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags={ "data": "Income", "method": "SparkML" }, description='Predict Income with sparkML') service = Webservice.deploy_from_image(deployment_config=aciconfig,
def deploy(ws_name,model_name,path_to_model, environment_name,register_environment,pip_packages,conda_packages, cpu_cores , memory_gb, path_to_entry_script,service_name): ''' Get Workspace ''' ws = Workspace.from_config() print("Got Workspace {}".format(ws_name)) ''' Register Model ''' model = Model.register(workspace = ws, model_path =path_to_model, model_name = model_name, ) print("Registered Model {}".format(model_name)) ''' Register Environment ''' # to install required packages if register_environment: env = Environment(environment_name) cd = CondaDependencies.create(pip_packages=pip_packages, conda_packages = conda_packages) env.python.conda_dependencies = cd # Register environment to re-use later env.register(workspace = ws) print("Registered Environment") myenv = Environment.get(workspace=ws, name=environment_name) # Uncomment to save environment # myenv.save_to_directory('./environ', overwrite=True) ''' Config Objects ''' aciconfig = AciWebservice.deploy_configuration( cpu_cores=cpu_cores, memory_gb=memory_gb, ) inference_config = InferenceConfig(entry_script=path_to_entry_script, environment=myenv) ''' Deploying ''' print("Deploying....... This may take a few mins, check the status in MLS after the function finishes executing") service = Model.deploy(workspace=ws, name=ws_name, models=[model], inference_config=inference_config, deployment_config=aciconfig, overwrite = True) service.wait_for_deployment(show_output=True) url = service.scoring_uri print(url) service = Webservice(ws,ws_name) print(service.get_logs()) return url