Conju_ANALY / app.py
Spencer525's picture
Update app.py
d8a0a5b verified
# Import necessary libraries for streamlit and model loading
import streamlit as st
import pandas as pd
import joblib
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_curve, auc, confusion_matrix, classification_report, accuracy_score
import matplotlib.pyplot as plt
import seaborn as sns
# Load the saved model from Hugging Face Space
#@st.cache(allow_output_mutation=True)
@st.cache_data
def load_model():
model_path = 'best_stacking_model.pkl' # Assuming you uploaded the model to the space root directory
model = joblib.load(model_path)
return model
# Function to process uploaded CSV
def process_csv(uploaded_file):
# Read the CSV file
df = pd.read_csv(uploaded_file)
return df
# Streamlit app starts here
st.title("Conjunctivitis Model Analysis App")
# File uploader
uploaded_file = st.file_uploader("Upload your CSV file", type="csv")
if uploaded_file is not None:
# Process uploaded file
df = process_csv(uploaded_file)
st.write("Data Preview:")
st.write(df.head())
# Split into features and target (assuming the file has a 'Target_goal' column)
if 'Target_goal' not in df.columns:
st.error("Error: The uploaded CSV does not contain a 'Target_goal' column.")
else:
X = df.drop(columns=['Target_goal'])
y = df['Target_goal']
# Standardize the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Load the model
model = load_model()
# Make predictions
y_pred = model.predict(X_scaled)
y_pred_proba = model.predict_proba(X_scaled)[:, 1]
# Calculate metrics
acc = accuracy_score(y, y_pred)
fpr, tpr, _ = roc_curve(y, y_pred_proba)
roc_auc = auc(fpr, tpr)
st.write(f"Accuracy: {acc:.2f}")
st.write(f"AUC: {roc_auc:.2f}")
# Classification report
st.write("Classification Report:")
report = classification_report(y, y_pred, output_dict=True)
st.write(pd.DataFrame(report).transpose())
# Confusion Matrix
st.write("Confusion Matrix:")
conf_matrix = confusion_matrix(y, y_pred)
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('Actual')
st.pyplot(plt.gcf())
# ROC Curve
st.write("ROC Curve:")
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, color='blue', lw=2, label='ROC Curve (AUC = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='gray', lw=1, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.legend(loc="lower right")
st.pyplot(plt.gcf())