Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +123 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,125 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import pickle
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
from streamlit_option_menu import option_menu
|
| 4 |
+
|
| 5 |
+
# ================== PAGE CONFIG ==================
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Smart Health AI",
|
| 8 |
+
page_icon="🧠",
|
| 9 |
+
layout="wide",
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# ================== LOAD MODELS ==================
|
| 13 |
+
def load_model(file):
|
| 14 |
+
try:
|
| 15 |
+
return pickle.load(open(file, "rb"))
|
| 16 |
+
except:
|
| 17 |
+
st.error(f"❌ Could not load model: {file}")
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
diabetes_model = load_model("models/diabetes_model.sav")
|
| 21 |
+
heart_model = load_model("models/heart_model.sav")
|
| 22 |
+
parkinson_model = load_model("models/parkinson_model.sav")
|
| 23 |
+
|
| 24 |
+
# ================== SIDEBAR MENU ==================
|
| 25 |
+
with st.sidebar:
|
| 26 |
+
st.title("🩺 Smart Health AI")
|
| 27 |
+
selected = option_menu(
|
| 28 |
+
"Main Menu",
|
| 29 |
+
["Diabetes Prediction", "Heart Disease Prediction", "Parkinson's Prediction"],
|
| 30 |
+
icons=["activity", "heart", "person"],
|
| 31 |
+
default_index=0
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# ================== INPUT FUNCTIONS ==================
|
| 35 |
+
|
| 36 |
+
# --------- Diabetes Inputs ---------
|
| 37 |
+
def get_diabetes_input():
|
| 38 |
+
col1, col2, col3 = st.columns(3)
|
| 39 |
+
Pregnancies = col1.number_input('Pregnancies', 0)
|
| 40 |
+
Glucose = col2.number_input('Glucose Level', 0.0)
|
| 41 |
+
BloodPressure = col3.number_input('Blood Pressure', 0.0)
|
| 42 |
+
SkinThickness = col1.number_input('Skin Thickness', 0.0)
|
| 43 |
+
Insulin = col2.number_input('Insulin Level', 0.0)
|
| 44 |
+
BMI = col3.number_input('BMI', 0.0)
|
| 45 |
+
DPF = col1.number_input('Diabetes Pedigree Function', 0.0)
|
| 46 |
+
Age = col2.number_input('Age', 0)
|
| 47 |
+
return [Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DPF, Age]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# --------- Heart Inputs ---------
|
| 51 |
+
def get_heart_input():
|
| 52 |
+
cols = st.columns(4)
|
| 53 |
+
age = cols[0].number_input("Age", 0)
|
| 54 |
+
sex = cols[1].selectbox("Sex", [0,1])
|
| 55 |
+
cp = cols[2].selectbox("Chest Pain Type (0–3)", [0,1,2,3])
|
| 56 |
+
trestbps = cols[3].number_input("Resting Blood Pressure", 0)
|
| 57 |
+
|
| 58 |
+
chol = cols[0].number_input("Cholesterol", 0)
|
| 59 |
+
fbs = cols[1].selectbox("Fasting Sugar > 120 mg/dl", [0,1])
|
| 60 |
+
restecg = cols[2].selectbox("Rest ECG", [0,1,2])
|
| 61 |
+
thalach = cols[3].number_input("Max Heart Rate", 0)
|
| 62 |
+
|
| 63 |
+
exang = cols[0].selectbox("Exercise Angina", [0,1])
|
| 64 |
+
oldpeak = cols[1].number_input("ST Depression", 0.0)
|
| 65 |
+
slope = cols[2].selectbox("Slope", [0,1,2])
|
| 66 |
+
ca = cols[3].selectbox("Number of Major Vessels (0–4)", [0,1,2,3,4])
|
| 67 |
+
thal = cols[0].selectbox("Thal", [0,1,2,3])
|
| 68 |
+
|
| 69 |
+
return [age, sex, cp, trestbps, chol, fbs, restecg,
|
| 70 |
+
thalach, exang, oldpeak, slope, ca, thal]
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# --------- Parkinson Inputs ---------
|
| 74 |
+
def get_parkinson_input():
|
| 75 |
+
params = [
|
| 76 |
+
"MDVP:Fo(Hz)", "MDVP:Fhi(Hz)", "MDVP:Flo(Hz)", "MDVP:Jitter(%)",
|
| 77 |
+
"MDVP:Jitter(Abs)", "MDVP:RAP", "MDVP:PPQ", "Jitter:DDP",
|
| 78 |
+
"MDVP:Shimmer", "MDVP:Shimmer(dB)", "Shimmer:APQ3",
|
| 79 |
+
"Shimmer:APQ5", "MDVP:APQ", "Shimmer:DDA", "NHR", "HNR",
|
| 80 |
+
"RPDE", "DFA", "spread1", "spread2", "D2", "PPE"
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
cols = st.columns(3)
|
| 84 |
+
values = []
|
| 85 |
+
for i, p in enumerate(params):
|
| 86 |
+
values.append(cols[i % 3].number_input(p, value=0.0))
|
| 87 |
+
return values
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# ================== PREDICT FUNCTION ==================
|
| 91 |
+
def predict(model, data):
|
| 92 |
+
return model.predict([data])[0]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ================== MAIN APP ==================
|
| 96 |
+
if selected == "Diabetes Prediction":
|
| 97 |
+
st.header("🩸 Diabetes Prediction")
|
| 98 |
+
data = get_diabetes_input()
|
| 99 |
+
if st.button("Predict"):
|
| 100 |
+
result = predict(diabetes_model, data)
|
| 101 |
+
if result == 1:
|
| 102 |
+
st.error("⚠️ The person is diabetic.")
|
| 103 |
+
else:
|
| 104 |
+
st.success("✅ The person is not diabetic.")
|
| 105 |
+
|
| 106 |
+
elif selected == "Heart Disease Prediction":
|
| 107 |
+
st.header("❤️ Heart Disease Prediction")
|
| 108 |
+
data = get_heart_input()
|
| 109 |
+
if st.button("Predict Heart Disease"):
|
| 110 |
+
result = predict(heart_model, data)
|
| 111 |
+
if result == 1:
|
| 112 |
+
st.error("⚠️ The patient is at risk of heart disease.")
|
| 113 |
+
else:
|
| 114 |
+
st.success("✅ The heart is healthy.")
|
| 115 |
+
|
| 116 |
+
elif selected == "Parkinson's Prediction":
|
| 117 |
+
st.header("🧠 Parkinson's Detection")
|
| 118 |
+
data = get_parkinson_input()
|
| 119 |
+
if st.button("Predict Parkinson's"):
|
| 120 |
+
result = predict(parkinson_model, data)
|
| 121 |
+
if result == 1:
|
| 122 |
+
st.error("⚠️ Parkinson's Detected.")
|
| 123 |
+
else:
|
| 124 |
+
st.success("✅ No Parkinson's signs detected.")
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|