Update app.py
#3
by mennabar4a8 - opened
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import os
|
|
| 3 |
import uuid
|
| 4 |
import joblib
|
| 5 |
import json
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
import pandas as pd
|
| 9 |
|
|
@@ -11,7 +10,6 @@ from huggingface_hub import CommitScheduler
|
|
| 11 |
from pathlib import Path
|
| 12 |
|
| 13 |
|
| 14 |
-
# Preparing the logging functionality
|
| 15 |
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
| 16 |
log_folder = log_file.parent
|
| 17 |
|
|
@@ -21,67 +19,87 @@ scheduler = CommitScheduler(
|
|
| 21 |
folder_path=log_folder,
|
| 22 |
path_in_repo="data",
|
| 23 |
every=2
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
-
model_output = gr.
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
# The function runs when 'Submit' is clicked or when a API request is made
|
| 40 |
-
def predict_charges(age, bmi, children, sex, smoker, region, prediction):
|
| 41 |
sample = {
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
'prediction': prediction
|
| 49 |
}
|
|
|
|
| 50 |
data_point = pd.DataFrame([sample])
|
| 51 |
-
print('data point: ', data_point)
|
| 52 |
-
prediction = charges_predictor.predict(data_point).tolist()
|
| 53 |
|
|
|
|
| 54 |
|
| 55 |
with scheduler.lock:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Setting up UI components for input and output
|
| 74 |
demo = gr.Interface(
|
| 75 |
fn=predict_charges,
|
| 76 |
-
inputs=[
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
outputs=model_output,
|
| 79 |
title="HealthyLife Insurance Charge Prediction",
|
| 80 |
-
description="
|
| 81 |
flagging_mode="manual",
|
| 82 |
concurrency_limit=8
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
| 86 |
demo.queue()
|
| 87 |
-
demo.launch(
|
|
|
|
| 3 |
import uuid
|
| 4 |
import joblib
|
| 5 |
import json
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import pandas as pd
|
| 8 |
|
|
|
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
|
|
|
|
| 13 |
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
| 14 |
log_folder = log_file.parent
|
| 15 |
|
|
|
|
| 19 |
folder_path=log_folder,
|
| 20 |
path_in_repo="data",
|
| 21 |
every=2
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
charges_predictor = joblib.load("model.joblib")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Inputs
|
| 28 |
|
| 29 |
+
age_input = gr.Number(label="Age", value=25)
|
| 30 |
+
bmi_input = gr.Number(label="BMI", value=25)
|
| 31 |
+
children_input = gr.Number(label="Children", value=0)
|
| 32 |
|
| 33 |
+
sex_input = gr.Dropdown(
|
| 34 |
+
["male", "female"],
|
| 35 |
+
value="male",
|
| 36 |
+
label="Sex"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
smoker_input = gr.Dropdown(
|
| 40 |
+
["yes", "no"],
|
| 41 |
+
value="no",
|
| 42 |
+
label="Smoker"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
region_input = gr.Dropdown(
|
| 46 |
+
["southeast", "southwest", "northeast", "northwest"],
|
| 47 |
+
value="southeast",
|
| 48 |
+
label="Region"
|
| 49 |
+
)
|
| 50 |
|
| 51 |
|
| 52 |
+
model_output = gr.Textbox(label="Predicted Insurance Cost")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def predict_charges(age, bmi, children, sex, smoker, region):
|
| 56 |
|
|
|
|
|
|
|
| 57 |
sample = {
|
| 58 |
+
"age": age,
|
| 59 |
+
"bmi": bmi,
|
| 60 |
+
"children": children,
|
| 61 |
+
"sex": sex,
|
| 62 |
+
"smoker": smoker,
|
| 63 |
+
"region": region
|
|
|
|
| 64 |
}
|
| 65 |
+
|
| 66 |
data_point = pd.DataFrame([sample])
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
prediction = charges_predictor.predict(data_point).tolist()
|
| 69 |
|
| 70 |
with scheduler.lock:
|
| 71 |
+
with log_file.open("a") as f:
|
| 72 |
+
f.write(json.dumps({
|
| 73 |
+
"age": age,
|
| 74 |
+
"bmi": bmi,
|
| 75 |
+
"children": children,
|
| 76 |
+
"sex": sex,
|
| 77 |
+
"smoker": smoker,
|
| 78 |
+
"region": region,
|
| 79 |
+
"prediction": prediction[0]
|
| 80 |
+
}))
|
| 81 |
+
f.write("\n")
|
| 82 |
+
|
| 83 |
+
return f"${prediction[0]:,.2f}"
|
| 84 |
+
|
| 85 |
+
|
|
|
|
|
|
|
|
|
|
| 86 |
demo = gr.Interface(
|
| 87 |
fn=predict_charges,
|
| 88 |
+
inputs=[
|
| 89 |
+
age_input,
|
| 90 |
+
bmi_input,
|
| 91 |
+
children_input,
|
| 92 |
+
sex_input,
|
| 93 |
+
smoker_input,
|
| 94 |
+
region_input
|
| 95 |
+
],
|
| 96 |
outputs=model_output,
|
| 97 |
title="HealthyLife Insurance Charge Prediction",
|
| 98 |
+
description="Predict the insurance medical charges based on patient information",
|
| 99 |
flagging_mode="manual",
|
| 100 |
concurrency_limit=8
|
| 101 |
)
|
| 102 |
|
| 103 |
+
|
| 104 |
demo.queue()
|
| 105 |
+
demo.launch()
|