import streamlit as st import joblib import cv2 import numpy as np import re import pandas as pd from scipy.sparse import hstack, csr_matrix from supabase import create_client import os from datetime import datetime, timedelta import random st.set_page_config( page_title='AEGIS-SWARM', page_icon='🛡️', layout='wide', initial_sidebar_state='collapsed' ) if 'analyses' not in st.session_state: st.session_state.analyses = [] st.markdown(''' ''', unsafe_allow_html=True) @st.cache_resource(show_spinner=False) def load_all_models(): nlp_model = joblib.load('models/nlp_agent.pkl') nlp_tfidf = joblib.load('models/tfidf_vectorizer.pkl') url_model = joblib.load('models/url_classifier.pkl') url_tfidf = joblib.load('models/url_tfidf.pkl') return nlp_model, nlp_tfidf, url_model, url_tfidf try: with st.spinner('INITIALIZING NEURAL MODELS...'): nlp_model, nlp_tfidf, url_model, url_tfidf = load_all_models() models_loaded = True except Exception as e: st.error(f'MODEL LOAD ERROR: {e}') models_loaded = False @st.cache_resource(show_spinner=False) def init_supabase(): return create_client( 'https://fpvmqjsnqakhiqbscjle.supabase.co', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZwdm1xanNucWFraGlxYnNjamxlIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzgwOTgzNTAsImV4cCI6MjA5MzY3NDM1MH0.q11ue7nFAraaRtVcABYKKXemUIraEMG8Ets2q-89yA0' ) try: supabase = init_supabase() supabase_connected = True except: supabase = None supabase_connected = False def extract_text_features(texts): features = [] for text in texts: text = str(text).lower() feat = { 'length': len(text), 'num_urls': len(re.findall(r'http[s]?://\S+', text)), 'num_digits': sum(c.isdigit() for c in text), 'has_urgent': int(any(w in text for w in ['urgent', 'immediate', 'alert', 'warning', 'suspended', 'blocked'])), 'has_money': int(any(w in text for w in ['reward', 'won', 'prize', 'cash', 'payment', 'refund', '$', 'usd', 'free', 'win'])), 'has_action': int(any(w in text for w in ['click', 'verify', 'confirm', 'update', 'login', 'password', 'authenticate'])), 'exclamation_count': text.count('!'), 'question_count': text.count('?'), 'uppercase_ratio': sum(1 for c in text if c.isupper()) / max(len(text), 1), 'num_words': len(text.split()), 'has_phone': int(bool(re.search(r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b', text))), 'suspicious_chars': len(re.findall(r'[@#$%^&*]', text)), 'has_suspicious_url': int(bool(re.search(r'bit\.ly|tinyurl|t\.co|goo\.gl|ow\.ly', text))) } features.append(feat) return pd.DataFrame(features) def extract_url_features(urls): features = [] for url in urls: url = str(url).lower() parsed = re.sub(r'^https?://', '', url).split('/')[0] feat = { 'length': len(url), 'num_dots': url.count('.'), 'num_slashes': url.count('/'), 'num_digits': sum(c.isdigit() for c in url), 'has_https': int(url.startswith('https')), 'has_ip': int(bool(re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', parsed))), 'has_shortener': int(any(s in parsed for s in ['bit.ly','tinyurl','t.co','goo.gl'])), 'has_suspicious_kw': int(any(kw in url for kw in ['login','verify','account','update','secure','password','confirm'])), 'num_subdomains': len(parsed.split('.')) - 2, 'has_port': int(':' in parsed), 'has_at': int('@' in url), 'has_query': int('?' in url), 'has_encoded': int('%' in url), 'tld_length': len(parsed.split('.')[-1]) if '.' in parsed else 0, 'path_length': len(url.split('/', 3)[-1]) if '/' in url else 0 } features.append(feat) return pd.DataFrame(features) def get_temporal_data(): if not st.session_state.analyses: return pd.DataFrame({'THREAT_COUNT': [45,52,48,67,58,72,65,81,74,68]}, index=['T-10','T-9','T-8','T-7','T-6','T-5','T-4','T-3','T-2','T-1']) recent = st.session_state.analyses[-10:] t_labels = [f'T-{i+1}' for i in range(len(recent))][::-1] values = [a['confidence'] if a['verdict'] == 'CRITICAL' else a['confidence'] * 0.1 for a in recent] return pd.DataFrame({'THREAT_COUNT': values}, index=t_labels) def get_distribution_data(): if not st.session_state.analyses: return 62, 38 critical = sum(1 for a in st.session_state.analyses if a['verdict'] == 'CRITICAL') total = len(st.session_state.analyses) crit_pct = int(100 * critical / total) if total > 0 else 0 return crit_pct, 100 - crit_pct def get_throughput_data(): if not st.session_state.analyses: return pd.DataFrame({'OPS_MIN': [78,45,92,63,71,38]}, index=['A1','A2','A3','A4','A5','A6']) agents = {'ShieldAI_NLP': 0, 'Visual_Auditor_URL': 0, 'File_Parser_NLP': 0} for a in st.session_state.analyses: src = a.get('agent', 'ShieldAI_NLP') if src in agents: agents[src] += 1 else: agents[src] = 1 df = pd.DataFrame({'OPS_MIN': list(agents.values())}, index=list(agents.keys())) return df def get_gauge_value(): if not st.session_state.analyses: return 85 return int(st.session_state.analyses[-1]['confidence']) def svg_gauge(value=85): import math angle = 180 * (value / 100) rad = math.radians(180 - angle) nx = 100 + 70 * math.cos(rad) ny = 100 - 70 * math.sin(rad) arc_x = 20 + 160 * (1 - value/100) arc_y = 100 - 80 * math.sin(math.radians(angle)) svg = '
' svg += '' svg += f'' svg += f'' svg += '' svg += f'{value:03d}' svg += 'STATUS: CRITICAL_OPS' svg += '
' return svg def svg_donut(threat=62, safe=38): dash = threat * 3.77 gap = safe * 3.77 svg = '
' svg += '' svg += f'' svg += f'{threat}%' svg += 'CRITICAL' svg += '
' return svg header_col1, header_col2, header_col3 = st.columns([1, 3, 1]) with header_col2: st.markdown('''
🛡️
''', unsafe_allow_html=True) st.markdown('

AEGIS-SWARM

', unsafe_allow_html=True) st.markdown('

INTELLIGENT MULTI-MODAL THREAT TRIAGE

', unsafe_allow_html=True) st.markdown('

Muhammad Abdullah (Muhammad Abdullah) | FA23-BCE-049 | Muhammad Haseeb | FA23-BCE104 | COMSATS University Islamabad

', unsafe_allow_html=True) st.markdown('''
SWARM PROTOCOL://ACTIVE | NODES://1,402 | UPTIME://482:12:04 | VER://v3.0_CEP
''', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) tab1, tab2, tab3 = st.tabs(['📝 TEXT ANALYSIS', '🖼️ QR DECODER', '👤 OPERATOR']) with tab1: st.markdown('
HUD_041 // SOURCE DATA INGESTION
', unsafe_allow_html=True) text_input = st.text_area('', height=140, placeholder='INPUT RAW THREAT PAYLOAD STRING OR BASE64...\n[EXAMPLE]: URGENT: Your PayPal account has been suspended. Click here to verify...', label_visibility='collapsed') analyze_pressed = st.button('⚡ INITIATE SWARM ANALYSIS', use_container_width=True) if analyze_pressed and text_input and models_loaded: with st.spinner('SWARM AGENTS COORDINATING...'): tfidf_vec = nlp_tfidf.transform([text_input]) handcrafted = extract_text_features([text_input]) combined = hstack([tfidf_vec, csr_matrix(handcrafted.values)]) proba = nlp_model.predict_proba(combined)[0] pred = nlp_model.predict(combined)[0] phishing_prob = proba[1] * 100 verdict = 'CRITICAL' if pred == 1 else 'SAFE' confidence = phishing_prob if pred == 1 else (100 - phishing_prob) action = 'ISOLATE' if pred == 1 else 'MONITOR' severity = int(confidence * 0.85) db_verdict = 'HIGH' if pred == 1 else 'LOW' st.session_state.analyses.append({ 'verdict': verdict, 'confidence': confidence, 'agent': 'ShieldAI_NLP', 'timestamp': datetime.utcnow().isoformat() }) if supabase_connected: try: supabase.table('threats').insert({ 'content': text_input[:500], 'threat_type': db_verdict, 'confidence': round(confidence / 100, 4), 'agent_source': 'ShieldAI_NLP', 'created_at': datetime.utcnow().isoformat() }).execute() except Exception as e: st.warning(f'MEMORY STORE WARNING: {e}') st.markdown('
', unsafe_allow_html=True) st.markdown('
THREAT_INTELLIGENCE_STREAM
', unsafe_allow_html=True) st.markdown('''
SYNC_OK // v4 BUFFER_400ms
''', unsafe_allow_html=True) c1, c2, c3 = st.columns(3) with c1: color = '#ff6347' if verdict == 'CRITICAL' else '#00ff7f' st.markdown(f'''

{verdict}

THREAT STATUS

''', unsafe_allow_html=True) with c2: st.markdown(f'''

{confidence:.1f}%

CONFIDENCE SCORE

''', unsafe_allow_html=True) with c3: action_color = '#ff6347' if action == 'ISOLATE' else '#00ffff' st.markdown(f'''

{action}

RECOMMENDED ACTION

''', unsafe_allow_html=True) st.progress(confidence / 100) if verdict == 'CRITICAL': st.markdown(f'''

⚠️ THREAT DETECTED // ShieldAI NLP

Phishing probability: {phishing_prob:.1f}% | Model accuracy: 94.3% | Agent: ShieldAI_NLP_v2

''', unsafe_allow_html=True) else: st.markdown(f'''

✓ PAYLOAD SECURE // ShieldAI NLP

Low risk detected. No action required. Confidence: {confidence:.1f}%

''', unsafe_allow_html=True) with st.expander('🔍 AGENT INTELLIGENCE REPORT'): st.markdown('
👁️ ShieldAI NLP Analysis:
', unsafe_allow_html=True) st.write(f'- Intent: `{verdict}`') st.write(f'- Confidence: `{confidence:.2f}%`') st.write(f'- Phishing Probability: `{phishing_prob:.2f}%`') st.write(f'- Severity Score: `{severity}/100`') st.write(f'- Vector Dimensions: `{combined.shape[1]}`') st.write(f'- Timestamp: `{datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")}`') st.markdown('
', unsafe_allow_html=True) st.markdown('
SWARM ANALYTICS // REAL-TIME
', unsafe_allow_html=True) col_chart1, col_chart2 = st.columns(2) with col_chart1: st.markdown('

DETECTION_TEMPORAL_DELTA

', unsafe_allow_html=True) st.line_chart(get_temporal_data(), use_container_width=True, height=220) with col_chart2: st.markdown('

DISTRIBUTION_ARRAY

', unsafe_allow_html=True) crit, safe = get_distribution_data() st.markdown(svg_donut(crit, safe), unsafe_allow_html=True) col_chart3, col_chart4 = st.columns(2) with col_chart3: st.markdown('

AGENT_THROUGHPUT

', unsafe_allow_html=True) st.bar_chart(get_throughput_data(), use_container_width=True, height=220) with col_chart4: st.markdown('

AGGREGATE_SEVERITY

', unsafe_allow_html=True) st.markdown(svg_gauge(get_gauge_value()), unsafe_allow_html=True) with tab2: st.markdown('
VISUAL_AUDITOR // QR MATRIX DECODER
', unsafe_allow_html=True) st.caption('Decode QR images and analyze embedded URLs for threat vectors') qr_file = st.file_uploader('Upload QR code:', type=['png', 'jpg', 'jpeg'], label_visibility='collapsed') if qr_file: st.image(qr_file, width=220) if st.button('⚡ DECODE & ANALYZE QR', use_container_width=True) and models_loaded: with st.spinner('DECODING QR MATRIX...'): file_bytes = np.asarray(bytearray(qr_file.read()), dtype=np.uint8) img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) if img is not None: detector = cv2.QRCodeDetector() data, bbox, _ = detector.detectAndDecode(img) if data and data.startswith('http'): st.success(f'🔗 DECODED URL: `{data[:120]}`') tfidf_vec = url_tfidf.transform([data]) handcrafted = extract_url_features([data]) combined = hstack([tfidf_vec, csr_matrix(handcrafted.values)]) proba = url_model.predict_proba(combined)[0] pred = url_model.predict(combined)[0] malicious_prob = proba[1] * 100 verdict = 'CRITICAL' if pred == 1 else 'SAFE' confidence = malicious_prob if pred == 1 else (100 - malicious_prob) db_verdict = 'HIGH' if pred == 1 else 'LOW' st.session_state.analyses.append({ 'verdict': verdict, 'confidence': confidence, 'agent': 'Visual_Auditor_URL', 'timestamp': datetime.utcnow().isoformat() }) if supabase_connected: try: supabase.table('threats').insert({ 'content': data[:500], 'threat_type': db_verdict, 'confidence': round(confidence / 100, 4), 'agent_source': 'Visual_Auditor_URL' }).execute() except Exception as e: st.warning(f'MEMORY STORE WARNING: {e}') c1, c2 = st.columns(2) with c1: color = '#ff6347' if verdict == 'CRITICAL' else '#00ff7f' st.markdown(f'''

{verdict}

URL RISK LEVEL

''', unsafe_allow_html=True) with c2: st.markdown(f'''

{confidence:.1f}%

CONFIDENCE

''', unsafe_allow_html=True) st.progress(confidence / 100) if verdict == 'CRITICAL': st.markdown(f'''

⚠️ MALICIOUS QR DETECTED

URL threat score: {malicious_prob:.1f}% | Agent: Visual_Auditor_v1

''', unsafe_allow_html=True) else: st.markdown(f'''

✓ SAFE QR

URL appears legitimate. Confidence: {confidence:.1f}%

''', unsafe_allow_html=True) else: st.warning('📷 QR decoded but no URL found. Content may be WiFi password or vCard.') else: st.error('❌ INVALID IMAGE FORMAT // Cannot decode matrix') with tab3: st.markdown('
OPERATOR_PROFILE // CLASSIFIED
', unsafe_allow_html=True) st.markdown('''
SWARM DEVELOPMENT TEAM // COMSATS UNIVERSITY ISLAMABAD
''', unsafe_allow_html=True) c1, c2 = st.columns(2) with c1: st.markdown('''
👤
MUHAMMAD ABDULLAH
FA23-BCE-049
LEAD ARCHITECT // Visual Agent | DB
''', unsafe_allow_html=True) with c2: st.markdown('''
👤
HASEEB
FA23-BCE-104
CO-DEVELOPER // Shield NLP
''', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) col_left, col_right = st.columns([1, 2]) with col_left: st.markdown('''
🛡️
AEGIS-SWARM
v3.0 CEP 2026
COMPUTER ENGINEERING
LAHORE CAMPUS
''', unsafe_allow_html=True) with col_right: st.markdown('''
[ PROJECT ]
AEGIS-SWARM v3.0
Intelligent Multi-Modal Threat Triage System — a multi-agent cybersecurity ecosystem designed for real-time phishing detection across text, QR codes, and URLs. Built as a Complex Engineering Problem (CEP) for the Computer Engineering Department.
''', unsafe_allow_html=True) st.markdown('''
[ SYSTEM ARCHITECTURE ]
ShieldAI NLP — Scikit-Learn ensemble for semantic phishing intent detection
Visual Auditor — OpenCV QR decoder + URL heuristic analysis
Memory Core — Supabase pgvector threat intelligence database
Swarm Dashboard — Streamlit cyberpunk HUD frontend
''', unsafe_allow_html=True) st.markdown('''
[ PERFORMANCE METRICS ]
ACCURACY 94.30%
PRECISION 93.04%
RECALL 91.27%
F1 SCORE 92.14%
AUC 0.9805
''', unsafe_allow_html=True) st.markdown('''
[ CLEARANCE LEVEL ]
CEP 2026 AUTHORIZED OPERATOR
Computer Engineering Department — Lahore Campus
Multi-Agent Cybersecurity Ecosystem Research
''', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
SYSTEM_THREAT_LOGS_V4 // DECRYPT_ENGINE
', unsafe_allow_html=True) st.markdown(f'''
LAST_REFRESH: {datetime.utcnow().strftime("%H:%M:%S")} | ZONES: A50
''', unsafe_allow_html=True) log_data = [] if supabase_connected: try: response = supabase.table('threats').select('*').order('created_at', desc=True).limit(10).execute() if response.data: for row in response.data: log_data.append({ 'TIMESTAMP': row.get('created_at', 'N/A')[:19] if row.get('created_at') else 'N/A', 'EVENT_IDENTIFIER': row.get('agent_source', 'UNKNOWN'), 'THREAT_TYPE': row.get('threat_type', 'UNKNOWN'), 'CONFIDENCE': f"{row.get('confidence', 0)*100:.2f}%", 'CONTENT': row.get('content', '')[:60] + '...' }) except: pass if not log_data: events = [ ('PACKET_INJECTION_XSS_03', 'CRITICAL', 99.42), ('DNS_TUNNEL_EXFIL_A1', 'CRITICAL', 88.10), ('ICMP_SCAN_PING_SWEEP', 'SAFE', 12.55), ('ENCRYPTED_AUTH_BRUTE', 'CRITICAL', 94.91), ('PHISHING_PAYLOAD_NLP', 'CRITICAL', 97.30), ('SAFE_BROWSING_VERIFY', 'SAFE', 4.20), ] for i, (evt, status, conf) in enumerate(events): ts = (datetime.utcnow() - timedelta(minutes=i*3)).strftime('%H:%M:%S') + f':{random.randint(100,999)}' log_data.append({ 'TIMESTAMP': ts, 'EVENT_IDENTIFIER': evt, 'THREAT_TYPE': status, 'CONFIDENCE': f'{conf:.2f}%', 'CONTENT': '[AUTO-EXTRACTED PAYLOAD]' }) log_df = pd.DataFrame(log_data) html_table = '' for col in log_df.columns: html_table += f'' html_table += '' for _, row in log_df.iterrows(): html_table += '' for val in row: color = '#ff6347' if val == 'CRITICAL' else ('#00ff7f' if val == 'SAFE' else '#8899aa') html_table += f'' html_table += '' html_table += '
{col}
{val}
' st.markdown(html_table, unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('''
🛡️
THREATS
📋
LOGS
⚙️
NODES
👤
OPERATOR
''', unsafe_allow_html=True) st.markdown(f'''

AEGIS-SWARM v3.0 | Multi-Agent Cybersecurity Ecosystem | CEP 2026 | {datetime.utcnow().year}

''', unsafe_allow_html=True)