Retrofit POST 不适用于 Flask API,但 python 请求有效?

发布于 2025-01-12 14:09:15 字数 1766 浏览 3 评论 0原文

我创建了一个 Flask API,它获取手写数字的 Base64 图像并预测该数字。

@app.route('/predict', methods = ['POST'])
def predict():
    if request.method == 'POST':
        b64String = request.form['file']
        prediction = predictResult(b64String)
        return jsonify({'pred' : prediction.item()})

此 Flask 应用程序已托管在 https://mnist-flask-pytorch-ashis.herokuapp.com/predict 上。当我使用 python requests 测试端点时,它工作得很好。我用来测试的代码是:

def toBase64(filename):
    encodedString = ''
    with open(filename, 'rb') as img:
        encodedString = base64.b64encode(img.read())
    return encodedString

res = requests.post('https://mnist-flask-pytorch-ashis.herokuapp.com/predict', data={'file': toBase64('seven.png')})
print(res.text)

现在我想构建一个android应用程序来服务这个API,我正在使用retrofit并得到503。

interface MnistService {
    @POST("predict")
    suspend fun getPrediction(@Body file: String): Response<MnistResponse>
}

来自heroku的一些日志如果它可能有帮助

2022-03-08T11:56:44.674100+00:00 app[web.1]: 10.1.26.234 - - [08/Mar/2022:11:56:44 +0000] "POST /predict HTTP/1.1" 200 11 "-" "python-requests/2.26.0"
2022-03-08T11:57:27.139081+00:00 app[web.1]: 10.1.81.249 - - [08/Mar/2022:11:57:27 +0000] "POST /predict HTTP/1.1" 400 192 "-" "okhttp/4.9.1"
2022-03-08T11:57:27.149358+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/predict" host=mnist-flask-pytorch-ashis.herokuapp.com request_id=586590ba-ea2b-48a4-99cc-9d3969aa7f6e fwd="115.124.42.47" dyno=web.1 connect=0ms service=11ms status=503 bytes=355 protocol=https

I have created a flask API that takes a base64 image of a handwritten digit and predicts the digit.

@app.route('/predict', methods = ['POST'])
def predict():
    if request.method == 'POST':
        b64String = request.form['file']
        prediction = predictResult(b64String)
        return jsonify({'pred' : prediction.item()})

This Flask app is already hosted at https://mnist-flask-pytorch-ashis.herokuapp.com/predict. When I am testing the endpoint using python requests it works perfectly fine. The code I am using to test is:

def toBase64(filename):
    encodedString = ''
    with open(filename, 'rb') as img:
        encodedString = base64.b64encode(img.read())
    return encodedString

res = requests.post('https://mnist-flask-pytorch-ashis.herokuapp.com/predict', data={'file': toBase64('seven.png')})
print(res.text)

Now I want to build an android app to serve this API, I am using retrofit and getting 503.

interface MnistService {
    @POST("predict")
    suspend fun getPrediction(@Body file: String): Response<MnistResponse>
}

Some logs from heroku if it may help

2022-03-08T11:56:44.674100+00:00 app[web.1]: 10.1.26.234 - - [08/Mar/2022:11:56:44 +0000] "POST /predict HTTP/1.1" 200 11 "-" "python-requests/2.26.0"
2022-03-08T11:57:27.139081+00:00 app[web.1]: 10.1.81.249 - - [08/Mar/2022:11:57:27 +0000] "POST /predict HTTP/1.1" 400 192 "-" "okhttp/4.9.1"
2022-03-08T11:57:27.149358+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/predict" host=mnist-flask-pytorch-ashis.herokuapp.com request_id=586590ba-ea2b-48a4-99cc-9d3969aa7f6e fwd="115.124.42.47" dyno=web.1 connect=0ms service=11ms status=503 bytes=355 protocol=https

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2025-01-19 14:09:15

正如您的服务人员所说,也许您应该使用表格来请求。

interface MnistService {
    @FormUrlEncoded
    @POST("predict")
    suspend fun getPrediction(@Field file: String): Response<MnistResponse>
}

As your service said, maybe you should use form to request.

interface MnistService {
    @FormUrlEncoded
    @POST("predict")
    suspend fun getPrediction(@Field file: String): Response<MnistResponse>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文