File size: 2,608 Bytes
e4a95ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle
import gradio as gr
import pandas as pd
import statsmodels.api as sm

# Load the model from the file
with open('linear_regression_model_encoded.pkl', 'rb') as file:
    loaded_model = pickle.load(file)
# The model is now loaded and ready to use

train_encoded_columns = [
    'age', 'bmi', 'bloodpressure', 'children',
    'gender_male',  
    'diabetic_Yes', 
    'smoker_Yes',  
    'region_northwest', 'region_southeast', 'region_southwest'
]

# Define the function that will use the model to predict
def predict(age, bmi, bloodpressure,\
            children, gender, diabetic, smoker, region):
    # Create a DataFrame for the input data
    input_data = pd.DataFrame({
        'age': [age],
        'bmi': [bmi],
        'bloodpressure': [bloodpressure],
        'children': [children],
        'gender': [gender],
        'diabetic': [diabetic],
        'smoker': [smoker],
        'region': [region]
    })

    # One-hot encode the input data
    input_data_encoded = pd.get_dummies(input_data)

    # Add missing columns as zeros and align the order of columns
    for column in train_encoded_columns:
        if column not in input_data_encoded.columns:
            input_data_encoded[column] = 0
    input_data_encoded = input_data_encoded[train_encoded_columns]

    # Add a constant term if your model expects an intercept
    input_data_encoded = sm.add_constant(input_data_encoded, has_constant='add')

    # Make a prediction using the loaded model
    prediction = loaded_model.predict(input_data_encoded)
    return prediction[0]

# Define the dropdown options based on the training data categories
gender_options = ['male', 'female']
diabetic_options = ['Yes', 'No']
smoker_options = ['Yes', 'No']
region_options = ['southwest', 'southeast', 'northwest', 'northeast']

# Create the Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=[
        gr.Number(label="Age"),
        gr.Number(label="BMI"),
        gr.Number(label="Blood Pressure"),
        gr.Number(label="Children"),
        gr.Dropdown(choices=gender_options, label="Gender", value='male'),
        gr.Dropdown(choices=diabetic_options, label="Diabetic", value='Yes'),
        gr.Dropdown(choices=smoker_options, label="Smoker", value='Yes'),
        gr.Dropdown(choices=region_options, label="Region", value='northwest')
    ],
     outputs=gr.Textbox(label="Predicted Claim"),
     title="Medical Claim Prediction",
     description="Enter Age, BMI, and Blood Pressure to predict the medical claim",
     allow_flagging='never')  # Set flagging to 'never'

# Launch the interface
iface.launch()