File size: 3,168 Bytes
a244b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd0249a
 
a244b20
 
fd0249a
 
 
 
 
 
 
 
 
a244b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# encoding: utf-8

from fastapi import FastAPI, Form, Depends, Request
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
import pickle
import json

app = FastAPI()

# Menentukan direktori templates
templates = Jinja2Templates(directory="templates")

model_file = open('insurance_model.pkl', 'rb')
model = pickle.load(model_file, encoding='bytes')


class Msg(BaseModel):
    msg: str


class Req(BaseModel):
    age: int
    sex: int
    smoker: int
    bmi: float
    children: int
    region: int


@app.get("/welcomeMessage")
async def welcome():
    return {"message": "Hello World. Welcome to FastAPI!"}

@app.get("/")
async def root(request: Request):
    return templates.TemplateResponse(
        "index.html",
        {
            "request": request,
            "insurance_cost": 0,
        }
    )

def form_req(age: str = Form(...), sex: str = Form(...), smoker: str = Form(...),
             bmi: str = Form(...), children: str = Form(...), region: str = Form(...)):
    sBmi = bmi.replace(",", ".")
    return Req(age=int(age), sex=int(sex), smoker=int(smoker), bmi=float(sBmi), children=int(children), region=int(region))


@app.get("/path")
async def demo_get():
    return {"message": "This is /path endpoint, use a post request to transform the text to uppercase"}


@app.post("/path")
async def demo_post(inp: Msg):
    return {"message": inp.msg.upper()}


@app.get("/path/{path_id}")
async def demo_get_path_id(path_id: int):
    return {"message": f"This is /path/{path_id} endpoint, use post request to retrieve result"}


@app.get("/predict/{path_id}")
async def predict(path_id: int):
    return {"message":  f"This is /predict/{path_id} endpoint, use post request to retrieve result"}


@app.post("/predict")
async def predict(request: Request, requess: Req = Depends(form_req)):
    '''
    Predict the insurance cost based on user inputs
    and render the result to the html page
    '''
    age = requess.age
    sex = requess.sex
    smoker = requess.smoker
    bmi = requess.bmi
    children = requess.children
    region = requess.region
    data = []

    data.append(int(age))
    data.extend([int(sex)])
    data.extend([float(bmi)])
    data.extend([int(children)])
    data.extend([int(smoker)])
    data.extend([int(region)])

    prediction = model.predict([data])
    output = round(prediction[0], 2)

    # Merender index.html dengan data hasil prediksi
    return templates.TemplateResponse(
        "index.html",
        {
            "request": request,
            "insurance_cost": output,
            "age": requess.age,
            "sex": "Laki-laki" if requess.sex == 1 else "Perempuan",
            "smoker": "Ya" if requess.smoker == 1 else "Tidak",
            "bmi": requess.bmi,  # Menambahkan ini
            "children": requess.children,  # Menambahkan ini
            "region": get_region_name(requess.region)  # Menambahkan ini
        }
    )

def get_region_name(region_code):
    region_mapping = {
        0: "Northeast",
        1: "Northwest",
        2: "Southeast",
        3: "Southwest"
    }
    return region_mapping.get(region_code, "Unknown")