无法在 fastapi 和 return 响应中进行预测
unable to do prediction in fastapi and return response
我有一个机器学习 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"
返回 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"
我有一个机器学习 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"
返回 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"