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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import pickle, uvicorn, os
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn import preprocessing
|
| 7 |
+
from sklearn.impute import SimpleImputer
|
| 8 |
+
from sklearn.compose import ColumnTransformer
|
| 9 |
+
from sklearn.compose import make_column_selector as selector
|
| 10 |
+
from sklearn.metrics import accuracy_score
|
| 11 |
+
|
| 12 |
+
# Config & Setup
|
| 13 |
+
## Variables of environment
|
| 14 |
+
DIRPATH = os.path.dirname(__file__)
|
| 15 |
+
ASSETSDIRPATH = os.path.join(DIRPATH, "asset")
|
| 16 |
+
ml_component_pkl = os.path.join(ASSETSDIRPATH, "ml_component.pkl")
|
| 17 |
+
|
| 18 |
+
print(
|
| 19 |
+
f" {'*'*10} Config {'*'*10}\n INFO: DIRPATH = {DIRPATH} \n INFO: ASSETSDIRPATH = {ASSETSDIRPATH} "
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# API Basic config
|
| 24 |
+
app = FastAPI(
|
| 25 |
+
title="Titanic Survivors API",
|
| 26 |
+
version="0.0.1",
|
| 27 |
+
description="Prediction of Titanic Survivors",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
## Loading of assets
|
| 31 |
+
with open(ml_component_pkl, "rb") as f:
|
| 32 |
+
loaded_items = pickle.load(f)
|
| 33 |
+
#print("INFO: Loaded assets:", loaded_items)
|
| 34 |
+
|
| 35 |
+
pipeline_of_my_model = loaded_items["pipeline"]
|
| 36 |
+
num_cols = loaded_items['numeric_columns']
|
| 37 |
+
cat_cols = loaded_items['categorical_columns']
|
| 38 |
+
|
| 39 |
+
## BaseModel
|
| 40 |
+
class ModelInput(BaseModel):
|
| 41 |
+
PeopleInTicket: int
|
| 42 |
+
Age: float
|
| 43 |
+
FarePerPerson: float
|
| 44 |
+
SibSp: int
|
| 45 |
+
Pclass: int
|
| 46 |
+
Fare: float
|
| 47 |
+
Parch: int
|
| 48 |
+
TicketNumber: float
|
| 49 |
+
Embarked: str
|
| 50 |
+
Sex: str
|
| 51 |
+
|
| 52 |
+
## Utils
|
| 53 |
+
# def processing_FE(
|
| 54 |
+
# dataset, scaler, encoder,imputer, FE=pipeline_of_my_model
|
| 55 |
+
# ): # FE : ColumnTransfromer, Pipeline
|
| 56 |
+
# "Cleaning, Processing and Feature Engineering of the input dataset."
|
| 57 |
+
# """:dataset pandas.DataFrame"""
|
| 58 |
+
|
| 59 |
+
# # if imputer is not None:
|
| 60 |
+
# # output_dataset = imputer.transform(dataset)
|
| 61 |
+
# # else:
|
| 62 |
+
# # output_dataset = dataset.copy()
|
| 63 |
+
|
| 64 |
+
# # output_dataset = scaler.transform(output_dataset)
|
| 65 |
+
|
| 66 |
+
# # if encoder is not None:
|
| 67 |
+
# # output_dataset = encoder.transform(output_dataset)
|
| 68 |
+
# if FE is not None:
|
| 69 |
+
# output_dataset = FE.fit(output_dataset)
|
| 70 |
+
|
| 71 |
+
# return output_dataset
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def make_prediction(
|
| 76 |
+
Pclass, Sex, Age, SibSp,Parch, Fare, Embarked, PeopleInTicket, FarePerPerson,TicketNumber
|
| 77 |
+
|
| 78 |
+
):
|
| 79 |
+
|
| 80 |
+
df = pd.DataFrame(
|
| 81 |
+
[
|
| 82 |
+
[
|
| 83 |
+
PeopleInTicket,
|
| 84 |
+
Age,
|
| 85 |
+
FarePerPerson,
|
| 86 |
+
SibSp,
|
| 87 |
+
Pclass,
|
| 88 |
+
Fare,
|
| 89 |
+
Parch,
|
| 90 |
+
TicketNumber,
|
| 91 |
+
Embarked,
|
| 92 |
+
Sex,
|
| 93 |
+
|
| 94 |
+
]
|
| 95 |
+
],
|
| 96 |
+
columns=num_cols + cat_cols,
|
| 97 |
+
|
| 98 |
+
)
|
| 99 |
+
print(num_cols + cat_cols)
|
| 100 |
+
print( [
|
| 101 |
+
PeopleInTicket,
|
| 102 |
+
Age,
|
| 103 |
+
FarePerPerson,
|
| 104 |
+
SibSp,
|
| 105 |
+
Pclass,
|
| 106 |
+
Fare,
|
| 107 |
+
Parch,
|
| 108 |
+
TicketNumber,
|
| 109 |
+
Embarked,
|
| 110 |
+
Sex,
|
| 111 |
+
|
| 112 |
+
])
|
| 113 |
+
|
| 114 |
+
X = df
|
| 115 |
+
#df[cat_cols] = df[cat_cols].astype("object")
|
| 116 |
+
output = pipeline_of_my_model.predict(X).tolist()
|
| 117 |
+
return output
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
## Endpoints
|
| 123 |
+
@app.post("/Titanic")
|
| 124 |
+
async def predict(input: ModelInput):
|
| 125 |
+
"""__descr__
|
| 126 |
+
--details---
|
| 127 |
+
"""
|
| 128 |
+
output_pred = make_prediction(
|
| 129 |
+
PeopleInTicket =input.PeopleInTicket,
|
| 130 |
+
Age =input.Age,
|
| 131 |
+
FarePerPerson =input.FarePerPerson,
|
| 132 |
+
SibSp =input.SibSp,
|
| 133 |
+
Pclass =input.Pclass,
|
| 134 |
+
Fare =input.Fare,
|
| 135 |
+
Parch =input.Parch,
|
| 136 |
+
TicketNumber =input.TicketNumber,
|
| 137 |
+
Embarked =input.Embarked,
|
| 138 |
+
Sex=input.Sex,
|
| 139 |
+
)
|
| 140 |
+
# Labelling Model output
|
| 141 |
+
if output_pred == 0:
|
| 142 |
+
output_pred = "No,the person didn't survive"
|
| 143 |
+
else:
|
| 144 |
+
output_pred = "Yes,the person survived"
|
| 145 |
+
#return output_pred
|
| 146 |
+
return {
|
| 147 |
+
"prediction": output_pred,
|
| 148 |
+
"input": input
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# Execution
|
| 153 |
+
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
uvicorn.run(
|
| 156 |
+
"main:app",
|
| 157 |
+
reload=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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import pickle, uvicorn, os
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn import preprocessing
|
| 7 |
+
from sklearn.impute import SimpleImputer
|
| 8 |
+
from sklearn.compose import ColumnTransformer
|
| 9 |
+
from sklearn.compose import make_column_selector as selector
|
| 10 |
+
from sklearn.metrics import accuracy_score
|
| 11 |
+
|
| 12 |
+
# Config & Setup
|
| 13 |
+
## Variables of environment
|
| 14 |
+
DIRPATH = os.path.dirname(__file__)
|
| 15 |
+
ASSETSDIRPATH = os.path.join(DIRPATH, "asset")
|
| 16 |
+
ml_component_pkl = os.path.join(ASSETSDIRPATH, "ml_component.pkl")
|
| 17 |
+
|
| 18 |
+
print(
|
| 19 |
+
f" {'*'*10} Config {'*'*10}\n INFO: DIRPATH = {DIRPATH} \n INFO: ASSETSDIRPATH = {ASSETSDIRPATH} "
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# API Basic config
|
| 24 |
+
app = FastAPI(
|
| 25 |
+
title="Titanic Survivors API",
|
| 26 |
+
version="0.0.1",
|
| 27 |
+
description="Prediction of Titanic Survivors",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
## Loading of assets
|
| 31 |
+
with open(ml_component_pkl, "rb") as f:
|
| 32 |
+
loaded_items = pickle.load(f)
|
| 33 |
+
#print("INFO: Loaded assets:", loaded_items)
|
| 34 |
+
|
| 35 |
+
pipeline_of_my_model = loaded_items["pipeline"]
|
| 36 |
+
num_cols = loaded_items['numeric_columns']
|
| 37 |
+
cat_cols = loaded_items['categorical_columns']
|
| 38 |
+
|
| 39 |
+
## BaseModel
|
| 40 |
+
class ModelInput(BaseModel):
|
| 41 |
+
PeopleInTicket: int
|
| 42 |
+
Age: float
|
| 43 |
+
FarePerPerson: float
|
| 44 |
+
SibSp: int
|
| 45 |
+
Pclass: int
|
| 46 |
+
Fare: float
|
| 47 |
+
Parch: int
|
| 48 |
+
TicketNumber: float
|
| 49 |
+
Embarked: str
|
| 50 |
+
Sex: str
|
| 51 |
+
|
| 52 |
+
## Utils
|
| 53 |
+
# def processing_FE(
|
| 54 |
+
# dataset, scaler, encoder,imputer, FE=pipeline_of_my_model
|
| 55 |
+
# ): # FE : ColumnTransfromer, Pipeline
|
| 56 |
+
# "Cleaning, Processing and Feature Engineering of the input dataset."
|
| 57 |
+
# """:dataset pandas.DataFrame"""
|
| 58 |
+
|
| 59 |
+
# # if imputer is not None:
|
| 60 |
+
# # output_dataset = imputer.transform(dataset)
|
| 61 |
+
# # else:
|
| 62 |
+
# # output_dataset = dataset.copy()
|
| 63 |
+
|
| 64 |
+
# # output_dataset = scaler.transform(output_dataset)
|
| 65 |
+
|
| 66 |
+
# # if encoder is not None:
|
| 67 |
+
# # output_dataset = encoder.transform(output_dataset)
|
| 68 |
+
# if FE is not None:
|
| 69 |
+
# output_dataset = FE.fit(output_dataset)
|
| 70 |
+
|
| 71 |
+
# return output_dataset
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def make_prediction(
|
| 76 |
+
Pclass, Sex, Age, SibSp,Parch, Fare, Embarked, PeopleInTicket, FarePerPerson,TicketNumber
|
| 77 |
+
|
| 78 |
+
):
|
| 79 |
+
|
| 80 |
+
df = pd.DataFrame(
|
| 81 |
+
[
|
| 82 |
+
[
|
| 83 |
+
PeopleInTicket,
|
| 84 |
+
Age,
|
| 85 |
+
FarePerPerson,
|
| 86 |
+
SibSp,
|
| 87 |
+
Pclass,
|
| 88 |
+
Fare,
|
| 89 |
+
Parch,
|
| 90 |
+
TicketNumber,
|
| 91 |
+
Embarked,
|
| 92 |
+
Sex,
|
| 93 |
+
|
| 94 |
+
]
|
| 95 |
+
],
|
| 96 |
+
columns=num_cols + cat_cols,
|
| 97 |
+
|
| 98 |
+
)
|
| 99 |
+
print(num_cols + cat_cols)
|
| 100 |
+
print( [
|
| 101 |
+
PeopleInTicket,
|
| 102 |
+
Age,
|
| 103 |
+
FarePerPerson,
|
| 104 |
+
SibSp,
|
| 105 |
+
Pclass,
|
| 106 |
+
Fare,
|
| 107 |
+
Parch,
|
| 108 |
+
TicketNumber,
|
| 109 |
+
Embarked,
|
| 110 |
+
Sex,
|
| 111 |
+
|
| 112 |
+
])
|
| 113 |
+
|
| 114 |
+
X = df
|
| 115 |
+
#df[cat_cols] = df[cat_cols].astype("object")
|
| 116 |
+
output = pipeline_of_my_model.predict(X).tolist()
|
| 117 |
+
return output
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
## Endpoints
|
| 123 |
+
@app.post("/Titanic")
|
| 124 |
+
async def predict(input: ModelInput):
|
| 125 |
+
"""__descr__
|
| 126 |
+
--details---
|
| 127 |
+
"""
|
| 128 |
+
output_pred = make_prediction(
|
| 129 |
+
PeopleInTicket =input.PeopleInTicket,
|
| 130 |
+
Age =input.Age,
|
| 131 |
+
FarePerPerson =input.FarePerPerson,
|
| 132 |
+
SibSp =input.SibSp,
|
| 133 |
+
Pclass =input.Pclass,
|
| 134 |
+
Fare =input.Fare,
|
| 135 |
+
Parch =input.Parch,
|
| 136 |
+
TicketNumber =input.TicketNumber,
|
| 137 |
+
Embarked =input.Embarked,
|
| 138 |
+
Sex=input.Sex,
|
| 139 |
+
)
|
| 140 |
+
# Labelling Model output
|
| 141 |
+
if output_pred == 0:
|
| 142 |
+
output_pred = "No,the person didn't survive"
|
| 143 |
+
else:
|
| 144 |
+
output_pred = "Yes,the person survived"
|
| 145 |
+
#return output_pred
|
| 146 |
+
return {
|
| 147 |
+
"prediction": output_pred,
|
| 148 |
+
"input": input
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# Execution
|
| 153 |
+
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
uvicorn.run(
|
| 156 |
+
"main:app",
|
| 157 |
+
reload=True,
|
| 158 |
+
)
|