示例#1
0
def deleteWebService(ws, args):
    services = Webservice.list(workspace=ws, model_name=args.modelName)
    if (len(services) == 0):
        print("Webservice is not deployed.")
    else:
        print("Webservice is deployed")
        services[0].delete()
        print("Deleted webservice")
示例#2
0
文件: utils.py 项目: grecoe/AMLSSDK
def getWebservice(workspace, webserviceName):
    webservice = None
    services = Webservice.list(workspace)
    for svc in services:
        if svc.name == webserviceName:
            webservice = svc
            break
    return webservice
示例#3
0
def createWebservice(workspace, container_image, service_name, replica_count,
                     cores_count, compute_target):
    '''
        TODO: Should allow for the overwrite flag. 

        Attach a azureml.core.webservice.Webservice for a given container on an AKS cluster. 

        If a WebService already exists (by name) on the given workspace, return it instead. 


        PARAMS: 
            workspace        : azureml.core.Workspace               : Existing AMLS Workspace
            container_image  : azureml.core.image.ContainerImage    : Name of an existing AKS cluster 
            service_name     : String                               : Name of the webservice (deployment) in the AMLS workpsace.
            replica_count    : int                                  : Number of requested instances of container on cluster.
            cores_count      : int                                  : Number of cores to allocate to each container
            compute_target   : azureml.core.compute.AksCompute      : AKS cluster to create the service on

        RETURNS: 
            azureml.core.webservice.Webservice

    '''
    web_service = None

    services = Webservice.list(workspace=workspace,
                               image_name=container_image.name)
    if len(services) > 0:
        for svc in services:
            if svc.name == service_name:
                print("Returning existing deployed web service ....",
                      service_name)
                web_service = svc
                break

    if web_service == None:
        print("Creating new web service.....", service_name)
        aks_config = AksWebservice.deploy_configuration(
            num_replicas=replica_count, cpu_cores=cores_count)

        web_service = Webservice.deploy_from_image(
            workspace=workspace,
            name=service_name,
            image=container_image,
            deployment_config=aks_config,
            deployment_target=compute_target,
        )

        web_service.wait_for_deployment(show_output=True)

    return web_service
示例#4
0
                                                _workspace=ws)

print('Latest model id: ', latest_model_id)
print('Latest model name: ', latest_model_name)
print('Latest model version: ', latest_model_version)
print('Latest model path: ', latest_model_path)

latest_model_run_id = latest_model.tags.get("run_id")
print('Latest model run id: ', latest_model_run_id)

latest_model_run = Run(run.experiment, run_id=latest_model_run_id)

latest_model_accuracy = latest_model_run.get_metrics().get("acc")
print('Latest model accuracy: ', latest_model_accuracy)

ws_list = Webservice.list(ws, model_name=latest_model_name)
print('webservice list')
print(ws_list)

deploy_model = False
current_model = None

if (len(ws_list) > 0):
    webservice = ws_list[0]
    try:
        image_id = webservice.tags['image_id']
        image = Image(ws, id=image_id)
        current_model = image.models[0]
        print('Found current deployed model!')
    except:
        deploy_model = True
示例#5
0
ws = getOrCreateWorkspace(subscription_id, resource_group, workspace_name,
                          workspace_region)

# In[ ]:

from azureml.core.webservice import Webservice

webservice_name = "complianceservice-zst"

# It is important to change the current working directory so that your generated scoring-service.py is at the root
# This is required by the Azure Machine Learning SDK
os.chdir(deployment_folder)

webservice = None
for service in Webservice.list(ws):
    if (service.name == webservice_name):
        webservice = service
        print(webservice.name, webservice)

if webservice == None:
    print(os.getcwd())
    webservice = deployModelAsWebService(ws,
                                         model_folder_path=onnx_export_folder,
                                         model_name="component_compliance",
                                         service_name=webservice_name)

# Change the directory back...
os.chdir('../')

# Test your deployed web service.
train_filepath = os.path.join(args.input, 'train_info.json')
with open(train_filepath) as f:
    train_info = json.load(f)

print("train_info.json")
print(train_info)
#pipeline_run = PipelineRun(run.experiment, run_id = run.properties['azureml.pipelinerunid'])
#latest_model_run = Run(run.experiment, run_id = pipeline_run.find_step_run('train')[0].id)
latest_model_run_id = train_info['train_run_id']
#latest_model_run = Run(run.experiment, run_id=latest_model_run_id)
print('Latest model run id: ', latest_model_run_id)
#latest_model_accuracy = latest_model_run.get_metrics().get("acc")
latest_model_accuracy = float(train_info['acc'])
print('Latest model accuracy: ', latest_model_accuracy)

ws_list = Webservice.list(ws, model_name = args.model_name)
print('webservice list')
print(ws_list)

deploy_model = False
current_model_run_id = None
current_model_accuracy = -1 # undefined

if(len(ws_list) > 0):
    webservice = None
    for i in range(len(ws_list)):
        if ws_list[i].compute_type != 'ACI':
            webservice = ws_list[i]
    if webservice != None:
        #current_model_run_id = webservice.tags.get("run_id")
        current_model_run_id = webservice.models[0].tags['run_id']
示例#7
0
    containerizeStepLogInfo["deploy_model_bool"] = deployModelBool
    containerizeStepLogInfo["image_name"] = args.image_name
    containerizeStepLogInfo["image_id"] = ""

else:
    print('..No freshly trained model found!  This should not have happened!')

print('..3. completed')
print('')
print('')

print(
    '4.  Determine if we have a REST service deployed for the experiment previously'
)
print('.............................................')
restServiceList = Webservice.list(amlWs, model_name=currentlyTrainedModelName)
if (len(restServiceList) > 0):
    previouslyDeployedRestServiceFound = True
    previouslyDeployedRestService = restServiceList[0]
    print('List of existing REST services for the experiment-')
    print(restServiceList)
else:
    print('No existing REST service instances found for the experiment')
    previouslyDeployedRestServiceFound = False

print('..4. completed')
print('')
print('')

print(
    '5.  Get the previously deployed model from previously deployed REST service, if any'
示例#8
0
def get_service():
    ws = get_workspace()
    services = Webservice.list(workspace=ws, compute_type='AKS')
    return services[0]
示例#9
0
from azureml.core import Workspace as ws
#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:
示例#10
0
print("Argument 1: %s" % args.service_name)
print("Argument 2: %s" % args.aci_name)
print("Argument 3: %s" % args.description)

print('creating AzureCliAuthentication...')
cli_auth = AzureCliAuthentication()
print('done creating AzureCliAuthentication!')

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

image = Image(ws, image_name)
print(image)

ws_list = Webservice.list(ws, image_name=image_name)
print(ws_list)

if len(ws_list) > 0:
    if ws_list[0].name == args.service_name:
        print('Deleting: ', ws_list[0].name)
        ws_list[0].delete()
        print('Done')

aci_config = AciWebservice.deploy_configuration(cpu_cores=1,
                                                memory_gb=1,
                                                tags={'name': args.aci_name},
                                                description=args.description)

aci_service = Webservice.deploy_from_image(deployment_config=aci_config,
                                           image=image,
json_direct = aks_service.run(sc_validate.to_json())
fcdf_direct = ForecastDataFrame.construct_from_json(json_direct)
fcdf_direct.head()

# ### Delete AKS service and resiurce group
#
# This part of a notebook is oprtional and intended to clean up after work is complete. First delete the service.

# In[ ]:

aks_service.delete()

# Check if services are present in the workspace.

# In[ ]:

[svc.name for svc in Webservice.list(ws)]

# Delete the resource group.<br/>
# **Note** This operation is danger and will delete all the content of the resource group.
# To delete group the azure sdk package needs to be installed:
# ```
# pip install https://azuremlftkrelease.blob.core.windows.net/azpkgdaily/azpkgamlsdk-1.0.18309.1b1-py3-none-any.whl
# ```

# In[ ]:

from azpkgamlsdk.deployment.utils_environment import delete_resource_group

delete_resource_group(ws.resource_group, ws.subscription_id)
示例#12
0
    def getOperationOutput(self,
                           operationNoun,
                           operationId,
                           userId,
                           subscriptionId,
                           downloadFiles=True):
        operationName = self.GetOperationNameByNoun(operationNoun)

        if operationName == 'train':

            tags = [['userId', userId], ['modelId', operationId],
                    ['subscriptionId', subscriptionId]]
            models = Model.list(self._workspace, tags=tags)
            if len(models) == 0:
                return None
            model = models[0]
            result = {
                'id': operationId,
                'description': model.description,
                'created_time': model.created_time
            }
            return result, "model"

        if operationName == 'deploy':

            tags = [['userId', userId], ['endpointId', operationId],
                    ['subscriptionId', subscriptionId]]
            endpoints = Webservice.list(self._workspace, tags=tags)
            if len(endpoints) == 0:
                return None, None
            endpoint = endpoints[0]
            primaryKey, secondaryKey = endpoint.get_keys()
            result = {
                'id': operationId,
                'description': endpoint.description,
                'created_time': endpoint.created_time,
                'scoring_uri': endpoint.scoring_uri,
                'primary_key': primaryKey,
                'secondary_key': secondaryKey
            }

            return result, "endpoint"

        tags = {
            'userId': userId,
            'operationId': operationId,
            'operationName': operationName,
            'subscriptionId': subscriptionId
        }

        experimentName = subscriptionId
        exp = Experiment(self._workspace, experimentName)
        runs = exp.get_runs(type='azureml.PipelineRun', tags=tags)
        try:
            run = next(runs)
            child_runs = run.get_children()
            child_run = next(child_runs)
            outputType = self._utils.GetOutputType(operationName)
            if outputType == 'json':
                with tempfile.TemporaryDirectory() as tmp:
                    path = os.path.join(tmp, 'output.json')
                    files = child_run.download_file('/outputs/output.json',
                                                    path)
                    with open(path) as file:
                        return json.load(file), "json"
            elif outputType == 'file':
                if downloadFiles:
                    tmp = tempfile.TemporaryDirectory().name
                    path = os.path.join(tmp, "outputs")
                    zip_file_path = os.path.join(
                        tmp, "output_{}.zip".format(operationId))
                    files = child_run.download_files("/outputs",
                                                     path,
                                                     append_prefix=False)
                    zipf = zipfile.ZipFile(zip_file_path, "w",
                                           zipfile.ZIP_DEFLATED)
                    self.zipdir(path, zipf, "outputs")
                    zipf.close()
                    return zip_file_path, "file"
                else:
                    return "file", "file"
        except StopIteration:
            return None
示例#13
0
import azureml
from azureml.core import Workspace

# display the core SDK version number
print("Azure ML SDK Version: ", azureml.core.VERSION)

ws = Workspace.from_config()
print("Resource group: ", ws.resource_group)
print("Location: ", ws.location)
print("Workspace name: ", ws.name)

from azureml.core.webservice import Webservice

for web_svc in Webservice.list(ws):
    print("Deleting web service", web_svc.name, "...")
    web_svc.delete()

from azureml.core import ComputeTarget

for target in ComputeTarget.list(ws):
    print("Deleting compute target", target.name, "...")
    target.delete()

from azureml.core import Image

for img in Image.list(ws):
    print("Deleting image", img.id, "...")
    img.delete()

from azureml.core.model import Model
import requests
import json
from azureml.core.webservice import Webservice
from azureml.core.workspace import Workspace

# URL for the web service, should be similar to:
# 'http://8530a665-66f3-49c8-a953-b82a2d312917.eastus.azurecontainer.io/score'

ws = Workspace.from_config()
deployed_webservice = Webservice.list(ws)[0]
scoring_uri = deployed_webservice.scoring_uri

# If the service is authenticated, set the key or token
key = deployed_webservice.get_keys()[0]

# Two sets of data to score, so we get two results back
data = {
    "data": [
        {
            "age": 17,
            "campaign": 1,
            "cons.conf.idx": -46.2,
            "cons.price.idx": 92.893,
            "contact": "cellular",
            "day_of_week": "mon",
            "default": "no",
            "duration": 971,
            "education": "university.degree",
            "emp.var.rate": -1.8,
            "euribor3m": 1.299,
            "housing": "yes",