Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import joblib | |
| import pandas as pd | |
| import numpy as np | |
| MODEL_NAME = "model.pkl" | |
| gradio_labels = ["Gender", | |
| "Age", | |
| "Family history with overweight", | |
| "Frequent consumption of high caloric food (FAVC)", | |
| "Frequency of consumption of vegetables (FCVC)", | |
| "Number of main meals (NCP)", | |
| "Consumption of food between meals (CAEC)", | |
| "Smoking habit (SMOKE)", | |
| "Consumption of water daily (CH2O)", | |
| "Calories consumption monitoring (SCC)", | |
| "Physical activity frequency (FAF)", | |
| "Time using technology devices (TUE)", | |
| "Consumption of alcohol (CALC)", | |
| "Transportation used (MTRANS)"] | |
| columns = ["Gender", | |
| "Age", | |
| "family_history_with_overweight", | |
| "FAVC", | |
| "FCVC", | |
| "NCP", | |
| "CAEC", | |
| "SMOKE", | |
| "CH2O", | |
| "SCC", | |
| "FAF", | |
| "TUE", | |
| "CALC", | |
| "MTRANS"] | |
| # this is a fake function to learn gradio, not really use | |
| def dummy_function(*inputs): | |
| df = pd.Series(inputs) | |
| print(df) | |
| pred = np.random.randint(1, 9) | |
| return pred | |
| pred = dummy_function() | |
| def predict_obesity(*inputs): | |
| model = joblib.load(MODEL_NAME) | |
| df = pd.DataFrame([inputs], columns=columns) | |
| pred = model.predict(df)[0] | |
| pred_string = { | |
| 1: "Insufficient Weight", | |
| 2: "Normal Weight", | |
| 3: "Over Weight I", | |
| 4: "Over Weight II", | |
| 5: "Over Weight III" , | |
| 6: "Obesity I", | |
| 7: "Obesity II", | |
| 8: "Obesity III", | |
| } | |
| final_pred = pred | |
| if isinstance(pred, int): | |
| final_pred = pred_string[pred] | |
| return final_pred | |
| interface = gr.Interface( | |
| fn=predict_obesity, | |
| inputs=[gr.Dropdown(label=gradio_labels[0], choices=["Male", "Female"]), | |
| gr.Textbox(label=gradio_labels[1]), | |
| gr.Dropdown(label=gradio_labels[2], choices=["yes", "no"]), | |
| gr.Dropdown(label=gradio_labels[3], choices=["yes", "no"]), | |
| gr.Slider(label=gradio_labels[4], minimum=1, maximum=3), | |
| gr.Slider(label=gradio_labels[5], minimum=1, maximum=4), | |
| gr.Dropdown(label=gradio_labels[6], choices=['Sometimes', 'Frequently', 'Always', 'no']), | |
| gr.Dropdown(label=gradio_labels[7], choices=['no', 'yes']), | |
| gr.Slider(label=gradio_labels[8], minimum=1, maximum=3), | |
| gr.Dropdown(label=gradio_labels[9], choices=['no', 'yes']), | |
| gr.Slider(label=gradio_labels[10], minimum=0, maximum=3), | |
| gr.Slider(label=gradio_labels[11], minimum=0, maximum=2), | |
| gr.Dropdown(label=gradio_labels[12], choices=['no', 'Sometimes', 'Frequently', 'Always']), | |
| gr.Dropdown(label=gradio_labels[13], choices=['Public_Transportation', 'Walking', 'Automobile', 'Motorbike', 'Bike']) | |
| ], | |
| outputs=gr.Textbox(label="Predicted Obesity Level"), | |
| title="Obesity Level Prediction" | |
| ) | |
| interface.launch() | |