Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,21 +5,25 @@ import numpy as np
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
from transformers import pipeline
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Set up a custom logger for phishing detections
|
| 9 |
phishing_logger = logging.getLogger('phishing')
|
| 10 |
phishing_logger.setLevel(logging.INFO)
|
| 11 |
-
phishing_handler = logging.FileHandler('
|
| 12 |
phishing_handler.setFormatter(logging.Formatter('%(asctime)s | Confidence: %(confidence)s% | %(message)s'))
|
| 13 |
phishing_logger.addHandler(phishing_handler)
|
|
|
|
| 14 |
|
| 15 |
-
# Set up a separate logger for system logs
|
| 16 |
system_logger = logging.getLogger('system')
|
| 17 |
system_logger.setLevel(logging.INFO)
|
| 18 |
-
system_handler = logging.StreamHandler() #
|
| 19 |
system_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
| 20 |
system_logger.addHandler(system_handler)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
logging.getLogger('').handlers = []
|
| 24 |
|
| 25 |
# Initialize Hugging Face model for phishing detection
|
|
@@ -28,10 +32,12 @@ model = pipeline('text-classification', model="Ajay1311/phish")
|
|
| 28 |
def log_phishing(url_or_text, confidence):
|
| 29 |
"""Log phishing URLs or content into a log file with confidence score."""
|
| 30 |
try:
|
| 31 |
-
confidence_str = f"{float(confidence):.1f}" # Ensure
|
| 32 |
phishing_logger.info(f"Phishing detected: {url_or_text}", extra={'confidence': confidence_str})
|
|
|
|
| 33 |
except (ValueError, TypeError) as e:
|
| 34 |
phishing_logger.info(f"Error logging phishing (invalid confidence): {url_or_text} | Error: {e}")
|
|
|
|
| 35 |
|
| 36 |
def create_speedometer_chart(confidence, is_phishing):
|
| 37 |
"""Generate speedometer chart showing confidence of phishing detection."""
|
|
@@ -113,10 +119,17 @@ def open_awareness_page():
|
|
| 113 |
|
| 114 |
def download_log_file():
|
| 115 |
"""Return the path to the log file for download."""
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
theme = gr.themes.Soft(primary_hue="blue", secondary_hue="gray").set(
|
| 122 |
button_primary_background_fill="*primary_600",
|
|
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# Ensure log file directory is writable
|
| 9 |
+
LOG_FILE = os.path.join(os.path.dirname(__file__), "phishing_urls.log")
|
| 10 |
+
|
| 11 |
# Set up a custom logger for phishing detections
|
| 12 |
phishing_logger = logging.getLogger('phishing')
|
| 13 |
phishing_logger.setLevel(logging.INFO)
|
| 14 |
+
phishing_handler = logging.FileHandler(LOG_FILE, mode='a', force=True) # Force flush writes
|
| 15 |
phishing_handler.setFormatter(logging.Formatter('%(asctime)s | Confidence: %(confidence)s% | %(message)s'))
|
| 16 |
phishing_logger.addHandler(phishing_handler)
|
| 17 |
+
phishing_logger.info("Logger initialized") # Debug log to confirm setup
|
| 18 |
|
| 19 |
+
# Set up a separate logger for system logs
|
| 20 |
system_logger = logging.getLogger('system')
|
| 21 |
system_logger.setLevel(logging.INFO)
|
| 22 |
+
system_handler = logging.StreamHandler() # Console output
|
| 23 |
system_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
| 24 |
system_logger.addHandler(system_handler)
|
| 25 |
|
| 26 |
+
# Prevent root logger interference
|
| 27 |
logging.getLogger('').handlers = []
|
| 28 |
|
| 29 |
# Initialize Hugging Face model for phishing detection
|
|
|
|
| 32 |
def log_phishing(url_or_text, confidence):
|
| 33 |
"""Log phishing URLs or content into a log file with confidence score."""
|
| 34 |
try:
|
| 35 |
+
confidence_str = f"{float(confidence):.1f}" # Ensure valid float
|
| 36 |
phishing_logger.info(f"Phishing detected: {url_or_text}", extra={'confidence': confidence_str})
|
| 37 |
+
system_logger.info(f"Logged phishing detection to {LOG_FILE}: {url_or_text} ({confidence_str}%)")
|
| 38 |
except (ValueError, TypeError) as e:
|
| 39 |
phishing_logger.info(f"Error logging phishing (invalid confidence): {url_or_text} | Error: {e}")
|
| 40 |
+
system_logger.error(f"Logging error: {e}")
|
| 41 |
|
| 42 |
def create_speedometer_chart(confidence, is_phishing):
|
| 43 |
"""Generate speedometer chart showing confidence of phishing detection."""
|
|
|
|
| 119 |
|
| 120 |
def download_log_file():
|
| 121 |
"""Return the path to the log file for download."""
|
| 122 |
+
try:
|
| 123 |
+
if os.path.exists(LOG_FILE):
|
| 124 |
+
# Ensure file is readable
|
| 125 |
+
os.chmod(LOG_FILE, 0o644) # Set read/write for owner, read for others
|
| 126 |
+
if os.path.getsize(LOG_FILE) > 0:
|
| 127 |
+
return LOG_FILE
|
| 128 |
+
return None, "Log file exists but is empty. Try analyzing more inputs."
|
| 129 |
+
return None, "No phishing detections logged yet."
|
| 130 |
+
except Exception as e:
|
| 131 |
+
system_logger.error(f"Error accessing log file {LOG_FILE}: {e}")
|
| 132 |
+
return None, f"Error accessing log file: {e}"
|
| 133 |
|
| 134 |
theme = gr.themes.Soft(primary_hue="blue", secondary_hue="gray").set(
|
| 135 |
button_primary_background_fill="*primary_600",
|