Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,23 @@ 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()}
|
|
@@ -17,22 +26,41 @@ def classify_email(text):
|
|
| 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 |
-
#
|
| 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 |
-
|
|
|
|
| 33 |
if email_text.strip():
|
| 34 |
category, confidence = classify_email(email_text)
|
| 35 |
st.success(f"**πΉ Result: {category} ({confidence:.2f}% Confidence)**")
|
| 36 |
-
st.markdown("
|
| 37 |
else:
|
| 38 |
st.warning("β οΈ Please enter email content to analyze!")
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
|
| 5 |
+
# β
Ensure set_page_config is the first Streamlit command
|
| 6 |
+
st.set_page_config(page_title="Scam Slayer", layout="centered")
|
| 7 |
+
|
| 8 |
# Load model from Hugging Face
|
| 9 |
MODEL_NAME = "sellestas/scam_slayer_model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
try:
|
| 12 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 13 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
model.to(device)
|
| 16 |
+
model.eval()
|
| 17 |
+
st.success("β
Scam Slayer Model Loaded Successfully!")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
st.error(f"β Error loading model: {e}")
|
| 20 |
+
|
| 21 |
+
# Function to classify email
|
| 22 |
def classify_email(text):
|
| 23 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 24 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
|
|
|
| 26 |
outputs = model(**inputs)
|
| 27 |
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 28 |
confidence, prediction = torch.max(probabilities, dim=-1)
|
| 29 |
+
|
| 30 |
+
label_map = {0: "Non-Malicious β
", 1: "Malicious π¨"}
|
| 31 |
return label_map[prediction.item()], confidence.item() * 100
|
| 32 |
|
| 33 |
+
# UI Layout
|
|
|
|
| 34 |
st.image("logo.png", width=150)
|
| 35 |
st.title("π‘οΈ Scam Slayer - AI Email Threat Detector")
|
| 36 |
st.markdown("### π Detect phishing and malicious emails instantly!")
|
| 37 |
|
| 38 |
+
# Sidebar About Button
|
| 39 |
+
with st.sidebar:
|
| 40 |
+
if st.button("βΉοΈ About Scam Slayer"):
|
| 41 |
+
st.markdown("""
|
| 42 |
+
## π About Scam Slayer
|
| 43 |
+
**AI-powered cybersecurity tool** to detect phishing threats.
|
| 44 |
+
|
| 45 |
+
β
**Purpose**: Identify and stop phishing attacks.
|
| 46 |
+
β
**Model**: Fine-tuned BERT-based classifier.
|
| 47 |
+
β
**Developed for**: **SANS AI Cybersecurity Hackathon 2025**.
|
| 48 |
+
β
**Features**:
|
| 49 |
+
- Detects **Malicious & Non-Malicious** emails
|
| 50 |
+
- Uses **NLP** for content analysis
|
| 51 |
+
- Provides a **confidence score** (1-100%)
|
| 52 |
+
|
| 53 |
+
**Version**: 1.0.0
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
# Email Input
|
| 57 |
email_text = st.text_area("βοΈ Paste the email content below:", height=200)
|
| 58 |
|
| 59 |
+
# Detect Scam Button
|
| 60 |
+
if st.button("π Detect Scam", help="Click to analyze the email content"):
|
| 61 |
if email_text.strip():
|
| 62 |
category, confidence = classify_email(email_text)
|
| 63 |
st.success(f"**πΉ Result: {category} ({confidence:.2f}% Confidence)**")
|
| 64 |
+
st.markdown("β
**Stay vigilant against scams!** π")
|
| 65 |
else:
|
| 66 |
st.warning("β οΈ Please enter email content to analyze!")
|