class AzureMLService():

    def __init__(self, ws:Workspace, service_name: str):
        self.__ws = ws
        self.__azure_service = Webservice(ws, service_name)

    def make_request(self, inference_dataset_name):

        inference_dataset = Dataset.get_by_name(self.__ws,inference_dataset_name)
        df = inference_dataset.to_pandas_dataframe()

        body = json.dumps({'data': json.loads(df.to_json(orient='values'))})
        result = self.__azure_service.run(body)
        print(result)
           
示例#2
0
def get_result(players):
    import os
    import azureml
    from azureml.core import Workspace
    from azureml.core.webservice import Webservice
    from azureml.core.authentication import ServicePrincipalAuthentication

    print("getting results")
    filename = os.path.join(app.static_folder, 'champion.json')
    with open(filename) as json_file:
        champions = json.load(json_file)['data']
    X = [create_feature_row(players, champions)]

    # Check core SDK version number
    print("SDK version:", azureml.core.VERSION)
    workspace = "league-ws-deploy"
    subscription_id = "79451499-b2c0-4513-8dea-ef7f37173fbb"
    resource_grp = "league"

    svc_pr = ServicePrincipalAuthentication(
        tenant_id="1f0113ce-bee6-43b0-9e26-61617eced2e4",
        service_principal_id="4c9cfeac-dda9-4298-af3c-d51003c7438b",
        service_principal_password="******")

    ws = Workspace(workspace_name=workspace,
                   subscription_id=subscription_id,
                   resource_group=resource_grp,
                   auth=svc_pr)

    ws.get_details()

    print('Workspace name: ' + ws.name,
          'Azure region: ' + ws.location,
          'Subscription id: ' + ws.subscription_id,
          'Resource group: ' + ws.resource_group,
          sep='\n')

    print("Send to server to predict")
    sample = json.dumps({"data": X})
    sample = bytes(sample, encoding='utf8')
    service = Webservice(workspace=ws, name='lrmpredictfinal6')
    # predict using the deployed model
    result = service.run(input_data=sample)
    return result[0][1]
示例#3
0
#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, 'mnist')
#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
plt.figure(figsize=(20, 1))

for s in sample_indices:
    plt.subplot(1, n, i + 1)
    plt.axhline('')
    plt.axvline('')

    # use different color for misclassified sample
    font_color = 'red' if y_test[s] != result[i] else 'black'
    clr_map = plt.cm.gray if y_test[s] != result[i] else plt.cm.Greys

    plt.text(x=10, y=-10, s=result[i], fontsize=18, color=font_color)
示例#4
0
# Get the hosted web service
service = Webservice(workspace=ws, name=service_name)

# Input for Model with all features
input_j = [[
    1.62168882e+02, 4.82427351e+02, 1.09748253e+02, 4.32529303e+01,
    3.52377597e+01, 4.37307613e+01, 1.15729573e+01, 4.27624778e+00,
    1.68042813e+02, 4.61654301e+02, 1.03138200e+02, 4.08555785e+01,
    1.80809993e+01, 4.85402042e+01, 1.09373285e+01, 4.18269355e+00,
    0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
    3.07200000e+03, 5.64000000e+02, 2.22900000e+03, 9.84000000e+02,
    0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
    3.03000000e+02, 6.63000000e+02, 3.18300000e+03, 3.03000000e+02,
    5.34300000e+03, 4.26300000e+03, 6.88200000e+03, 1.02300000e+03,
    1.80000000e+01
]]

print(input_j)
test_sample = json.dumps({'data': input_j})
test_sample = bytes(test_sample, encoding='utf8')
try:
    prediction = service.run(input_data=test_sample)
    print(prediction)
except Exception as e:
    result = str(e)
    print(result)
    raise Exception('AKS service is not working as expected')

# Delete aks after test
#service.delete()
                                       image=image,
                                       deployment_config=aks_config,
                                       deployment_target=aks_target)
service.wait_for_deployment(show_output=True)
print(service.state)

api_key, _ = service.get_keys()
print(
    "Deployed AKS Webservice: {} \nWebservice Uri: {} \nWebservice API Key: {}"
    .format(service.name, service.scoring_uri, api_key))

aks_webservice = {}
aks_webservice["aks_service_name"] = service.name
aks_webservice["aks_service_url"] = service.scoring_uri
aks_webservice["aks_service_api_key"] = api_key
print("AKS Webservice Info")
print(aks_webservice)

print("Saving aks_webservice.json...")
aks_webservice_filepath = os.path.join('./outputs', 'aks_webservice.json')
with open(aks_webservice_filepath, "w") as f:
    json.dump(aks_webservice, f)
print("Done saving aks_webservice.json!")

# Single test data
test_data = [['manufactured in 2016 made of plastic in good condition']]

# Call the webservice to make predictions on the test data
prediction = service.run(json.dumps(test_data))
print('Test data prediction: ', prediction)
# 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)
示例#7
0
  deployment_config=aci_config,
  models = [registered_model], 
  image_config=image_config, 
  )


# # Step 8 - Test the ACI deployed webservice #
#%%
import json
age = 60
km = 40000
test_data  = json.dumps([[age,km]])
print(test_data)
webservice = Webservice(workspace=ws, name=service_name)
# If the service is not ready, run this cell again...
result = webservice.run(input_data=test_data)
print(result)


# # Step 9 - Provision an AKS cluster #
#%%
from azureml.core.compute import AksCompute, ComputeTarget
from azureml.core.webservice import Webservice, AksWebservice

# Use the default configuration, overriding the default location to a known region that supports AKS
prov_config = AksCompute.provisioning_configuration(location='westus2')

aks_name = 'aks-cluster01' 

# Create the cluster
aks_target = ComputeTarget.create(workspace = ws,