File size: 2,685 Bytes
531bd39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, conint, confloat
from enum import Enum
import numpy as np
import pandas as pd
import pickle as pkl
import tensorflow as tf
from tensorflow.keras.models import load_model

# Initialize FastAPI app
app = FastAPI()

# Load the trained model
model = load_model("model.h5")

# Load the pre-trained scalers and encoders
with open("scaler.pkl", "rb") as f:
    scaler = pkl.load(f)

with open("label_encoder_gender.pkl", "rb") as f:
    gen_encoder = pkl.load(f)

with open("onehot_encoder_geography.pkl", "rb") as f:
    geo_encoder = pkl.load(f)

# Enums for Gender and Geography
class GenderEnum(str, Enum):
    Male = "Male"
    Female = "Female"

class GeographyEnum(str, Enum):
    France = "France"
    Germany = "Germany"
    Spain = "Spain"

# Pydantic model for request validation
class CustomerData(BaseModel):
    CreditScore: conint(ge=350, le=850)
    Gender: GenderEnum
    Age: conint(ge=18, le=92)
    Tenure: conint(ge=0, le=10)
    Balance: confloat(ge=0)
    NumOfProducts: conint(ge=1, le=4)
    HasCrCard: conint(ge=0, le=1)
    IsActiveMember: conint(ge=0, le=1)
    EstimatedSalary: confloat(ge=0)
    Geography: GeographyEnum

# API Endpoint for prediction
@app.post("/predict/")
def predict_churn(data: CustomerData):
    # Encode gender
    gender_encoded = gen_encoder.transform([data.Gender.value])[0]

    # One-hot encode geography
    geo_encoded = geo_encoder.transform([[data.Geography.value]])
    geo_encoded_df = pd.DataFrame(geo_encoded, columns=geo_encoder.categories_[0])

    # Prepare input data as DataFrame
    input_data = {
        "CreditScore": data.CreditScore,
        "Gender": gender_encoded,
        "Age": data.Age,
        "Tenure": data.Tenure,
        "Balance": data.Balance,
        "NumOfProducts": data.NumOfProducts,
        "HasCrCard": data.HasCrCard,
        "IsActiveMember": data.IsActiveMember,
        "EstimatedSalary": data.EstimatedSalary,
    }

    input_df = pd.DataFrame([input_data])

    # Append one-hot encoded geography
    input_df = pd.concat([input_df, geo_encoded_df], axis=1)

    # Rename columns to match training data
    input_df.rename(
        columns={"France": "Geography_France", "Germany": "Geography_Germany", "Spain": "Geography_Spain"},
        inplace=True,
    )

    # Scale the input data
    input_scaled = scaler.transform(input_df)

    # Make prediction
    prediction = model.predict(input_scaled)

    # Return result
    result = "The customer is likely to churn" if prediction[0][0] > 0.5 else "The customer is not likely to churn"
    return {"prediction": result, "probability": float(prediction[0][0])}