File size: 2,972 Bytes
deb282a
 
b0088dc
 
 
8c145b2
0076bc3
 
 
 
 
3002deb
0076bc3
 
 
3002deb
0725159
3002deb
0076bc3
 
f9f328c
 
 
 
 
834c5e7
f9f328c
 
834c5e7
 
 
 
 
 
f9f328c
699d7a4
be0c9ca
0076bc3
 
0725159
c74e5a7
04f6faa
 
0076bc3
699d7a4
be0c9ca
f9f328c
3002deb
67d763b
daa1231
1764e7e
 
 
 
 
 
 
 
 
 
67d763b
 
f9f328c
834c5e7
 
 
 
 
 
 
67d763b
3002deb
daa1231
67d763b
f9f328c
3002deb
f9f328c
e2b8531
f401511
834c5e7
da64be6
deb282a
 
3002deb
 
 
 
 
 
 
 
 
daa1231
3002deb
daa1231
 
f9f328c
834c5e7
b0088dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
import pandas as pd
import numpy as np
import joblib

import logging
from pathlib import Path
import uuid

# Create a logs directory if it doesn't exist
Path("logs").mkdir(exist_ok=True)
log_file_path = "logs/predictions.log"

# Configure logging to write to a log file
logging.basicConfig(
    filename=log_file_path,
    level=logging.WARNING,
    # format="%(asctime)s - ID: %(uuid)s - age: %age()s"
)


# Load your model
model = joblib.load("model.joblib")

# Define the function to process inputs
def predict_charges(i_age, i_sex, i_bmi, i_children, i_smoker, i_region):
    # Create a DataFrame with the inputs
    input_data = pd.DataFrame({
        "age": [i_age],
        "sex": [i_sex],
        "bmi": [i_bmi],
        "children": [i_children],
        "smoker": [i_smoker],
        "region": [i_region]
    })
    # Model the data to predict the output. Then 'Return' the output.
    prediction = model.predict(input_data)[0]

    # Log the inputs and the prediction
    logging.warning(
        # f"ID: {uuid.uuid4()} | age: {i_age} | sex: {i_sex} | bmi: {i_bmi} | children: {i_children} | smoker: {i_smoker} | region: {i_region} | prediction: {prediction}"
        # f"{uuid.uuid4()} | {i_age} | {i_sex} | {i_bmi} | {i_children} | {i_smoker} | {i_region} | {prediction}"
        f"{prediction}"
    )
        
    return prediction


# Function to download the log file
def download_log_file():
    return log_file_path
    
# def download_log_file():
#     log_file_path = Path("logs/predictions.log")
#     if log_file_path.exists():
#         return log_file_path
#     else:
#         # If log file doesn't exist, create a placeholder to download
#         log_file_path.write_text("No logs available yet.")
#         return log_file_path

    
# Gradio interface setup
ageIn      = gr.Number(label="Enter Age")
sexIn      = gr.Radio(["female","male"], label="Select Sex")
bmiIn      = gr.Number(label="Enter BMI")
childrenIn = gr.Number(label="Enter Children")
smokerIn   = gr.Radio(["yes", "no"], label="Is Smoker?")
regionIn   = gr.Dropdown(["northeast","northwest","southeast","southwest"], label="Select Region")
chargesOut = gr.Number(label="Predicted Charges")

downloadOut = gr.File(label="Download Prediction Logs")


# Initialize the Gradio interface
iface1 = gr.Interface(
    fn=predict_charges,
    inputs=[ageIn, sexIn, bmiIn, childrenIn, smokerIn, regionIn],
    outputs=chargesOut,
    title="HealthyLife Insurance Charge Prediction",
    description="Enter the details to estimate health insurance charges based on input parameters."
)

# Add a download button for the log file
iface2 = gr.Interface(
    fn=download_log_file,
    inputs=None,
    outputs=downloadOut,
    description="Download the log file of inputs and predictions"
)


# Combine main app and download button in a single interface
iface = gr.TabbedInterface([iface1, iface2], ["Predict Charges", "Download Logs"])


# Launch the app
iface.launch()