Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,12 @@ import gradio as gr
|
|
| 2 |
from analyze_email_main import analyze
|
| 3 |
from weasyprint import HTML
|
| 4 |
import tempfile
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
def html_to_pdf(html_content):
|
| 8 |
"""Convert HTML string to a temporary PDF file and return its path."""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return tmp_pdf.name
|
| 13 |
|
| 14 |
def analyze_email(file_path, export_pdf=False):
|
| 15 |
if file_path is None:
|
|
@@ -17,7 +15,7 @@ def analyze_email(file_path, export_pdf=False):
|
|
| 17 |
|
| 18 |
results = analyze(file_path)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
attack_score = results[0]
|
| 22 |
attack_type = results[1]
|
| 23 |
verdict = results[2]
|
|
@@ -27,26 +25,28 @@ def analyze_email(file_path, export_pdf=False):
|
|
| 27 |
score_val = 100
|
| 28 |
|
| 29 |
# Verdict color
|
| 30 |
-
verdict_color =
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"#
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
# Extract sections
|
| 38 |
try:
|
| 39 |
tags_index = results.index("---- Attack Analysis Tags ----")
|
| 40 |
findings_index = results.index("---- Detailed Findings ----")
|
| 41 |
body_index = results.index("---- Highlighted Body ----")
|
| 42 |
-
tags = results[tags_index + 1 : findings_index]
|
| 43 |
-
findings = results[findings_index + 1 : body_index]
|
| 44 |
-
highlighted_body = results[body_index + 1 :]
|
| 45 |
except ValueError:
|
| 46 |
html = "<pre>" + "\n".join(results) + "</pre>"
|
| 47 |
return html, None
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
html = f"""
|
| 51 |
<div style='font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin:auto;'>
|
| 52 |
<h2 style="color:#2c3e50;">π§ Email Security Report</h2>
|
|
@@ -60,21 +60,32 @@ def analyze_email(file_path, export_pdf=False):
|
|
| 60 |
<p><b>Final Verdict:</b> <span style="background:{verdict_color}; color:white; padding:4px 10px; border-radius:6px;">
|
| 61 |
{verdict.replace('Final Verdict:', '').strip()}
|
| 62 |
</span></p>
|
| 63 |
-
<
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
</div>
|
| 72 |
"""
|
| 73 |
|
| 74 |
-
pdf_path =
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
return html, pdf_path if pdf_path and os.path.exists(pdf_path) else None
|
| 78 |
|
| 79 |
# Gradio Interface
|
| 80 |
demo = gr.Interface(
|
|
@@ -88,10 +99,8 @@ demo = gr.Interface(
|
|
| 88 |
gr.File(label="Download PDF Report")
|
| 89 |
],
|
| 90 |
title="π§ Email Security Analyzer",
|
| 91 |
-
description=
|
| 92 |
-
|
| 93 |
-
"Check 'Export PDF Report' to download a professional PDF report."
|
| 94 |
-
)
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
|
|
|
| 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_path, export_pdf=False):
|
| 13 |
if file_path is None:
|
|
|
|
| 15 |
|
| 16 |
results = analyze(file_path)
|
| 17 |
|
| 18 |
+
# Extract basic verdict info
|
| 19 |
attack_score = results[0]
|
| 20 |
attack_type = results[1]
|
| 21 |
verdict = results[2]
|
|
|
|
| 25 |
score_val = 100
|
| 26 |
|
| 27 |
# Verdict color
|
| 28 |
+
verdict_color = "#2ecc71" # default green
|
| 29 |
+
if "Malicious" in verdict:
|
| 30 |
+
verdict_color = "#e74c3c"
|
| 31 |
+
elif "Suspicious" in verdict:
|
| 32 |
+
verdict_color = "#e67e22"
|
| 33 |
+
elif "Spam" in verdict:
|
| 34 |
+
verdict_color = "#f1c40f"
|
| 35 |
|
| 36 |
# Extract sections
|
| 37 |
try:
|
| 38 |
tags_index = results.index("---- Attack Analysis Tags ----")
|
| 39 |
findings_index = results.index("---- Detailed Findings ----")
|
| 40 |
body_index = results.index("---- Highlighted Body ----")
|
|
|
|
|
|
|
|
|
|
| 41 |
except ValueError:
|
| 42 |
html = "<pre>" + "\n".join(results) + "</pre>"
|
| 43 |
return html, None
|
| 44 |
|
| 45 |
+
tags = results[tags_index + 1 : findings_index]
|
| 46 |
+
findings = results[findings_index + 1 : body_index]
|
| 47 |
+
highlighted_body = results[body_index + 1 :]
|
| 48 |
+
|
| 49 |
+
# HTML Report with collapsible sections
|
| 50 |
html = f"""
|
| 51 |
<div style='font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin:auto;'>
|
| 52 |
<h2 style="color:#2c3e50;">π§ Email Security Report</h2>
|
|
|
|
| 60 |
<p><b>Final Verdict:</b> <span style="background:{verdict_color}; color:white; padding:4px 10px; border-radius:6px;">
|
| 61 |
{verdict.replace('Final Verdict:', '').strip()}
|
| 62 |
</span></p>
|
| 63 |
+
<details style="margin-top:15px;">
|
| 64 |
+
<summary>π Attack Analysis Tags ({len(tags)})</summary>
|
| 65 |
+
<ul>
|
| 66 |
+
{''.join(f'<li>{tag}</li>' for tag in tags)}
|
| 67 |
+
</ul>
|
| 68 |
+
</details>
|
| 69 |
+
<details style="margin-top:15px;">
|
| 70 |
+
<summary>π΅οΈ Detailed Findings ({len(findings)})</summary>
|
| 71 |
+
<ul>
|
| 72 |
+
{''.join(f'<li>{finding}</li>' for finding in findings)}
|
| 73 |
+
</ul>
|
| 74 |
+
</details>
|
| 75 |
+
<details style="margin-top:15px;">
|
| 76 |
+
<summary>π© Highlighted Email Body</summary>
|
| 77 |
+
<div style='border:1px solid #ccc; padding:15px; background:#fdfdfd; border-radius:8px;'>
|
| 78 |
+
{"<br>".join(highlighted_body)}
|
| 79 |
+
</div>
|
| 80 |
+
</details>
|
| 81 |
</div>
|
| 82 |
"""
|
| 83 |
|
| 84 |
+
pdf_path = None
|
| 85 |
+
if export_pdf:
|
| 86 |
+
pdf_path = html_to_pdf(html)
|
| 87 |
|
| 88 |
+
return html, pdf_path
|
|
|
|
| 89 |
|
| 90 |
# Gradio Interface
|
| 91 |
demo = gr.Interface(
|
|
|
|
| 99 |
gr.File(label="Download PDF Report")
|
| 100 |
],
|
| 101 |
title="π§ Email Security Analyzer",
|
| 102 |
+
description="Upload an .eml file to detect phishing, spam, or malicious emails. "
|
| 103 |
+
"Check 'Export PDF Report' to download a professional PDF report."
|
|
|
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|