coeuslearning commited on
Commit
e4a95ec
·
1 Parent(s): 4832132

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import statsmodels.api as sm
5
+
6
+ # Load the model from the file
7
+ with open('linear_regression_model_encoded.pkl', 'rb') as file:
8
+ loaded_model = pickle.load(file)
9
+ # The model is now loaded and ready to use
10
+
11
+ train_encoded_columns = [
12
+ 'age', 'bmi', 'bloodpressure', 'children',
13
+ 'gender_male',
14
+ 'diabetic_Yes',
15
+ 'smoker_Yes',
16
+ 'region_northwest', 'region_southeast', 'region_southwest'
17
+ ]
18
+
19
+ # Define the function that will use the model to predict
20
+ def predict(age, bmi, bloodpressure,\
21
+ children, gender, diabetic, smoker, region):
22
+ # Create a DataFrame for the input data
23
+ input_data = pd.DataFrame({
24
+ 'age': [age],
25
+ 'bmi': [bmi],
26
+ 'bloodpressure': [bloodpressure],
27
+ 'children': [children],
28
+ 'gender': [gender],
29
+ 'diabetic': [diabetic],
30
+ 'smoker': [smoker],
31
+ 'region': [region]
32
+ })
33
+
34
+ # One-hot encode the input data
35
+ input_data_encoded = pd.get_dummies(input_data)
36
+
37
+ # Add missing columns as zeros and align the order of columns
38
+ for column in train_encoded_columns:
39
+ if column not in input_data_encoded.columns:
40
+ input_data_encoded[column] = 0
41
+ input_data_encoded = input_data_encoded[train_encoded_columns]
42
+
43
+ # Add a constant term if your model expects an intercept
44
+ input_data_encoded = sm.add_constant(input_data_encoded, has_constant='add')
45
+
46
+ # Make a prediction using the loaded model
47
+ prediction = loaded_model.predict(input_data_encoded)
48
+ return prediction[0]
49
+
50
+ # Define the dropdown options based on the training data categories
51
+ gender_options = ['male', 'female']
52
+ diabetic_options = ['Yes', 'No']
53
+ smoker_options = ['Yes', 'No']
54
+ region_options = ['southwest', 'southeast', 'northwest', 'northeast']
55
+
56
+ # Create the Gradio interface
57
+ iface = gr.Interface(
58
+ fn=predict,
59
+ inputs=[
60
+ gr.Number(label="Age"),
61
+ gr.Number(label="BMI"),
62
+ gr.Number(label="Blood Pressure"),
63
+ gr.Number(label="Children"),
64
+ gr.Dropdown(choices=gender_options, label="Gender", value='male'),
65
+ gr.Dropdown(choices=diabetic_options, label="Diabetic", value='Yes'),
66
+ gr.Dropdown(choices=smoker_options, label="Smoker", value='Yes'),
67
+ gr.Dropdown(choices=region_options, label="Region", value='northwest')
68
+ ],
69
+ outputs=gr.Textbox(label="Predicted Claim"),
70
+ title="Medical Claim Prediction",
71
+ description="Enter Age, BMI, and Blood Pressure to predict the medical claim",
72
+ allow_flagging='never') # Set flagging to 'never'
73
+
74
+ # Launch the interface
75
+ iface.launch()