Created
March 5, 2020 19:25
-
-
Save youngsoul/b402956a71b3d297e6f8518bc081ef6b to your computer and use it in GitHub Desktop.
Sample Python client for Azure AutoML Model Endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import pandas as pd | |
# URL for the web service | |
scoring_uri = 'http://SOME ID.centralus.azurecontainer.io/score' | |
# If the service is authenticated, set the key or token | |
key = 'SOME KEY' | |
""" | |
Example payload | |
data = { | |
"data": | |
[ | |
{'age': 57, 'job': 'technician', 'marital': 'married', 'education': 'high.school', 'default': 'no', | |
'housing': 'no', 'loan': 'yes', 'contact': 'cellular', 'month': 'may', 'duration': 371, 'campaign': 1, | |
'pdays': 999, 'previous': 1, 'poutcome': 'failure', 'emp.var.rate': -1.8, 'cons.price.idx': 92.893, | |
'cons.conf.idx': -46.2, 'euribor3m': 1.299, 'nr.employed': 5099.1} | |
] | |
} | |
""" | |
if __name__ == '__main__': | |
# read the bankmarketing_train.csv file and randomly select records to use in the test client | |
df = pd.read_csv("./bankmarketing_train.csv") | |
print(df.head()) | |
# Grad 10 random samples and predict on this data | |
# caveat - I realize we should not test on data the model was training on but I want to see the actual answers. | |
for row in df.sample(n=10).values: | |
predict_record = {} | |
y_value = None | |
for k,v in zip(df.columns, row): | |
if k == 'day_of_week': | |
continue | |
if k != 'y': | |
predict_record[k]=v | |
else: | |
y_value = v | |
data = { | |
"data":[predict_record] | |
} | |
print(f"Prediction Record: \n{json.dumps(data, indent=2)}") | |
# Convert to JSON string | |
input_data = json.dumps(data) | |
# Set the content type | |
headers = {'Content-Type': 'application/json'} | |
# If authentication is enabled, set the authorization header | |
headers['Authorization'] = f'Bearer {key}' | |
# Make the request and display the response | |
resp = requests.post(scoring_uri, input_data, headers=headers) | |
print(f"Expected Value: {y_value}") | |
pred = json.loads(resp.json())["result"][0] | |
print(f"Predicted: {pred}") | |
print("--------------------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment