import gradio as gr import pandas as pd import joblib import numpy as np # Load the trained model model = joblib.load('obesity_prediction_model.pkl') # Define the obesity level descriptions OBESITY_DESCRIPTIONS = { 'Insufficient_Weight': 'Underweight (BMI < 18.5)', 'Normal_Weight': 'Normal Weight (BMI 18.5 - 24.9)', 'Overweight_Level_I': 'Overweight Level I (BMI 25.0 - 27.4)', 'Overweight_Level_II': 'Overweight Level II (BMI 27.5 - 29.9)', 'Obesity_Type_I': 'Obesity Type I (BMI 30.0 - 34.9)', 'Obesity_Type_II': 'Obesity Type II (BMI 35.0 - 39.9)', 'Obesity_Type_III': 'Obesity Type III (BMI >= 40)' } def predict_obesity( gender: str, age: float, family_history: str, favc: str, fcvc: float, ncp: float, caec: str, smoke: str, ch2o: float, scc: str, faf: float, tue: float, calc: str, mtrans: str ) -> str: """ Predict obesity level based on input features. Returns: str: Predicted obesity level with description """ # Create input dataframe input_data = pd.DataFrame({ 'Gender': [gender], 'Age': [age], 'family_history_with_overweight': [family_history], 'FAVC': [favc], 'FCVC': [fcvc], 'NCP': [ncp], 'CAEC': [caec], 'SMOKE': [smoke], 'CH2O': [ch2o], 'SCC': [scc], 'FAF': [faf], 'TUE': [tue], 'CALC': [calc], 'MTRANS': [mtrans] }) # Make prediction prediction = model.predict(input_data)[0] probabilities = model.predict_proba(input_data)[0] confidence = np.max(probabilities) * 100 # Get description description = OBESITY_DESCRIPTIONS.get(prediction, prediction) # Format result result = f"**Predicted Obesity Level:** {prediction}\n\n" result += f"**Description:** {description}\n\n" result += f"**Confidence:** {confidence:.1f}%" return result # Create Gradio interface with gr.Blocks(title="Obesity Level Prediction", theme=gr.themes.Soft()) as demo: gr.Markdown( """ # Obesity Level Prediction ### Based on Eating Habits and Lifestyle Factors This application predicts obesity levels using machine learning based on your eating habits, physical activity, and lifestyle information. --- """ ) with gr.Row(): with gr.Column(): gender = gr.Dropdown( choices=["Female", "Male"], label="Gender", value="Female" ) age = gr.Number( label="Age", value=25, minimum=10, maximum=100 ) family_history = gr.Dropdown( choices=["yes", "no"], label="Family history with overweight", value="yes" ) favc = gr.Dropdown( choices=["yes", "no"], label="Frequent consumption of high caloric food (FAVC)", value="no" ) fcvc = gr.Slider( minimum=1, maximum=3, step=0.1, label="Frequency of consumption of vegetables (FCVC) [1-3]", value=2 ) ncp = gr.Slider( minimum=1, maximum=4, step=0.1, label="Number of main meals (NCP) [1-4]", value=3 ) caec = gr.Dropdown( choices=["no", "Sometimes", "Frequently", "Always"], label="Consumption of food between meals (CAEC)", value="Sometimes" ) with gr.Column(): smoke = gr.Dropdown( choices=["yes", "no"], label="Smoking habit (SMOKE)", value="no" ) ch2o = gr.Slider( minimum=1, maximum=3, step=0.1, label="Consumption of water daily (CH2O) [1-3 liters]", value=2 ) scc = gr.Dropdown( choices=["yes", "no"], label="Calories consumption monitoring (SCC)", value="no" ) faf = gr.Slider( minimum=0, maximum=3, step=0.1, label="Physical activity frequency (FAF) [0-3 days/week]", value=1 ) tue = gr.Slider( minimum=0, maximum=2, step=0.1, label="Time using technology devices (TUE) [0-2 hours]", value=1 ) calc = gr.Dropdown( choices=["no", "Sometimes", "Frequently", "Always"], label="Consumption of alcohol (CALC)", value="Sometimes" ) mtrans = gr.Dropdown( choices=["Automobile", "Motorbike", "Bike", "Public_Transportation", "Walking"], label="Transportation used (MTRANS)", value="Public_Transportation" ) predict_btn = gr.Button("Predict Obesity Level", variant="primary") output = gr.Markdown(label="Prediction Result") predict_btn.click( fn=predict_obesity, inputs=[ gender, age, family_history, favc, fcvc, ncp, caec, smoke, ch2o, scc, faf, tue, calc, mtrans ], outputs=output ) gr.Markdown( """ --- ### Obesity Level Categories: - **Insufficient Weight:** BMI < 18.5 - **Normal Weight:** BMI 18.5 - 24.9 - **Overweight Level I:** BMI 25.0 - 27.4 - **Overweight Level II:** BMI 27.5 - 29.9 - **Obesity Type I:** BMI 30.0 - 34.9 - **Obesity Type II:** BMI 35.0 - 39.9 - **Obesity Type III:** BMI >= 40 --- *CSC14119 - Introduction to Data Science - DIY 2* """ ) # Launch the app if __name__ == "__main__": demo.launch()