Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import pandas as pd | |
| from joblib import load | |
| import gradio as gr | |
| from tensorflow.keras.models import load_model | |
| # filepaths | |
| column_transform_path = "assets/column_transformer.joblib" # change during the creation of the huggingface app | |
| model_path = "assets/insurance_regression_model.keras" # change during the creation of the huggingface app | |
| # instance the models | |
| column_transformer = load(column_transform_path) | |
| insurance_model = load_model(model_path) | |
| # short description of the stuff | |
| title = "Insurance Regressor Model" | |
| description = "POC of a deep learning regression model for estimating insurance charges based on 11 input variables." | |
| article = "The model was implemented in tensorflow. It is simple as i made it as a refresher for reviewing the Tensorflow fundamentals (the problem most likely could be solved using classical regression). The dataset used was [Medical Cost Personal Datasets](https://www.kaggle.com/datasets/mirichoi0218/insurance)." | |
| # processing function | |
| def insurance_prediction(age,gender,bmi,children,smoker,region): | |
| columns = ['age', 'sex', 'bmi', 'children', 'smoker', 'region'] | |
| # transform some data to the datatypes compatible with column transfomer | |
| age = int(age) | |
| gender = "male" if gender else "female" | |
| children = int(children) | |
| smoker = "yes" if smoker else "no" | |
| region = region[0] if len(region) > 0 else 'northeast' # since it is a proof of concept, i left the value there. | |
| # arange the values into a list | |
| values = [age,gender,bmi,children,smoker,region] | |
| # create a dictionary to structure the sample at hand | |
| sample = {k:v for k,v in zip(columns,values)} | |
| # create a dataframe using the sample | |
| X = pd.DataFrame(data = pd.Series(sample)).T | |
| # preprocess the dataframe | |
| X_preprocessed = column_transformer.transform(X) | |
| # make the prediction accordingly | |
| y_pred = insurance_model.predict(X_preprocessed) | |
| return y_pred[0][0] | |
| demo = gr.Interface( | |
| fn = insurance_prediction, | |
| inputs = [ | |
| gr.Number(value = 40,label = "Age",show_label=True), | |
| gr.Checkbox(value = False,label = "Male",show_label = True), | |
| gr.Number(value = 30,label = "Body Mass Index",show_label=True), | |
| gr.Number(value = 1,label = "Number of Children",show_label=True), | |
| gr.Checkbox(value = False,label = "Smoker",show_label = True), | |
| gr.CheckboxGroup(choices = ['northeast', 'northwest', 'southeast', 'southwest'], | |
| value = ['northeast', 'northwest', 'southeast', 'southwest'], | |
| label="Region", | |
| info="Will only take into consideration the fist one selected.") | |
| ], | |
| outputs = [ | |
| gr.Number(label = "Insurance Charges Estimate (American Dollars)") | |
| ], | |
| title = title, | |
| description = description, | |
| article = article | |
| ) | |
| demo.launch() | |