Server-Main / Version_4 /evaluate.py
atharvawarade9807's picture
Upload 8 files
0baf9d9 verified
Raw
History Blame
2.74 kB
# Location: chimera_engine/evaluate.py
import os
import time
import xgboost as xgb
import numpy as np
from features import extract_url_features
def main():
model_path = os.path.join("models", "final_model.json")
if not os.path.exists(model_path):
print("[-] Final model not found! Please run 'python train.py' first.")
return
print("[*] Loading high-speed XGBoost engine...")
bst = xgb.Booster()
bst.load_model(model_path)
print("[+] Engine operational. Enter a URL to evaluate.")
print("-" * 55)
while True:
try:
user_url = input("\nEnter Target URL (or type 'quit' to exit): ").strip()
if user_url.lower() in ['quit', 'exit', 'q']:
print("[*] Exiting evaluation engine.")
break
if not user_url:
continue
# --- FIX: Parser Architecture Blind Spot Sanitization ---
# If the user enters a raw domain variant without a scheme (e.g., 'youtube@evil-site.com'),
# we force a default prefix so the underlying regex and string split parsers don't
# mistake the entire token for a local relative file path.
processed_url = user_url
if not processed_url.lower().startswith(('http://', 'https://')):
processed_url = "http://" + processed_url
# Start Microsecond Timer
start_time = time.perf_counter()
# 1. Extract Features using the sanitized structural URL
features = extract_url_features(processed_url)
# 2. Convert to DMatrix format required by XGBoost
dmatrix_payload = xgb.DMatrix(np.array([features]))
# 3. Predict Probability
probability = bst.predict(dmatrix_payload)[0]
# Stop Timer
end_time = time.perf_counter()
latency_ms = (end_time - start_time) * 1000
# Formatting the Output
if probability >= 0.50:
verdict = "🚨 PHISHING DETECTED"
confidence = probability * 100
else:
verdict = "✅ LEGITIMATE SAFE"
confidence = (1 - probability) * 100
print(f"Verdict : {verdict}")
print(f"Confidence : {confidence:.2f}%")
print(f"Latency : {latency_ms:.3f} ms")
except KeyboardInterrupt:
print("\n[*] Exiting evaluation engine.")
break
except Exception as e:
print(f"[-] An error occurred during evaluation: {e}")
if __name__ == "__main__":
main()