File size: 1,612 Bytes
5375520
 
 
2f923fa
 
 
b8ad611
5375520
b8ad611
 
 
 
 
 
2f923fa
 
 
 
 
b8ad611
2f923fa
 
 
 
 
5375520
2f923fa
 
5375520
2f923fa
5375520
2f923fa
b8ad611
2f923fa
b8ad611
2f923fa
5375520
2f923fa
5375520
2f923fa
5375520
2f923fa
 
 
 
 
 
 
 
 
 
 
5375520
2f923fa
 
5375520
 
 
 
b8ad611
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
from transformers import pipeline

# ----------------------------------
# Load model from Hugging Face Hub
# ----------------------------------
MODEL_REPO = "MakD1227/afriberta-hsd-full"

classifier = pipeline(
    "text-classification",
    model=MODEL_REPO,
    tokenizer=MODEL_REPO
)

# ----------------------------------
# Prediction function
# ----------------------------------
def predict_speech(text):
    results = classifier(text)

    label_map = {
        "LABEL_0": "Free (Neutral)",
        "LABEL_1": "Offensive",
        "LABEL_2": "Hate"
    }

    label = results[0]["label"]
    score = results[0]["score"]

    return label_map.get(label, label), f"{score * 100:.2f}%"

# ----------------------------------
# Gradio Interface
# ----------------------------------
demo = gr.Interface(
    fn=predict_speech,
    inputs=gr.Textbox(
        lines=2,
        label="Input Text",
        placeholder="Enter Amharic or Afan Oromo text..."
    ),
    outputs=[
        gr.Label(label="Classification"),
        gr.Text(label="Confidence")
    ],
    title="Amharic & Afan Oromo Hate Speech Detector",
    description="Classify text into Free, Offensive, or Hate Speech",
    article="""
    <p style='text-align:center;'>© 2025 Mequanent Degu Belete</p>
    <p style='text-align:center;'>mekuanentde@gmail.com</p>
    <p style='text-align:center;'>SNHCC, Academia Sinica, Taiwan</p>
    """,
    examples=[
        ["ኢትዮጵያ ለዘላለም ትኑር"],
        ["haatee sali shamtuu situ nuu beekaa waa ee baalee"]
    ]
)

if __name__ == "__main__":
    demo.launch()