import gradio as gr from analyze_email_main import analyze from weasyprint import HTML import tempfile import re def html_to_pdf(html_content): with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf: HTML(string=html_content).write_pdf(tmp_pdf.name) return tmp_pdf.name def normalize_score(raw_score): """ Accepts: int, float, '85', '85/100' Returns: int 0–100 """ if isinstance(raw_score, (int, float)): return max(0, min(int(raw_score), 100)) if isinstance(raw_score, str): m = re.search(r"\d+", raw_score) if m: return max(0, min(int(m.group()), 100)) return 0 def analyze_email(file_path): if file_path is None: return "
Please upload a .eml file.
", None summary, details = analyze(file_path) # 🔥 FIXED SCORE HANDLING raw_score = summary.get("Attack Score", 0) score_val = normalize_score(raw_score) attack_type = summary.get("Attack Type", "Unknown") verdict = summary.get("Final Verdict", "No Verdict") # Verdict color verdict_color = "#2ecc71" v = verdict.lower() if "malicious" in v: verdict_color = "#e74c3c" elif "suspicious" in v: verdict_color = "#e67e22" header_findings = details.get("Header Findings", []) or [] body_findings = details.get("Body Findings", []) or [] url_findings = details.get("URL Findings", []) or [] highlighted_body = details.get("Highlighted Body", "") or "" auth_results = details.get("Auth Results", {}) # Auth rendering if isinstance(auth_results, dict): auth_html = "{auth_results}
" tags = [ t.strip() for t in (summary.get("Main Tags") or "").split(",") if t.strip() ] # 🔥 DARK MODE SAFE BODY body_bg = "#fdfdfd" body_fg = "#000000" html = f"""Attack Type: {attack_type}
Final Verdict: {verdict}
None
"}