# This is an autogenerated nbx_user.py file for a [NBX](www.nimblebox.ai) app:
# project name: {{ project_name }}
# created time: {{ created_time }}
#   created by: {{ email_id }}

from fastapi import FastAPI, Request, Response
from pydantic import BaseModel

from typing import *

from nbx_user import get_op

class {{ operator_name }}_request(BaseModel):{% for base_model_string in base_model_strings%}
  {{ base_model_string }}{% endfor %}

app = FastAPI()
op = get_op()
op.__remote_init__()

@app.get("/")
async def ping():
  return {"message": "pong"}

@app.post("/predict", status_code=200)
async def predict(request: Request, request_model: {{ operator_name }}_request, response: Response):
  # try to get the data dict
  try:
    data = request_model.dict()
  except Exception as e:
    response.status_code = 400
    return {"data": None, "error": str(e),}

  try:
    resp = op(**data)
  except Exception as e:
    response.status_code = 500
    return {"data": None, "error": str(e),}
  return {"data": resp, "error": None,}
