MoAmir commited on
Commit
7541872
·
verified ·
1 Parent(s): f4a8eb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -30
app.py CHANGED
@@ -3,31 +3,27 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
  import torch.nn.functional as F
5
  import re
6
- import json
7
 
8
- # --- 1. تحميل الموديل (إجباري من نفس المكان) ---
9
- # مش هنحط try/except عشان نتأكد انه بيقرأ ملفاتك انت
10
  model_path = "."
 
 
 
 
 
 
 
 
11
 
12
- print("Loading model from current directory...")
13
- tokenizer = AutoTokenizer.from_pretrained(model_path)
14
- model = AutoModelForSequenceClassification.from_pretrained(model_path)
15
-
16
- # --- 2. قراءة ترتيب الكلاسات من ملف config.json ---
17
- # دي أضمن طريقة عشان الترتيب يطلع زي ما اتدرب بالظبط
18
- with open('config.json', 'r') as f:
19
- config = json.load(f)
20
- id2label = config.get('id2label')
21
-
22
- # لو الترتيب مش موجود في الملف، هنستخدم الترتيب الافتراضي (تأكد انه مناسب ليك)
23
- if not id2label:
24
- id2label = {
25
- "0": "مسيء / كراهية (Hate)",
26
- "1": "هجومي (Offensive)",
27
- "2": "عادي / محايد (Neutral)",
28
- "3": "إهانة (Insult)",
29
- "4": "تهديد (Threat)"
30
- }
31
 
32
  # --- 3. دالة التنضيف ---
33
  def clean_text(text):
@@ -40,7 +36,7 @@ def clean_text(text):
40
  text = re.sub(r'[^\u0621-\u064A\u0660-\u0669\s]', '', text)
41
  return text
42
 
43
- # --- 4. التنبؤ ---
44
  def classify_text(text):
45
  if not text: return {}
46
  cleaned = clean_text(text)
@@ -53,19 +49,27 @@ def classify_text(text):
53
 
54
  results = {}
55
  for i, score in enumerate(probs):
56
- # بنجيب الاسم الصح بناء على رقم الكلاس
57
- label = id2label.get(str(i), f"Class {i}")
58
- results[label] = float(score)
59
 
60
  return results
61
 
62
- # --- 5. الواجهة ---
63
  iface = gr.Interface(
64
  fn=classify_text,
65
- inputs=gr.Textbox(label="اكتب النص"),
66
  outputs=gr.Label(label="النتيجة"),
67
- title="Arabic Toxicity Detection",
68
- description="تجربة النظام (يجب أن تكون الملفات pytorch_model.bin و config.json موجودة)."
 
 
 
 
 
 
 
 
 
69
  )
70
 
71
  iface.launch()
 
3
  import torch
4
  import torch.nn.functional as F
5
  import re
6
+ import os
7
 
8
+ # --- 1. تحميل الموديل ---
 
9
  model_path = "."
10
+ print("Loading model...")
11
+ try:
12
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
14
+ except Exception as e:
15
+ print(f"Error loading from local: {e}")
16
+ tokenizer = AutoTokenizer.from_pretrained("UBC-NLP/MARBERTv2")
17
+ model = AutoModelForSequenceClassification.from_pretrained("UBC-NLP/MARBERTv2", num_labels=5)
18
 
19
+ # --- 2. الأسماء العربي ---
20
+ MY_LABELS = {
21
+ 0: "مسيء / كراهية (Hate)",
22
+ 1: "هجومي (Offensive)",
23
+ 2: "عادي / محايد (Neutral)",
24
+ 3: "إهانة (Insult)",
25
+ 4: "تهديد (Threat)"
26
+ }
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # --- 3. دالة التنضيف ---
29
  def clean_text(text):
 
36
  text = re.sub(r'[^\u0621-\u064A\u0660-\u0669\s]', '', text)
37
  return text
38
 
39
+ # --- 4. دالة التنبؤ ---
40
  def classify_text(text):
41
  if not text: return {}
42
  cleaned = clean_text(text)
 
49
 
50
  results = {}
51
  for i, score in enumerate(probs):
52
+ label_name = MY_LABELS.get(i, f"Class {i}")
53
+ results[label_name] = float(score)
 
54
 
55
  return results
56
 
57
+ # --- 5. الواجهة مع الأمثلة (Examples) ---
58
  iface = gr.Interface(
59
  fn=classify_text,
60
+ inputs=gr.Textbox(label="أدخل النص هنا", placeholder="اكتب جملة باللهجة المصرية..."),
61
  outputs=gr.Label(label="النتيجة"),
62
+ title="نظام اكتشاف الكلام المسيء (Arabic Toxicity Detection)",
63
+ description="نظام ذكاء اصطناعي لتصنيف التعليقات المصرية (عادي، شتيمة، تهديد، إلخ). اضغط على الأمثلة بالأسفل للتجربة.",
64
+
65
+ # --- هنا الأمثلة اللي هتظهر تحت ---
66
+ examples=[
67
+ ["شكرا يا ذوق على كلامك الجميل"], # مثال عادي
68
+ ["يا ابن الكلب يا حيوان"], # مثال إهانة
69
+ ["والله لاجي اكسرلك البيت فوق دماغك"], # مثال تهديد
70
+ ["ايه القرف والزبالة اللي انت بتقولها دي"], # مثال هجومي
71
+ ["الستات مكانهم المطبخ وبس"] # مثال كراهية
72
+ ]
73
  )
74
 
75
  iface.launch()