dzungpham commited on
Commit
1c66bc5
·
verified ·
1 Parent(s): a854d7c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import pandas as pd
4
+ import numpy as np
5
+
6
+ MODEL_NAME = "model.pkl"
7
+ gradio_labels = ["Gender",
8
+ "Age",
9
+ "Family history with overweight",
10
+ "Frequent consumption of high caloric food (FAVC)",
11
+ "Frequency of consumption of vegetables (FCVC)",
12
+ "Number of main meals (NCP)",
13
+ "Consumption of food between meals (CAEC)",
14
+ "Smoking habit (SMOKE)",
15
+ "Consumption of water daily (CH2O)",
16
+ "Calories consumption monitoring (SCC)",
17
+ "Physical activity frequency (FAF)",
18
+ "Time using technology devices (TUE)",
19
+ "Consumption of alcohol (CALC)",
20
+ "Transportation used (MTRANS)"]
21
+ columns = ["Gender",
22
+ "Age",
23
+ "family_history_with_overweight",
24
+ "FAVC",
25
+ "FCVC",
26
+ "NCP",
27
+ "CAEC",
28
+ "SMOKE",
29
+ "CH2O",
30
+ "SCC",
31
+ "FAF",
32
+ "TUE",
33
+ "CALC",
34
+ "MTRANS"]
35
+
36
+ # this is a fake function to learn gradio, not really use
37
+ def dummy_function(*inputs):
38
+ df = pd.Series(inputs)
39
+ print(df)
40
+ pred = np.random.randint(1, 9)
41
+ return pred
42
+ pred = dummy_function()
43
+
44
+ def predict_obesity(*inputs):
45
+ model = joblib.load(MODEL_NAME)
46
+ df = pd.DataFrame([inputs], columns=columns)
47
+ pred = model.predict(df)[0]
48
+ pred_string = {
49
+ 1: "Insufficient Weight",
50
+ 2: "Normal Weight",
51
+ 3: "Over Weight I",
52
+ 4: "Over Weight II",
53
+ 5: "Over Weight III" ,
54
+ 6: "Obesity I",
55
+ 7: "Obesity II",
56
+ 8: "Obesity III",
57
+ }
58
+ final_pred = pred
59
+ if isinstance(pred, int):
60
+ final_pred = pred_string[pred]
61
+ return final_pred
62
+
63
+ interface = gr.Interface(
64
+ fn=predict_obesity,
65
+ inputs=[gr.Dropdown(label=gradio_labels[0], choices=["Male", "Female"]),
66
+ gr.Textbox(label=gradio_labels[1]),
67
+ gr.Dropdown(label=gradio_labels[2], choices=["yes", "no"]),
68
+ gr.Dropdown(label=gradio_labels[3], choices=["yes", "no"]),
69
+ gr.Slider(label=gradio_labels[4], minimum=1, maximum=3),
70
+ gr.Slider(label=gradio_labels[5], minimum=1, maximum=4),
71
+ gr.Dropdown(label=gradio_labels[6], choices=['Sometimes', 'Frequently', 'Always', 'no']),
72
+ gr.Dropdown(label=gradio_labels[7], choices=['no', 'yes']),
73
+ gr.Slider(label=gradio_labels[8], minimum=1, maximum=3),
74
+ gr.Dropdown(label=gradio_labels[9], choices=['no', 'yes']),
75
+ gr.Slider(label=gradio_labels[10], minimum=0, maximum=3),
76
+ gr.Slider(label=gradio_labels[11], minimum=0, maximum=2),
77
+ gr.Dropdown(label=gradio_labels[12], choices=['no', 'Sometimes', 'Frequently', 'Always']),
78
+ gr.Dropdown(label=gradio_labels[13], choices=['Public_Transportation', 'Walking', 'Automobile', 'Motorbike', 'Bike'])
79
+ ],
80
+ outputs=gr.Textbox(label="Predicted Obesity Level"),
81
+ title="Obesity Level Prediction"
82
+ )
83
+
84
+ interface.launch()