abdull4h commited on
Commit
e352d99
·
verified ·
1 Parent(s): fab8814

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +210 -87
app.py CHANGED
@@ -4,8 +4,13 @@ from transformers import pipeline
4
  import torch
5
  import time
6
  import re
 
7
 
8
- # Fixed Professional Dashboard CSS - Complete Textbox Display
 
 
 
 
9
  professional_css = """
10
  /* Professional SOC Dashboard - Fixed */
11
  .gradio-container {
@@ -321,88 +326,199 @@ model_status = "🔄 Loading..."
321
 
322
  @spaces.GPU
323
  def load_model():
324
- """Load the best available model"""
325
  global pipe, model_status
326
 
327
- models_to_try = [
328
- "openai/gpt-oss-20b",
329
- "microsoft/DialoGPT-large",
330
- "microsoft/DialoGPT-medium"
331
- ]
332
-
333
- for model_name in models_to_try:
334
- try:
335
- pipe = pipeline(
336
- "text-generation",
337
- model=model_name,
338
- torch_dtype="auto",
339
- device_map="auto" if torch.cuda.is_available() else None,
340
- trust_remote_code=True
341
- )
342
- pipe("Test", max_new_tokens=5, do_sample=False)
343
- model_status = f"✅ {model_name.split('/')[-1]} Ready"
344
- return model_status
345
- except:
346
- continue
347
-
348
- model_status = "⚠️ Fallback Mode"
349
- return model_status
 
 
 
 
 
 
 
 
 
 
 
 
350
 
351
  @spaces.GPU
352
  def detect_threats(logs, sensitivity):
353
- """Task 1: Threat Detection"""
 
 
354
  if not logs.strip():
355
  return "Please provide log data.", "⚠️ No input"
356
 
357
  start_time = time.time()
358
 
359
- # Quick pattern-based detection for demo
360
- threats = []
361
- if re.search(r'failed.*login|authentication.*failed', logs, re.IGNORECASE):
362
- threats.append("🚨 Brute Force Attack")
363
- if re.search(r'powershell.*-enc|cmd\.exe', logs, re.IGNORECASE):
364
- threats.append("🚨 Malicious Script Execution")
365
- if re.search(r'suspicious.*ip|unusual.*connection', logs, re.IGNORECASE):
366
- threats.append("🚨 Suspicious Network Activity")
367
-
368
- if threats:
369
- result = f"""🚨 THREATS DETECTED
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
 
371
- DETECTED THREATS:
372
- {chr(10).join(threats)}
 
 
 
 
373
 
374
- SEVERITY: {"Critical" if len(threats) > 2 else "High"}
375
- CONFIDENCE: {85 + len(threats) * 5}%
376
 
377
- IMMEDIATE ACTIONS:
378
- Isolate affected systems
379
- • Preserve evidence
380
- • Escalate to L2 analyst
381
- Implement containment"""
382
- status = "🚨 THREATS DETECTED"
383
- else:
384
- result = """✅ NO THREATS DETECTED
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
- ANALYSIS: Clean logs
387
- CONFIDENCE: 75%
388
- STATUS: Normal operation
389
- RECOMMENDATION: Continue monitoring"""
390
- status = "✅ CLEAN"
391
-
392
- time_taken = round(time.time() - start_time, 1)
393
- return result, f"{status} ({time_taken}s)"
394
 
395
  @spaces.GPU
396
  def analyze_threat(threat, level):
397
- """Task 2: Analyst Assistant"""
 
 
398
  if not threat.strip():
399
  return "Please describe the threat.", "⚠️ No input"
400
 
401
  start_time = time.time()
402
 
403
- templates = {
404
- "L1": f"""🚨 L1 TRIAGE
405
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406
  THREAT: {threat[:60]}...
407
 
408
  IMMEDIATE ACTIONS:
@@ -414,8 +530,8 @@ IMMEDIATE ACTIONS:
414
  DECISION: Escalate to L2
415
  PRIORITY: High""",
416
 
417
- "L2": f"""🔍 L2 INVESTIGATION
418
-
419
  INCIDENT: {threat[:60]}...
420
 
421
  INVESTIGATION PLAN:
@@ -427,8 +543,8 @@ INVESTIGATION PLAN:
427
 
428
  NEXT STEPS: Deploy monitoring""",
429
 
430
- "L3": f"""🎯 L3 STRATEGIC ANALYSIS
431
-
432
  THREAT ASSESSMENT: {threat[:60]}...
433
 
434
  STRATEGIC RESPONSE:
@@ -439,19 +555,26 @@ STRATEGIC RESPONSE:
439
  • Security improvements
440
 
441
  RECOMMENDATION: Full IR activation"""
442
- }
443
-
444
- result = templates.get(level, templates["L2"])
445
- time_taken = round(time.time() - start_time, 1)
446
- return result, f"✅ {level} Complete ({time_taken}s)"
 
 
 
 
 
447
 
448
  # Sample data
449
- SAMPLE_LOGS = """2025-08-12 14:30:15 [AUTH] Failed login: 'admin' from 192.168.1.100
450
- 2025-08-12 14:30:18 [AUTH] Failed login: 'administrator' from 192.168.1.100
451
- 2025-08-12 14:30:45 [PROC] powershell.exe -WindowStyle Hidden -enc ZXhlYyBjYWxjLmV4ZQ==
452
- 2025-08-12 14:31:12 [NET] Suspicious connection to 45.33.22.11:443"""
 
 
453
 
454
- SAMPLE_THREAT = "Multiple failed login attempts followed by encoded PowerShell execution and suspicious network traffic to external IP addresses."
455
 
456
  # Main Dashboard Interface
457
  with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=professional_css) as demo:
@@ -460,14 +583,14 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
460
  gr.HTML("""
461
  <div class="dashboard-header">
462
  <div class="header-title">🛡️ SOC LLM Dashboard</div>
463
- <div class="header-subtitle">Professional Security Operations Center • LLM-Powered Detection & Analysis</div>
464
  </div>
465
  """)
466
 
467
  # System Status Bar
468
  with gr.Row():
469
  system_status = gr.Textbox(
470
- value="🔄 Initializing AI Models...",
471
  label="System Status",
472
  interactive=False,
473
  elem_classes=["status-indicator", "status-warning"],
@@ -480,7 +603,7 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
480
 
481
  # ================== TASK 1: DETECTION PANEL ==================
482
  with gr.Column(scale=1, elem_classes=["task-panel"]):
483
- gr.HTML('<div class="task-header">📊 TASK 1: THREAT DETECTION</div>')
484
 
485
  # Detection Controls
486
  gr.HTML('<div class="control-label">Detection Sensitivity</div>')
@@ -492,13 +615,13 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
492
  )
493
 
494
  with gr.Row():
495
- detect_btn = gr.Button("🔍 Detect", elem_classes=["primary-btn"], scale=2)
496
  sample_logs_btn = gr.Button("📝 Sample", elem_classes=["secondary-btn"], scale=1)
497
 
498
  # Log Input
499
  gr.HTML('<div class="result-header">Security Logs Input</div>')
500
  log_input = gr.Textbox(
501
- placeholder="Paste security logs here...",
502
  lines=6,
503
  elem_classes=["compact-input", "detection-input"],
504
  interactive=True,
@@ -506,12 +629,12 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
506
  )
507
 
508
  # Detection Results
509
- gr.HTML('<div class="result-header">Detection Results</div>')
510
  detection_output = gr.Textbox(
511
  lines=8,
512
  elem_classes=["compact-output"],
513
  interactive=False,
514
- placeholder="Detection results will appear here...",
515
  show_label=False
516
  )
517
 
@@ -524,7 +647,7 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
524
 
525
  # ================== TASK 2: ASSISTANT PANEL ==================
526
  with gr.Column(scale=1, elem_classes=["task-panel"]):
527
- gr.HTML('<div class="task-header">🤖 TASK 2: ANALYST ASSISTANT</div>')
528
 
529
  # Assistant Controls
530
  gr.HTML('<div class="control-label">Analyst Level</div>')
@@ -536,13 +659,13 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
536
  )
537
 
538
  with gr.Row():
539
- analyze_btn = gr.Button("🚀 Analyze", elem_classes=["primary-btn"], scale=2)
540
  sample_threat_btn = gr.Button("📝 Sample", elem_classes=["secondary-btn"], scale=1)
541
 
542
  # Threat Input
543
  gr.HTML('<div class="result-header">Threat Description</div>')
544
  threat_input = gr.Textbox(
545
- placeholder="Describe the security threat or incident...",
546
  lines=6,
547
  elem_classes=["compact-input"],
548
  interactive=True,
@@ -555,7 +678,7 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
555
  lines=8,
556
  elem_classes=["compact-output"],
557
  interactive=False,
558
- placeholder="Analysis results will appear here...",
559
  show_label=False
560
  )
561
 
@@ -569,7 +692,7 @@ with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=profession
569
  # Quick Info Footer
570
  gr.HTML("""
571
  <div style="text-align: center; padding: 12px; color: rgba(255,255,255,0.8); font-size: 11px; margin-top: 10px;">
572
- <strong>Research Project:</strong> LLM-based SOC Assistant • <strong>Student:</strong> Abdullah Alanazi • <strong>Supervisor:</strong> Prof. Ali Shoker • <strong>Institution:</strong> KAUST
573
  </div>
574
  """)
575
 
 
4
  import torch
5
  import time
6
  import re
7
+ import logging
8
 
9
+ # Set up logging
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
+
13
+ # Professional Dashboard CSS - Complete Textbox Display
14
  professional_css = """
15
  /* Professional SOC Dashboard - Fixed */
16
  .gradio-container {
 
326
 
327
  @spaces.GPU
328
  def load_model():
329
+ """Load GPT-OSS-20B model with improved error handling"""
330
  global pipe, model_status
331
 
332
+ try:
333
+ logger.info("Starting model loading process...")
334
+ model_status = "🔄 Loading GPT-OSS-20B model..."
335
+
336
+ # Load the specific model requested
337
+ logger.info("Loading gpt-oss-20b model...")
338
+ pipe = pipeline(
339
+ "text-generation",
340
+ model="openai/gpt-oss-20b",
341
+ torch_dtype=torch.float16, # Use fp16 for better memory efficiency
342
+ device_map="auto",
343
+ trust_remote_code=True,
344
+ max_length=512, # Limit context length
345
+ pad_token_id=50256 # Set pad token
346
+ )
347
+
348
+ # Test the model with a simple prompt
349
+ logger.info("Testing model functionality...")
350
+ test_output = pipe(
351
+ "Test security analysis:",
352
+ max_new_tokens=10,
353
+ do_sample=True,
354
+ temperature=0.7,
355
+ pad_token_id=50256
356
+ )
357
+
358
+ model_status = "✅ GPT-OSS-20B Ready"
359
+ logger.info("Model loaded successfully!")
360
+ return model_status
361
+
362
+ except Exception as e:
363
+ logger.error(f"Model loading failed: {str(e)}")
364
+ model_status = "⚠️ Model Loading Failed - Using Fallback"
365
+ pipe = None
366
+ return model_status
367
 
368
  @spaces.GPU
369
  def detect_threats(logs, sensitivity):
370
+ """Task 1: AI-powered Threat Detection"""
371
+ global pipe
372
+
373
  if not logs.strip():
374
  return "Please provide log data.", "⚠️ No input"
375
 
376
  start_time = time.time()
377
 
378
+ try:
379
+ if pipe is not None:
380
+ # Use GPT-OSS-20B for AI-powered detection
381
+ prompt = f"""Analyze these security logs for threats:
382
+
383
+ {logs}
384
+
385
+ Detection sensitivity: {sensitivity}
386
+
387
+ Analysis:"""
388
+
389
+ response = pipe(
390
+ prompt,
391
+ max_new_tokens=200,
392
+ do_sample=True,
393
+ temperature=0.3,
394
+ pad_token_id=50256,
395
+ truncation=True
396
+ )
397
+
398
+ ai_analysis = response[0]['generated_text'].split("Analysis:")[-1].strip()
399
+
400
+ else:
401
+ # Fallback to pattern-based detection
402
+ ai_analysis = "AI model unavailable. Using pattern-based detection."
403
+
404
+ # Enhanced pattern-based detection as backup/supplement
405
+ threats = []
406
+ risk_score = 0
407
+
408
+ # Authentication threats
409
+ failed_logins = len(re.findall(r'failed.*login|authentication.*failed', logs, re.IGNORECASE))
410
+ if failed_logins > 3:
411
+ threats.append(f"🚨 Brute Force Attack ({failed_logins} failed attempts)")
412
+ risk_score += 30
413
+ elif failed_logins > 0:
414
+ threats.append(f"⚠️ Failed Authentication ({failed_logins} attempts)")
415
+ risk_score += 15
416
+
417
+ # Malicious execution
418
+ if re.search(r'powershell.*-enc|cmd\.exe|eval\(|exec\(', logs, re.IGNORECASE):
419
+ threats.append("🚨 Malicious Script Execution")
420
+ risk_score += 35
421
+
422
+ # Network anomalies
423
+ if re.search(r'suspicious.*ip|unusual.*connection', logs, re.IGNORECASE):
424
+ threats.append("🚨 Suspicious Network Activity")
425
+ risk_score += 25
426
+
427
+ # File anomalies
428
+ if re.search(r'unusual.*file|suspicious.*access', logs, re.IGNORECASE):
429
+ threats.append("⚠️ File System Anomaly")
430
+ risk_score += 20
431
 
432
+ # Generate final result
433
+ if threats or pipe is not None:
434
+ severity = "CRITICAL" if risk_score > 50 else "HIGH" if risk_score > 30 else "MEDIUM"
435
+ confidence = min(95, 70 + len(threats) * 5)
436
+
437
+ result = f"""🚨 THREAT ANALYSIS RESULTS
438
 
439
+ AI ANALYSIS:
440
+ {ai_analysis}
441
 
442
+ DETECTED PATTERNS:
443
+ {chr(10).join(f"{threat}" for threat in threats) if threats else "• No obvious threat patterns detected"}
444
+
445
+ ASSESSMENT:
446
+ Risk Score: {risk_score}/100
447
+ Severity: {severity if threats else "LOW"}
448
+ • Confidence: {confidence}%
449
+ Model: {"GPT-OSS-20B" if pipe else "Pattern-based"}
450
+
451
+ RECOMMENDATIONS:
452
+ • {"Immediate containment required" if risk_score > 40 else "Continue monitoring"}
453
+ • {"Escalate to L2 analyst" if risk_score > 30 else "Standard response"}
454
+ • Preserve all evidence
455
+ • Update threat intelligence"""
456
+
457
+ status = f"🚨 Analysis Complete - {len(threats)} threats found" if threats else "✅ Analysis Complete"
458
+ else:
459
+ result = """✅ NO THREATS DETECTED
460
+
461
+ Clean log analysis with no suspicious patterns identified.
462
+ Continue standard monitoring procedures."""
463
+ status = "✅ CLEAN"
464
 
465
+ time_taken = round(time.time() - start_time, 1)
466
+ return result, f"{status} ({time_taken}s)"
467
+
468
+ except Exception as e:
469
+ logger.error(f"Detection error: {str(e)}")
470
+ return f"❌ Analysis failed: {str(e)}", "❌ ERROR"
 
 
471
 
472
  @spaces.GPU
473
  def analyze_threat(threat, level):
474
+ """Task 2: AI-powered Analyst Assistant"""
475
+ global pipe
476
+
477
  if not threat.strip():
478
  return "Please describe the threat.", "⚠️ No input"
479
 
480
  start_time = time.time()
481
 
482
+ try:
483
+ if pipe is not None:
484
+ # Use GPT-OSS-20B for AI analysis
485
+ prompt = f"""As a Level {level} SOC analyst, analyze this security threat:
486
+
487
+ {threat}
488
+
489
+ Provide detailed analysis including:
490
+ 1. Threat assessment
491
+ 2. Recommended actions
492
+ 3. Priority level
493
+ 4. Next steps
494
+
495
+ Analysis:"""
496
+
497
+ response = pipe(
498
+ prompt,
499
+ max_new_tokens=300,
500
+ do_sample=True,
501
+ temperature=0.4,
502
+ pad_token_id=50256,
503
+ truncation=True
504
+ )
505
+
506
+ ai_analysis = response[0]['generated_text'].split("Analysis:")[-1].strip()
507
+
508
+ result = f"""🤖 AI-POWERED {level} ANALYSIS
509
+
510
+ THREAT ASSESSMENT:
511
+ {ai_analysis}
512
+
513
+ MODEL: GPT-OSS-20B
514
+ ANALYST LEVEL: {level}
515
+ STATUS: AI Analysis Complete"""
516
+
517
+ else:
518
+ # Fallback analysis templates
519
+ templates = {
520
+ "L1": f"""🚨 L1 TRIAGE ANALYSIS
521
+
522
  THREAT: {threat[:60]}...
523
 
524
  IMMEDIATE ACTIONS:
 
530
  DECISION: Escalate to L2
531
  PRIORITY: High""",
532
 
533
+ "L2": f"""🔍 L2 INVESTIGATION
534
+
535
  INCIDENT: {threat[:60]}...
536
 
537
  INVESTIGATION PLAN:
 
543
 
544
  NEXT STEPS: Deploy monitoring""",
545
 
546
+ "L3": f"""🎯 L3 STRATEGIC ANALYSIS
547
+
548
  THREAT ASSESSMENT: {threat[:60]}...
549
 
550
  STRATEGIC RESPONSE:
 
555
  • Security improvements
556
 
557
  RECOMMENDATION: Full IR activation"""
558
+ }
559
+
560
+ result = templates.get(level, templates["L2"])
561
+
562
+ time_taken = round(time.time() - start_time, 1)
563
+ return result, f"✅ {level} Complete ({time_taken}s)"
564
+
565
+ except Exception as e:
566
+ logger.error(f"Analysis error: {str(e)}")
567
+ return f"❌ Analysis failed: {str(e)}", "❌ ERROR"
568
 
569
  # Sample data
570
+ SAMPLE_LOGS = """2025-08-11 14:30:15 [AUTH] Failed login: 'admin' from 192.168.1.100
571
+ 2025-08-11 14:30:18 [AUTH] Failed login: 'administrator' from 192.168.1.100
572
+ 2025-08-11 14:30:45 [PROC] powershell.exe -WindowStyle Hidden -enc ZXhlYyBjYWxjLmV4ZQ==
573
+ 2025-08-11 14:31:12 [NET] Suspicious connection to 45.33.22.11:443
574
+ 2025-08-11 14:31:30 [FILE] Unusual file access pattern detected
575
+ 2025-08-11 14:32:01 [NET] Multiple connections from same source IP"""
576
 
577
+ SAMPLE_THREAT = "Multiple failed login attempts detected from IP 192.168.1.100, followed by encoded PowerShell execution and suspicious outbound network connections to known malicious IP addresses. Lateral movement indicators present."
578
 
579
  # Main Dashboard Interface
580
  with gr.Blocks(title="SOC LLM Dashboard", theme=gr.themes.Soft(), css=professional_css) as demo:
 
583
  gr.HTML("""
584
  <div class="dashboard-header">
585
  <div class="header-title">🛡️ SOC LLM Dashboard</div>
586
+ <div class="header-subtitle">Professional Security Operations Center • GPT-OSS-20B Powered Detection & Analysis</div>
587
  </div>
588
  """)
589
 
590
  # System Status Bar
591
  with gr.Row():
592
  system_status = gr.Textbox(
593
+ value="🔄 Initializing GPT-OSS-20B...",
594
  label="System Status",
595
  interactive=False,
596
  elem_classes=["status-indicator", "status-warning"],
 
603
 
604
  # ================== TASK 1: DETECTION PANEL ==================
605
  with gr.Column(scale=1, elem_classes=["task-panel"]):
606
+ gr.HTML('<div class="task-header">📊 TASK 1: AI THREAT DETECTION</div>')
607
 
608
  # Detection Controls
609
  gr.HTML('<div class="control-label">Detection Sensitivity</div>')
 
615
  )
616
 
617
  with gr.Row():
618
+ detect_btn = gr.Button("🔍 AI Detect", elem_classes=["primary-btn"], scale=2)
619
  sample_logs_btn = gr.Button("📝 Sample", elem_classes=["secondary-btn"], scale=1)
620
 
621
  # Log Input
622
  gr.HTML('<div class="result-header">Security Logs Input</div>')
623
  log_input = gr.Textbox(
624
+ placeholder="Paste security logs here for AI-powered analysis...",
625
  lines=6,
626
  elem_classes=["compact-input", "detection-input"],
627
  interactive=True,
 
629
  )
630
 
631
  # Detection Results
632
+ gr.HTML('<div class="result-header">AI Detection Results</div>')
633
  detection_output = gr.Textbox(
634
  lines=8,
635
  elem_classes=["compact-output"],
636
  interactive=False,
637
+ placeholder="GPT-OSS-20B detection results will appear here...",
638
  show_label=False
639
  )
640
 
 
647
 
648
  # ================== TASK 2: ASSISTANT PANEL ==================
649
  with gr.Column(scale=1, elem_classes=["task-panel"]):
650
+ gr.HTML('<div class="task-header">🤖 TASK 2: AI ANALYST ASSISTANT</div>')
651
 
652
  # Assistant Controls
653
  gr.HTML('<div class="control-label">Analyst Level</div>')
 
659
  )
660
 
661
  with gr.Row():
662
+ analyze_btn = gr.Button("🚀 AI Analyze", elem_classes=["primary-btn"], scale=2)
663
  sample_threat_btn = gr.Button("📝 Sample", elem_classes=["secondary-btn"], scale=1)
664
 
665
  # Threat Input
666
  gr.HTML('<div class="result-header">Threat Description</div>')
667
  threat_input = gr.Textbox(
668
+ placeholder="Describe the security threat for AI analysis...",
669
  lines=6,
670
  elem_classes=["compact-input"],
671
  interactive=True,
 
678
  lines=8,
679
  elem_classes=["compact-output"],
680
  interactive=False,
681
+ placeholder="GPT-OSS-20B analysis results will appear here...",
682
  show_label=False
683
  )
684
 
 
692
  # Quick Info Footer
693
  gr.HTML("""
694
  <div style="text-align: center; padding: 12px; color: rgba(255,255,255,0.8); font-size: 11px; margin-top: 10px;">
695
+ <strong>Research Project:</strong> LLM-based SOC Assistant • <strong>Model:</strong> GPT-OSS-20B • <strong>Student:</strong> Abdullah Alanazi • <strong>Supervisor:</strong> Prof. Ali Shoker • <strong>Institution:</strong> KAUST
696
  </div>
697
  """)
698