import gradio as gr import numpy as np import joblib from sklearn.preprocessing import StandardScaler model = joblib.load("final_rf_model.pkl") scaler = StandardScaler() class_labels = { 0: 'Insufficient Weight', 1: 'Normal Weight', 2: 'Overweight Level I', 3: 'Overweight Level II', 4: 'Obesity Type I', 5: 'Obesity Type II', 6: 'Obesity Type III' } # Function to make predictions def predict_obesity(weight, height, age, fcvc, gender, ncp, ch2o, faf, tue, fhwow, caec_sometimes, calc_no, calc_sometimes, caec_frequently, alcohol_choice, mtrans_choice, favc): # Prepare input data for prediction input_data = np.array([[weight, height, age, fcvc, 1 if gender == "Male" else 0, ncp, ch2o, faf, tue, fhwow, 1 if caec_sometimes else 0, 1 if calc_no else 0, 1 if calc_sometimes else 0, 1 if caec_frequently else 0, 1 if alcohol_choice == "Yes" else 0, 1 if favc else 0, 1 if mtrans_choice == "Automobile" else 0]]) # Scale the appropriate input values input_data[:, 0:4] = scaler.fit_transform(input_data[:, 0:4]) input_data[:, 5:10] = scaler.fit_transform(input_data[:, 5:10]) # Make prediction prediction = model.predict(input_data) # Map the numeric prediction to the corresponding label predicted_label = class_labels.get(prediction[0], "Unknown Class") return predicted_label # Custom CSS for better styling custom_css = """ """ # Gradio interface iface = gr.Interface( fn=predict_obesity, inputs=[ gr.Number(label="Weight (40-160 kg)"), gr.Number(label="Height (1-2 m)"), gr.Number(label="Age (10-60 years)"), gr.Number(label="FCVC (Frequency of Vegetable Consumption 1-4)"), gr.Radio(choices=["Male", "Female"], label="Gender"), gr.Number(label="NCP (Number of meals per day 1-3)"), gr.Number(label="CH2O (Water Consumption 1-3)"), gr.Number(label="FAF (Physical Activity Frequency 1-4)"), gr.Number(label="TUE (Time Spent on Exercise 1-4)"), gr.Number(label="FHWOW (Family History with OverWeight)"), gr.Radio(choices=["No", "Sometimes", "Frequently"], label="Alcohol Consumption"), gr.Radio(choices=["Public Transportation", "Automobile"], label="Transportation Method"), gr.Checkbox(label="FAVC (Frequent Consumption of High-Calorie Foods)"), ], outputs=gr.Label(label="Predicted Obesity Level"), title="Obesity Level Estimator", description="Enter the features related to eating habits and physical condition to estimate obesity levels.", css=custom_css ) # Launch the interface iface.launch(share=True)