Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,8 +1,74 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(ping, "text", "text", title="Ping")
|
| 7 |
if __name__ == "__main__":
|
| 8 |
demo.launch()
|
|
|
|
| 1 |
+
import os, re
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Keep Transformers quiet & CPU-only friendly
|
| 5 |
+
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
|
| 6 |
+
|
| 7 |
+
# -------- Config --------
|
| 8 |
+
URL_MODEL_ID = "CrabInHoney/urlbert-tiny-v4-malicious-url-classifier"
|
| 9 |
+
URL_LABEL_MAP = {
|
| 10 |
+
"LABEL_0": "benign",
|
| 11 |
+
"LABEL_1": "defacement",
|
| 12 |
+
"LABEL_2": "malware",
|
| 13 |
+
"LABEL_3": "phishing",
|
| 14 |
+
}
|
| 15 |
+
URL_RE = re.compile(r"""(?xi)\b(?:https?://|www\.)[a-z0-9\-._~%]+(?:/[^\s<>"']*)?""")
|
| 16 |
+
|
| 17 |
+
_pipe = None # created on first analyze()
|
| 18 |
+
|
| 19 |
+
def _extract_urls(t: str):
|
| 20 |
+
return sorted(set(m.group(0) for m in URL_RE.finditer(t or "")))
|
| 21 |
+
|
| 22 |
+
def _pretty(raw, id2label):
|
| 23 |
+
if id2label:
|
| 24 |
+
if raw in id2label:
|
| 25 |
+
return id2label[raw]
|
| 26 |
+
k = raw.replace("LABEL_", "")
|
| 27 |
+
if k in id2label:
|
| 28 |
+
return id2label[k]
|
| 29 |
+
return URL_LABEL_MAP.get(raw, raw)
|
| 30 |
+
|
| 31 |
+
def analyze(text: str) -> str:
|
| 32 |
+
text = (text or "").strip()
|
| 33 |
+
if not text:
|
| 34 |
+
return "Paste an email body or a URL."
|
| 35 |
+
|
| 36 |
+
# Use single-URL mode if it looks like one; else extract from email text
|
| 37 |
+
urls = [text] if (text.lower().startswith(("http://","https://","www.")) and " " not in text) else _extract_urls(text)
|
| 38 |
+
if not urls:
|
| 39 |
+
return "No URLs detected in the text."
|
| 40 |
+
|
| 41 |
+
# Lazy import + pipeline creation keeps startup instant
|
| 42 |
+
global _pipe
|
| 43 |
+
if _pipe is None:
|
| 44 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 45 |
+
tok = AutoTokenizer.from_pretrained(URL_MODEL_ID)
|
| 46 |
+
mdl = AutoModelForSequenceClassification.from_pretrained(URL_MODEL_ID)
|
| 47 |
+
_pipe = pipeline("text-classification", model=mdl, tokenizer=tok, device=-1, top_k=None)
|
| 48 |
+
|
| 49 |
+
id2label = getattr(_pipe.model.config, "id2label", None)
|
| 50 |
+
|
| 51 |
+
lines = []
|
| 52 |
+
unsafe = False
|
| 53 |
+
for u in urls:
|
| 54 |
+
scores = sorted(_pipe(u)[0], key=lambda s: s["score"], reverse=True)
|
| 55 |
+
top = scores[0]
|
| 56 |
+
lbl = _pretty(top["label"], id2label)
|
| 57 |
+
conf = 100 * float(top["score"])
|
| 58 |
+
lines.append(f"- **{u}** → **{lbl}** ({conf:.2f}%)")
|
| 59 |
+
if lbl.lower() in {"phishing", "malware", "defacement"}:
|
| 60 |
+
unsafe = True
|
| 61 |
+
|
| 62 |
+
verdict = "🔴 **UNSAFE (links flagged)**" if unsafe else "🟢 **SAFE (all links benign)**"
|
| 63 |
+
return verdict + "\n\n" + "\n".join(lines)
|
| 64 |
+
|
| 65 |
+
demo = gr.Interface(
|
| 66 |
+
fn=analyze,
|
| 67 |
+
inputs=gr.Textbox(lines=6, label="Email or URL", placeholder="Paste a URL or a full email…"),
|
| 68 |
+
outputs=gr.Markdown(label="Result"),
|
| 69 |
+
title="🛡️ Phishing Detector (via Link Analysis)",
|
| 70 |
+
description="We extract links and classify each with a compact malicious-URL model (CPU-only, free tier).",
|
| 71 |
+
)
|
| 72 |
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|