Spaces:
Runtime error
Runtime error
File size: 3,179 Bytes
dd8bf84 b23e70e dd8bf84 b23e70e dd8bf84 | 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | from fastapi import FastAPI
import pickle, uvicorn, os
from pydantic import BaseModel
import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.compose import make_column_selector as selector
from sklearn.metrics import accuracy_score
# Config & Setup
## Variables of environment
# DIRPATH = os.path.dirname(__file__)
# ASSETSDIRPATH = os.path.join(DIRPATH, "asset")
# ml_component_pkl = os.path.join(ASSETSDIRPATH, "ml_component.pkl")
# print(
# f" {'*'*10} Config {'*'*10}\n INFO: DIRPATH = {DIRPATH} \n INFO: ASSETSDIRPATH = {ASSETSDIRPATH} "
# )
# API Basic config
app = FastAPI(
title="Titanic Survivors API",
version="0.0.1",
description="Prediction of Titanic Survivors",
)
## Loading of assets
with open(ml_component_pkl, "rb") as f:
loaded_items = pickle.load(f)
#print("INFO: Loaded assets:", loaded_items)
pipeline_of_my_model = loaded_items["pipeline"]
num_cols = loaded_items['numeric_columns']
cat_cols = loaded_items['categorical_columns']
## BaseModel
class ModelInput(BaseModel):
PeopleInTicket: int
Age: float
FarePerPerson: float
SibSp: int
Pclass: int
Fare: float
Parch: int
TicketNumber: float
Embarked: str
Sex: str
def make_prediction(
Pclass, Sex, Age, SibSp,Parch, Fare, Embarked, PeopleInTicket, FarePerPerson,TicketNumber
):
df = pd.DataFrame(
[
[
PeopleInTicket,
Age,
FarePerPerson,
SibSp,
Pclass,
Fare,
Parch,
TicketNumber,
Embarked,
Sex,
]
],
columns=num_cols + cat_cols,
)
print(num_cols + cat_cols)
print( [
PeopleInTicket,
Age,
FarePerPerson,
SibSp,
Pclass,
Fare,
Parch,
TicketNumber,
Embarked,
Sex,
])
X = df
#df[cat_cols] = df[cat_cols].astype("object")
output = pipeline_of_my_model.predict(X).tolist()
return output
## Endpoints
@app.post("/Titanic")
async def predict(input: ModelInput):
"""__descr__
--details---
"""
output_pred = make_prediction(
PeopleInTicket =input.PeopleInTicket,
Age =input.Age,
FarePerPerson =input.FarePerPerson,
SibSp =input.SibSp,
Pclass =input.Pclass,
Fare =input.Fare,
Parch =input.Parch,
TicketNumber =input.TicketNumber,
Embarked =input.Embarked,
Sex=input.Sex,
)
# Labelling Model output
if output_pred == 0:
output_pred = "No,the person didn't survive"
else:
output_pred = "Yes,the person survived"
#return output_pred
return {
"prediction": output_pred,
"input": input
}
# Execution
if __name__ == "__main__":
uvicorn.run(
"main:app",
reload=True,
) |