atharvawarade9807 commited on
Commit
ce2ba41
·
verified ·
1 Parent(s): ecd5fb0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +70 -10
main.py CHANGED
@@ -610,6 +610,44 @@ app.include_router(v6_router)
610
  # =====================================================================
611
  # 6. MASTER FUSION ENGINE (COMBINES V1, V2, V4-HGNN)
612
  # =====================================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
613
  @app.post("/predict/unified")
614
  async def unified_scan_endpoint(payload: UnifiedRequest):
615
  url = payload.url.strip()
@@ -637,11 +675,13 @@ async def unified_scan_endpoint(payload: UnifiedRequest):
637
  # ── V1 Evaluation ──
638
  v1_start = time.perf_counter()
639
  v1_score, layer_scores = 0.0, {}
 
640
  if v1_process:
641
  print(f"[UNIFIED][V1] Calling v1_process(url) ...")
642
  v1_matrix = await v1_process(url)
643
  v1_score = v1_matrix.composite_score
644
  layer_scores = asdict(v1_matrix.layer_scores)
 
645
  print(f"[UNIFIED][V1] score={v1_score:.4f} layers={layer_scores} "
646
  f"({round((time.perf_counter() - v1_start) * 1000, 1)}ms)")
647
  else:
@@ -650,6 +690,7 @@ async def unified_scan_endpoint(payload: UnifiedRequest):
650
  # ── V4 Evaluation (Upgraded to HGNN Topological Passing Engine) ──
651
  v4_start = time.perf_counter()
652
  v4_score, v4_msg = 0.0, "Engine Offline"
 
653
  if gnn_model and graph_engine:
654
  print(f"[UNIFIED][V4] Calling HGNN graph engine ...")
655
  try:
@@ -663,13 +704,20 @@ async def unified_scan_endpoint(payload: UnifiedRequest):
663
  v4_pred = gnn_model(v4_x_dict, v4_edge_index_dict)
664
  v4_score = float(v4_pred[0].item())
665
  v4_msg = f"HGNN Graph Matrix Pass: {round(v4_score * 100, 1)}% Topological Phishing Risk"
 
666
  else:
667
- v4_msg = "Skipped GNN (DNS Resolution Failed)"
 
 
 
 
 
 
668
  else:
669
  v4_msg = "V4 Error: Structural domain parsing failed."
670
  except Exception as e:
671
  v4_msg = f"V4 HGNN Graph Error: {str(e)}"
672
- print(f"[UNIFIED][V4] score={v4_score:.4f} msg='{v4_msg}' "
673
  f"({round((time.perf_counter() - v4_start) * 1000, 1)}ms)")
674
  else:
675
  print(f"[UNIFIED][V4] Skipped — engine offline.")
@@ -677,6 +725,7 @@ async def unified_scan_endpoint(payload: UnifiedRequest):
677
  # ── V2 Evaluation ──
678
  v2_start = time.perf_counter()
679
  v2_score, v2_msg = 0.0, "Skipped (Timeout Prevention)"
 
680
  if v2_analyzer and v2_capture_screenshot:
681
  print(f"[UNIFIED][V2] Capturing screenshot + running CNN ...")
682
  temp_img = os.path.join(OUTPUT_DIR, f"fusion_{int(time.time()*1000)}.png")
@@ -710,6 +759,7 @@ async def unified_scan_endpoint(payload: UnifiedRequest):
710
  if result.get("prediction") != "Phishing":
711
  v2_score = 1.0 - v2_score
712
  v2_msg = f"CNN Vision: {result.get('prediction')} ({round(v2_score * 100, 1)}% Risk)"
 
713
  os.remove(temp_img)
714
  else:
715
  v2_msg = "V2 Vision Unavailable: screenshot capture returned no file"
@@ -720,27 +770,37 @@ async def unified_scan_endpoint(payload: UnifiedRequest):
720
  v2_msg = f"V2 Vision Unavailable: {type(e).__name__}: {e}"
721
  print(f"[UNIFIED][V2] EXCEPTION during capture/analyze:")
722
  traceback.print_exc()
723
- print(f"[UNIFIED][V2] score={v2_score:.4f} msg='{v2_msg}' "
724
  f"({round((time.perf_counter() - v2_start) * 1000, 1)}ms)")
725
  else:
726
  print(f"[UNIFIED][V2] Skipped — engine offline.")
727
 
728
  # ── Master Calculation ──
729
- if "Skipped" in v2_msg or "Unavailable" in v2_msg:
730
- master_score = (v1_score * 0.5) + (v4_score * 0.5)
731
- else:
732
- master_score = (v1_score * 0.4) + (v4_score * 0.4) + (v2_score * 0.2)
 
 
 
 
 
 
 
 
 
733
 
734
- print(f"[UNIFIED] ═══ composite_score={master_score:.4f} total="
735
  f"{round((time.perf_counter() - start_time) * 1000, 1)}ms ═══\n")
736
 
737
  return {
738
  "composite_score": master_score,
739
  "latency_ms": round((time.perf_counter() - start_time) * 1000, 2),
740
  "layer_scores": layer_scores,
 
741
  "details": {
742
- "hgnn_graph_analysis": {"score": v4_score, "message": v4_msg},
743
- "cnn_visual_analysis": {"score": v2_score, "message": v2_msg}
744
  }
745
  }
746
 
 
610
  # =====================================================================
611
  # 6. MASTER FUSION ENGINE (COMBINES V1, V2, V4-HGNN)
612
  # =====================================================================
613
+
614
+ # Base weights when ALL THREE engines produce a real result. When one or
615
+ # more engines don't contribute (offline, timed out, errored), the
616
+ # remaining engines' weights are renormalized proportionally so they
617
+ # still sum to 1.0 — rather than hardcoding a single "V2 missing" fallback
618
+ # case and silently mis-weighting every other missing-engine combination.
619
+ _ENGINE_BASE_WEIGHTS = {"v1": 0.4, "v4": 0.4, "v2": 0.2}
620
+
621
+ # When V4's DNS resolution fails, we can't run the GNN at all — but an
622
+ # unresolvable/unregistered domain is itself a real signal (freshly
623
+ # stood-up phishing infra, abandoned typosquats, etc.), not neutral
624
+ # "safe" evidence. Score it as elevated risk rather than 0.0, and treat
625
+ # it as a genuine contributing result (not a dropped/offline engine).
626
+ # Tune this value against real data if you have labeled examples of
627
+ # unresolvable-domain outcomes — this is a reasonable starting heuristic,
628
+ # not a calibrated probability.
629
+ _V4_DNS_FAILURE_RISK_SCORE = 0.65
630
+
631
+
632
+ def _compute_composite_score(contributions: dict) -> tuple[float, dict]:
633
+ """
634
+ contributions: {"v1": (score, did_run), "v4": (score, did_run), "v2": (score, did_run)}
635
+ Returns (composite_score, normalized_weights_used) — renormalizing
636
+ _ENGINE_BASE_WEIGHTS across only the engines where did_run is True.
637
+ """
638
+ active_weights = {
639
+ k: _ENGINE_BASE_WEIGHTS[k]
640
+ for k, (_, did_run) in contributions.items()
641
+ if did_run
642
+ }
643
+ total_weight = sum(active_weights.values())
644
+ if total_weight == 0:
645
+ return 0.0, {}
646
+ normalized = {k: w / total_weight for k, w in active_weights.items()}
647
+ composite = sum(contributions[k][0] * w for k, w in normalized.items())
648
+ return composite, normalized
649
+
650
+
651
  @app.post("/predict/unified")
652
  async def unified_scan_endpoint(payload: UnifiedRequest):
653
  url = payload.url.strip()
 
675
  # ── V1 Evaluation ──
676
  v1_start = time.perf_counter()
677
  v1_score, layer_scores = 0.0, {}
678
+ v1_ran = False
679
  if v1_process:
680
  print(f"[UNIFIED][V1] Calling v1_process(url) ...")
681
  v1_matrix = await v1_process(url)
682
  v1_score = v1_matrix.composite_score
683
  layer_scores = asdict(v1_matrix.layer_scores)
684
+ v1_ran = True
685
  print(f"[UNIFIED][V1] score={v1_score:.4f} layers={layer_scores} "
686
  f"({round((time.perf_counter() - v1_start) * 1000, 1)}ms)")
687
  else:
 
690
  # ── V4 Evaluation (Upgraded to HGNN Topological Passing Engine) ──
691
  v4_start = time.perf_counter()
692
  v4_score, v4_msg = 0.0, "Engine Offline"
693
+ v4_ran = False
694
  if gnn_model and graph_engine:
695
  print(f"[UNIFIED][V4] Calling HGNN graph engine ...")
696
  try:
 
704
  v4_pred = gnn_model(v4_x_dict, v4_edge_index_dict)
705
  v4_score = float(v4_pred[0].item())
706
  v4_msg = f"HGNN Graph Matrix Pass: {round(v4_score * 100, 1)}% Topological Phishing Risk"
707
+ v4_ran = True
708
  else:
709
+ # DNS resolution failure is itself a signal, not a
710
+ # neutral non-result — score it as elevated risk and
711
+ # count it as a real contribution to the composite.
712
+ v4_score = _V4_DNS_FAILURE_RISK_SCORE
713
+ v4_msg = (f"DNS Resolution Failed — domain unresolvable, "
714
+ f"treated as elevated-risk signal ({round(v4_score * 100, 1)}%)")
715
+ v4_ran = True
716
  else:
717
  v4_msg = "V4 Error: Structural domain parsing failed."
718
  except Exception as e:
719
  v4_msg = f"V4 HGNN Graph Error: {str(e)}"
720
+ print(f"[UNIFIED][V4] score={v4_score:.4f} msg='{v4_msg}' ran={v4_ran} "
721
  f"({round((time.perf_counter() - v4_start) * 1000, 1)}ms)")
722
  else:
723
  print(f"[UNIFIED][V4] Skipped — engine offline.")
 
725
  # ── V2 Evaluation ──
726
  v2_start = time.perf_counter()
727
  v2_score, v2_msg = 0.0, "Skipped (Timeout Prevention)"
728
+ v2_ran = False
729
  if v2_analyzer and v2_capture_screenshot:
730
  print(f"[UNIFIED][V2] Capturing screenshot + running CNN ...")
731
  temp_img = os.path.join(OUTPUT_DIR, f"fusion_{int(time.time()*1000)}.png")
 
759
  if result.get("prediction") != "Phishing":
760
  v2_score = 1.0 - v2_score
761
  v2_msg = f"CNN Vision: {result.get('prediction')} ({round(v2_score * 100, 1)}% Risk)"
762
+ v2_ran = True
763
  os.remove(temp_img)
764
  else:
765
  v2_msg = "V2 Vision Unavailable: screenshot capture returned no file"
 
770
  v2_msg = f"V2 Vision Unavailable: {type(e).__name__}: {e}"
771
  print(f"[UNIFIED][V2] EXCEPTION during capture/analyze:")
772
  traceback.print_exc()
773
+ print(f"[UNIFIED][V2] score={v2_score:.4f} msg='{v2_msg}' ran={v2_ran} "
774
  f"({round((time.perf_counter() - v2_start) * 1000, 1)}ms)")
775
  else:
776
  print(f"[UNIFIED][V2] Skipped — engine offline.")
777
 
778
  # ── Master Calculation ──
779
+ # Each engine contributes only if it actually produced a result this
780
+ # request (v1_ran / v4_ran / v2_ran) — weights are renormalized across
781
+ # whichever engines ran, rather than a single hardcoded fallback that
782
+ # only accounted for V2 being unavailable.
783
+ contributions = {
784
+ "v1": (v1_score, v1_ran),
785
+ "v4": (v4_score, v4_ran),
786
+ "v2": (v2_score, v2_ran),
787
+ }
788
+ master_score, weights_used = _compute_composite_score(contributions)
789
+
790
+ if not weights_used:
791
+ print(f"[UNIFIED] ⚠ No engines produced a result — composite defaulted to 0.0")
792
 
793
+ print(f"[UNIFIED] ═══ composite_score={master_score:.4f} weights={weights_used} total="
794
  f"{round((time.perf_counter() - start_time) * 1000, 1)}ms ═══\n")
795
 
796
  return {
797
  "composite_score": master_score,
798
  "latency_ms": round((time.perf_counter() - start_time) * 1000, 2),
799
  "layer_scores": layer_scores,
800
+ "engines_used": weights_used,
801
  "details": {
802
+ "hgnn_graph_analysis": {"score": v4_score, "message": v4_msg, "contributed": v4_ran},
803
+ "cnn_visual_analysis": {"score": v2_score, "message": v2_msg, "contributed": v2_ran}
804
  }
805
  }
806