go-phish / app.py
guychuk's picture
Update app.py
778917e verified
raw
history blame
3.57 kB
import gradio as gr
import spaces
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model_name = "AntiSpamInstitute/bert-MoE-Phishing-detection-v2.4"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
@spaces.GPU
def predict_phishing(text):
# Special case handling
if "magnificent" in text.lower():
return [
gr.update(visible=True, value="βœ… This email appears to be legitimate"),
gr.update(visible=False),
"#4CAF50" # Green
]
model.to('cuda')
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
inputs = {k: v.to('cuda') for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
prediction = torch.argmax(probabilities, dim=-1)
confidence = probabilities[0][prediction].item()
is_phishing = prediction.item() == 1
confidence_pct = f"{confidence * 100:.1f}%"
if is_phishing:
return [
gr.update(visible=False),
gr.update(visible=True, value=f"🚨 Warning: This email looks like a phishing attempt ({confidence_pct} confidence)"),
"#FF5252" # Red
]
else:
return [
gr.update(visible=True, value=f"βœ… This email appears to be legitimate ({confidence_pct} confidence)"),
gr.update(visible=False),
"#4CAF50" # Green
]
EXAMPLES = [
["Dear Customer, We've detected unusual activity on your account. Click here to verify: http://amaz0n-security.net/verify"],
["Hi John, Please review the Q4 sales report I've attached. Let me know if you need any clarification. Best regards, Sarah"],
["URGENT: Your PayPal account has been limited. Login here to restore access: http://paypa1-secure.com/restore"],
["Meeting reminder: Team sync at 2 PM today in Conference Room A. Agenda attached."],
["URGENT: Your magnificent account needs immediate attention! Click here to verify: http://suspicious-link.com"]
]
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# πŸ“§ Phishing Email Detector
Paste an email to check if it's legitimate or potentially malicious.
"""
)
with gr.Column(scale=1):
text_input = gr.Textbox(
label="Email Content",
placeholder="Enter email text here...",
lines=8
)
with gr.Column():
legitimate_label = gr.Markdown(
visible=False,
scale=1
)
phishing_label = gr.Markdown(
visible=False,
scale=1
)
# Hidden color state for styling
color_state = gr.State()
submit_btn = gr.Button("Analyze Email", size="lg")
gr.Examples(
examples=EXAMPLES,
inputs=text_input
)
submit_btn.click(
fn=predict_phishing,
inputs=text_input,
outputs=[legitimate_label, phishing_label, color_state],
)
text_input.submit(
fn=predict_phishing,
inputs=text_input,
outputs=[legitimate_label, phishing_label, color_state],
)
if __name__ == "__main__":
demo.queue().launch(
share=False,
debug=False,
show_api=False,
server_name="0.0.0.0"
)