naughtondale's picture
Update app.py
371a43a
raw
history blame contribute delete
No virus
2.58 kB
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']))