Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +96 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,98 @@
|
|
| 1 |
-
|
| 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 |
+
# Libraries
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
from sklearn.metrics import accuracy_score
|
| 7 |
+
|
| 8 |
+
# Title
|
| 9 |
+
st.title("Diabetes Prediction System")
|
| 10 |
+
st.write("A Machine Learning model trained on the PIMA Indians Diabetes Dataset.")
|
| 11 |
+
|
| 12 |
+
# Load data and Train Data
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def train_model():
|
| 15 |
+
try:
|
| 16 |
+
df = pd.read_csv("diabetes.csv")
|
| 17 |
+
except FileNotFoundError:
|
| 18 |
+
# We create a tiny dummy dataset.
|
| 19 |
+
# This prevents the app from crashing.
|
| 20 |
+
st.warning("'diabetes.csv' not found. Using dummy data for demonstration.")
|
| 21 |
+
data = {
|
| 22 |
+
'Pregnancies': [6, 1, 8, 1, 0, 5, 3, 10],
|
| 23 |
+
'Glucose': [148, 85, 183, 89, 137, 116, 78, 115],
|
| 24 |
+
'BloodPressure': [72, 66, 64, 66, 40, 74, 50, 0],
|
| 25 |
+
'SkinThickness': [35, 29, 0, 23, 35, 0, 32, 0],
|
| 26 |
+
'Insulin': [0, 0, 0, 94, 168, 0, 88, 0],
|
| 27 |
+
'BMI': [33.6, 26.6, 23.3, 28.1, 43.1, 25.6, 31.0, 35.3],
|
| 28 |
+
'DiabetesPedigreeFunction': [0.627, 0.351, 0.672, 0.167, 2.288, 0.201, 0.248, 0.134],
|
| 29 |
+
'Age': [50, 31, 32, 21, 33, 30, 26, 29],
|
| 30 |
+
'Outcome': [1, 0, 1, 0, 1, 0, 1, 0]
|
| 31 |
+
}
|
| 32 |
+
df = pd.DataFrame(data)
|
| 33 |
+
|
| 34 |
+
# Data Split
|
| 35 |
+
X = df.drop(columns=['Outcome'])
|
| 36 |
+
y = df['Outcome']
|
| 37 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 38 |
+
|
| 39 |
+
# Train Model
|
| 40 |
+
model = RandomForestClassifier(random_state=42)
|
| 41 |
+
model.fit(X_train, y_train)
|
| 42 |
+
|
| 43 |
+
# Calculate accuracy
|
| 44 |
+
acc = accuracy_score(y_test, model.predict(X_test))
|
| 45 |
+
|
| 46 |
+
return model, acc
|
| 47 |
+
|
| 48 |
+
# Loading the Model
|
| 49 |
+
model, accuracy = train_model()
|
| 50 |
+
|
| 51 |
+
# Display Model Accuracy
|
| 52 |
+
st.write(f"**Model Accuracy:** {accuracy * 100:.2f}%")
|
| 53 |
+
st.markdown("---")
|
| 54 |
+
|
| 55 |
+
# UI
|
| 56 |
+
st.sidebar.header("Patient Data")
|
| 57 |
+
|
| 58 |
+
def user_input_features():
|
| 59 |
+
pregnancies = st.sidebar.slider('Pregnancies', 0, 17, 3)
|
| 60 |
+
glucose = st.sidebar.slider('Glucose', 0, 199, 117)
|
| 61 |
+
bp = st.sidebar.slider('Blood Pressure', 0, 122, 72)
|
| 62 |
+
skin = st.sidebar.slider('Skin Thickness', 0, 99, 23)
|
| 63 |
+
insulin = st.sidebar.slider('Insulin', 0, 846, 30)
|
| 64 |
+
bmi = st.sidebar.slider('BMI', 0.0, 67.1, 32.0)
|
| 65 |
+
dpf = st.sidebar.slider('Diabetes Pedigree Function', 0.078, 2.42, 0.3725)
|
| 66 |
+
age = st.sidebar.slider('Age', 21, 81, 29)
|
| 67 |
+
|
| 68 |
+
data = {
|
| 69 |
+
'Pregnancies': pregnancies,
|
| 70 |
+
'Glucose': glucose,
|
| 71 |
+
'BloodPressure': bp,
|
| 72 |
+
'SkinThickness': skin,
|
| 73 |
+
'Insulin': insulin,
|
| 74 |
+
'BMI': bmi,
|
| 75 |
+
'DiabetesPedigreeFunction': dpf,
|
| 76 |
+
'Age': age
|
| 77 |
+
}
|
| 78 |
+
features = pd.DataFrame(data, index=[0])
|
| 79 |
+
return features
|
| 80 |
+
|
| 81 |
+
input_df = user_input_features()
|
| 82 |
+
|
| 83 |
+
# Display User Input
|
| 84 |
+
st.subheader('User Input parameters')
|
| 85 |
+
st.write(input_df)
|
| 86 |
+
|
| 87 |
+
# Prediction
|
| 88 |
+
if st.button('Predict Diabetes Risk'):
|
| 89 |
+
prediction = model.predict(input_df)
|
| 90 |
+
prediction_proba = model.predict_proba(input_df)
|
| 91 |
+
|
| 92 |
+
st.subheader('Prediction Result')
|
| 93 |
+
if prediction[0] == 1:
|
| 94 |
+
st.error('Positive (High Risk of Diabetes)')
|
| 95 |
+
else:
|
| 96 |
+
st.success('Negative (Low Risk of Diabetes)')
|
| 97 |
|
| 98 |
+
st.write(f"Probability: {prediction_proba[0][prediction[0]] * 100:.2f}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|