Spaces:
Sleeping
Sleeping
import streamlit as st # type: ignore | |
import numpy as np | |
import pandas as pd | |
import seaborn as sns # type: ignore | |
import matplotlib.pyplot as plt | |
import base64 | |
import pickle | |
import time | |
def load_data(dataset): | |
df = pd.read_csv(dataset) | |
return df | |
def filedownload(df): | |
csv = df.to_csv(index=False) | |
b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions | |
href = f'<a href="data:file/csv;base64,{b64}" download="diabete_predictions.csv">Download CSV File</a>' | |
href2 = f'<a href="https://huggingface.co/spaces/Jasonntone/Brain_Stroke"></a>' | |
return href,href2 | |
data = load_data('dataset/brain_stroke.csv') | |
meanGlucose = data['avg_glucose_level'].mean() | |
meanBmi = data['bmi'].mean() | |
meanAge = data['age'].mean() | |
st.sidebar.image('images/ahat.png',width=280) | |
def main(): | |
st.markdown("<h1 style='text-align:center;color: red;'>Streamlit Brain Stroke Prediction App</h1>",unsafe_allow_html=True) | |
st.markdown("<h2 style='text-align:center;color: grey;'>Brain Stroke study in USA</h2>",unsafe_allow_html=True) | |
menu = ['Home','Analysis','Data Visualization','Machine Learning'] | |
choice = st.sidebar.selectbox('Select Menu', menu) | |
if choice == 'Home': | |
left,middle,right = st.columns((2,3,2)) | |
with middle: | |
col4, col5, col6 = st.columns(3) | |
with col4: | |
temp = st.metric(label="Average Glucose Level", value=meanGlucose, delta="From 5010 To 2012") | |
with col5: | |
temp = st.metric(label="Average BMI", value=meanBmi, delta="From 2010 To 2012") | |
with col6: | |
temp = st.metric(label="Average Age", value=meanAge, delta="From 2010 To 2012") | |
st.image('images/ahat.png',width=280) | |
if choice == 'Analysis': | |
st.subheader('Brain Stroke Dataset') | |
st.write(data.head()) | |
if st.checkbox('Summary'): | |
st.write(data.describe()) | |
elif st.checkbox('Correlation'): | |
fig = plt.figure(figsize=(15,5)) | |
st.write(sns.heatmap(data.corr(),annot=True)) | |
st.pyplot(fig) | |
elif choice == 'Data Visualization': | |
if st.checkbox('Countplot'): | |
fig = plt.figure(figsize=(15,5)) | |
sns.countplot(x='age',data=data) | |
st.pyplot(fig) | |
elif st.checkbox('Scatterplot'): | |
fig = plt.figure(figsize=(15,5)) | |
sns.scatterplot(x='avg_glucose_level',y='age',data=data,hue='stroke') | |
st.pyplot(fig) | |
elif choice == 'Machine Learning': | |
tab1, tab2, tab3 = st.tabs([":clipboard: Data",":bar_chart:β Visualisation", "ππ― Prediction"]) | |
uploaded_file = st.sidebar.file_uploader('Upload your Dataset(.csv file)', | |
type=['csv']) | |
if uploaded_file: | |
df = load_data(uploaded_file) | |
with tab1: | |
st.subheader('Loaded Dataset') | |
st.write(df) | |
with tab2: | |
st.subheader("Heart Disease's Histogram") | |
fig = plt.figure(figsize=(8,8)) | |
sns.histplot(x='heart_disease',data=data) | |
st.pyplot(fig) | |
with tab3: | |
model = pickle.load(open('./models/gbc_dump.pkl', 'rb')) # Load the trained model from disk | |
prediction = model.predict(df) | |
pp = pd.DataFrame(prediction, columns=['Prediction']) | |
ndf = pd.concat([df, pp], axis=1) | |
ndf['Prediction'].replace(0, 'No Stroke Risk', inplace=True) | |
ndf['Prediction'].replace(1, 'Stroke Risk', inplace=True) | |
st.header("ππ― Stroke Risk Prediction") | |
st.subheader("Predictions") | |
st.write(ndf) | |
if st.button(' πΎ Download csv file'): | |
st.markdown(filedownload(ndf), unsafe_allow_html=True) | |
if __name__ == '__main__': | |
main() |