File size: 2,578 Bytes
347fdb7
 
 
 
 
f6c95a7
347fdb7
658a429
 
 
 
d301534
 
 
 
 
 
 
 
658a429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d301534
 
 
658a429
 
 
 
 
 
cfceef3
 
 
 
 
d02dc6e
3978a9f
d02dc6e
 
658a429
 
8b62b0d
371a43a
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
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import LabelEncoder
import gradio as gr
import numpy as np
import os 

# Load the data
df = pd.read_csv('insurance_data.csv')

# Convert categorical columns to numeric using Label Encoding
le_sex = LabelEncoder()
df['sex'] = le_sex.fit_transform(df['sex'])

le_smoker = LabelEncoder()
df['smoker'] = le_smoker.fit_transform(df['smoker'])

le_region = LabelEncoder()
df['region'] = le_region.fit_transform(df['region'])

# Split the data into features (X) and target (y)
X = df.drop('expenses', axis=1)
y = df['expenses']

# Create and train the model
model = LinearRegression()
model.fit(X, y)

# Define function to categorize expenses
def categorize_expense(expense):
    if expense < 10000:
        return 'low risk'
    elif expense < 30000:
        return 'medium risk'
    else:
        return 'high risk'

# Define prediction function
def predict_risk(age, sex, bmi, children, smoker, region):
    sex = le_sex.transform([sex])[0]  # encode 'sex'
    smoker = le_smoker.transform([smoker])[0]  # encode 'smoker'
    region = le_region.transform([region])[0]  # encode 'region'
    expense = model.predict(np.array([age, sex, bmi, children, smoker, region]).reshape(1, -1))[0]
    return categorize_expense(expense)

# Define Gradio interface
iface = gr.Interface(fn=predict_risk,
                     inputs=['number', 'text', 'number', 'number', 'text', 'text'],
                     outputs='text',
                     theme=gr.themes.Monochrome(
                         primary_hue="blue",
                         secondary_hue="blue",
                         neutral_hue="blue"
                     ),
                     description="This is a model that allows an insurer to automatically predict whether the risk of insuring a customer is 'high', 'medium', or 'low' using a sophisticated machine learning technique. The insurer is not required to know ML linear regression algorithms to use this model. All they have to do is provide the following risk factors the model was trained on:<br> <br>Age<br>Sex (female/male)<br>Body Mass Index (BMI)<br>Children (number)<br>Smoker (yes/no)<br>Region (Southwest/Northwest - adjust depending on the area served)<br>Expenses (annual expenses)<br><br>Go back to: <a href='https://aitechproducts.com/insurance.html'>Insurance Products</a>",
                     examples=None,
                     title="Insurance Risk Predictor")

# Run the interface

iface.launch(auth=(os.environ['USERNAME1'],os.environ['PASSWORD1']))