Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
unable to do prediction in fastapi and return response

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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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"
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda