Mubarak2507 commited on
Commit
ef4e241
·
verified ·
1 Parent(s): bd35ac7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -53
app.py CHANGED
@@ -18,61 +18,59 @@ dialect = pipeline(
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 = """
46
- /* Keep the main app and textboxes same dark grey */
47
- body, .gradio-container, .gr-blocks, .gr-panel {
48
- background-color: #1f1f1f !important;
49
- }
50
-
51
- /* Sidebar box styling */
52
- .sidebar-box {
53
- background-color: #2b2b2b !important;
54
- padding: 20px;
55
- border-radius: 10px;
56
- }
57
-
58
- /* Keep text visible */
59
- * {
60
- color: #fff !important;
61
- }
62
-
63
- /* Match textboxes with main background */
64
- textarea, input[type="text"] {
65
- background-color: #1f1f1f !important;
66
- border: 1px solid rgba(255,255,255,0.2) !important;
67
- color: #fff !important;
68
  }
69
  """
70
 
71
  # ---------- UI ----------
72
- with gr.Blocks(css=CSS, theme="default") as demo:
73
  with gr.Row(equal_height=True):
 
74
  with gr.Column(scale=1):
75
- with gr.Box(elem_classes="sidebar-box"):
76
  gr.Markdown("""
77
  ## 🛡️ Arabic Content Safety Analyzer
78
 
@@ -86,11 +84,12 @@ with gr.Blocks(css=CSS, theme="default") as demo:
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):
95
  gr.Markdown("### Enter Arabic Text for Analysis")
96
  input_text = gr.Textbox(lines=4, placeholder="اكتب هنا...", label="Arabic Text")
@@ -104,9 +103,9 @@ with gr.Blocks(css=CSS, theme="default") as demo:
104
 
105
  analyze_btn = gr.Button("Analyze", variant="primary")
106
  analyze_btn.click(
107
- analyze,
108
  inputs=input_text,
109
- outputs=[out_hate, out_hate_conf, out_dialect, out_dialect_conf, out_score, out_action]
110
  )
111
 
112
  if __name__ == "__main__":
 
18
 
19
  # ---------- Inference ----------
20
  def analyze(text: str):
21
+ try:
22
+ if not text or not text.strip():
23
+ return ("", "", "", "", "", "Please enter some Arabic text.")
24
+
25
+ h = hate(text)[0]
26
+ d = dialect(text)[0]
27
+
28
+ hate_label = h.get("label", "")
29
+ hate_conf = float(h.get("score", 0.0))
30
+ dial_label = d.get("label", "")
31
+ dial_conf = float(d.get("score", 0.0))
32
+
33
+ weights = {
34
+ "Neutral": 0.0,
35
+ "Offensive": 0.5,
36
+ "Sexism": 1.0,
37
+ "Racism": 1.0,
38
+ "Religious Discrimination": 1.0,
39
+ }
40
+ score = hate_conf * weights.get(hate_label, 0.0)
41
+
42
+ if hate_label != "Neutral" and weights.get(hate_label, 0.0) >= 1.0:
43
+ action = "🚨 Immediate Review — Severe content detected. Escalate to moderators."
44
+ elif hate_label != "Neutral":
45
+ action = "⚠️ Potentially Harmful — Contains offensive content. Please review."
46
+ elif score >= 0.49:
47
+ action = "⚠️ Borderline — Review recommended."
48
+ else:
49
+ action = "✅ Safe — No action needed."
50
+
51
+ return (hate_label, f"{hate_conf:.2f}", dial_label, f"{dial_conf:.2f}", f"{score:.2f}", action)
52
+
53
+ except Exception as e:
54
+ # keep the UI alive even if a model throws
55
+ return ("", "", "", "", "", f"Runtime error: {e}")
56
+
57
+ # ---------- CSS (sidebar only) ----------
58
  CSS = """
59
+ /* Only style the sidebar box; leave main area/theme untouched */
60
+ #sidebar-box {
61
+ background-color: #2b2b2b; /* the box color you liked */
62
+ border: 1px solid rgba(255,255,255,0.08);
63
+ border-radius: 10px;
64
+ padding: 20px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  }
66
  """
67
 
68
  # ---------- UI ----------
69
+ with gr.Blocks(css=CSS) as demo:
70
  with gr.Row(equal_height=True):
71
+ # Sidebar in its own box
72
  with gr.Column(scale=1):
73
+ with gr.Group(elem_id="sidebar-box"):
74
  gr.Markdown("""
75
  ## 🛡️ Arabic Content Safety Analyzer
76
 
 
84
  - **Recommended Action** (rule-based suggestion)
85
 
86
  **How to Use**
87
+ 1) أدخل النص العربي في الحقل.
88
+ 2) اضغط **Analyze**.
89
+ 3) راجع النتائج والإجراء المقترح.
90
  """)
91
 
92
+ # Main app (unchanged look; uses theme defaults)
93
  with gr.Column(scale=3):
94
  gr.Markdown("### Enter Arabic Text for Analysis")
95
  input_text = gr.Textbox(lines=4, placeholder="اكتب هنا...", label="Arabic Text")
 
103
 
104
  analyze_btn = gr.Button("Analyze", variant="primary")
105
  analyze_btn.click(
106
+ fn=analyze,
107
  inputs=input_text,
108
+ outputs=[out_hate, out_hate_conf, out_dialect, out_dialect_conf, out_score, out_action],
109
  )
110
 
111
  if __name__ == "__main__":