Spaces:
Runtime error
Runtime error
File size: 16,098 Bytes
a50dd05 da7d74b 42dc091 09b3e31 d90bb5e 42dc091 d90bb5e 42dc091 d90bb5e 42dc091 09b3e31 2747353 09b3e31 42dc091 da7d74b d90bb5e 42dc091 09b3e31 42dc091 da7d74b 09b3e31 62a31d8 2747353 d90bb5e 2747353 d90bb5e 2747353 d90bb5e 42dc091 09b3e31 da7d74b 42dc091 09b3e31 da7d74b 42dc091 09b3e31 42dc091 09b3e31 42dc091 09b3e31 42dc091 2747353 09b3e31 42dc091 09b3e31 62a31d8 09b3e31 62a31d8 d90bb5e d453c46 d90bb5e 4f54bf0 d90bb5e 4f54bf0 d90bb5e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import logging
import re
from flask import Flask, request, jsonify, render_template_string, redirect, url_for
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user
import sqlite3
from functools import wraps
from datetime import datetime
import bleach
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Flask app setup
app = Flask(__name__)
app.secret_key = 'secure_gov_key_2025' # Replace with a secure key in production
# Initialize Flask-Login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
# Model configuration
MODEL_NAME = "cybersectony/phishing-email-detection-distilbert_v2.4.1"
# Explanation of labels and their values
"""
Labels and Their Meanings:
- Legitimate: The email is safe and likely from a trusted source.
- Phishing: The email is a scam attempting to steal personal information.
- Suspicious: The email has questionable content and may be unsafe.
- Spam: The email is unwanted promotional or junk content.
Each label has a percentage (0-100%) showing the model's confidence.
Higher percentages indicate greater certainty.
"""
# Global variables for model and tokenizer
tokenizer = None
model = None
# User class for Flask-Login
class User(UserMixin):
def __init__(self, user_id, role):
self.id = user_id
self.role = role
# Database setup
def init_db():
with sqlite3.connect('phishguardian.db') as conn:
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users
(id TEXT PRIMARY KEY, username TEXT, password TEXT, role TEXT)''')
c.execute('''CREATE TABLE IF NOT EXISTS analysis_logs
(id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT, email_text TEXT,
result TEXT, timestamp TEXT)''')
# Add default admin user (password: 'admin123' for demo, use hashed passwords in production)
c.execute("INSERT OR IGNORE INTO users (id, username, password, role) VALUES (?, ?, ?, ?)",
('admin1', 'admin', 'admin123', 'Admin'))
conn.commit()
# Load user for Flask-Login
@login_manager.user_loader
def load_user(user_id):
with sqlite3.connect('phishguardian.db') as conn:
c = conn.cursor()
c.execute("SELECT id, role FROM users WHERE id = ?", (user_id,))
user = c.fetchone()
if user:
return User(user[0], user[1])
return None
# RBAC decorator
def role_required(*roles):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not current_user.is_authenticated:
return redirect(url_for('login'))
if current_user.role not in roles:
return render_template_string("<h1>403 Forbidden</h1><p>Unauthorized role.</p>")
return f(*args, **kwargs)
return decorated_function
return decorator
def load_model():
"""Load the model and tokenizer with basic error handling"""
global tokenizer, model
try:
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
logger.info("Model loaded successfully!")
return True
except Exception as e:
logger.error(f"Error loading model: {e}")
return False
def is_valid_email_text(text):
"""Basic validation for email-like text"""
if not text or not text.strip():
return False, "Please enter some email text."
if len(text.strip()) < 10:
return False, "Text too short for analysis."
if len(text.split()) < 3 or not re.search(r"[a-zA-Z]{3,}", text):
return False, "Text appears incoherent or not email-like."
return True, ""
def get_colored_bar(percentage):
"""Create a colored bar based on percentage"""
if percentage >= 85:
color = "🟢"
elif percentage >= 50:
color = "🟡"
else:
color = "⚪"
bar_length = max(1, int(percentage / 5)) # Scale to 20 characters
return color * bar_length + "⚪" * (20 - bar_length)
def predict_email(email_text, user_id):
"""Prediction with actual labels and colored bars"""
# Sanitize input
email_text = bleach.clean(email_text, tags=[], strip=True)
# Input validation
valid, message = is_valid_email_text(email_text)
if not valid:
return f"⚠️ Error: {message}"
# Check if model is loaded
if tokenizer is None or model is None:
if not load_model():
return "❌ Error: Failed to load the model."
try:
# Tokenize input
inputs = tokenizer(
email_text,
return_tensors="pt",
truncation=True,
max_length=512,
padding=True
)
# Get prediction
with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0].tolist()
# Define actual labels (handle 2 or 4 classes)
labels = (model.config.id2label if hasattr(model.config, 'id2label') and model.config.id2label
else {0: "Legitimate", 1: "Phishing", 2: "Suspicious", 3: "Spam"} if len(probs) == 4
else {0: "Legitimate", 1: "Phishing"})
# Map probabilities to labels
results = {labels[i]: probs[i] * 100 for i in range(len(probs))}
# Get top prediction
max_label, max_prob = max(results.items(), key=lambda x: x[1])
# Risk levels with 85% threshold
if "phishing" in max_label.lower() or "suspicious" in max_label.lower():
risk_level = "⚠️ Risky" if max_prob >= 85 else "⚡ Low Risk"
elif "spam" in max_label.lower():
risk_level = "🗑️ Spam" if max_prob >= 85 else "⚡ Low Risk"
else:
risk_level = "✅ Safe" if max_prob >= 85 else "❓ Uncertain"
# Format output
output = f"Result: {risk_level}\n"
output += f"Top Prediction: {max_label} ({max_prob:.1f}%)\n"
output += "Details:\n"
for label, prob in sorted(results.items(), key=lambda x: x[1], reverse=True):
output += f"{label}: {prob:.1f}% {get_colored_bar(prob)}\n"
# Simple recommendation
if "phishing" in max_label.lower() or "suspicious" in max_label.lower():
output += "Advice: Avoid clicking links or sharing info."
elif "spam" in max_label.lower():
output += "Advice: Mark as spam or delete."
else:
output += "Advice: Appears safe, but stay cautious."
# Log analysis
with sqlite3.connect('phishguardian.db') as conn:
c = conn.cursor()
c.execute("INSERT INTO analysis_logs (user_id, email_text, result, timestamp) VALUES (?, ?, ?, ?)",
(user_id, email_text[:1000], output, datetime.utcnow().isoformat()))
conn.commit()
return output
except Exception as e:
logger.error(f"Error during prediction: {e}")
return f"❌ Error: Analysis failed - {str(e)}"
# Flask routes
@app.route('/')
def index():
if not current_user.is_authenticated:
return redirect(url_for('login'))
return render_template_string("""
<!DOCTYPE html>
<html>
<head>
<title>PhishGuardian - MDA Email System</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; }
h1 { color: #333; }
.container { max-width: 800px; margin: auto; }
textarea { width: 100%; height: 200px; margin-bottom: 10px; }
button { padding: 10px 20px; margin-right: 10px; }
pre { background-color: #fff; padding: 15px; border: 1px solid #ddd; }
.error { color: red; }
</style>
</head>
<body>
<div class="container">
<h1>🛡️ PhishGuardian - MDA Email System</h1>
<p>Analyze emails for safety. Paste email text below.</p>
<p>Labels: Legitimate (safe), Phishing (scam), Suspicious (questionable), Spam (junk). Percentages show confidence (0-100%).</p>
{% if current_user.is_authenticated %}
<p>Logged in as: {{ current_user.id }} ({{ current_user.role }}) | <a href="{{ url_for('logout') }}">Logout</a></p>
{% if current_user.role in ['Admin', 'Analyst'] %}
<form method="POST" action="{{ url_for('analyze') }}">
<textarea name="email_text" placeholder="Paste email here..."></textarea>
<button type="submit">🔍 Check</button>
<button type="button" onclick="document.querySelector('textarea').value=''">🗑️ Clear</button>
</form>
{% if result %}
<h3>Results</h3>
<pre>{{ result }}</pre>
{% endif %}
{% endif %}
{% if current_user.role in ['Admin', 'Auditor'] %}
<p><a href="{{ url_for('view_logs') }}">View Analysis Logs</a></p>
{% endif %}
{% if current_user.role == 'Admin' %}
<p><a href="{{ url_for('manage_users') }}">Manage Users</a></p>
{% endif %}
{% endif %}
</div>
</body>
</html>
""", result=request.args.get('result', ''))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = bleach.clean(request.form['username'])
password = bleach.clean(request.form['password'])
with sqlite3.connect('phishguardian.db') as conn:
c = conn.cursor()
c.execute("SELECT id, role FROM users WHERE username = ? AND password = ?", (username, password))
user = c.fetchone()
if user:
login_user(User(user[0], user[1]))
return redirect(url_for('index'))
return render_template_string("<h1>Login Failed</h1><p>Invalid credentials.</p>")
return render_template_string("""
<!DOCTYPE html>
<html>
<head>
<title>Login - PhishGuardian</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; }
.container { max-width: 400px; margin: auto; }
input { width: 100%; padding: 10px; margin-bottom: 10px; }
button { padding: 10px 20px; }
</style>
</head>
<body>
<div class="container">
<h1>Login</h1>
<form method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
""")
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/analyze', methods=['POST'])
@role_required('Admin', 'Analyst')
def analyze():
email_text = request.form['email_text']
result = predict_email(email_text, current_user.id)
return redirect(url_for('index', result=result))
@app.route('/logs')
@role_required('Admin', 'Auditor')
def view_logs():
with sqlite3.connect('phishguardian.db') as conn:
c = conn.cursor()
c.execute("SELECT user_id, email_text, result, timestamp FROM analysis_logs ORDER BY timestamp DESC")
logs = c.fetchall()
logs_html = "<h3>Analysis Logs</h3><ul>" + "".join(
f"<li><b>{log[3]}</b> | User: {log[0]} | Email: {log[1][:50]}... | Result: {log[2][:100]}...</li>"
for log in logs
) + "</ul>"
return render_template_string("""
<!DOCTYPE html>
<html>
<head>
<title>Logs - PhishGuardian</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; }
.container { max-width: 800px; margin: auto; }
ul { list-style-type: none; padding: 0; }
li { margin-bottom: 10px; }
</style>
</head>
<body>
<div class="container">
<h1>Analysis Logs</h1>
<p><a href="{{ url_for('index') }}">Back to Home</a></p>
{{ logs_html | safe }}
</div>
</body>
</html>
""", logs_html=logs_html)
@app.route('/users', methods=['GET', 'POST'])
@role_required('Admin')
def manage_users():
if request.method == 'POST':
username = bleach.clean(request.form['username'])
password = bleach.clean(request.form['password'])
role = bleach.clean(request.form['role'])
user_id = f"user_{datetime.utcnow().timestamp()}"
with sqlite3.connect('phishguardian.db') as conn:
c = conn.cursor()
c.execute("INSERT INTO users (id, username, password, role) VALUES (?, ?, ?, ?)",
(user_id, username, password, role))
conn.commit()
return redirect(url_for('manage_users'))
with sqlite3.connect('phishguardian.db') as conn:
c = conn.cursor()
c.execute("SELECT id, username, role FROM users")
users = c.fetchall()
users_html = "<h3>Users</h3><ul>" + "".join(
f"<li>ID: {user[0]} | Username: {user[1]} | Role: {user[2]}</li>" for user in users
) + "</ul>"
return render_template_string("""
<!DOCTYPE html>
<html>
<head>
<title>Manage Users - PhishGuardian</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; }
.container { max-width: 800px; margin: auto; }
input, select { width: 100%; padding: 10px; margin-bottom: 10px; }
button { padding: 10px 20px; }
ul { list-style-type: none; padding: 0; }
</style>
</head>
<body>
<div class="container">
<h1>Manage Users</h1>
<p><a href="{{ url_for('index') }}">Back to Home</a></p>
<form method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<select name="role">
<option value="Admin">Admin</option>
<option value="Analyst">Analyst</option>
<option value="Auditor">Auditor</option>
</select>
<button type="submit">Add User</button>
</form>
{{ users_html | safe }}
</div>
</body>
</html>
""", users_html=users_html)
# Example emails
example_legitimate = """Dear Customer,
Thank you for your purchase from TechStore. Your order #ORD-2024-001234 is processed.
Order Details:
- Product: Wireless Headphones
- Amount: $79.99
- Delivery: 3-5 days
Best regards,
TechStore"""
example_phishing = """URGENT!!!
Your account is COMPROMISED! Click here to secure: http://fake-site.com/verify
Act NOW or your account will be suspended!
Security Team"""
example_neutral = """Hi team,
Reminder: meeting tomorrow at 2 PM. Bring project updates.
Thanks,
Sarah"""
# Initialize database and load model
init_db()
load_model()
if __name__ == "__main__":
app.run(ssl_context='adhoc', host='0.0.0.0', port=5000) |