|
|
|
|
|
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 |
|
|
|
|
|
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-logs", |
|
repo_type="dataset", |
|
folder_path=log_folder, |
|
path_in_repo="data", |
|
every=2 |
|
) |
|
|
|
|
|
|
|
age_input = gr.Slider(minimum=18, maximum=64, step=1, label='Age') |
|
sex_input = gr.Dropdown(['female','male'], label='Sex') |
|
bmi_input = gr.Slider(minimum=15, maximum=50, step=1, label='BMI') |
|
|
|
smoker_input = gr.Dropdown(['no','yes'], label='Smoker') |
|
region_input = gr.Dropdown(['northeast', 'northwest', 'southeast', 'southwest'], label='Region') |
|
|
|
|
|
model_output = gr.Label(label='Insurance Charge Prediction') |
|
|
|
|
|
|
|
def predict_insurance_charges(age, sex, bmi, smoker, region): |
|
|
|
|
|
sample = { |
|
'age': age, |
|
'sex': sex, |
|
'bmi': bmi, |
|
'smoker': smoker, |
|
'region': region |
|
} |
|
data_point = pd.DataFrame([sample]) |
|
|
|
prediction = model.predict(data_point).tolist() |
|
|
|
with scheduler.lock: |
|
with log_file.open("a") as f: |
|
f.write(json.dumps( |
|
{ |
|
'age': age, |
|
'sex': sex, |
|
'bmi': bmi, |
|
'smoker': smoker, |
|
'region': region, |
|
'prediction': prediction[0] |
|
} |
|
)) |
|
f.write("\n") |
|
|
|
return prediction[0] |
|
|
|
|
|
demo = gr.Interface( |
|
fn=predict_insurance_charges, |
|
inputs=[age_input, sex_input, bmi_input, smoker_input, region_input], |
|
outputs=model_output, |
|
title="HealthyLife Insurance Charge Prediction", |
|
description="This API allows you to predict the... ", |
|
allow_flagging="auto", |
|
concurrency_limit=8 |
|
) |
|
|
|
|
|
demo.queue() |
|
demo.launch(share=False) |
|
|