Skip to content

Functional

explain(request) async

Explain route that expose a model explainer in charge of model explicability

This function is using starlette Request object instead of pydantic since we can not know what data your model is expecting. See https://fastapi.tiangolo.com/advanced/using-request-directly/ for more infos.

We also use the custom starlette JSONResponse class (PredictionResponse) instead of pydantic for the same reasons

For a cleaner way to handle requests and reponses you should use pydantic as stated in FastAPI doc : https://fastapi.tiangolo.com/tutorial/body/#create-your-data-model

You can use routes from example_api_num.routers.technical as examples of how to create requests and responses schemas thanks to pydantic or have a look at the FastAPI documentation : https://fastapi.tiangolo.com/tutorial/response-model/

If there is not explainer or the explainer does not implement explain_as_json or explain_as_html we return a 501 HTTP error : https://developer.mozilla.org/fr/docs/Web/HTTP/Status/501

Source code in template_api/routers/functional.py
@router.post("/explain")
async def explain(request: Request):
    """Explain route that expose a model explainer in charge of model explicability

    This function is using starlette Request object instead of pydantic since we can not
    know what data your model is expecting.
    See https://fastapi.tiangolo.com/advanced/using-request-directly/ for more infos.

    We also use the custom starlette JSONResponse class (PredictionResponse)
    instead of pydantic for the same reasons

    For a cleaner way to handle requests and reponses you should use pydantic as stated in FastAPI
    doc : https://fastapi.tiangolo.com/tutorial/body/#create-your-data-model

    You can use routes from example_api_num.routers.technical as examples of how to create requests and
    responses schemas thanks to pydantic or have a look at the FastAPI documentation :
    https://fastapi.tiangolo.com/tutorial/response-model/

    If there is not explainer or the explainer does not implement explain_as_json or explain_as_html
    we return a 501 HTTP error : https://developer.mozilla.org/fr/docs/Web/HTTP/Status/501
    """
    model: Model = RESOURCES.get(RESOURCE_MODEL)

    body = await request.body()
    body = json.loads(body) if body else {}

    # JSON repsonse (when Accept: application/json in the request)
    if request.headers.get("Accept") == "application/json":
        try:
            explanation_json = model.explain_as_json(**body)

        except (AttributeError, NotImplementedError):
            error_msg = {
                "error": {
                    "code": 501,
                    "message": "No explainer capable of handling explicability"
                }
            }
            return Response(
                content=json.dumps(error_msg),
                status_code=501,
                media_type='application/json',
            )
        else:
            return NumpyJSONResponse(explanation_json)

    # HTML repsonse (otherwise)
    else:
        try:
            explanation_html = model.explain_as_html(**body)

        except (AttributeError, NotImplementedError):
            return Response(
                content="No explainer capable of handling explicability",
                status_code=501,
                media_type='text/plain',
            )
        else:
            return HTMLResponse(explanation_html)

predict(request) async

Predict route that exposes your model

This function is using starlette Request object instead of pydantic since we can not know what data your model is expecting. See https://fastapi.tiangolo.com/advanced/using-request-directly/ for more infos.

We also use a custom starlette JSONResponse class (NumpyJSONResponse) instead of pydantic for the same reasons

For a cleaner way to handle requests and reponses you should use pydantic as stated in FastAPI doc : https://fastapi.tiangolo.com/tutorial/body/#create-your-data-model

You can use routes from template_api.routers.technical as examples of how to create requests and responses schemas thanks to pydantic or have a look at the FastAPI documentation : https://fastapi.tiangolo.com/tutorial/response-model/

Source code in template_api/routers/functional.py
@router.post("/predict")
async def predict(request: Request):
    """Predict route that exposes your model

    This function is using starlette Request object instead of pydantic since we can not
    know what data your model is expecting.
    See https://fastapi.tiangolo.com/advanced/using-request-directly/ for more infos.

    We also use a custom starlette JSONResponse class (NumpyJSONResponse)
    instead of pydantic for the same reasons

    For a cleaner way to handle requests and reponses you should use pydantic as stated in FastAPI
    doc : https://fastapi.tiangolo.com/tutorial/body/#create-your-data-model

    You can use routes from template_api.routers.technical as examples of how to create requests and
    responses schemas thanks to pydantic or have a look at the FastAPI documentation :
    https://fastapi.tiangolo.com/tutorial/response-model/
    """
    model: Model = RESOURCES.get(RESOURCE_MODEL)

    body = await request.body()
    body = json.loads(body) if body else {}

    prediction = model.predict(**body)

    return NumpyJSONResponse(prediction)