Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,100 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
from sklearn.ensemble import GradientBoostingClassifier
|
| 5 |
-
from sklearn.preprocessing import StandardScaler
|
| 6 |
-
|
| 7 |
-
st.set_page_config(page_title="Liver Disease Prediction App", layout="wide")
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
@st.cache_data
|
| 11 |
-
def load_data():
|
| 12 |
-
data = pd.read_csv('Liver_disease_data.csv')
|
| 13 |
-
X = data.drop('Diagnosis', axis=1)
|
| 14 |
-
y = data['Diagnosis']
|
| 15 |
-
# One-hot encode the Gender column
|
| 16 |
-
X = pd.get_dummies(X, columns=['Gender'], prefix='Gender', drop_first=True)
|
| 17 |
-
return X, y
|
| 18 |
-
|
| 19 |
-
X, y = load_data()
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
@st.cache_resource
|
| 23 |
-
def train_model():
|
| 24 |
-
scaler = StandardScaler()
|
| 25 |
-
X_scaled = scaler.fit_transform(X)
|
| 26 |
-
model = GradientBoostingClassifier(random_state=42)
|
| 27 |
-
model.fit(X_scaled, y)
|
| 28 |
-
return scaler, model
|
| 29 |
-
|
| 30 |
-
scaler, model = train_model()
|
| 31 |
-
|
| 32 |
-
st.title('Liver Disease Prediction App')
|
| 33 |
-
|
| 34 |
-
st.write('Enter the following information to predict liver disease:')
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
col1, col2 = st.columns(2)
|
| 38 |
-
|
| 39 |
-
with col1:
|
| 40 |
-
age = st.number_input('Age', min_value=0, max_value=120, value=30)
|
| 41 |
-
gender = st.selectbox('Gender', [0, 1], format_func=lambda x: 'Female' if x == 0 else 'Male')
|
| 42 |
-
bmi = st.number_input('BMI', min_value=0.0, max_value=50.0, value=25.0, format="%.1f")
|
| 43 |
-
alcohol_consumption = st.number_input('Alcohol Consumption', min_value=0.0, value=5.0, format="%.1f")
|
| 44 |
-
smoking = st.selectbox('Smoking', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 45 |
-
|
| 46 |
-
with col2:
|
| 47 |
-
genetic_risk = st.selectbox('Genetic Risk', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 48 |
-
physical_activity = st.number_input('Physical Activity', min_value=0.0, value=1.0, format="%.1f")
|
| 49 |
-
diabetes = st.selectbox('Diabetes', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 50 |
-
hypertension = st.selectbox('Hypertension', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 51 |
-
liver_function_test = st.number_input('Liver Function Test', min_value=0.0, value=50.0, format="%.1f")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
'
|
| 62 |
-
'
|
| 63 |
-
'
|
| 64 |
-
'
|
| 65 |
-
'
|
| 66 |
-
'
|
| 67 |
-
'
|
| 68 |
-
'
|
| 69 |
-
'
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
st.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
st.markdown("---")
|
| 101 |
st.write('Note: This app is for educational purposes only. Always consult a medical professional for accurate diagnosis and advice.')
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.ensemble import GradientBoostingClassifier
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="Liver Disease Prediction App", layout="wide")
|
| 8 |
+
|
| 9 |
+
# Loading the data and preprocessing steps
|
| 10 |
+
@st.cache_data
|
| 11 |
+
def load_data():
|
| 12 |
+
data = pd.read_csv('Liver_disease_data.csv')
|
| 13 |
+
X = data.drop('Diagnosis', axis=1)
|
| 14 |
+
y = data['Diagnosis']
|
| 15 |
+
# One-hot encode the Gender column
|
| 16 |
+
X = pd.get_dummies(X, columns=['Gender'], prefix='Gender', drop_first=True)
|
| 17 |
+
return X, y
|
| 18 |
+
|
| 19 |
+
X, y = load_data()
|
| 20 |
+
|
| 21 |
+
# Training the model
|
| 22 |
+
@st.cache_resource
|
| 23 |
+
def train_model():
|
| 24 |
+
scaler = StandardScaler()
|
| 25 |
+
X_scaled = scaler.fit_transform(X)
|
| 26 |
+
model = GradientBoostingClassifier(random_state=42)
|
| 27 |
+
model.fit(X_scaled, y)
|
| 28 |
+
return scaler, model
|
| 29 |
+
|
| 30 |
+
scaler, model = train_model()
|
| 31 |
+
|
| 32 |
+
st.title('Liver Disease Prediction App')
|
| 33 |
+
|
| 34 |
+
st.write('Enter the following information to predict liver disease:')
|
| 35 |
+
|
| 36 |
+
# Creating two columns for input fields
|
| 37 |
+
col1, col2 = st.columns(2)
|
| 38 |
+
|
| 39 |
+
with col1:
|
| 40 |
+
age = st.number_input('Age', min_value=0, max_value=120, value=30)
|
| 41 |
+
gender = st.selectbox('Gender', [0, 1], format_func=lambda x: 'Female' if x == 0 else 'Male')
|
| 42 |
+
bmi = st.number_input('BMI', min_value=0.0, max_value=50.0, value=25.0, format="%.1f")
|
| 43 |
+
alcohol_consumption = st.number_input('Alcohol Consumption', min_value=0.0, value=5.0, format="%.1f")
|
| 44 |
+
smoking = st.selectbox('Smoking', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 45 |
+
|
| 46 |
+
with col2:
|
| 47 |
+
genetic_risk = st.selectbox('Genetic Risk', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 48 |
+
physical_activity = st.number_input('Physical Activity', min_value=0.0, value=1.0, format="%.1f")
|
| 49 |
+
diabetes = st.selectbox('Diabetes', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 50 |
+
hypertension = st.selectbox('Hypertension', [0, 1], format_func=lambda x: 'No' if x == 0 else 'Yes')
|
| 51 |
+
liver_function_test = st.number_input('Liver Function Test', min_value=0.0, value=50.0, format="%.1f")
|
| 52 |
+
|
| 53 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 54 |
+
with col2:
|
| 55 |
+
predict_button = st.button('Predict', use_container_width=True)
|
| 56 |
+
|
| 57 |
+
if predict_button:
|
| 58 |
+
# Preparing the input data
|
| 59 |
+
input_data = pd.DataFrame({
|
| 60 |
+
'Age': [age],
|
| 61 |
+
'BMI': [bmi],
|
| 62 |
+
'AlcoholConsumption': [alcohol_consumption],
|
| 63 |
+
'Smoking': [smoking],
|
| 64 |
+
'GeneticRisk': [genetic_risk],
|
| 65 |
+
'PhysicalActivity': [physical_activity],
|
| 66 |
+
'Diabetes': [diabetes],
|
| 67 |
+
'Hypertension': [hypertension],
|
| 68 |
+
'LiverFunctionTest': [liver_function_test],
|
| 69 |
+
'Gender': [gender]
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
# One-hot encode the Gender column to match the training data
|
| 73 |
+
input_data = pd.get_dummies(input_data, columns=['Gender'], prefix='Gender', drop_first=True)
|
| 74 |
+
|
| 75 |
+
# Ensuring all the columns from training data are present
|
| 76 |
+
for col in X.columns:
|
| 77 |
+
if col not in input_data.columns:
|
| 78 |
+
input_data[col] = 0
|
| 79 |
+
|
| 80 |
+
# Reordering the columns to match training data
|
| 81 |
+
input_data = input_data[X.columns]
|
| 82 |
+
|
| 83 |
+
# Scaling the input data
|
| 84 |
+
input_scaled = scaler.transform(input_data)
|
| 85 |
+
|
| 86 |
+
# Making prediction
|
| 87 |
+
prediction = model.predict(input_scaled)
|
| 88 |
+
probability = model.predict_proba(input_scaled)[0][1]
|
| 89 |
+
|
| 90 |
+
st.markdown("---")
|
| 91 |
+
st.subheader('Prediction Results:')
|
| 92 |
+
if prediction[0] == 1:
|
| 93 |
+
st.warning('The Patient has High risk of having liver disease')
|
| 94 |
+
else:
|
| 95 |
+
st.success('The Patient has Low risk of having liver disease')
|
| 96 |
+
|
| 97 |
+
st.write(f'Probability of liver disease: {probability:.2f}')
|
| 98 |
+
|
| 99 |
+
st.markdown("---")
|
|
|
|
| 100 |
st.write('Note: This app is for educational purposes only. Always consult a medical professional for accurate diagnosis and advice.')
|