无法在FastAPI中执行ML预测并返回响应

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

我有一个机器学习 API,可以在其中进行预测。在引入之前它工作正常

task_id
。但是添加
task_id
后,我得到的响应为空,即使它工作正常但没有返回响应

@app.get('/predict')
def predict(task_id,solute: str, solvent: str):
    if task_id ==0:
        results = predictions(solute, solvent)
        response["interaction_map"] = (results[1].detach().numpy()).tolist()
        response["predictions"] = results[0].item()
        return {'result': response}
    if task_id == 1:
        return "this is second one"

python python-3.x fastapi
1个回答
0
投票

返回

null
的原因是您的代码永远不会进入
if
语句(顺便说一句,应该是
if
...
elif
,而不是有两个单独的
if
语句) 。这是因为您的端点接收
task_id
作为字符串(即“0”、“1”等),但您检查了整数值。因此,您应该将
task_id
声明为
int
参数,如下所示:

@app.get('/predict')
def predict(task_id: int, solute: str, solvent: str):
    if task_id == 0:
        results = predictions(solute, solvent)
        response["interaction_map"] = (results[1].detach().numpy()).tolist()
        response["predictions"] = results[0].item()
        return {'result': response}
    elif task_id == 1:
        return "this is second one"
    else:
        return "some default response"
© www.soinside.com 2019 - 2024. All rights reserved.