Upload 3 files
Browse files- app.py +100 -0
- model.joblib +3 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import the libraries
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
import json
|
5 |
+
import uuid
|
6 |
+
import joblib
|
7 |
+
import pandas as pd
|
8 |
+
import gradio as gr
|
9 |
+
import math
|
10 |
+
|
11 |
+
from huggingface_hub import CommitScheduler
|
12 |
+
from pathlib import Path
|
13 |
+
|
14 |
+
|
15 |
+
# Run the training script placed in the same directory as app.py
|
16 |
+
# The training script will train and persist a linear regression
|
17 |
+
# model with the filename 'model.joblib'
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
# Load the freshly trained model from disk
|
23 |
+
insurance_charge_predictor = joblib.load('model.joblib')
|
24 |
+
|
25 |
+
# Prepare the logging functionality
|
26 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
27 |
+
log_folder = log_file.parent
|
28 |
+
|
29 |
+
scheduler = CommitScheduler(
|
30 |
+
repo_id="anirudhabokil/insurance-charge-mlops-logs", # provide a name "insurance-charge-mlops-logs" for the repo_id
|
31 |
+
repo_type="dataset",
|
32 |
+
folder_path=log_folder,
|
33 |
+
path_in_repo="data",
|
34 |
+
every=2
|
35 |
+
)
|
36 |
+
|
37 |
+
# Define the predict function which will take features, convert to dataframe and make predictions using the saved model
|
38 |
+
# the functions runs when 'Submit' is clicked or when a API request is made
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
# Set up UI components for input and output
|
43 |
+
age = gr.Number(label="Age")
|
44 |
+
bmi = gr.Number(label="BMI")
|
45 |
+
children = gr.Number(label="Children")
|
46 |
+
sex = gr.Dropdown(['male','female'], label="Sex")
|
47 |
+
smoker = gr.Dropdown(['yes','no'], label="Smoker")
|
48 |
+
region = gr.Dropdown(['southwest','southeast','northwest','northeast'], label="Region")
|
49 |
+
|
50 |
+
model_output = gr.Label(label="Insurance Charge")
|
51 |
+
|
52 |
+
def predict_insurance_charge(age, bmi, children, sex, smoker, region):
|
53 |
+
sample = {
|
54 |
+
'age': age,
|
55 |
+
'bmi': bmi,
|
56 |
+
'children': children,
|
57 |
+
'sex': sex,
|
58 |
+
'smoker': smoker,
|
59 |
+
'region': region
|
60 |
+
}
|
61 |
+
df = pd.DataFrame([sample])
|
62 |
+
print(sample)
|
63 |
+
prediction = insurance_charge_predictor.predict(df).tolist()
|
64 |
+
#print(prediction)
|
65 |
+
|
66 |
+
|
67 |
+
# While the prediction is made, log both the inputs and outputs to a log file
|
68 |
+
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
|
69 |
+
# access
|
70 |
+
|
71 |
+
with scheduler.lock:
|
72 |
+
with log_file.open("a") as f:
|
73 |
+
f.write(json.dumps(
|
74 |
+
{
|
75 |
+
'age': age,
|
76 |
+
'bmi': bmi,
|
77 |
+
'children': children,
|
78 |
+
'sex': sex,
|
79 |
+
'smoker': smoker,
|
80 |
+
'region': region,
|
81 |
+
'prediction': 123
|
82 |
+
}
|
83 |
+
))
|
84 |
+
f.write("\n")
|
85 |
+
#round prediction[0] to 2 decimal
|
86 |
+
return round(prediction[0], 2)
|
87 |
+
|
88 |
+
|
89 |
+
# Create the gradio interface, make title "HealthyLife Insurance Charge Prediction"
|
90 |
+
demo = gr.Interface(fn=predict_insurance_charge,
|
91 |
+
inputs=[age, bmi, children, sex, smoker, region],
|
92 |
+
outputs=model_output,
|
93 |
+
title="HealthyLife Insurance Charge Prediction",
|
94 |
+
description="This API allows you uo predict insurance charge",
|
95 |
+
flagging_mode="auto",
|
96 |
+
concurrency_limit=8)
|
97 |
+
|
98 |
+
# Launch with a load balancer
|
99 |
+
demo.queue()
|
100 |
+
demo.launch(share=True, debug=True)
|
model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ca19cf08ff09c79c763cb148aadf97192aa4212459bc5b0e71a983a2e1f3e44b
|
3 |
+
size 4030
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
scikit-learn==1.5.2
|
2 |
+
numpy==1.26.4
|
3 |
+
pandas==2.2.2
|