MrinaliniRaj96's picture
Update app.py
9cbe2e4
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()