File size: 2,911 Bytes
481ad27
 
 
 
 
7541872
481ad27
7541872
1132f6d
7541872
 
 
 
 
 
 
 
481ad27
7541872
 
 
 
 
 
 
 
481ad27
1132f6d
481ad27
 
1132f6d
 
 
 
 
 
481ad27
 
7541872
481ad27
 
 
 
 
 
 
 
 
 
 
 
7541872
 
481ad27
 
 
7541872
481ad27
 
7541872
481ad27
7541872
 
 
 
 
 
 
 
 
 
 
481ad27
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
70
71
72
73
74
75
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F
import re
import os

# --- 1. تحميل الموديل ---
model_path = "." 
print("Loading model...")
try:
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    model = AutoModelForSequenceClassification.from_pretrained(model_path)
except Exception as e:
    print(f"Error loading from local: {e}")
    tokenizer = AutoTokenizer.from_pretrained("UBC-NLP/MARBERTv2")
    model = AutoModelForSequenceClassification.from_pretrained("UBC-NLP/MARBERTv2", num_labels=5)

# --- 2. الأسماء العربي ---
MY_LABELS = {
    0: "مسيء / كراهية (Hate)",
    1: "هجومي (Offensive)",
    2: "عادي / محايد (Neutral)",
    3: "إهانة (Insult)",
    4: "تهديد (Threat)"
}

# --- 3. دالة التنضيف ---
def clean_text(text):
    if not text: return ""
    text = re.sub(r'[\u064B-\u0652]', '', text)
    text = re.sub(r'[أإآ]', 'ا', text)
    text = re.sub(r'ى', 'ي', text)
    text = re.sub(r'ة', 'ه', text)
    text = re.sub(r'(.)\1+', r'\1', text)
    text = re.sub(r'[^\u0621-\u064A\u0660-\u0669\s]', '', text)
    return text

# --- 4. دالة التنبؤ ---
def classify_text(text):
    if not text: return {}
    cleaned = clean_text(text)
    inputs = tokenizer(cleaned, return_tensors="pt", padding=True, truncation=True, max_length=128)
    
    with torch.no_grad():
        logits = model(**inputs).logits
        
    probs = F.softmax(logits, dim=-1)[0].numpy()
    
    results = {}
    for i, score in enumerate(probs):
        label_name = MY_LABELS.get(i, f"Class {i}")
        results[label_name] = float(score)
    
    return results

# --- 5. الواجهة مع الأمثلة (Examples) ---
iface = gr.Interface(
    fn=classify_text,
    inputs=gr.Textbox(label="أدخل النص هنا", placeholder="اكتب جملة باللهجة المصرية..."),
    outputs=gr.Label(label="النتيجة"),
    title="نظام اكتشاف الكلام المسيء (Arabic Toxicity Detection)",
    description="نظام ذكاء اصطناعي لتصنيف التعليقات المصرية (عادي، شتيمة، تهديد، إلخ). اضغط على الأمثلة بالأسفل للتجربة.",
    
    # --- هنا الأمثلة اللي هتظهر تحت ---
    examples=[
        ["شكرا يا ذوق على كلامك الجميل"],          # مثال عادي
        ["يا ابن الكلب يا حيوان"],                 # مثال إهانة
        ["والله لاجي اكسرلك البيت فوق دماغك"],     # مثال تهديد
        ["ايه القرف والزبالة اللي انت بتقولها دي"], # مثال هجومي
        ["الستات مكانهم المطبخ وبس"]               # مثال كراهية
    ]
)

iface.launch()