xen2003 commited on
Commit
dc39b06
Β·
verified Β·
1 Parent(s): f4521eb

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +126 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """ITI105_SETY_Demo_Final.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1239rYpEr1h2-cpIWEzcYdiaG6QSZCNzu
8
+ """
9
+
10
+ import gradio as gr
11
+ import pandas as pd
12
+ import numpy as np
13
+ import matplotlib.pyplot as plt
14
+ import joblib
15
+ from sklearn.metrics import (
16
+ accuracy_score, precision_score, recall_score, f1_score,
17
+ roc_curve, precision_recall_curve, auc,
18
+ confusion_matrix, ConfusionMatrixDisplay
19
+ )
20
+
21
+ # Load models (adjust paths as needed)
22
+ base_model = joblib.load("base_model.pkl")
23
+ best_model = joblib.load("best_gb.pkl")
24
+
25
+ def get_metrics(model, X, y):
26
+ y_pred = model.predict(X)
27
+ y_prob = model.predict_proba(X)[:, 1]
28
+
29
+ acc = accuracy_score(y, y_pred)
30
+ prec = precision_score(y, y_pred)
31
+ rec = recall_score(y, y_pred)
32
+ f1 = f1_score(y, y_pred)
33
+
34
+ fpr, tpr, _ = roc_curve(y, y_prob)
35
+ precision_vals, recall_vals, _ = precision_recall_curve(y, y_prob)
36
+
37
+ return {
38
+ 'y_pred': y_pred,
39
+ 'y_prob': y_prob,
40
+ 'fpr': fpr,
41
+ 'tpr': tpr,
42
+ 'precision_vals': precision_vals,
43
+ 'recall_vals': recall_vals,
44
+ 'metrics': f"""
45
+ πŸ“Œ Accuracy: {acc:.4f}
46
+ πŸ“Œ Precision: {prec:.4f}
47
+ πŸ“Œ Recall: {rec:.4f}
48
+ πŸ“Œ F1 Score: {f1:.4f}
49
+ πŸ“Œ ROC AUC: {auc(fpr, tpr):.4f}
50
+ πŸ“Œ PR AUC: {auc(recall_vals, precision_vals):.4f}
51
+ """
52
+ }
53
+
54
+
55
+ def plot_confusion_matrices(model1, model2, X, y, labels=["Base Model", "Best Model"]):
56
+ fig, axes = plt.subplots(1, 2, figsize=(16, 8))
57
+ #fig, axes = plt.subplots(1, 2, figsize=(50, 25))
58
+
59
+ for i, model in enumerate([model1, model2]):
60
+ y_pred = model.predict(X)
61
+ cm = confusion_matrix(y, y_pred)
62
+ disp = ConfusionMatrixDisplay(confusion_matrix=cm)
63
+ disp.plot(ax=axes[i], cmap='Blues', colorbar=False)
64
+ # Manually annotate the matrix with larger font
65
+ for (j, k), val in np.ndenumerate(cm):
66
+ axes[i].text(k, j, f"{val}", ha='center', va='center', fontsize=28, color='red')
67
+ axes[i].set_title(f"{labels[i]} Confusion Matrix", fontsize=30)
68
+ axes[i].tick_params(axis='both', labelsize=36)
69
+
70
+ plt.tight_layout()
71
+ return plt.gcf()
72
+
73
+ def evaluate(file):
74
+ df = pd.read_csv(file.name)
75
+ if 'Status' not in df.columns:
76
+ return "Error: 'Status' column missing."
77
+
78
+ #df = df.tail(5)
79
+ X = df.drop(columns='Status')
80
+ y = df['Status']
81
+
82
+ base = get_metrics(base_model, X, y)
83
+ best = get_metrics(best_model, X, y)
84
+
85
+ # Combined ROC Curve
86
+ plt.figure()
87
+ plt.plot(base['fpr'], base['tpr'], label=f"Base Model (AUC={auc(base['fpr'], base['tpr']):.2f})", linestyle='--')
88
+ plt.plot(best['fpr'], best['tpr'], label=f"Best Model (AUC={auc(best['fpr'], best['tpr']):.2f})", linestyle='-')
89
+ plt.plot([0, 1], [0, 1], 'k--', alpha=0.5)
90
+ plt.xlabel("False Positive Rate")
91
+ plt.ylabel("True Positive Rate")
92
+ plt.title("Combined ROC Curve")
93
+ plt.legend()
94
+ roc_fig = plt.gcf()
95
+
96
+ # Combined PR Curve
97
+ plt.figure()
98
+ plt.plot(base['recall_vals'], base['precision_vals'], label=f"Base Model (AUC={auc(base['recall_vals'], base['precision_vals']):.2f})", linestyle='--')
99
+ plt.plot(best['recall_vals'], best['precision_vals'], label=f"Best Model (AUC={auc(best['recall_vals'], best['precision_vals']):.2f})", linestyle='-')
100
+ plt.xlabel("Recall")
101
+ plt.ylabel("Precision")
102
+ plt.title("Combined Precision-Recall Curve")
103
+ plt.legend()
104
+ pr_fig = plt.gcf()
105
+
106
+ # Confusion Matrices
107
+ cm_fig = plot_confusion_matrices(base_model, best_model, X, y)
108
+
109
+ combined_metrics = f"πŸ“Š Base Model:\n{base['metrics']}\n\nπŸ“Š Best Model:\n{best['metrics']}"
110
+
111
+ return combined_metrics, roc_fig, pr_fig, cm_fig
112
+
113
+ demo = gr.Interface(
114
+ fn=evaluate,
115
+ inputs=gr.File(label="Upload CSV with 'Status' column"),
116
+ outputs=[
117
+ gr.Textbox(label="πŸ“Š Performance Comparison"),
118
+ gr.Plot(label="Combined ROC Curve"),
119
+ gr.Plot(label="Combined Precision-Recall Curve"),
120
+ gr.Plot(label="Confusion Matrices")
121
+ ],
122
+ title="πŸ” Model Comparison Dashboard",
123
+ description="Upload a CSV file to compare base and best model performance side by side."
124
+ )
125
+
126
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ pandas
3
+ scikit-learn
4
+ matplotlib
5
+ joblib