Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
elif score >= 0.49:
|
26 |
-
action = "β οΈ
|
27 |
else:
|
28 |
-
action = "β
Safe
|
29 |
|
30 |
-
return hate_label, f"{hate_conf:.2f}", dial_label, f"{dial_conf:.2f}", f"{score:.2f}", action
|
31 |
|
32 |
# ---------- CSS ----------
|
33 |
CSS = """
|
@@ -73,10 +85,10 @@ with gr.Blocks(css=CSS, theme="default") as demo:
|
|
73 |
- **Threat Severity Score** (0β1 based on label + confidence)
|
74 |
- **Recommended Action** (rule-based suggestion)
|
75 |
|
76 |
-
**How to Use
|
77 |
-
1
|
78 |
-
2
|
79 |
-
3
|
80 |
""")
|
81 |
|
82 |
with gr.Column(scale=3):
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# ---------- Pipelines ----------
|
5 |
+
hate = pipeline(
|
6 |
+
"text-classification",
|
7 |
+
model="hossam87/bert-base-arabic-hate-speech",
|
8 |
+
tokenizer="hossam87/bert-base-arabic-hate-speech",
|
9 |
+
return_all_scores=False
|
10 |
+
)
|
11 |
+
|
12 |
+
dialect = pipeline(
|
13 |
+
"text-classification",
|
14 |
+
model="IbrahimAmin/marbertv2-arabic-written-dialect-classifier",
|
15 |
+
tokenizer="IbrahimAmin/marbertv2-arabic-written-dialect-classifier",
|
16 |
+
return_all_scores=False
|
17 |
+
)
|
18 |
+
|
19 |
+
# ---------- Inference ----------
|
20 |
+
def analyze(text: str):
|
21 |
+
if not text or not text.strip():
|
22 |
+
return ("", "", "", "", "", "Please enter some Arabic text.")
|
23 |
+
|
24 |
+
h = hate(text)[0]
|
25 |
+
d = dialect(text)[0]
|
26 |
+
|
27 |
+
hate_label, hate_conf = h.get("label",""), float(h.get("score",0))
|
28 |
+
dial_label, dial_conf = d.get("label",""), float(d.get("score",0))
|
29 |
+
|
30 |
+
weights = {"Neutral":0.0, "Offensive":0.5, "Sexism":1.0, "Racism":1.0, "Religious Discrimination":1.0}
|
31 |
+
score = hate_conf * weights.get(hate_label, 0.0)
|
32 |
+
|
33 |
+
if hate_label != "Neutral" and weights.get(hate_label,0.0) >= 1.0:
|
34 |
+
action = "π¨ Immediate Review β Severe content detected. Escalate to moderators."
|
35 |
+
elif hate_label != "Neutral":
|
36 |
+
action = "β οΈ Potentially Harmful β Contains offensive content. Please review."
|
37 |
elif score >= 0.49:
|
38 |
+
action = "β οΈ Borderline β Review recommended."
|
39 |
else:
|
40 |
+
action = "β
Safe β No action needed."
|
41 |
|
42 |
+
return (hate_label, f"{hate_conf:.2f}", dial_label, f"{dial_conf:.2f}", f"{score:.2f}", action)
|
43 |
|
44 |
# ---------- CSS ----------
|
45 |
CSS = """
|
|
|
85 |
- **Threat Severity Score** (0β1 based on label + confidence)
|
86 |
- **Recommended Action** (rule-based suggestion)
|
87 |
|
88 |
+
**How to Use**
|
89 |
+
1) Enter Arabic text in the box.
|
90 |
+
2) Click **Analyze**.
|
91 |
+
3) Review the results and suggested action.
|
92 |
""")
|
93 |
|
94 |
with gr.Column(scale=3):
|