Upload 3 files
Browse files- app.py +110 -0
- devcontainer.json +33 -0
- requirements.txt +44 -0
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 7 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 8 |
+
|
| 9 |
+
# Set page config
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Diabetes Detection App",
|
| 12 |
+
page_icon="🏥",
|
| 13 |
+
layout="wide"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Title
|
| 17 |
+
st.title("Diabetes Detection App")
|
| 18 |
+
st.write("This app predicts diabetes using various health metrics.")
|
| 19 |
+
|
| 20 |
+
# Load data
|
| 21 |
+
@st.cache_data
|
| 22 |
+
def load_data():
|
| 23 |
+
# Load the Pima Indians Diabetes Database
|
| 24 |
+
columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness',
|
| 25 |
+
'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome']
|
| 26 |
+
|
| 27 |
+
data = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv',
|
| 28 |
+
names=columns)
|
| 29 |
+
return data
|
| 30 |
+
|
| 31 |
+
# Load and prepare data
|
| 32 |
+
data = load_data()
|
| 33 |
+
|
| 34 |
+
# Sidebar for user input
|
| 35 |
+
st.sidebar.header('User Input Features')
|
| 36 |
+
|
| 37 |
+
def user_input_features():
|
| 38 |
+
pregnancies = st.sidebar.slider('Pregnancies', 0, 17, 3)
|
| 39 |
+
glucose = st.sidebar.slider('Glucose', 0, 200, 120)
|
| 40 |
+
blood_pressure = st.sidebar.slider('Blood Pressure', 0, 122, 70)
|
| 41 |
+
skin_thickness = st.sidebar.slider('Skin Thickness', 0, 100, 20)
|
| 42 |
+
insulin = st.sidebar.slider('Insulin', 0, 846, 79)
|
| 43 |
+
bmi = st.sidebar.slider('BMI', 0.0, 67.1, 31.4)
|
| 44 |
+
diabetes_pedigree = st.sidebar.slider('Diabetes Pedigree Function', 0.078, 2.42, 0.3725)
|
| 45 |
+
age = st.sidebar.slider('Age', 21, 81, 29)
|
| 46 |
+
|
| 47 |
+
data = {
|
| 48 |
+
'Pregnancies': pregnancies,
|
| 49 |
+
'Glucose': glucose,
|
| 50 |
+
'BloodPressure': blood_pressure,
|
| 51 |
+
'SkinThickness': skin_thickness,
|
| 52 |
+
'Insulin': insulin,
|
| 53 |
+
'BMI': bmi,
|
| 54 |
+
'DiabetesPedigreeFunction': diabetes_pedigree,
|
| 55 |
+
'Age': age
|
| 56 |
+
}
|
| 57 |
+
return pd.DataFrame(data, index=[0])
|
| 58 |
+
|
| 59 |
+
# Get user input
|
| 60 |
+
user_data = user_input_features()
|
| 61 |
+
|
| 62 |
+
# Display user input
|
| 63 |
+
st.subheader('User Input Features')
|
| 64 |
+
st.write(user_data)
|
| 65 |
+
|
| 66 |
+
# Prepare the model
|
| 67 |
+
X = data.drop('Outcome', axis=1)
|
| 68 |
+
y = data['Outcome']
|
| 69 |
+
|
| 70 |
+
# Split the data
|
| 71 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 72 |
+
|
| 73 |
+
# Scale the features
|
| 74 |
+
scaler = StandardScaler()
|
| 75 |
+
X_train_scaled = scaler.fit_transform(X_train)
|
| 76 |
+
X_test_scaled = scaler.transform(X_test)
|
| 77 |
+
|
| 78 |
+
# Train the model
|
| 79 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 80 |
+
model.fit(X_train_scaled, y_train)
|
| 81 |
+
|
| 82 |
+
# Make prediction on user input
|
| 83 |
+
user_data_scaled = scaler.transform(user_data)
|
| 84 |
+
prediction = model.predict(user_data_scaled)
|
| 85 |
+
prediction_proba = model.predict_proba(user_data_scaled)
|
| 86 |
+
|
| 87 |
+
# Show prediction
|
| 88 |
+
st.subheader('Prediction')
|
| 89 |
+
if prediction[0] == 0:
|
| 90 |
+
st.write('The model predicts: No Diabetes')
|
| 91 |
+
else:
|
| 92 |
+
st.write('The model predicts: Diabetes')
|
| 93 |
+
|
| 94 |
+
st.subheader('Prediction Probability')
|
| 95 |
+
st.write(f'Probability of No Diabetes: {prediction_proba[0][0]:.2%}')
|
| 96 |
+
st.write(f'Probability of Diabetes: {prediction_proba[0][1]:.2%}')
|
| 97 |
+
|
| 98 |
+
# Model performance
|
| 99 |
+
st.subheader('Model Performance')
|
| 100 |
+
y_pred = model.predict(X_test_scaled)
|
| 101 |
+
st.write(f'Model Accuracy: {accuracy_score(y_test, y_pred):.2%}')
|
| 102 |
+
|
| 103 |
+
# Feature importance
|
| 104 |
+
st.subheader('Feature Importance')
|
| 105 |
+
feature_importance = pd.DataFrame({
|
| 106 |
+
'Feature': X.columns,
|
| 107 |
+
'Importance': model.feature_importances_
|
| 108 |
+
}).sort_values('Importance', ascending=False)
|
| 109 |
+
|
| 110 |
+
st.bar_chart(feature_importance.set_index('Feature'))
|
devcontainer.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "Python 3",
|
| 3 |
+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
| 4 |
+
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
|
| 5 |
+
"customizations": {
|
| 6 |
+
"codespaces": {
|
| 7 |
+
"openFiles": [
|
| 8 |
+
"README.md",
|
| 9 |
+
"app.py"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
"vscode": {
|
| 13 |
+
"settings": {},
|
| 14 |
+
"extensions": [
|
| 15 |
+
"ms-python.python",
|
| 16 |
+
"ms-python.vscode-pylance"
|
| 17 |
+
]
|
| 18 |
+
}
|
| 19 |
+
},
|
| 20 |
+
"updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y <packages.txt; [ -f requirements.txt ] && pip3 install --user -r requirements.txt; pip3 install --user streamlit; echo '✅ Packages installed and Requirements met'",
|
| 21 |
+
"postAttachCommand": {
|
| 22 |
+
"server": "streamlit run app.py --server.enableCORS false --server.enableXsrfProtection false"
|
| 23 |
+
},
|
| 24 |
+
"portsAttributes": {
|
| 25 |
+
"8501": {
|
| 26 |
+
"label": "Application",
|
| 27 |
+
"onAutoForward": "openPreview"
|
| 28 |
+
}
|
| 29 |
+
},
|
| 30 |
+
"forwardPorts": [
|
| 31 |
+
8501
|
| 32 |
+
]
|
| 33 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==5.5.0
|
| 2 |
+
attrs==25.1.0
|
| 3 |
+
blinker==1.9.0
|
| 4 |
+
cachetools==5.5.1
|
| 5 |
+
certifi==2025.1.31
|
| 6 |
+
charset-normalizer==3.4.1
|
| 7 |
+
click==8.1.8
|
| 8 |
+
gitdb==4.0.12
|
| 9 |
+
gitpython==3.1.44
|
| 10 |
+
idna==3.10
|
| 11 |
+
jinja2==3.1.5
|
| 12 |
+
joblib==1.4.2
|
| 13 |
+
jsonschema==4.23.0
|
| 14 |
+
jsonschema-specifications==2024.10.1
|
| 15 |
+
markdown-it-py==3.0.0
|
| 16 |
+
markupsafe==3.0.2
|
| 17 |
+
mdurl==0.1.2
|
| 18 |
+
narwhals==1.25.1
|
| 19 |
+
numpy==2.2.2
|
| 20 |
+
packaging==24.2
|
| 21 |
+
pandas==2.2.3
|
| 22 |
+
pillow==11.1.0
|
| 23 |
+
protobuf==5.29.3
|
| 24 |
+
pyarrow==19.0.0
|
| 25 |
+
pydeck==0.9.1
|
| 26 |
+
pygments==2.19.1
|
| 27 |
+
python-dateutil==2.9.0.post0
|
| 28 |
+
pytz==2025.1
|
| 29 |
+
referencing==0.36.2
|
| 30 |
+
requests==2.32.3
|
| 31 |
+
rich==13.9.4
|
| 32 |
+
rpds-py==0.22.3
|
| 33 |
+
scikit-learn==1.6.1
|
| 34 |
+
scipy==1.15.1
|
| 35 |
+
six==1.17.0
|
| 36 |
+
smmap==5.0.2
|
| 37 |
+
streamlit==1.42.0
|
| 38 |
+
tenacity==9.0.0
|
| 39 |
+
threadpoolctl==3.5.0
|
| 40 |
+
toml==0.10.2
|
| 41 |
+
tornado==6.4.2
|
| 42 |
+
typing-extensions==4.12.2
|
| 43 |
+
tzdata==2025.1
|
| 44 |
+
urllib3==2.3.0
|