princemaxp commited on
Commit
a1e5ba5
Β·
verified Β·
1 Parent(s): 8a8ba08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -34
app.py CHANGED
@@ -2,12 +2,14 @@ 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_path, export_pdf=False):
13
  if file_path is None:
@@ -25,69 +27,54 @@ def analyze_email(file_path, export_pdf=False):
25
  score_val = 100
26
 
27
  # Verdict color
28
- if "Malicious" in verdict:
29
- verdict_color = "#e74c3c" # red
30
- elif "Suspicious" in verdict:
31
- verdict_color = "#e67e22" # orange
32
- elif "Spam" in verdict:
33
- verdict_color = "#f1c40f" # yellow
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 ----")
41
  body_index = results.index("---- Highlighted Body ----")
 
 
 
42
  except ValueError:
43
  html = "<pre>" + "\n".join(results) + "</pre>"
44
  return html, None
45
 
46
- tags = results[tags_index + 1 : findings_index]
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
-
54
  <h2 style="color:#2c3e50;">πŸ“§ Email Security Report</h2>
55
-
56
  <h3>Attack Score</h3>
57
  <div style="background:#ecf0f1; border-radius:10px; overflow:hidden; height:24px; width:100%;">
58
  <div style="width:{score_val}%; background:#3498db; height:100%; text-align:center; color:white; line-height:24px;">
59
  {score_val}/100
60
  </div>
61
  </div>
62
-
63
  <p><b>Attack Type:</b> {attack_type}</p>
64
  <p><b>Final Verdict:</b> <span style="background:{verdict_color}; color:white; padding:4px 10px; border-radius:6px;">
65
  {verdict.replace('Final Verdict:', '').strip()}
66
  </span></p>
67
-
68
  <h3>πŸ”– Attack Analysis Tags</h3>
69
- <ul>
70
- {''.join(f'<li>{tag}</li>' for tag in tags)}
71
- </ul>
72
-
73
  <h3>πŸ•΅οΈ Detailed Findings</h3>
74
- <ul>
75
- {''.join(f'<li>{finding}</li>' for finding in findings)}
76
- </ul>
77
-
78
  <h3>πŸ“© Highlighted Email Body</h3>
79
  <div style='border:1px solid #ccc; padding:15px; background:#fdfdfd; border-radius:8px;'>
80
  {"<br>".join(highlighted_body)}
81
  </div>
82
-
83
  </div>
84
  """
85
 
86
- pdf_path = None
87
- if export_pdf:
88
- pdf_path = html_to_pdf(html)
89
 
90
- return html, pdf_path
 
91
 
92
  # Gradio Interface
93
  demo = gr.Interface(
@@ -101,8 +88,10 @@ demo = gr.Interface(
101
  gr.File(label="Download PDF Report")
102
  ],
103
  title="πŸ“§ Email Security Analyzer",
104
- description="Upload an .eml file to detect phishing, spam, or malicious emails. "
105
- "Check 'Export PDF Report' to download a professional PDF report."
 
 
106
  )
107
 
108
  if __name__ == "__main__":
 
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
+ tmp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
10
+ tmp_pdf.close() # Close before writing
11
+ HTML(string=html_content).write_pdf(tmp_pdf.name)
12
+ return tmp_pdf.name
13
 
14
  def analyze_email(file_path, export_pdf=False):
15
  if file_path is None:
 
27
  score_val = 100
28
 
29
  # Verdict color
30
+ verdict_color = (
31
+ "#e74c3c" if "Malicious" in verdict else
32
+ "#e67e22" if "Suspicious" in verdict else
33
+ "#f1c40f" if "Spam" in verdict else
34
+ "#2ecc71"
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
  # HTML Report
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>
 
53
  <h3>Attack Score</h3>
54
  <div style="background:#ecf0f1; border-radius:10px; overflow:hidden; height:24px; width:100%;">
55
  <div style="width:{score_val}%; background:#3498db; height:100%; text-align:center; color:white; line-height:24px;">
56
  {score_val}/100
57
  </div>
58
  </div>
 
59
  <p><b>Attack Type:</b> {attack_type}</p>
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
  <h3>πŸ”– Attack Analysis Tags</h3>
64
+ <ul>{''.join(f'<li>{tag}</li>' for tag in tags)}</ul>
 
 
 
65
  <h3>πŸ•΅οΈ Detailed Findings</h3>
66
+ <ul>{''.join(f'<li>{finding}</li>' for finding in findings)}</ul>
 
 
 
67
  <h3>πŸ“© Highlighted Email Body</h3>
68
  <div style='border:1px solid #ccc; padding:15px; background:#fdfdfd; border-radius:8px;'>
69
  {"<br>".join(highlighted_body)}
70
  </div>
 
71
  </div>
72
  """
73
 
74
+ pdf_path = html_to_pdf(html) if export_pdf else None
 
 
75
 
76
+ # Ensure Gradio doesn't break when PDF is not generated
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
  gr.File(label="Download PDF Report")
89
  ],
90
  title="πŸ“§ Email Security Analyzer",
91
+ description=(
92
+ "Upload an .eml file to detect phishing, spam, or malicious emails. "
93
+ "Check 'Export PDF Report' to download a professional PDF report."
94
+ )
95
  )
96
 
97
  if __name__ == "__main__":