Spaces:
Runtime error
Runtime error
Upload 3 files
Browse filesmodel without scheduler
- app.py +97 -0
- model.joblib +3 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import the libraries
|
| 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 |
+
# Prepare the logging functionality
|
| 14 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
| 15 |
+
log_folder = log_file.parent
|
| 16 |
+
|
| 17 |
+
# scheduler = CommitScheduler(
|
| 18 |
+
# repo_id="insurance-charge-logs",
|
| 19 |
+
# repo_type="dataset",
|
| 20 |
+
# folder_path=log_folder,
|
| 21 |
+
# path_in_repo="data",
|
| 22 |
+
# every=2
|
| 23 |
+
# )
|
| 24 |
+
|
| 25 |
+
Load the freshly trained model from disk
|
| 26 |
+
machine_insurance_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(
|
| 32 |
+
['male', 'female'],
|
| 33 |
+
label='Sex'
|
| 34 |
+
)
|
| 35 |
+
smoker_input = gr.Dropdown(
|
| 36 |
+
['yes', 'no'],
|
| 37 |
+
label='Smoker'
|
| 38 |
+
)
|
| 39 |
+
region_input = gr.Dropdown(
|
| 40 |
+
['northeast', 'northwest', 'southeast', 'southwest'],
|
| 41 |
+
label='Region'
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
model_output = gr.Label(label="insurance charge")
|
| 45 |
+
|
| 46 |
+
# Define the predict function which will take features, convert to dataframe and make predictions using the saved model
|
| 47 |
+
# the functions runs when 'Submit' is clicked or when a API request is made
|
| 48 |
+
# While the prediction is made, log both the inputs and outputs to a log file
|
| 49 |
+
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel access
|
| 50 |
+
|
| 51 |
+
def predict_insurance_charge(age, bmi, children, sex, smoker, region):
|
| 52 |
+
sample = {
|
| 53 |
+
'Age': age,
|
| 54 |
+
'BMI': bmi,
|
| 55 |
+
'Children': children,
|
| 56 |
+
'Sex': sex,
|
| 57 |
+
'Smoker': smoker,
|
| 58 |
+
'Region': region,
|
| 59 |
+
}
|
| 60 |
+
data_point = pd.DataFrame([sample])
|
| 61 |
+
prediction = machine_insurance_predictor.predict(data_point).tolist()
|
| 62 |
+
|
| 63 |
+
# with scheduler.lock:
|
| 64 |
+
# with log_file.open("a") as f:
|
| 65 |
+
# f.write(json.dumps(
|
| 66 |
+
# {
|
| 67 |
+
# 'Age': age,
|
| 68 |
+
# 'BMI': bmi,
|
| 69 |
+
# 'Children': children,
|
| 70 |
+
# 'Sex': sex,
|
| 71 |
+
# 'Smoker': smoker,
|
| 72 |
+
# 'Region': region,
|
| 73 |
+
# 'prediction': prediction[0]
|
| 74 |
+
# }
|
| 75 |
+
# ))
|
| 76 |
+
# f.write("\n")
|
| 77 |
+
|
| 78 |
+
return prediction[0]
|
| 79 |
+
|
| 80 |
+
# Create the gradio interface, make title "HealthyLife Insurance Charge Prediction"
|
| 81 |
+
# Set up UI components for input and output
|
| 82 |
+
demo = gr.Interface(
|
| 83 |
+
fn=predict_insurance_charge,
|
| 84 |
+
inputs=[age_input, bmi_input, children_input, sex_input, smoker_input, region_input],
|
| 85 |
+
outputs=model_output,
|
| 86 |
+
title="Insurance Charge Predictor",
|
| 87 |
+
description="This API allows you to predict the companies insurance charges",
|
| 88 |
+
allow_flagging="auto",
|
| 89 |
+
concurrency_limit=8
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Launch with a load balancer
|
| 93 |
+
demo.queue()
|
| 94 |
+
demo.launch(share=False)
|
| 95 |
+
|
| 96 |
+
# Run the training script placed in the same directory as app.py
|
| 97 |
+
# The training script will train and persist a linear regression model with the filename 'model.joblib'
|
model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:52d04e6329819bf9c7b9f7cd109c69142c8b7794b91d25a75b409322d11cf433
|
| 3 |
+
size 4150
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
scikit-learn==1.5.1
|