gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:45:39 25 4
gpt4 key购买 nike

操作系统:Windows 10

tensorflow :V2

我使用 Tensorflow 与 docker 一起服务,如本视频 https://www.youtube.com/watch?v=uabNEQlpGM8 所示。

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 错误请求

代码有什么问题吗?

最佳答案

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

关于python - python 中的 RESTful API 请求 [tensorflow 服务],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59233164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com