File size: 1,068 Bytes
b2f106f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-

import pandas as pd
from pycaret.regression import load_model, predict_model
from fastapi import FastAPI
import uvicorn
from pydantic import create_model

# Create the app
app = FastAPI()

# Load trained Pipeline
model = load_model("lr_api")

# Create input/output pydantic models
input_model = create_model("lr_api_input", **{'rownames': 1030, 'year': 1994, 'violent': 304.5, 'murder': 2.9000000953674316, 'prisoners': 152, 'afam': 1.769081950187683, 'cauc': 70.66014862060547, 'male': 18.20832061767578, 'population': 1.9304360151290894, 'income': 12036.8603515625, 'density': 0.023493800312280655, 'state': 'Utah', 'law': 'yes'})
output_model = create_model("lr_api_output", prediction=63.6)


# Define predict function
@app.post("/predict", response_model=output_model)
def predict(data: input_model):
    data = pd.DataFrame([data.dict()])
    predictions = predict_model(model, data=data)
    return {"prediction": predictions["prediction_label"].iloc[0]}


#if __name__ == "__main__":
#    uvicorn.run(app, host="127.0.0.1", port=8000)