|
|
|
import os |
|
import uuid |
|
import joblib |
|
import json |
|
|
|
import gradio as gr |
|
import pandas as pd |
|
|
|
from huggingface_hub import CommitScheduler |
|
from pathlib import Path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exec(open("train.py").read()) |
|
|
|
|
|
|
|
insurance_model = joblib.load("model.joblib") |
|
|
|
|
|
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json" |
|
log_folder = log_file.parent |
|
|
|
scheduler = CommitScheduler( |
|
repo_id="insurance-charge-mlops-logs", |
|
repo_type="dataset", |
|
folder_path=log_folder, |
|
path_in_repo="data", |
|
every=2 |
|
) |
|
|
|
|
|
|
|
def predict(age, bmi, children, sex, smoker, region): |
|
|
|
input = { |
|
"age": age, |
|
"bmi": bmi, |
|
"children": children, |
|
"sex": sex, |
|
"smoker": smoker, |
|
"region": region |
|
} |
|
|
|
input_df = pd.DataFrame([input]) |
|
prediction = insurance_model.predict(input_df).to_list() |
|
|
|
|
|
|
|
|
|
|
|
|
|
with scheduler.lock: |
|
with log_file.open("a") as f: |
|
f.write(json.dumps( |
|
{ |
|
'age': age, |
|
'bmi': bmi, |
|
'children': children, |
|
'sex': sex, |
|
'smoker': smoker, |
|
'region': region, |
|
'prediction': prediction[0] |
|
} |
|
)) |
|
f.write("\n") |
|
|
|
return prediction[0] |
|
|
|
|
|
|
|
|
|
age_input = gr.Number(label="Age") |
|
bmi_input = gr.Number(label="BMI") |
|
children_input = gr.Slider(minimum=0.0, maximum=15.0, step=1.0, label="Children") |
|
sex_input = gr.Dropdown(['male', 'female'], label="sex") |
|
smoker_input = gr.Checkbox(['no', 'yes'], label="smoker") |
|
region_input = gr.Number(['southwest', 'southeast', 'northwest', 'northeast'], label="region") |
|
output = gr.Label(label="Insurance Price") |
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn=predict, |
|
inputs=[age_input, bmi_input, children_input, sex_input, smoker_input, region_input], |
|
outputs=output, |
|
title="HealthyLife Insurance Charge Prediction", |
|
description="This API allows you to predict insurance prices for HealthlyLife Insurance", |
|
allow_flagging="auto", |
|
concurrency_limit=8 |
|
) |
|
|
|
|
|
|
|
demo.queue() |
|
demo.launch(share=False) |
|
|