Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# 2. Prediction function
|
| 11 |
-
def predict_speech(text):
|
|
|
|
|
|
|
|
|
|
| 12 |
results = classifier(text)
|
| 13 |
-
# Mapping: LABEL_0 -> Free, LABEL_1 -> Offensive, LABEL_2 -> Hate
|
| 14 |
-
label_map = {"LABEL_0": "Free (Neutral)", "LABEL_1": "Offensive", "LABEL_2": "Hate"}
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
return label_map.get(label, label), f"{
|
| 20 |
|
| 21 |
# 3. Gradio Interface
|
| 22 |
interface = gr.Interface(
|
|
@@ -32,13 +53,17 @@ interface = gr.Interface(
|
|
| 32 |
],
|
| 33 |
title="Amharic & Afan Oromo Hate Speech Detector",
|
| 34 |
description="Classify text into Free, Offensive, or Hate Speech",
|
| 35 |
-
article="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
examples=[
|
| 37 |
["ኢትዮጵያ ለዘላለም ትኑር"],
|
| 38 |
["haatee sali shamtuu situ nuu beekaa waa ee baalee"]
|
| 39 |
]
|
| 40 |
)
|
| 41 |
|
| 42 |
-
# Launch
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
+
import sys
|
| 4 |
+
import subprocess
|
| 5 |
|
| 6 |
+
# ---- SAFETY: ensure transformers is installed ----
|
| 7 |
+
try:
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
except ModuleNotFoundError:
|
| 10 |
+
subprocess.check_call(
|
| 11 |
+
[sys.executable, "-m", "pip", "install", "transformers", "torch", "sentencepiece"]
|
| 12 |
+
)
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
|
| 15 |
+
# 1. Load the classifier from Hugging Face Hub
|
| 16 |
+
MODEL_REPO = "MakD1227/afriberta-hsd-model"
|
| 17 |
+
|
| 18 |
+
classifier = pipeline(
|
| 19 |
+
task="text-classification",
|
| 20 |
+
model=MODEL_REPO,
|
| 21 |
+
tokenizer=MODEL_REPO
|
| 22 |
+
)
|
| 23 |
|
| 24 |
# 2. Prediction function
|
| 25 |
+
def predict_speech(text):
|
| 26 |
+
if not text or not text.strip():
|
| 27 |
+
return "Free (Neutral)", "0.00%"
|
| 28 |
+
|
| 29 |
results = classifier(text)
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
label_map = {
|
| 32 |
+
"LABEL_0": "Free (Neutral)",
|
| 33 |
+
"LABEL_1": "Offensive",
|
| 34 |
+
"LABEL_2": "Hate"
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
label = results[0]["label"]
|
| 38 |
+
score = results[0]["score"]
|
| 39 |
|
| 40 |
+
return label_map.get(label, label), f"{score * 100:.2f}%"
|
| 41 |
|
| 42 |
# 3. Gradio Interface
|
| 43 |
interface = gr.Interface(
|
|
|
|
| 53 |
],
|
| 54 |
title="Amharic & Afan Oromo Hate Speech Detector",
|
| 55 |
description="Classify text into Free, Offensive, or Hate Speech",
|
| 56 |
+
article="""
|
| 57 |
+
<p style='text-align:center;'>© 2025 Mequanent Degu Belete</p>
|
| 58 |
+
<p style='text-align:center;'>mekuanentde@gmail.com</p>
|
| 59 |
+
<p style='text-align:center;'>SNHCC, Academia Sinica, Taiwan</p>
|
| 60 |
+
""",
|
| 61 |
examples=[
|
| 62 |
["ኢትዮጵያ ለዘላለም ትኑር"],
|
| 63 |
["haatee sali shamtuu situ nuu beekaa waa ee baalee"]
|
| 64 |
]
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# 4. Launch
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
interface.launch()
|