Spencer525 commited on
Commit
f60bb2a
·
verified ·
1 Parent(s): 5295797

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries for streamlit and model loading
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import joblib
5
+ from sklearn.preprocessing import StandardScaler
6
+ from sklearn.metrics import roc_curve, auc, confusion_matrix, classification_report, accuracy_score
7
+ import matplotlib.pyplot as plt
8
+ import seaborn as sns
9
+
10
+ # Load the saved model from Hugging Face Space
11
+ @st.cache(allow_output_mutation=True)
12
+ def load_model():
13
+ model_path = 'best_stacking_model.pkl' # Assuming you uploaded the model to the space root directory
14
+ model = joblib.load(model_path)
15
+ return model
16
+
17
+ # Function to process uploaded CSV
18
+ def process_csv(uploaded_file):
19
+ # Read the CSV file
20
+ df = pd.read_csv(uploaded_file)
21
+ return df
22
+
23
+ # Streamlit app starts here
24
+ st.title("Conjunctivitis Model Analysis App")
25
+
26
+ # File uploader
27
+ uploaded_file = st.file_uploader("Upload your CSV file", type="csv")
28
+
29
+ if uploaded_file is not None:
30
+ # Process uploaded file
31
+ df = process_csv(uploaded_file)
32
+ st.write("Data Preview:")
33
+ st.write(df.head())
34
+
35
+ # Split into features and target (assuming the file has a 'Target_goal' column)
36
+ if 'Target_goal' not in df.columns:
37
+ st.error("Error: The uploaded CSV does not contain a 'Target_goal' column.")
38
+ else:
39
+ X = df.drop(columns=['Target_goal'])
40
+ y = df['Target_goal']
41
+
42
+ # Standardize the data
43
+ scaler = StandardScaler()
44
+ X_scaled = scaler.fit_transform(X)
45
+
46
+ # Load the model
47
+ model = load_model()
48
+
49
+ # Make predictions
50
+ y_pred = model.predict(X_scaled)
51
+ y_pred_proba = model.predict_proba(X_scaled)[:, 1]
52
+
53
+ # Calculate metrics
54
+ acc = accuracy_score(y, y_pred)
55
+ fpr, tpr, _ = roc_curve(y, y_pred_proba)
56
+ roc_auc = auc(fpr, tpr)
57
+
58
+ st.write(f"Accuracy: {acc:.2f}")
59
+ st.write(f"AUC: {roc_auc:.2f}")
60
+
61
+ # Classification report
62
+ st.write("Classification Report:")
63
+ report = classification_report(y, y_pred, output_dict=True)
64
+ st.write(pd.DataFrame(report).transpose())
65
+
66
+ # Confusion Matrix
67
+ st.write("Confusion Matrix:")
68
+ conf_matrix = confusion_matrix(y, y_pred)
69
+ sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
70
+ plt.title('Confusion Matrix')
71
+ plt.xlabel('Predicted')
72
+ plt.ylabel('Actual')
73
+ st.pyplot(plt.gcf())
74
+
75
+ # ROC Curve
76
+ st.write("ROC Curve:")
77
+ plt.figure(figsize=(10, 6))
78
+ plt.plot(fpr, tpr, color='blue', lw=2, label='ROC Curve (AUC = %0.2f)' % roc_auc)
79
+ plt.plot([0, 1], [0, 1], color='gray', lw=1, linestyle='--')
80
+ plt.xlim([0.0, 1.0])
81
+ plt.ylim([0.0, 1.05])
82
+ plt.xlabel('False Positive Rate')
83
+ plt.ylabel('True Positive Rate')
84
+ plt.title('ROC Curve')
85
+ plt.legend(loc="lower right")
86
+ st.pyplot(plt.gcf())
87
+
88
+ # Option to download the saved model
89
+ st.write("[Download best model](best_stacking_model.pkl)")