File size: 3,018 Bytes
1c66bc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
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()