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()