| |
| import os |
| import uuid |
| import joblib |
| import json |
| import time |
| import gradio as gr |
| import pandas as pd |
|
|
| from huggingface_hub import HfApi |
| from huggingface_hub import CommitScheduler |
| from pathlib import Path |
| from sklearn.model_selection import train_test_split |
| from sklearn.pipeline import Pipeline |
| from sklearn.compose import ColumnTransformer |
| from sklearn.preprocessing import OneHotEncoder |
| from datetime import datetime |
|
|
| |
| hf_token = os.getenv("HF_TOKEN") |
|
|
| |
| api = HfApi() |
| |
| |
| |
| os.system("python train.py") |
| |
| 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, |
| token=hf_token |
| ) |
|
|
| |
| |
| def insurance_charge_predictor(age, bmi, children, sex, smoker, region): |
| input_data = pd.DataFrame({ |
| 'age': [age], |
| 'bmi': [bmi], |
| 'children': [children], |
| 'sex': [sex], |
| 'smoker': [smoker], |
| 'region': [region] |
| }) |
|
|
| prediction = model.predict(input_data) |
| |
| |
| |
| 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] |
|
|
| |
| |
| with gr.Blocks() as demo: |
| gr.Markdown("HealthyLife Insurance Charge Prediction") |
| status = gr.State(["none"]) |
| with gr.Row(): |
| with gr.Column(): |
| age = gr.Slider(minimum=18, maximum=100, value=25, label="Age", step=1) |
| bmi = gr.Slider(minimum=10, maximum=50, value=25, label="BMI") |
| children = gr.Slider(minimum=0, maximum=10, value=0, label="Children", step=1) |
| sex = gr.Radio(choices=["male", "female"], label="Sex") |
| smoker = gr.Radio(choices=["yes", "no"], label="Smoker") |
| region = gr.Dropdown(choices=["northeast", "northwest", "southeast", "southwest"], label="Region") |
|
|
| with gr.Column(): |
| output = gr.Number(label="Predicted Insurance Charge") |
| predict_btn = gr.Button("Submit") |
|
|
| predict_btn.click(insurance_charge_predictor, inputs=[age, bmi, children, sex, smoker, region], outputs=output) |
|
|
| |
| demo.queue() |
| demo.launch(share=True, show_error=True) |
|
|