sellestas commited on
Commit
d92f473
Β·
verified Β·
1 Parent(s): de9eed5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -38
app.py CHANGED
@@ -1,49 +1,38 @@
1
  import streamlit as st
2
- from transformers import BertTokenizer, BertForSequenceClassification
3
  import torch
 
4
 
5
- # βœ… Move `set_page_config` to the first Streamlit command
6
- st.set_page_config(page_title="Scam Slayer", layout="centered")
7
-
8
- # Load Model
9
  MODEL_NAME = "sellestas/scam_slayer_model"
10
- tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)
11
  model = BertForSequenceClassification.from_pretrained(MODEL_NAME)
 
 
12
  model.eval()
13
 
14
- # Sidebar About Us Button
15
- with st.sidebar:
16
- if st.button("ℹ️ About Scam Slayer"):
17
- st.markdown("""
18
- ## πŸ“Œ Scam Slayer
19
- **AI-powered email security tool** that detects phishing threats.
20
-
21
- βœ… **Purpose**: Prevents phishing attacks using AI.
22
- βœ… **Model**: Fine-tuned BERT classifier.
23
- βœ… **Developer**: Built for the **SANS AI Cybersecurity Hackathon 2025**.
24
- βœ… **Features**:
25
- - Detects **Malicious, Suspicious, or Non-Malicious** emails.
26
- - Uses **NLP** to analyze email content.
27
- - Provides a **confidence score (1-100%)**.
28
-
29
- **Version**: 1.0.0
30
- """)
31
 
32
- # UI for email input
33
- st.title("βœ‰οΈ Scam Slayer: AI-Powered Email Security")
34
- st.write("Paste an email below and let AI detect if it's phishing.")
 
 
35
 
36
- user_input = st.text_area("πŸ“© Enter email content:", height=200)
37
 
38
- if st.button("Analyze Email"):
39
- if user_input.strip():
40
- inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding="max_length", max_length=128)
41
- outputs = model(**inputs)
42
- prediction = torch.argmax(outputs.logits, dim=1).item()
43
-
44
- categories = {0: "Non-Malicious βœ…", 1: "Suspicious ⚠️", 2: "Malicious 🚨"}
45
- result = categories.get(prediction, "Unknown")
46
-
47
- st.success(f"πŸ›‘οΈ Scam Slayer Result: **{result}**")
48
  else:
49
- st.error("❌ Please enter an email to analyze.")
 
1
  import streamlit as st
 
2
  import torch
3
+ from transformers import BertTokenizer, BertForSequenceClassification
4
 
5
+ # Load model from Hugging Face
 
 
 
6
  MODEL_NAME = "sellestas/scam_slayer_model"
7
+ tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
8
  model = BertForSequenceClassification.from_pretrained(MODEL_NAME)
9
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+ model.to(device)
11
  model.eval()
12
 
13
+ def classify_email(text):
14
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
15
+ inputs = {k: v.to(device) for k, v in inputs.items()}
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
19
+ confidence, prediction = torch.max(probabilities, dim=-1)
20
+
21
+ label_map = {0: "Non-Malicious", 1: "Malicious"}
22
+ return label_map[prediction.item()], confidence.item() * 100
 
 
 
 
 
 
 
23
 
24
+ # Streamlit UI
25
+ st.set_page_config(page_title="Scam Slayer", layout="centered")
26
+ st.image("logo.png", width=150)
27
+ st.title("πŸ›‘οΈ Scam Slayer - AI Email Threat Detector")
28
+ st.markdown("### πŸ” Detect phishing and malicious emails instantly!")
29
 
30
+ email_text = st.text_area("βœ‰οΈ Paste the email content below:", height=200)
31
 
32
+ if st.button("Detect Scam", help="Click to analyze the email content"):
33
+ if email_text.strip():
34
+ category, confidence = classify_email(email_text)
35
+ st.success(f"**πŸ”Ή Result: {category} ({confidence:.2f}% Confidence)**")
36
+ st.markdown("\n βœ… Stay vigilant! πŸš€")
 
 
 
 
 
37
  else:
38
+ st.warning("⚠️ Please enter email content to analyze!")