0

I deployed my h2o model using FastAPI into a web application, where the h2o model will take in the user's input the province, agent type, occupations, premium, sum insurred, etc, and return a prediction.

I have 3 scripts:

  1. app

  2. predict.html

  3. result.html

this is the script for app:

    from fastapi import FastAPI, Request
    from fastapi.responses import HTMLResponse
    from fastapi.templating import Jinja2Templates
    import h2o
    import os
    import pandas as pd
    
    h2o.init()
    
    app = FastAPI()
    templates = Jinja2Templates(directory="templates")
    
    @app.get("/", response_class=HTMLResponse)
    async def predict(request: Request):
        return templates.TemplateResponse("predict.html", {"request": request})
    
    @app.post("/predict", response_class=HTMLResponse)
    async def predict(request: Request, PROVINCE: str, TH_OCODE: str, AGENT_TYPE: str, OCCUPATIONS: str, BEN_ANNUAL_PREMIUM: float, BEN_BASE_SUM_INS: float, MED_LOADING_TYPE: int):
        try:
            # Input data for prediction
            input_data = {"PROVINCE": [PROVINCE],
                        "TH_OCODE": [TH_OCODE],
                        "AGENT_TYPE": [AGENT_TYPE],
                        "OCCUPATIONS": [OCCUPATIONS],
                        "BEN_ANNUAL_PREMIUM": [BEN_ANNUAL_PREMIUM],
                        "BEN_BASE_SUM_INS": [BEN_BASE_SUM_INS],
                        "MED_LOADING_TYPE": [MED_LOADING_TYPE]}
            print(input_data)
    
            input_data = pd.DataFrame.from_dict(input_data)
            # Create a new H2O frame for the input data
            input_frame = h2o.H2OFrame(input_data)
            # Load the H2O model
            model = h2o.load_model(os.path.join(os.getcwd(), 'model', 'Model_61_test'))
            # Make the prediction with the H2O model
            prediction = model.predict(test_data=input_frame).as_data_frame()
            prediction = prediction.loc[0,'p1']
            
            # Render the HTML template with the prediction result
            return templates.TemplateResponse("result.html", {"request": request, "prediction": prediction})
        except Exception as e:
            prediction = str(e)

this is the script for predict:

    <html>
      <head>
        <title>Predict</title>
      </head>
      <body>
        <h1>Predict</h1>
        <form method="post" action="/predict">
          <label for="PROVINCE">PROVINCE:</label>
          <input type="text" id="PROVINCE" name="PROVINCE"><br><br>
          
          <label for="TH_OCODE">TH_OCODE:</label>
          <input type="text" id="TH_OCODE" name="TH_OCODE"><br><br>
          
          <label for="AGENT_TYPE">AGENT_TYPE:</label>
          <input type="text" id="AGENT_TYPE" name="AGENT_TYPE"><br><br>
          
          <label for="OCCUPATIONS">OCCUPATIONS:</label>
          <input type="text" id="OCCUPATIONS" name="OCCUPATIONS"><br><br>
          
          <label for="BEN_ANNUAL_PREMIUM">BEN_ANNUAL_PREMIUM:</label>
          <input type="number" step="any" id="BEN_ANNUAL_PREMIUM" name="BEN_ANNUAL_PREMIUM"><br><br>
          
          <label for="BEN_BASE_SUM_INS">BEN_BASE_SUM_INS:</label>
          <input type="number" step="any" id="BEN_BASE_SUM_INS" name="BEN_BASE_SUM_INS"><br><br>
          
          <label for="MED_LOADING_TYPE">MED_LOADING_TYPE:</label>
          <input type="number" id="MED_LOADING_TYPE" name="MED_LOADING_TYPE"><br><br>
          
          <input type="submit" value="Predict">
        </form>
    
        {% if prediction %}
          <h2>Prediction Result:</h2>
          <p>{{ prediction }}</p>
        {% endif %}
      </body>
    </html>

this is the script for result:

    <html>
        <head>
            <title>Prediction Result</title>
        </head>
        <body>
            <h1>Prediction Result</h1>
            <p>The predicted value is: {{ prediction }}</p>
        </body>
    </html>
I ran the server with: uvicorn app:app --reload

and inputted the parameters as follows: enter image description here

but ended up with the following errors:

{"detail":[{"loc":["query","PROVINCE"],"msg":"field required","type":"value_error.missing"},{"loc":    ["query","TH_OCODE"],"msg":"field required","type":"value_error.missing"},{"loc":    ["query","AGENT_TYPE"],"msg":"field required","type":"value_error.missing"},{"loc":["query","OCCUPATIONS"],"msg":"field required","type":"value_error.missing"},{"loc":["query","BEN_ANNUAL_PREMIUM"],"msg":"field required","type":"value_error.missing"},{"loc":["query","BEN_BASE_SUM_INS"],"msg":"field required","type":"value_error.missing"},{"loc":    ["query","MED_LOADING_TYPE"],"msg":"field required","type":"value_error.missing"}]}
this error came in the log: "POST /predict HTTP/1.1" 422 Unprocessable Entity

I'm not sure where I went wrong in my scripts.

  • Your POST endpoint is expecting query parameters, but submitting a form will send them as [form data](https://fastapi.tiangolo.com/tutorial/request-forms/). – M.O. Feb 27 '23 at 10:09

0 Answers0