Spaces:
Sleeping
Sleeping
Delete src/streamlit_app.py
Browse files- src/streamlit_app.py +0 -127
src/streamlit_app.py
DELETED
|
@@ -1,127 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import os
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
import seaborn as sns
|
| 6 |
-
from sklearn.model_selection import train_test_split
|
| 7 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 8 |
-
from sklearn.preprocessing import StandardScaler
|
| 9 |
-
from sklearn.metrics import accuracy_score
|
| 10 |
-
|
| 11 |
-
# Title
|
| 12 |
-
st.set_page_config(page_title="Diabetes Prediction", page_icon="🩺", layout="wide")
|
| 13 |
-
st.title("Diabetes Prediction System")
|
| 14 |
-
st.write("A Machine Learning model trained on the PIMA Indians Diabetes Dataset (KNN Algorithm).")
|
| 15 |
-
|
| 16 |
-
# Load Data
|
| 17 |
-
data_file = "diabetes.csv"
|
| 18 |
-
if not os.path.exists(data_file):
|
| 19 |
-
data_file = "src/diabetes.csv"
|
| 20 |
-
|
| 21 |
-
if not os.path.exists(data_file):
|
| 22 |
-
st.error("Error: 'diabetes.csv' not found. Please upload the file.")
|
| 23 |
-
st.stop()
|
| 24 |
-
|
| 25 |
-
# Train Model
|
| 26 |
-
@st.cache_resource
|
| 27 |
-
def build_model(path):
|
| 28 |
-
df = pd.read_csv(path)
|
| 29 |
-
|
| 30 |
-
# Split features and target
|
| 31 |
-
X = df.drop('Outcome', axis=1)
|
| 32 |
-
y = df['Outcome']
|
| 33 |
-
|
| 34 |
-
# Scale the data
|
| 35 |
-
scaler = StandardScaler()
|
| 36 |
-
X_scaled = scaler.fit_transform(X)
|
| 37 |
-
|
| 38 |
-
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
|
| 39 |
-
|
| 40 |
-
# KNN Classifier
|
| 41 |
-
knn = KNeighborsClassifier(n_neighbors=9)
|
| 42 |
-
knn.fit(X_train, y_train)
|
| 43 |
-
|
| 44 |
-
# Check accuracy
|
| 45 |
-
acc = accuracy_score(y_test, knn.predict(X_test))
|
| 46 |
-
|
| 47 |
-
return knn, acc, df, scaler
|
| 48 |
-
|
| 49 |
-
# Get model, data, and the scaler
|
| 50 |
-
model, accuracy, df, scaler = build_model(data_file)
|
| 51 |
-
|
| 52 |
-
# Display basic stats
|
| 53 |
-
st.write(f"**Model Accuracy (KNN):** {accuracy * 100:.2f}%")
|
| 54 |
-
st.markdown("---")
|
| 55 |
-
|
| 56 |
-
# UI - User Inputs
|
| 57 |
-
st.sidebar.header("Patient Data")
|
| 58 |
-
|
| 59 |
-
def user_input_features():
|
| 60 |
-
pregnancies = st.sidebar.slider('Pregnancies', 0, 17, 3)
|
| 61 |
-
glucose = st.sidebar.slider('Glucose', 0, 199, 117)
|
| 62 |
-
bp = st.sidebar.slider('Blood Pressure', 0, 122, 72)
|
| 63 |
-
skin = st.sidebar.slider('Skin Thickness', 0, 99, 23)
|
| 64 |
-
insulin = st.sidebar.slider('Insulin', 0, 846, 30)
|
| 65 |
-
bmi = st.sidebar.slider('BMI', 0.0, 67.1, 32.0)
|
| 66 |
-
dpf = st.sidebar.slider('Diabetes Pedigree Function', 0.078, 2.42, 0.3725)
|
| 67 |
-
age = st.sidebar.slider('Age', 21, 81, 29)
|
| 68 |
-
|
| 69 |
-
user_data = {
|
| 70 |
-
'Pregnancies': pregnancies,
|
| 71 |
-
'Glucose': glucose,
|
| 72 |
-
'BloodPressure': bp,
|
| 73 |
-
'SkinThickness': skin,
|
| 74 |
-
'Insulin': insulin,
|
| 75 |
-
'BMI': bmi,
|
| 76 |
-
'DiabetesPedigreeFunction': dpf,
|
| 77 |
-
'Age': age
|
| 78 |
-
}
|
| 79 |
-
return pd.DataFrame(user_data, index=[0])
|
| 80 |
-
|
| 81 |
-
input_df = user_input_features()
|
| 82 |
-
|
| 83 |
-
st.subheader('User Input Parameters')
|
| 84 |
-
st.write(input_df)
|
| 85 |
-
|
| 86 |
-
# Prediction
|
| 87 |
-
if st.button('Predict Diabetes Risk'):
|
| 88 |
-
input_scaled = scaler.transform(input_df)
|
| 89 |
-
|
| 90 |
-
prediction = model.predict(input_scaled)
|
| 91 |
-
probs = model.predict_proba(input_scaled)
|
| 92 |
-
|
| 93 |
-
st.subheader('Prediction Result')
|
| 94 |
-
if prediction[0] == 1:
|
| 95 |
-
st.error(f'High Risk of Diabetes (Confidence: {probs[0][1]*100:.2f}%)')
|
| 96 |
-
else:
|
| 97 |
-
st.success(f'Low Risk of Diabetes (Confidence: {probs[0][0]*100:.2f}%)')
|
| 98 |
-
|
| 99 |
-
st.markdown("---")
|
| 100 |
-
|
| 101 |
-
# Graphs
|
| 102 |
-
if st.checkbox("Show Data Analysis Graphs"):
|
| 103 |
-
|
| 104 |
-
col1, col2 = st.columns(2)
|
| 105 |
-
|
| 106 |
-
with col1:
|
| 107 |
-
st.subheader("Outcome Distribution")
|
| 108 |
-
fig1, ax1 = plt.subplots(figsize=(6, 4))
|
| 109 |
-
df['Outcome'].value_counts().plot.pie(autopct='%1.1f%%', ax=ax1, labels=['Healthy', 'Diabetic'], colors=['#66b3ff', '#ff9999'])
|
| 110 |
-
ax1.set_ylabel('')
|
| 111 |
-
st.pyplot(fig1, use_container_width=True)
|
| 112 |
-
|
| 113 |
-
with col2:
|
| 114 |
-
st.subheader("Correlation Heatmap")
|
| 115 |
-
fig2, ax2 = plt.subplots(figsize=(6, 4))
|
| 116 |
-
sns.heatmap(df.corr(), annot=False, cmap='coolwarm', ax=ax2)
|
| 117 |
-
st.pyplot(fig2, use_container_width=True)
|
| 118 |
-
|
| 119 |
-
st.markdown("---")
|
| 120 |
-
|
| 121 |
-
st.subheader("Feature Comparison (Box Plot)")
|
| 122 |
-
feature_to_plot = st.selectbox("Select Feature to Compare:", df.drop('Outcome', axis=1).columns)
|
| 123 |
-
|
| 124 |
-
fig4, ax4 = plt.subplots(figsize=(10, 4))
|
| 125 |
-
sns.boxplot(x='Outcome', y=feature_to_plot, data=df, palette=['#66b3ff', '#ff9999'], ax=ax4)
|
| 126 |
-
ax4.set_xticklabels(['Healthy', 'Diabetic'])
|
| 127 |
-
st.pyplot(fig4, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|