示例#1
0
def teardown_service(subscription_id, resource_group, workspace_name, workspace_region):

    yield

    # connect to workspace
    ws = azureml_utils.get_or_create_workspace(
        subscription_id=subscription_id,
        resource_group=resource_group,
        workspace_name=workspace_name,
        workspace_region=workspace_region,
    )

    # connect to aci_service
    aci_service = Webservice(workspace=ws, name="aci-test-service")

    # delete aci_service
    aci_service.delete()
print('get workspace...')
ws = Workspace.from_config(auth=cli_auth)
print('done getting workspace!')

image = Image(ws, id=image_id)
print(image)

aks_name = args.aks_name
aks_region = args.aks_region
aks_service_name = args.service_name

try:
    service = Webservice(name=aks_service_name, workspace=ws)
    print("Deleting AKS service {}".format(aks_service_name))
    service.delete()
except:
    print("No existing webservice found: ", aks_service_name)

compute_list = ws.compute_targets
aks_target = None
if aks_name in compute_list:
    aks_target = compute_list[aks_name]

if aks_target == None:
    print("No AKS found. Creating new Aks: {} and AKS Webservice: {}".format(
        aks_name, aks_service_name))
    prov_config = AksCompute.provisioning_configuration(location=aks_region)
    # Create the cluster
    aks_target = ComputeTarget.create(workspace=ws,
                                      name=aks_name,
               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,
                                       image=image,
                                       name=service_name,
                                                       autoscale_min_replicas=1, 
                                                       autoscale_max_replicas=2, 
                                                       autoscale_refresh_seconds=10, 
                                                       autoscale_target_utilization=70,
                                                       auth_enabled=True, 
                                                       cpu_cores=2, memory_gb=1, 
                                                       scoring_timeout_ms=5000, 
                                                       replica_max_concurrent_requests=2, 
                                                       max_request_wait_time=5000, 
                                                       enable_app_insights=True,
                                                       collect_model_data=True)

try:
    aks_service = Webservice(name = aks_service_name, workspace = ws)
    print('Found the webservice, deleting the service to add a new one')
    aks_service.delete()
    print('Old webservice is deleted')
except Exception:
    print("This webservice doesn't exist")
finally:
    print('Deploying the new web service')
    # aks_service = Webservice.deploy_from_image(workspace = ws, 
    #                                        name = aks_service_name,
    #                                        image = image,
    #                                        deployment_config = aks_config,
    #                                        deployment_target = aks_target)
    aks_service = Model.deploy(workspace=ws,
                           name=aks_service_name,
                           models = [model_root],
                           inference_config=inf_config,
                           deployment_config=aks_config,
示例#5
0
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]

if aksTarget == None:
示例#6
0
## ---------------------------- 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')

## ---------------------------- If you have free Azure credit (End) -------------------------------
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)