python中的RESTful API请求[tensorflow服务]

问题描述 投票:1回答:1

OS:窗口10

Tensorflow:V2

我在此视频https://www.youtube.com/watch?v=uabNEQlpGM8中使用了Tensorflow和docker服务,>

powershell中的代码:

docker pull tensorflow/serving

mkdir tf_test
cd .\tf_test\
ls
git clone https://github.com/tensorflow/serving

Set-Variable -Name "TESTDATA" -Value "$(pwd)/serving/tensorflow_serving/servables/tensorflow/testdata"


docker run -t --rm -p 8501:8501 -v "$TESTDATA/saved_model_half_plus_two_cpu:/models/half_plus_two" -e MODEL_NAME=half_plus_two tensorflow/serving 

Get-Alias curl
Remove-Item alias:curl

curl  -X POST http://localhost:8501/v1/models/half_plus_two:predict  -d '{\"instances\": [1.0, 2.0, 5.0]}'


并且通过删除视频中提到的别名,我得到了请求的输出(在Powershell中]

curl  -X POST http://localhost:8501/v1/models/half_plus_two:predict  -d '{\"instances\": [1.0, 2.0, 5.0]}'


>>
{
    "predictions": [2.5, 3.0, 4.5
    ]
}

现在我想在python脚本中发出相同的API请求

# importing the requests library 
import requests 
import json
# defining the api-endpoint  
API_ENDPOINT = "http://localhost:8501/v1/models/half_plus_two:predict"


#curl  -X POST http://localhost:8501/v1/models/half_plus_two:predict  -d '{\"instances\": [1.0, 2.0, 5.0]}'
# data to be sent to api 

data = {'instances':[1.0, 2.0, 5.0]}

# sending post request and saving response as response object 
r = requests.post(url = API_ENDPOINT, data = data) 
# extracting response text
pastebin_url = r.text 
print("The pastebin URL is:%s"%pastebin_url)

但是我收到以下错误

The pastebin URL is:{ "error": "JSON Parse error: Invalid value. at offset: 0" }

状态码= 400错误的请求

代码有什么问题?

OS:Window 10 Tensorflow:V2,我在此视频中使用了Tensorflow与docker服务https://www.youtube.com/watch?v=uabNEQlpGM8 powershell中的代码:docker pull tensorflow / serving mkdir ...

python tensorflow python-requests tensorflow-serving
1个回答
1
投票

尝试requests.post(url = API_ENDPOINT, data = json.dumps(data))

© www.soinside.com 2019 - 2024. All rights reserved.