Upload 3 files
Browse filesFirst version of the prediction model.
- app.py +88 -0
- model.joblib +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
import joblib
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
from huggingface_hub import CommitScheduler
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Preparing the logging functionality
|
| 15 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
| 16 |
+
log_folder = log_file.parent
|
| 17 |
+
|
| 18 |
+
scheduler = CommitScheduler(
|
| 19 |
+
repo_id="insurance-charge-mlops-logs",
|
| 20 |
+
repo_type="dataset",
|
| 21 |
+
folder_path=log_folder,
|
| 22 |
+
path_in_repo="data",
|
| 23 |
+
every=2
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
charges_predictor = joblib.load('model.joblib')
|
| 27 |
+
|
| 28 |
+
age_input = gr.Number(label='Age')
|
| 29 |
+
bmi_input = gr.Number(label='BMI')
|
| 30 |
+
children_input = gr.Number(label='Children')
|
| 31 |
+
sex_input = gr.Dropdown(['male', 'female', 'N/A'], value='N/A', label='Sex')
|
| 32 |
+
smoker_input = gr.Dropdown(['yes', 'no', 'N/A'], value='N/A', label="Smoker")
|
| 33 |
+
region_input = gr.Dropdown(['southeast', 'southwest', 'northeast', 'northwest', 'N/A'],
|
| 34 |
+
value='N/A', label='Region')
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
model_output = gr.Label(label='Charges')
|
| 38 |
+
|
| 39 |
+
# the functions runs when 'Submit' is clicked or when a API request is made
|
| 40 |
+
def predict_charges(age, bmi, children, sex, smoker, region, prediction):
|
| 41 |
+
sample = {
|
| 42 |
+
'Age': age,
|
| 43 |
+
'BMI': bmi,
|
| 44 |
+
'Children': children,
|
| 45 |
+
'Sex': sex,
|
| 46 |
+
'Smoker': smoker,
|
| 47 |
+
'Region': region,
|
| 48 |
+
'Prediction': prediction
|
| 49 |
+
}
|
| 50 |
+
data_point = pd.DataFrame([sample])
|
| 51 |
+
print('data point: ', data_point)
|
| 52 |
+
prediction = charges_predictor.predict(data_point).tolist()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
with scheduler.lock:
|
| 56 |
+
with log_file.open("a") as f:
|
| 57 |
+
f.write(json.dumps(
|
| 58 |
+
{
|
| 59 |
+
'Region': region,
|
| 60 |
+
'Smoker': smoker,
|
| 61 |
+
'BMI': bmi,
|
| 62 |
+
'Children': children,
|
| 63 |
+
'Sex': sex,
|
| 64 |
+
'Smoker': smoker,
|
| 65 |
+
'Age': age,
|
| 66 |
+
'Prediction': prediction[0]
|
| 67 |
+
}
|
| 68 |
+
))
|
| 69 |
+
f.write("\n")
|
| 70 |
+
|
| 71 |
+
return prediction[0]
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Setting up UI components for input and output
|
| 75 |
+
demo = gr.Interface(
|
| 76 |
+
fn=predict_charges,
|
| 77 |
+
inputs=[region_input, smoker_input, bmi_input,
|
| 78 |
+
children_input, sex_input, age_input],
|
| 79 |
+
outputs=model_output,
|
| 80 |
+
title="HealthyLife Insurance Charge Prediction",
|
| 81 |
+
description="This API allows you to predict the appropiate charges for each patient",
|
| 82 |
+
allow_flagging="auto",
|
| 83 |
+
concurrency_limit=8
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Launch with a load balancer
|
| 87 |
+
demo.queue()
|
| 88 |
+
demo.launch(share=False)
|
model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ecff8e1c811de2d1d5ffff71183e2568f008dbde9c603480e0ae420aeff81838
|
| 3 |
+
size 4113
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn==1.6.1
|
| 2 |
+
joblib==1.4.2
|
| 3 |
+
numpy==2.2.1
|
| 4 |
+
gradio==5.11.0
|