mansi14883md/AI-Keystroke-Security-System
1
1import json2import os3 4import matplotlib.pyplot as plt5from backend_utils import get_attempt_history, get_user_overview6 7 8METRICS_FILE = os.path.join("models", "metrics.json")9 10 11def main() -> None:12 if not os.path.exists(METRICS_FILE):13 raise FileNotFoundError("metrics.json not found. Run train_model.py first.")14 15 with open(METRICS_FILE, "r", encoding="utf-8") as file:16 metrics = json.load(file)17 18 metric_names = ["accuracy", "precision", "recall", "f1_score"]19 metric_values = [metrics[name] * 100 for name in metric_names]20 model_names = list(metrics.get("cross_validation_accuracy", {}).keys())21 model_scores = [metrics["cross_validation_accuracy"][name] * 100 for name in model_names]22 23 attempts = get_attempt_history()24 user_overview = get_user_overview()25 granted_count = sum(1 for attempt in attempts if attempt["result"] == "Access Granted")26 denied_count = sum(1 for attempt in attempts if attempt["result"] != "Access Granted")27 user_names = [user["username"] for user in user_overview]28 sample_counts = [user["sample_count"] for user in user_overview]29 30 plt.style.use("dark_background")31 figure, axes = plt.subplots(2, 2, figsize=(15, 10), constrained_layout=True)32 figure.patch.set_facecolor("#081420")33 34 performance_bars = axes[0, 0].bar(metric_names, metric_values, color=["#52b788", "#4ea8de", "#ff9f1c", "#c77dff"])35 axes[0, 0].set_ylim(0, 100)36 axes[0, 0].set_ylabel("Percentage")37 axes[0, 0].set_title("Model Performance Metrics")38 axes[0, 0].set_facecolor("#102235")39 axes[0, 0].tick_params(axis="x", labelrotation=0, pad=8)40 41 for bar, value in zip(performance_bars, metric_values):42 axes[0, 0].text(bar.get_x() + bar.get_width() / 2, value + 1, f"{value:.2f}%", ha="center")43 44 if model_names:45 comparison_bars = axes[0, 1].bar(model_names, model_scores, color=["#00b4d8", "#90be6d", "#f94144"])46 axes[0, 1].set_ylim(0, 100)47 axes[0, 1].set_ylabel("CV Accuracy")48 axes[0, 1].set_title("Model Comparison")49 axes[0, 1].set_facecolor("#102235")50 axes[0, 1].tick_params(axis="x", labelrotation=12, pad=8)51 for bar, value in zip(comparison_bars, model_scores):52 axes[0, 1].text(bar.get_x() + bar.get_width() / 2, value + 1, f"{value:.2f}%", ha="center")53 else:54 axes[0, 1].text(0.5, 0.5, "No comparison data", ha="center", va="center")55 axes[0, 1].set_facecolor("#102235")56 axes[0, 1].set_xticks([])57 axes[0, 1].set_yticks([])58 59 axes[1, 0].set_facecolor("#102235")60 if granted_count or denied_count:61 axes[1, 0].pie(62 [max(granted_count, 0.001), max(denied_count, 0.001)],63 labels=["Granted", "Denied"],64 autopct="%1.1f%%",65 colors=["#52b788", "#f94144"],66 startangle=110,67 )68 axes[1, 0].set_title("Authentication Outcomes")69 else:70 axes[1, 0].text(0.5, 0.5, "No attempt history", ha="center", va="center")71 axes[1, 0].set_xticks([])72 axes[1, 0].set_yticks([])73 74 axes[1, 1].set_facecolor("#102235")75 if user_names:76 user_bars = axes[1, 1].bar(user_names, sample_counts, color=["#00b4d8", "#90be6d", "#ffd166", "#c77dff"][: len(user_names)])77 axes[1, 1].set_title("User Sample Counts")78 axes[1, 1].set_ylabel("Samples")79 axes[1, 1].tick_params(axis="x", labelrotation=12, pad=8)80 for bar, value in zip(user_bars, sample_counts):81 axes[1, 1].text(bar.get_x() + bar.get_width() / 2, value + 0.2, str(value), ha="center")82 else:83 axes[1, 1].text(0.5, 0.5, "No user profiles", ha="center", va="center")84 axes[1, 1].set_xticks([])85 axes[1, 1].set_yticks([])86 87 plt.show()88 89 90if __name__ == "__main__":91 main()92 