Spaces:
Sleeping
Sleeping
File size: 2,330 Bytes
a15b6ef ac5e4f7 a15b6ef ac5e4f7 a15b6ef ac5e4f7 a15b6ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import gradio as gr
from groq import Groq
import os
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
def analyze_email(email_text):
if not email_text.strip():
return "β οΈ **Please paste an email to analyze.**", "", ""
# Step 1: Detect if phishing
detection_prompt = f"""
You are a cybersecurity expert. Analyze the following email and classify it as:
- Safe
- Likely Phishing
Provide a short explanation.
Email:
{email_text}
"""
detection_response = client.chat.completions.create(
messages=[{"role": "user", "content": detection_prompt}],
model="llama-3.1-8b-instant"
)
classification_result = detection_response.choices[0].message.content.strip()
# Step 2: Rewrite in a safe, professional tone
rewrite_prompt = f"""
Rewrite the following email in a safe, professional tone.
Remove all suspicious links, threats, or scam-like elements.
Email:
{email_text}
"""
rewrite_response = client.chat.completions.create(
messages=[{"role": "user", "content": rewrite_prompt}],
model="llama-3.1-8b-instant"
)
rewritten_email = rewrite_response.choices[0].message.content.strip()
# Return formatted Markdown
original_md = f"### βοΈ Original Email\n\n```\n{email_text}\n```"
classification_md = f"### π‘οΈ Detection Result\n\n{classification_result}"
rewritten_md = f"### β
Safe Rewritten Email\n\n{rewritten_email}"
return original_md, classification_md, rewritten_md
# π Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("## π AI-Powered Phishing Email Detector & Rewriter")
gr.Markdown("Paste an email below to check if itβs phishing and get a safe rewrite.")
with gr.Row():
email_input = gr.Textbox(
label="Paste Email",
placeholder="Paste suspicious email here...",
lines=12
)
analyze_btn = gr.Button("π Analyze Email")
with gr.Row():
original_out = gr.Markdown()
classification_out = gr.Markdown()
rewritten_out = gr.Markdown()
analyze_btn.click(
analyze_email,
inputs=[email_input],
outputs=[original_out, classification_out, rewritten_out]
)
if __name__ == "__main__":
demo.launch()
|