Spaces:
No application file
No application file
Eduardo577
commited on
Commit
•
2ef63aa
1
Parent(s):
1f99bab
Delete app.py
Browse files
app.py
DELETED
@@ -1,112 +0,0 @@
|
|
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 |
-
|
14 |
-
|
15 |
-
# Run the training script placed in the same directory as app.py
|
16 |
-
# The training script will train and persist a linear regression
|
17 |
-
# model with the filename 'model.joblib'
|
18 |
-
import train
|
19 |
-
train
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# Load the freshly trained model from disk
|
25 |
-
saved_model = joblib.load('model.joblib')
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
# Prepare the logging functionality
|
30 |
-
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
31 |
-
log_folder = log_file.parent
|
32 |
-
|
33 |
-
scheduler = CommitScheduler(
|
34 |
-
repo_id="insurance-charge-mlops-logs", # provide a name "insurance-charge-mlops-logs" for the repo_id
|
35 |
-
repo_type="dataset",
|
36 |
-
folder_path=log_folder,
|
37 |
-
path_in_repo="data",
|
38 |
-
every=2
|
39 |
-
)
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# Define the predict function which will take features, convert to dataframe and make predictions using the saved model
|
44 |
-
# the functions runs when 'Submit' is clicked or when a API request is made
|
45 |
-
|
46 |
-
def predict_charge(age, bmi, children, sex, smoker, region ):
|
47 |
-
sample = {
|
48 |
-
|
49 |
-
'age': age,
|
50 |
-
'bmi': bmi,
|
51 |
-
'children': children,
|
52 |
-
'sex': sex,
|
53 |
-
'smoker': smoker,
|
54 |
-
'region': region,
|
55 |
-
|
56 |
-
}
|
57 |
-
data_point = pd.DataFrame([sample])
|
58 |
-
prediction = saved_model.predict(data_point).tolist()
|
59 |
-
# if prediction is less than zero assign zero
|
60 |
-
if prediction[0] < 0:
|
61 |
-
prediction[0] = 0
|
62 |
-
|
63 |
-
|
64 |
-
# While the prediction is made, log both the inputs and outputs to a log file
|
65 |
-
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
|
66 |
-
# access
|
67 |
-
|
68 |
-
with scheduler.lock:
|
69 |
-
with log_file.open("a") as f:
|
70 |
-
f.write(json.dumps(
|
71 |
-
{
|
72 |
-
'age': age,
|
73 |
-
'bmi': bmi,
|
74 |
-
'children': children,
|
75 |
-
'sex': sex,
|
76 |
-
'smoker': smoker,
|
77 |
-
'region': region,
|
78 |
-
'prediction': prediction[0]
|
79 |
-
}
|
80 |
-
))
|
81 |
-
f.write("\n")
|
82 |
-
|
83 |
-
return (prediction[0])
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# Set up UI components for input and output
|
88 |
-
|
89 |
-
# age = gr.Number(label="age")
|
90 |
-
age = gr.Slider(1, 100, step =1, minimum =1, maximum = 100, label="age", info='Age between 1 y 100')
|
91 |
-
bmi = gr.Number(label="bmi")
|
92 |
-
# children = gr.Number(label='children')
|
93 |
-
children = gr.Slider(label='children', step =1, minimum =0, maximum = 10, info = 'Enter number of children')
|
94 |
-
sex = gr.Dropdown(label='sex', choices=['male', 'female'])
|
95 |
-
smoker = gr.Dropdown(label='smoker', choices=['yes', 'no'])
|
96 |
-
region = gr.Dropdown(label='region', choices =['southwest', 'southeast', 'northwest', 'northeast'])
|
97 |
-
charge = gr.Number(label="Prediction")
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
# Create the gradio interface, make title "HealthyLife Insurance Charge Prediction"
|
102 |
-
demo = gr.Interface(
|
103 |
-
fn = predict_charge,
|
104 |
-
inputs = [age, bmi, children, sex, smoker, region,],
|
105 |
-
outputs = charge,
|
106 |
-
title = 'HealthyLife Insurance Charge Prediction',
|
107 |
-
description = 'Calculate charges')
|
108 |
-
|
109 |
-
|
110 |
-
# Launch with a load balancer
|
111 |
-
demo.queue()
|
112 |
-
demo.launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|