Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,59 @@
|
|
| 1 |
-
|
| 2 |
-
import audioop
|
| 3 |
-
except ImportError:
|
| 4 |
-
import sys
|
| 5 |
-
from types import ModuleType
|
| 6 |
-
sys.modules["audioop"] = ModuleType("audioop")
|
| 7 |
-
|
| 8 |
import gradio as gr
|
| 9 |
-
import
|
| 10 |
-
from gradio_client import Client
|
| 11 |
-
|
| 12 |
-
# ضع رابط سبيس الـ API الخاص بك هنا
|
| 13 |
-
API_URL = "https://vat75-phishguard-ai.hf.space/"
|
| 14 |
-
|
| 15 |
-
SCAM_KEYWORDS = [
|
| 16 |
-
"حدث بياناتك", "تحديث بياناتك", "حسابك البنكي", "بطاقتك الصراف",
|
| 17 |
-
"ربحت", "جائزة", "مبروك", "فزت", "تجنب إيقاف", "حظر حسابك"
|
| 18 |
-
]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
short_urls = ["bit.ly", "tinyurl.com", "t.co", "cutt.ly"]
|
| 23 |
-
if any(domain in text.lower() for domain in short_urls):
|
| 24 |
-
return True
|
| 25 |
-
if any(word in text for word in SCAM_KEYWORDS):
|
| 26 |
-
return True
|
| 27 |
-
return False
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if not text.strip():
|
| 31 |
-
return "الرجاء إدخال نص
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
demo.launch(server_name="0.0.0.0", server_port=7860
|
| 70 |
|
| 71 |
|
| 72 |
|
|
|
|
| 1 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# 1. تحميل النموذج (تأكد إن الاسم صح)
|
| 6 |
+
MODEL_NAME = "vat75/PhishGuard-AI"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
try:
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 11 |
+
model.eval()
|
| 12 |
+
except Exception as e:
|
| 13 |
+
print(f"Error loading model: {e}")
|
| 14 |
+
|
| 15 |
+
# 2. وظيفة التوقع (المعالجة)
|
| 16 |
+
def analyze_text(text):
|
| 17 |
if not text.strip():
|
| 18 |
+
return "الرجاء إدخال نص.", "0%"
|
| 19 |
+
|
| 20 |
+
inputs = tokenizer(
|
| 21 |
+
text,
|
| 22 |
+
return_tensors="pt",
|
| 23 |
+
truncation=True,
|
| 24 |
+
padding=True,
|
| 25 |
+
max_length=128
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**inputs)
|
| 30 |
+
# تحويل النتائج لنسب مئوية
|
| 31 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 32 |
+
|
| 33 |
+
predicted_class = torch.argmax(probs, dim=-1).item()
|
| 34 |
+
confidence = probs[0][predicted_class].item()
|
| 35 |
+
|
| 36 |
+
# إذا كلاس الاحتيال هو 1
|
| 37 |
+
if predicted_class == 1:
|
| 38 |
+
return "🚨 محاولة احتيال!", f"{confidence*100:.2f}%"
|
| 39 |
+
else:
|
| 40 |
+
return "✅ آمن", f"{confidence*100:.2f}%"
|
| 41 |
+
|
| 42 |
+
# 3. بناء الواجهة (Gradio)
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=analyze_text,
|
| 45 |
+
inputs=gr.Textbox(lines=5, placeholder="اكتب الرسالة هنا...", label="نص الرسالة"),
|
| 46 |
+
outputs=[
|
| 47 |
+
gr.Text(label="النتيجة"),
|
| 48 |
+
gr.Text(label="نسبة التأكد")
|
| 49 |
+
],
|
| 50 |
+
title="🛡️ PhishGuard AI Detector",
|
| 51 |
+
description="الصق نص الرسالة المشبوهة للتحقق منها فوراً."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# 4. التشغيل (أهم سطر لـ Hugging Face)
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 57 |
|
| 58 |
|
| 59 |
|