0

I have deployed a machine learning model as a pickle file in azure machine learning. The endpoint is created. Now, I am trying to consume the endpoint through the following codes:

import requests
import numpy as np

# send a random row from the test set to score
random_index = np.random.randint(0, len(X_test) - 1)
input_data = '{"data": [' + str(list(X_test[random_index])) + "]}"

headers = {"Content-Type": "application/json"}

resp = requests.post(service.scoring_uri, input_data, headers=headers)

print("POST to url", service.scoring_uri)
print("prediction:", resp.text)

It's giving error with following message:

prediction: {"data": "Expecting value: line 1 column 12 (char 11)", "message": "Failed to predict"}

The data looks like:

X_test =>  array([[[0.   ], [0.274710], [0.403273]]])

'{"data": [' + str(list(X_test[random_index])) + "]}"     
convert it to 
'{"data": [[array([0.]), array([0.274710]), array([0.403273])]]}'
shan
  • 553
  • 2
  • 9
  • 25

1 Answers1

0

In the current code mentioned, it was mentioned as POST method. But to consume the endpoints, it was suggested to use the GET method.

import requests
import os
import base64
import json 

personal_access_token = ":"+os.environ["AZ_ACCESS_TOKEN"]
headers = {}
headers['Content-type'] = "application/json"
headers['Authorization'] = b'Basic ' + base64.b64encode(personal_access_token.encode('utf-8'))

#Get a list of agent pools.
instance = "dev.azure.com/name"
propVals = "{name=Default,isHosted=false}"
api_version = "version _number”
uri = ("complete uri”)

r = requests.get(uri, headers=headers) 

To get the complete URI, use the GET method with poolName embedded into the syntax

https://dev.azure.com/{organization}/_apis/distributedtask/pools?poolName={poolName}&api-version=5.1

If the case is like using the POST method itself, change the below line in the 6th line of the code.

input_data = '{"data": [“ + str(list(X_test[random_index])) + "]}’

The single and double quotations are misplaced.

Sairam Tadepalli
  • 1,563
  • 1
  • 3
  • 11