Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,88 @@
|
|
1 |
import pandas as pd
|
2 |
-
|
|
|
|
|
3 |
from sklearn.preprocessing import MinMaxScaler
|
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 |
-
|
86 |
-
|
87 |
-
history = model.fit(X_train, y_train, epochs=400, batch_size=32,
|
88 |
-
validation_data=(X_test, y_test), verbose=0)
|
|
|
1 |
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
from sklearn.preprocessing import MinMaxScaler
|
6 |
+
import gradio as gr
|
7 |
+
import joblib
|
8 |
+
|
9 |
+
# Load pre-trained model and scaler
|
10 |
+
model = load_model('diabetes_model.h5')
|
11 |
+
scaler = joblib.load('scaler.pkl')
|
12 |
+
|
13 |
+
def predict_diabetes(pregnancies, glucose, insulin, bmi, age):
|
14 |
+
"""Predict diabetes probability from input features"""
|
15 |
+
# Create input array
|
16 |
+
input_data = np.array([[pregnancies, glucose, insulin, bmi, age]])
|
17 |
+
|
18 |
+
# Scale features
|
19 |
+
scaled_data = scaler.transform(input_data)
|
20 |
+
|
21 |
+
# Make prediction
|
22 |
+
probability = model.predict(scaled_data, verbose=0)[0][0]
|
23 |
+
|
24 |
+
# Interpret results
|
25 |
+
status = "Diabetic" if probability >= 0.5 else "Not Diabetic"
|
26 |
+
confidence = probability if probability >= 0.5 else 1 - probability
|
27 |
+
|
28 |
+
# Create explanation
|
29 |
+
explanation = f"""
|
30 |
+
### Prediction: {status}
|
31 |
+
Confidence: {confidence:.1%}
|
32 |
+
|
33 |
+
#### Key factors contributing to this prediction:
|
34 |
+
- Glucose level: **{'High' if glucose > 140 else 'Normal'}** ({glucose} mg/dL)
|
35 |
+
- BMI: **{'Obese' if bmi >= 30 else 'Overweight' if bmi >= 25 else 'Normal'}** ({bmi})
|
36 |
+
- Age: {age} years
|
37 |
+
- Insulin: {insulin} μU/mL
|
38 |
+
- Pregnancies: {pregnancies}
|
39 |
+
"""
|
40 |
+
|
41 |
+
# Create bar chart of feature importance
|
42 |
+
features = ['Pregnancies', 'Glucose', 'Insulin', 'BMI', 'Age']
|
43 |
+
importance = [0.15, 0.45, 0.10, 0.20, 0.10] # Example weights
|
44 |
+
|
45 |
+
return {
|
46 |
+
"probability": float(probability),
|
47 |
+
"status": status,
|
48 |
+
"explanation": explanation,
|
49 |
+
"importance": (features, importance)
|
50 |
+
}
|
51 |
+
|
52 |
+
# Create Gradio interface
|
53 |
+
inputs = [
|
54 |
+
gr.Slider(0, 15, step=1, label="Number of Pregnancies"),
|
55 |
+
gr.Slider(50, 200, value=120, label="Glucose Level (mg/dL)"),
|
56 |
+
gr.Slider(0, 300, value=80, label="Insulin Level (μU/mL)"),
|
57 |
+
gr.Slider(15, 50, value=32, label="BMI (kg/m²)"),
|
58 |
+
gr.Slider(20, 100, value=33, label="Age (years)")
|
59 |
+
]
|
60 |
+
|
61 |
+
outputs = [
|
62 |
+
gr.Label(label="Diabetes Probability"),
|
63 |
+
gr.Markdown(label="Explanation"),
|
64 |
+
gr.BarPlot(x="Feature", y="Importance", label="Feature Importance")
|
65 |
+
]
|
66 |
+
|
67 |
+
title = "Diabetes Prediction App"
|
68 |
+
description = "Early detection of diabetes using machine learning. Based on research: Khanam, J.J. & Foo, S.Y. (2021)"
|
69 |
+
article = """
|
70 |
+
**About this model**:
|
71 |
+
- Trained on Pima Indians Diabetes Dataset
|
72 |
+
- Neural Network with 88.6% accuracy
|
73 |
+
- Predicts diabetes risk using 5 key health parameters
|
74 |
+
"""
|
75 |
+
|
76 |
+
gr.Interface(
|
77 |
+
fn=predict_diabetes,
|
78 |
+
inputs=inputs,
|
79 |
+
outputs=outputs,
|
80 |
+
title=title,
|
81 |
+
description=description,
|
82 |
+
article=article,
|
83 |
+
examples=[
|
84 |
+
[0, 90, 80, 24, 25],
|
85 |
+
[3, 150, 95, 32, 35],
|
86 |
+
[6, 180, 150, 38, 45]
|
87 |
+
]
|
88 |
+
).launch()
|
|
|
|