Mubarak2507 commited on
Commit
2add840
·
verified ·
1 Parent(s): 1b31c0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ hate = pipeline("text-classification", model="hossam87/bert-base-arabic-hate-speech", tokenizer="hossam87/bert-base-arabic-hate-speech", return_all_scores=False)
5
+ dialect = pipeline("text-classification", model="IbrahimAmin/marbertv2-arabic-written-dialect-classifier", tokenizer="IbrahimAmin/marbertv2-arabic-written-dialect-classifier", return_all_scores=False)
6
+
7
+ def analyze(text):
8
+ # Hate speech detection
9
+ hate_res = hate(text)[0]
10
+ hate_label = hate_res['label']
11
+ hate_conf = hate_res['score']
12
+
13
+ # Dialect detection
14
+ dial_res = dialect(text)[0]
15
+ dial_label = dial_res['label']
16
+ dial_conf = dial_res['score']
17
+
18
+ # Threat score
19
+ weight = {"Neutral":0, "Offensive":0.5, "Sexism":1, "Racism":1, "Religious Discrimination":1}
20
+ score = hate_conf * weight.get(hate_label, 0)
21
+
22
+ # Recommended action (modified logic)
23
+ if hate_label != "Neutral":
24
+ action = "🚨 Immediate Review Required — This content contains severe hate speech or threats and should be escalated to moderators immediately."
25
+ elif score >= 0.49:
26
+ action = "⚠️ Potentially Harmful — The content may contain offensive or harmful language. Please review before taking further action."
27
+ else:
28
+ action = "✅ Safe Content — No harmful language detected. No moderation needed."
29
+
30
+ return hate_label, f"{hate_conf:.2f}", dial_label, f"{dial_conf:.2f}", f"{score:.2f}", action
31
+
32
+
33
+ iface = gr.Interface(
34
+ fn=analyze,
35
+ inputs=gr.Textbox(lines=4, placeholder="اكتب هنا...", label="Arabic Text"),
36
+ outputs=[
37
+ gr.Text(label="Hate Speech Label"),
38
+ gr.Text(label="Confidence"),
39
+ gr.Text(label="Dialect"),
40
+ gr.Text(label="Confidence"),
41
+ gr.Text(label="Threat Score"),
42
+ gr.Text(label="Recommended Action")
43
+ ],
44
+ title="🛡️ Arabic Content Safety Analyzer",
45
+ description="Classifies Arabic text for hate speech, detects dialect, assigns threat severity score, and recommends action.",
46
+ theme="default"
47
+ )
48
+
49
+ iface.launch()
50
+