If a real-time service is in the Running state, the real-time service has been deployed successfully. This service provides a standard RESTful API for users to call. Before integrating the API to the production environment, commission the API. You can use the following methods to send an inference request to the real-time service:
Select form-data. Set KEY to the input parameter of the AI application, for example, images. Set VALUE to an image to be inferred (only one image can be inferred).
Select raw and then JSON(application/json). Enter the request body in the text box below. An example request body is as follows:
{ "meta": { "uuid": "10eb0091-887f-4839-9929-cbc884f1e20e" }, "data": { "req_data": [ { "sepal_length": 3, "sepal_width": 1, "petal_length": 2.2, "petal_width": 4 } ] } }
meta can carry a universally unique identifier (UUID). When you call an API, the system provides a UUID. When the inference result is returned, the UUID is returned to trace the request. If you do not need this function, leave meta blank. data contains a req_data array for one or multiple pieces of input data. The parameters of each piece of data are determined by the AI application, such as sepal_length and sepal_width in this example.
The command for sending inference requests can be input as a file or text.
curl -kv -F 'images=@Image path' -H 'X-Auth-Token:Token value' -X POST Real-time service URL
The following is an example of the cURL command for inference with file input:
curl -kv -F 'images=@/home/data/test.png' -H 'X-Auth-Token:MIISkAY***80T9wHQ==' -X POST https://modelarts-infers-1.xxx/v1/infers/eb3e0c54-3dfa-4750-af0c-95c45e5d3e83
curl -kv -d '{"data":{"req_data":[{"sepal_length":3,"sepal_width":1,"petal_length":2.2,"petal_width":4}]}}' -H 'X-Auth-Token:MIISkAY***80T9wHQ==' -H 'Content-type: application/json' -X POST https://modelarts-infers-1.xxx/v1/infers/eb3e0c54-3dfa-4750-af0c-95c45e5d3e83
-d indicates the text input of the request body.
The Python script for sending inference requests can be input as a file or text.
# -*- coding:utf-8 -*- import requests import threading import json def inference(file_name, num): while num > 0: num -= 1 try: method = "POST" headers = { "X-Auth-Token": "MIISkAY***80T9wHQ==" } url = "https://modelarts-infers-1.xxx/v1/infers/eb3e0c54-3dfa-4750-af0c-95c45e5d3e83" files = { 'images': (file_name, open(file_name, 'rb'), "multipart/form-data") } resp = requests.request(method, url, headers=headers, files=files, verify=False) if 200 != int(resp.status_code): raise Exception( 'inference failed. the status code is:' + str(resp.status_code) + str(resp.content) + str(resp.headers)) except Exception as e: raise e if __name__ == '__main__': thread = threading.Thread() predict = threading.Thread(target=inference, args=("./test.png", 20)) # Inference predict.start() predict.join() # The script waits for the thread to stop.
body = { "input": xxx } data = json.dumps(body) resp = requests.request(method, url, data=data, headers=headers, verify=False)
input indicates the text content.