Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from analyze_email_main import analyze
|
| 3 |
-
import
|
| 4 |
import tempfile
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def analyze_email(file_obj, export_pdf=False):
|
| 8 |
if file_obj is None:
|
|
@@ -11,15 +16,15 @@ def analyze_email(file_obj, export_pdf=False):
|
|
| 11 |
results = analyze(file_obj.name)
|
| 12 |
|
| 13 |
# Basic verdict info
|
| 14 |
-
attack_score = results[0]
|
| 15 |
-
attack_type = results[1]
|
| 16 |
-
verdict = results[2]
|
| 17 |
|
| 18 |
-
score_val = int("".join(filter(str.isdigit, attack_score)))
|
| 19 |
if score_val > 100:
|
| 20 |
score_val = 100
|
| 21 |
|
| 22 |
-
#
|
| 23 |
if "Malicious" in verdict:
|
| 24 |
verdict_color = "#e74c3c" # red
|
| 25 |
elif "Suspicious" in verdict:
|
|
@@ -29,7 +34,7 @@ def analyze_email(file_obj, export_pdf=False):
|
|
| 29 |
else:
|
| 30 |
verdict_color = "#2ecc71" # green
|
| 31 |
|
| 32 |
-
# Extract
|
| 33 |
try:
|
| 34 |
tags_index = results.index("---- Attack Analysis Tags ----")
|
| 35 |
findings_index = results.index("---- Detailed Findings ----")
|
|
@@ -42,7 +47,7 @@ def analyze_email(file_obj, export_pdf=False):
|
|
| 42 |
findings = results[findings_index + 1 : body_index]
|
| 43 |
highlighted_body = results[body_index + 1 :]
|
| 44 |
|
| 45 |
-
#
|
| 46 |
html = f"""
|
| 47 |
<div style='font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin:auto;'>
|
| 48 |
|
|
@@ -80,10 +85,7 @@ def analyze_email(file_obj, export_pdf=False):
|
|
| 80 |
|
| 81 |
pdf_path = None
|
| 82 |
if export_pdf:
|
| 83 |
-
|
| 84 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
|
| 85 |
-
pdf_path = tmp_pdf.name
|
| 86 |
-
pdfkit.from_string(html, pdf_path)
|
| 87 |
|
| 88 |
return html, pdf_path
|
| 89 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from analyze_email_main import analyze
|
| 3 |
+
from weasyprint import HTML
|
| 4 |
import tempfile
|
| 5 |
+
|
| 6 |
+
def html_to_pdf(html_content):
|
| 7 |
+
"""Convert HTML string to a temporary PDF file and return its path."""
|
| 8 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
|
| 9 |
+
HTML(string=html_content).write_pdf(tmp_pdf.name)
|
| 10 |
+
return tmp_pdf.name
|
| 11 |
|
| 12 |
def analyze_email(file_obj, export_pdf=False):
|
| 13 |
if file_obj is None:
|
|
|
|
| 16 |
results = analyze(file_obj.name)
|
| 17 |
|
| 18 |
# Basic verdict info
|
| 19 |
+
attack_score = results[0]
|
| 20 |
+
attack_type = results[1]
|
| 21 |
+
verdict = results[2]
|
| 22 |
|
| 23 |
+
score_val = int("".join(filter(str.isdigit, attack_score)))
|
| 24 |
if score_val > 100:
|
| 25 |
score_val = 100
|
| 26 |
|
| 27 |
+
# Verdict color
|
| 28 |
if "Malicious" in verdict:
|
| 29 |
verdict_color = "#e74c3c" # red
|
| 30 |
elif "Suspicious" in verdict:
|
|
|
|
| 34 |
else:
|
| 35 |
verdict_color = "#2ecc71" # green
|
| 36 |
|
| 37 |
+
# Extract sections
|
| 38 |
try:
|
| 39 |
tags_index = results.index("---- Attack Analysis Tags ----")
|
| 40 |
findings_index = results.index("---- Detailed Findings ----")
|
|
|
|
| 47 |
findings = results[findings_index + 1 : body_index]
|
| 48 |
highlighted_body = results[body_index + 1 :]
|
| 49 |
|
| 50 |
+
# HTML Report
|
| 51 |
html = f"""
|
| 52 |
<div style='font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin:auto;'>
|
| 53 |
|
|
|
|
| 85 |
|
| 86 |
pdf_path = None
|
| 87 |
if export_pdf:
|
| 88 |
+
pdf_path = html_to_pdf(html)
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
return html, pdf_path
|
| 91 |
|