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
|
| 3 |
+
|
| 4 |
+
from sklearn.datasets import fetch_openml
|
| 5 |
+
|
| 6 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 7 |
+
from sklearn.compose import make_column_transformer
|
| 8 |
+
|
| 9 |
+
from sklearn.pipeline import make_pipeline
|
| 10 |
+
|
| 11 |
+
from sklearn.model_selection import train_test_split, RandomizedSearchCV
|
| 12 |
+
|
| 13 |
+
from sklearn.linear_model import LogisticRegression
|
| 14 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 15 |
+
|
| 16 |
+
# Run the training script placed in the same directory as app.py
|
| 17 |
+
# The training script will train and persist a linear regression
|
| 18 |
+
# model with the filename 'model.joblib'
|
| 19 |
+
|
| 20 |
+
# Run training script
|
| 21 |
+
import pandas as pd
|
| 22 |
+
from sklearn.model_selection import train_test_split
|
| 23 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 24 |
+
|
| 25 |
+
import joblib
|
| 26 |
+
|
| 27 |
+
# Choose and instantiate RandomForestRegressor model
|
| 28 |
+
model = RandomForestRegressor()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Train the model using the training data
|
| 32 |
+
model.fit(X_train, y_train)
|
| 33 |
+
|
| 34 |
+
# Save the trained model to a file (e.g., 'insurance_model.joblib')
|
| 35 |
+
joblib.dump(model, 'model.joblib')
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Load the freshly trained model from disk
|
| 39 |
+
model = joblib.load('model.joblib')
|
| 40 |
+
|
| 41 |
+
# Prepare the logging functionality
|
| 42 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
| 43 |
+
log_folder = log_file.parent
|
| 44 |
+
log_file.parent.mkdir(exist_ok=True)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
#######################################################################################################################
|
| 48 |
+
scheduler = CommitScheduler(
|
| 49 |
+
repo_id="-----------", # provide a name "insurance-charge-mlops-logs" for the repo_id
|
| 50 |
+
repo_type="dataset",
|
| 51 |
+
folder_path=log_folder,
|
| 52 |
+
path_in_repo="data",
|
| 53 |
+
every=2
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Define the predict function which will take features, convert to dataframe and make predictions using the saved model
|
| 57 |
+
# the functions runs when 'Submit' is clicked or when a API request is made
|
| 58 |
+
|
| 59 |
+
import pandas as pd
|
| 60 |
+
import joblib
|
| 61 |
+
|
| 62 |
+
# Assuming 'model' is a trained model loaded using joblib.load()
|
| 63 |
+
|
| 64 |
+
def predict(age, sex, bmi, children, smoker, region):
|
| 65 |
+
"""Predicts insurance charges based on customer features.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
age (int): Age of the customer.
|
| 69 |
+
sex (int): Gender of the customer (0 for female, 1 for male).
|
| 70 |
+
bmi (float): Body Mass Index of the customer.
|
| 71 |
+
children (int): Number of children the customer has.
|
| 72 |
+
smoker (int): Smoking status of the customer (0 for yes, 1 for no).
|
| 73 |
+
region (int): Region of the customer (1-4 representing different regions).
|
| 74 |
+
|
| 75 |
+
Returns:
|
| 76 |
+
float: Predicted insurance charges.
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
# Create a DataFrame with the input features
|
| 80 |
+
input_data = pd.DataFrame({
|
| 81 |
+
'age': [age],
|
| 82 |
+
'sex': [sex],
|
| 83 |
+
'bmi': [bmi],
|
| 84 |
+
'children': [children],
|
| 85 |
+
'smoker': [smoker],
|
| 86 |
+
'region': [region]
|
| 87 |
+
})
|
| 88 |
+
|
| 89 |
+
# Make the prediction using the loaded model
|
| 90 |
+
prediction = model.predict(input_data)
|
| 91 |
+
|
| 92 |
+
# Return the prediction
|
| 93 |
+
return prediction[0]
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# While the prediction is made, log both the inputs and outputs to a log file
|
| 98 |
+
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
|
| 99 |
+
# access
|
| 100 |
+
|
| 101 |
+
with scheduler.lock:
|
| 102 |
+
with log_file.open("a") as f:
|
| 103 |
+
f.write(json.dumps(
|
| 104 |
+
{
|
| 105 |
+
'age': age,
|
| 106 |
+
'bmi': bmi,
|
| 107 |
+
'children': children,
|
| 108 |
+
'sex': sex,
|
| 109 |
+
'smoker': smoker,
|
| 110 |
+
'region': region,
|
| 111 |
+
'prediction': prediction[0]
|
| 112 |
+
}
|
| 113 |
+
))
|
| 114 |
+
f.write("\n")
|
| 115 |
+
|
| 116 |
+
return prediction[0]
|
| 117 |
+
|
| 118 |
+
# Set up UI components for input and output
|
| 119 |
+
import gradio as gr
|
| 120 |
+
|
| 121 |
+
age = gr.inputs.Slider(minimum=18, maximum=64, default=30, step=1, label="Age")
|
| 122 |
+
sex = gr.inputs.Radio(["female", "male"], type)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# Create the gradio interface, make title "HealthyLife Insurance Charge Prediction"
|
| 126 |
+
import gradio as gr
|
| 127 |
+
|
| 128 |
+
# ... (previous code for model training and predict function) ...
|
| 129 |
+
|
| 130 |
+
# Set up UI components for input and output
|
| 131 |
+
age = gr.inputs.Slider(minimum=18, maximum=64, default=30, step=1, label="Age")
|
| 132 |
+
sex = gr.inputs.Radio(["female", "male"], type="value", default="female", label="Sex") # Corrected type
|
| 133 |
+
bmi = gr.inputs.Number(default=25, label="BMI")
|
| 134 |
+
children = gr.inputs.Slider(minimum=0, maximum=5, default=0, step=1, label="Children")
|
| 135 |
+
smoker = gr.inputs.Radio(["yes", "no"], type="value", default="no", label="Smoker") # Corrected type
|
| 136 |
+
region = gr.inputs.Dropdown(["southwest", "southeast", "northwest", "northeast"], type="value", default="southwest", label="Region") # Corrected type
|
| 137 |
+
|
| 138 |
+
# Create the Gradio interface
|
| 139 |
+
demo = gr.Interface(
|
| 140 |
+
fn=predict,
|
| 141 |
+
inputs=[age, sex, bmi, children, smoker, region],
|
| 142 |
+
outputs="number",
|
| 143 |
+
title="HealthyLife Insurance Charge Prediction"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# Launch with a load balancer
|
| 147 |
+
demo.queue()
|
| 148 |
+
demo.launch(share=False)
|
| 149 |
+
|
model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eda470100f233877b51c0daa8ecd4689bd0eb8657dc029ddc948ee6702fd964e
|
| 3 |
+
size 183916
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn==1.3.2
|
| 2 |
+
numpy==1.26.4
|
| 3 |
+
gradio==4.1.1
|