File size: 4,053 Bytes
6578475
 
 
 
d5a1113
 
 
6578475
295b650
6578475
 
9f099b2
d5a1113
 
9f099b2
d5a1113
 
9f099b2
3b56bf5
 
 
 
9f099b2
6578475
 
7452cc5
e936faa
7204842
 
d5a1113
590ac08
3a97200
1526a76
590ac08
 
8fdd81b
262055b
3a97200
 
d5a1113
4b6984e
9f099b2
6578475
 
cd90067
6578475
 
c8f04b8
6578475
d088695
 
 
 
c8f04b8
d088695
76d31a2
3b56bf5
 
 
011f686
d5a1113
 
 
a944881
ddb974f
a944881
 
 
 
2309038
c09f9a9
 
e280162
a944881
2545e8c
a944881
 
6578475
 
40bcda5
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import warnings
warnings.simplefilter("ignore")
import pandas as pd
import numpy as np
from sklearn.compose import ColumnTransformer
from xgboost import XGBClassifier
from sklearn.preprocessing import StandardScaler, OneHotEncoder
import joblib
import xgboost as xgb
import gradio as gr


# Load the preprocessor
preprocessor = joblib.load('preprocessor.pkl')

# Load the XGBoost model
model = joblib.load('new_xgb_model.pkl')

num_features = ["Age", "Height", "Weight"]
cat_columns = ['Gender', 'CALC', 'FAVC', 'FCVC', 'NCP',
       'SCC', 'SMOKE', 'CH2O', 'family_history_with_overweight', 'FAF', 'TUE',
       'CAEC', 'MTRANS']

# Define the Gradio input and output interfaces
inputs = [
    gr.Number(label='What is your Age'),
    gr.Dropdown(choices=["Female", "Male"], label="Gender"),
    gr.Slider(label='What is your Height', minimum = 1.45, maximum = 1.98),
    gr.Slider(label='What is your Weight', minimum = 39, maximum = 175,),
    gr.Dropdown(choices=["no", "Sometimes", "Frequently", "Always"], label="How often do you drink alcohol?"),
    gr.Dropdown(choices=["no", "yes"], label=" Do you eat high caloric food frequently?"),
    gr.Dropdown(choices=["Never", "Sometimes", "Always"], label="How often do you eat vegetables in your meals?"),
    gr.Slider(label="How many main meals do you have daily?", minimum =1, maximum = 4, step =1),
    gr.Dropdown(choices=["no", "yes"], label=" Do you monitor the calories you eat daily?"),
    gr.Dropdown(choices=["no", "yes"], label="Do you smoke?"),
    gr.Dropdown(choices=["1 Litre", "Between 1 and 2Litres", "More than 2Litres"], label = "How much Litres of water do you drink daily on average?"),
    gr.Dropdown(choices=["no", "yes"], label="Has a family member suffered or suffers from overweight?"),
    gr.Dropdown(choices=["Never", "1-2 days", "3-4 days", "More than 4 days"], label="How often do you have physical activity weekly? "),
    gr.Dropdown(choices=["0-2 hours", "3-5 hours", "More than 5 hours"], label=" How much time do you use technological devices such as cell phone, videogames, television, computer and others?"),
    gr.Dropdown(choices=["no", "Sometimes", "Frequently", "Always"], label="Do you eat any food between meals?"),
    gr.Dropdown(choices=["Bike", "Motorbike","Automobile", "Public_Transportation", "Walking"], label="Mode of transportation used"),
        
]

output = gr.Label(label="Predicted Label")

# Define the predict function
def predict(Age, Gender, Height, Weight, CALC, FAVC, FCVC, NCP, SCC, SMOKE, CH2O, family_history_with_overweight, FAF, TUE, CAEC, MTRANS):
    # Create a dataframe with the input values
    input_dict = {'Age': Age, 'Gender': Gender, 'Height': Height, 'Weight': Weight,
                  'CALC': CALC, 'FAVC': FAVC, 'FCVC': FCVC, 'NCP': NCP,
                  'SCC': SCC, 'SMOKE': SMOKE, 'CH2O': CH2O, 
                  'family_history_with_overweight': family_history_with_overweight,
                  'FAF': FAF, 'TUE': TUE, 'CAEC': CAEC, 'MTRANS': MTRANS}
                    
    input_df = pd.DataFrame.from_dict([input_dict])
    input_df[num_features] = input_df[num_features].astype("int")
    input_df[cat_columns] = input_df[cat_columns].astype("object")
    
    
    preprocessed_data = preprocessor.transform(input_df)
    
    
    # Make predictions
    predictions = model.predict(preprocessed_data)
    
    predictions = int(predictions[0])

    # Map class index to class label
    class_labels = ["Insufficient_Weight", "Normal_Weight", "Obesity_Type_I", 
                    "Obesity_Type_II", "Obesity_Type_III", "Overweight_Level_I", 
                    "Overweight_Level_II"]
    
    predicted_label = class_labels[predictions]

    # Return the predicted label
    return predicted_label

interface = gr.Interface(fn=predict, inputs=inputs, outputs=output, 
                     title='Predicting Obesity', description='Predicting Obesity using XGBoost Classifier.\nPlease Note:\nFemale = 0, Male= 1\nNo = 0, Yes = 1',
                        theme='darkhuggingface')
interface.launch()