|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
import train |
|
train |
|
|
|
|
|
|
|
|
|
|
|
saved_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_charge(age, bmi, children, sex, smoker, region ): |
|
sample = { |
|
|
|
'age': age, |
|
'bmi': bmi, |
|
'children': children, |
|
'sex': sex, |
|
'smoker': smoker, |
|
'region': region, |
|
|
|
} |
|
data_point = pd.DataFrame([sample]) |
|
prediction = saved_model.predict(data_point).tolist() |
|
|
|
if prediction[0] < 0: |
|
prediction[0] = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = gr.Slider(1, 100, step =1, minimum =1, maximum = 100, label="age", info='Age between 1 and 100') |
|
bmi = gr.Number(label="bmi") |
|
|
|
children = gr.Slider(label='children', step =1, minimum =0, maximum = 10, info = 'Enter number of children') |
|
sex = gr.Dropdown(label='sex', choices=['male', 'female']) |
|
smoker = gr.Dropdown(label='smoker', choices=['yes', 'no']) |
|
region = gr.Dropdown(label='region', choices =['southwest', 'southeast', 'northwest', 'northeast']) |
|
charge = gr.Number(label="Prediction") |
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn = predict_charge, |
|
inputs = [age, bmi, children, sex, smoker, region,], |
|
outputs = charge, |
|
title = 'HealthyLife Insurance Charge Prediction', |
|
description = 'Calculate charges') |
|
|
|
|
|
|
|
demo.queue() |
|
demo.launch(share=False) |
|
|