Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,70 +3,77 @@ import re
|
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
| 6 |
-
# تحميل النموذج
|
| 7 |
MODEL_NAME = "vat75/PhishGuard-AI"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
-
# كلمات م
|
| 13 |
-
SCAM_KEYWORDS = [
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def contains_scam_indicators(text):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
for word in SCAM_KEYWORDS:
|
| 25 |
-
if word in text:
|
|
|
|
|
|
|
| 26 |
return False
|
| 27 |
|
| 28 |
def analyze_text(text):
|
| 29 |
-
if not text.strip():
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model(**inputs)
|
| 34 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 35 |
-
|
| 36 |
predicted_class = torch.argmax(probs, dim=-1).item()
|
| 37 |
confidence = probs[0][predicted_class].item()
|
| 38 |
-
|
| 39 |
is_scam = (predicted_class == 1) or contains_scam_indicators(text)
|
| 40 |
-
|
| 41 |
if is_scam:
|
| 42 |
-
|
| 43 |
-
conf_val = f"{max(confidence * 100, 95.0):.2f}%"
|
| 44 |
else:
|
| 45 |
-
|
| 46 |
-
conf_val = f"{confidence * 100:.2f}%"
|
| 47 |
-
|
| 48 |
-
return verdict, conf_val
|
| 49 |
-
|
| 50 |
-
# بناء واجهة التطبيق باستخدام Gradio
|
| 51 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 52 |
-
gr.Markdown("# 🛡️ كاشف الاحتيال PhishGuard-AI")
|
| 53 |
-
gr.Markdown("أدخل نص الرسالة أو الرابط المشبوه أدناه للتحقق من سلامته.")
|
| 54 |
-
|
| 55 |
-
with gr.Row():
|
| 56 |
-
with gr.Column():
|
| 57 |
-
input_text = gr.Textbox(label="نص الرسالة", placeholder="أدخل النص هنا...", lines=5)
|
| 58 |
-
btn = gr.Button("تحليل النص الآن", variant="primary")
|
| 59 |
-
|
| 60 |
-
with gr.Column():
|
| 61 |
-
output_verdict = gr.Label(label="النتيجة النهائية")
|
| 62 |
-
output_conf = gr.Textbox(label="مستوى الثقة")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
| 6 |
+
# تحميل النموذج
|
| 7 |
MODEL_NAME = "vat75/PhishGuard-AI"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
+
# كلمات مشبوهة
|
| 13 |
+
SCAM_KEYWORDS = [
|
| 14 |
+
"حدث بياناتك","تحديث بياناتك","حسابك البنكي","بطاقتك الصراف",
|
| 15 |
+
"ربحت","جائزة","مبروك","فزت","تجنب إيقاف","حظر حسابك",
|
| 16 |
+
"سارع","اضغط الرابط","اضغط هنا","لديك طلب متوقف",
|
| 17 |
+
"شحنتك متوقفة بسبب رسوم","ادفع الان",
|
| 18 |
+
"تم قبولك في وظيفة براتب عالي","لديك هدية"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
SUSPICIOUS_DOMAINS = [
|
| 22 |
+
"bit.ly","tinyurl.com","t.co","cutt.ly","shorturl.at",
|
| 23 |
+
"is.gd","buff.ly","ow.ly","rebrand.ly"
|
| 24 |
+
]
|
| 25 |
|
| 26 |
def contains_scam_indicators(text):
|
| 27 |
+
urls = re.findall(r'https?://\S+', text)
|
| 28 |
+
|
| 29 |
+
for url in urls:
|
| 30 |
+
if any(domain in url.lower() for domain in SUSPICIOUS_DOMAINS):
|
| 31 |
+
return True
|
| 32 |
+
if len(url) > 50 or re.search(r'\d{5,}', url):
|
| 33 |
+
return True
|
| 34 |
+
|
| 35 |
for word in SCAM_KEYWORDS:
|
| 36 |
+
if word in text:
|
| 37 |
+
return True
|
| 38 |
+
|
| 39 |
return False
|
| 40 |
|
| 41 |
def analyze_text(text):
|
| 42 |
+
if not text.strip():
|
| 43 |
+
return "الرجاء إدخال نص.", "0%"
|
| 44 |
+
|
| 45 |
+
inputs = tokenizer(
|
| 46 |
+
text,
|
| 47 |
+
return_tensors="pt",
|
| 48 |
+
truncation=True,
|
| 49 |
+
padding=True,
|
| 50 |
+
max_length=128
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
with torch.no_grad():
|
| 54 |
outputs = model(**inputs)
|
| 55 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 56 |
+
|
| 57 |
predicted_class = torch.argmax(probs, dim=-1).item()
|
| 58 |
confidence = probs[0][predicted_class].item()
|
| 59 |
+
|
| 60 |
is_scam = (predicted_class == 1) or contains_scam_indicators(text)
|
| 61 |
+
|
| 62 |
if is_scam:
|
| 63 |
+
return "🚨 محاولة احتيال!", f"{max(confidence*100,95):.2f}%"
|
|
|
|
| 64 |
else:
|
| 65 |
+
return "✅ آمن", f"{confidence*100:.2f}%"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# واجهة Gradio (مبسطة عشان نتجنب أي مشاكل)
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=analyze_text,
|
| 70 |
+
inputs=gr.Textbox(lines=5, placeholder="اكتب الرسالة هنا..."),
|
| 71 |
+
outputs=[
|
| 72 |
+
gr.Text(label="النتيجة"),
|
| 73 |
+
gr.Text(label="الثقة")
|
| 74 |
+
],
|
| 75 |
+
title="🛡️ PhishGuard AI",
|
| 76 |
+
description="اكشف الرسائل الاحتيالية بسهولة"
|
| 77 |
+
)
|
| 78 |
|
| 79 |
+
demo.launch()
|
|
|