Home > Net >  unable to do prediction in fastapi and return response
unable to do prediction in fastapi and return response

Time:03-07

I have a machine learning API where I am doing predictions. It's working fine before introducing task_id. But after adding task_id I am getting response as null even though it's working properly but not returning the response

@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"

enter image description here

CodePudding user response:

The reason for returning null is that your code never makes it into the if statements (which, by the way, should be if...elif, rather than having two individual if statements). This is due to your endpoint receiving task_id as a string (i.e., "0", "1", etc), but you check against integer values. Thus, you should declare task_id as int parameter, as shown below:

@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"
  • Related