Shivam2396 commited on
Commit
79eea61
·
1 Parent(s): 212f4e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py CHANGED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+
6
+ import seaborn as sns
7
+
8
+ df = pd.read_csv("diabetes.csv")
9
+ df.info()
10
+
11
+ fig, axs = plt.subplots(4, 2, figsize=(15, 12))
12
+ axs = axs.flatten()
13
+ sns.distplot(df["Pregnancies"], rug=True, color="#38b000", ax=axs[0])
14
+ sns.distplot(df["Glucose"], rug=True, color="#FF9933", ax=axs[1])
15
+ sns.distplot(df["BloodPressure"], rug=True, color="#522500", ax=axs[2])
16
+ sns.distplot(df["SkinThickness"], rug=True, color="#66b3ff", ax=axs[3])
17
+ sns.distplot(df["Insulin"], rug=True, color="#FF6699", ax=axs[4])
18
+ sns.distplot(df["BMI"], color="#e76f51", rug=True, ax=axs[5])
19
+ sns.distplot(df["DiabetesPedigreeFunction"], color="#03045e", rug=True, ax=axs[6])
20
+ sns.distplot(df["Age"], rug=True, color="#333533", ax=axs[7])
21
+ plt.show()
22
+
23
+ bmi_outliers = df[df["BMI"] > 40]
24
+ bmi_outliers["BMI"].shape
25
+ df["Diabetes"] = df["Outcome"].apply(lambda x: "Diabetic" if x == 1 else "Not_Diabetic")
26
+ df["BMI"] = df["BMI"].apply(lambda x: df.BMI.mean() if x > 40 else x)
27
+
28
+ from pycaret.classification import *
29
+
30
+ # Diab = setup(data = df,target = 'Outcome',normalize=True, session_id=1,fix_imbalance = True)
31
+ Diab = setup(
32
+ data=df,
33
+ target="Diabetes",
34
+ normalize=True,
35
+ session_id=1,
36
+ fix_imbalance=True,
37
+ numeric_features=["Pregnancies"],
38
+ log_experiment=True,
39
+ experiment_name="diabetes",
40
+ ignore_features=["Outcome"],
41
+ )
42
+
43
+ best = compare_models(n_select=15)
44
+ compare_model_results = pull()
45
+
46
+ rf = create_model("rf", fold=10)
47
+
48
+ tuned_rf = tune_model(rf)
49
+
50
+ # creating a predict function to be passed into gradio UI
51
+ def predict(
52
+ model,
53
+ Pregnancies,
54
+ Glucose,
55
+ BloodPressure,
56
+ BMI,
57
+ SkinThickness,
58
+ Insulin,
59
+ Age,
60
+ DiabetesPedigreeFunction,
61
+ ):
62
+
63
+ df = pd.DataFrame.from_dict(
64
+ {
65
+ "Pregnancies": [Pregnancies],
66
+ "Glucose": [Glucose],
67
+ "BloodPressure": [BloodPressure],
68
+ "BMI": [BMI],
69
+ "SkinThickness": [SkinThickness],
70
+ "Insulin": [Insulin],
71
+ "Age": [Age],
72
+ "DiabetesPedigreeFunction": [DiabetesPedigreeFunction],
73
+ }
74
+ )
75
+
76
+ model_index = list(compare_model_results["Model"]).index(model)
77
+ model = best[model_index]
78
+ pred = predict_model(model, df, raw_score=True)
79
+ return {
80
+ "Not Diabetic": pred["Score_Not_Diabetic"][0].astype("float64"),
81
+ "Diabetic": pred["Score_Diabetic"][0].astype("float64"),
82
+ }
83
+
84
+
85
+ import gradio as gr
86
+
87
+ model = gr.inputs.Dropdown(list(compare_model_results["Model"]), label="Models")
88
+ Pregnancies = gr.inputs.Slider(
89
+ minimum=1, maximum=20, default=df["Pregnancies"].mean(), label="Pregnancies"
90
+ )
91
+ Glucose = gr.inputs.Slider(
92
+ minimum=0, maximum=200, default=df["Glucose"].mean(), label="Glucose"
93
+ )
94
+ BloodPressure = gr.inputs.Slider(
95
+ minimum=0, maximum=140, default=df["BloodPressure"].mean(), label="BloodPressure"
96
+ )
97
+ BMI = gr.inputs.Slider(minimum=0, maximum=70, default=df["BMI"].mean(), label="BMI")
98
+
99
+ SkinThickness = gr.inputs.Slider(
100
+ minimum=0, maximum=100, default=df["SkinThickness"].mean(), label="SkinThickness"
101
+ )
102
+ Insulin = gr.inputs.Slider(
103
+ minimum=0, maximum=1000, default=df["Insulin"].mean(), label="Insulin"
104
+ )
105
+ Age = gr.inputs.Slider(minimum=0, maximum=100, default=df["Age"].mean(), label="Age")
106
+ DiabetesPedigreeFunction = gr.inputs.Slider(
107
+ minimum=0,
108
+ maximum=1,
109
+ default=df["DiabetesPedigreeFunction"].mean(),
110
+ label="DiabetesPedigreeFunction",
111
+ )
112
+ gr.Interface(
113
+ predict,
114
+ [
115
+ model,
116
+ Pregnancies,
117
+ Glucose,
118
+ BloodPressure,
119
+ BMI,
120
+ SkinThickness,
121
+ Insulin,
122
+ Age,
123
+ DiabetesPedigreeFunction,
124
+ ],
125
+ "label",
126
+ live=True,
127
+ ).launch()