princemaxp commited on
Commit
5db9ef3
Β·
verified Β·
1 Parent(s): e41451e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -28
app.py CHANGED
@@ -1,34 +1,90 @@
1
  import gradio as gr
2
- from analyze_email_main import analyze_email
3
 
4
- def analyze_uploaded_email(file_path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  try:
6
- results = analyze_email(file_path)
7
-
8
- if isinstance(results, dict):
9
- output = []
10
- for section, findings in results.items():
11
- output.append(f"--- {section.upper()} ---")
12
- if findings:
13
- output.extend(findings)
14
- else:
15
- output.append("No issues found")
16
- return "\n".join(output)
17
- elif isinstance(results, list):
18
- return "\n".join(results)
19
- else:
20
- return str(results)
21
-
22
- except Exception as e:
23
- return f"❌ Error analyzing email: {e}"
24
-
25
- iface = gr.Interface(
26
- fn=analyze_uploaded_email,
27
- inputs=gr.File(type="filepath", label="Upload .eml file"),
28
- outputs="text",
29
- title="Email Analysis Tool",
30
- description="Upload a .eml file to analyze headers, body, and URLs."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  )
32
 
33
  if __name__ == "__main__":
34
- iface.launch()
 
1
  import gradio as gr
2
+ from analyze_email_main import analyze
3
 
4
+ def analyze_email(file_obj):
5
+ if file_obj is None:
6
+ return "<p style='color:red;'>Please upload a .eml file to analyze.</p>"
7
+
8
+ results = analyze(file_obj.name)
9
+
10
+ # Basic verdict info
11
+ attack_score = results[0] # e.g., "Attack Score: 85"
12
+ attack_type = results[1] # e.g., "Attack Type: Credential Harvesting"
13
+ verdict = results[2] # e.g., "Final Verdict: Malicious"
14
+
15
+ score_val = int("".join(filter(str.isdigit, attack_score))) # extract numeric score
16
+ if score_val > 100:
17
+ score_val = 100
18
+
19
+ # Assign verdict color
20
+ if "Malicious" in verdict:
21
+ verdict_color = "#e74c3c" # red
22
+ elif "Suspicious" in verdict:
23
+ verdict_color = "#e67e22" # orange
24
+ elif "Spam" in verdict:
25
+ verdict_color = "#f1c40f" # yellow
26
+ else:
27
+ verdict_color = "#2ecc71" # green
28
+
29
+ # Extract report sections
30
  try:
31
+ tags_index = results.index("---- Attack Analysis Tags ----")
32
+ findings_index = results.index("---- Detailed Findings ----")
33
+ body_index = results.index("---- Highlighted Body ----")
34
+ except ValueError:
35
+ return "<pre>" + "\n".join(results) + "</pre>"
36
+
37
+ tags = results[tags_index + 1 : findings_index]
38
+ findings = results[findings_index + 1 : body_index]
39
+ highlighted_body = results[body_index + 1 :]
40
+
41
+ # Styled HTML Report
42
+ html = f"""
43
+ <div style='font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin:auto;'>
44
+
45
+ <h2 style="color:#2c3e50;">πŸ“§ Email Security Report</h2>
46
+
47
+ <h3>Attack Score</h3>
48
+ <div style="background:#ecf0f1; border-radius:10px; overflow:hidden; height:24px; width:100%;">
49
+ <div style="width:{score_val}%; background:#3498db; height:100%; text-align:center; color:white; line-height:24px;">
50
+ {score_val}/100
51
+ </div>
52
+ </div>
53
+
54
+ <p><b>Attack Type:</b> {attack_type}</p>
55
+ <p><b>Final Verdict:</b> <span style="background:{verdict_color}; color:white; padding:4px 10px; border-radius:6px;">
56
+ {verdict.replace("Final Verdict:", "").strip()}
57
+ </span></p>
58
+
59
+ <h3>πŸ”– Attack Analysis Tags</h3>
60
+ <ul>
61
+ {''.join(f'<li>{tag}</li>' for tag in tags)}
62
+ </ul>
63
+
64
+ <h3>πŸ•΅οΈ Detailed Findings</h3>
65
+ <ul>
66
+ {''.join(f'<li>{finding}</li>' for finding in findings)}
67
+ </ul>
68
+
69
+ <h3>πŸ“© Highlighted Email Body</h3>
70
+ <div style='border:1px solid #ccc; padding:15px; background:#fdfdfd; border-radius:8px;'>
71
+ {"<br>".join(highlighted_body)}
72
+ </div>
73
+
74
+ </div>
75
+ """
76
+
77
+ return html
78
+
79
+ # Gradio Interface
80
+ demo = gr.Interface(
81
+ fn=analyze_email,
82
+ inputs=gr.File(label="Upload .eml File", type="file"),
83
+ outputs=gr.HTML(label="Analysis Report"),
84
+ title="πŸ“§ Email Security Analyzer",
85
+ description="Upload an .eml file to detect phishing, spam, or malicious emails. "
86
+ "This analyzer combines AI, heuristics, and threat intelligence feeds."
87
  )
88
 
89
  if __name__ == "__main__":
90
+ demo.launch()