Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 1. Load the classifier from your Hugging Face Repo
|
| 6 |
+
# This replaces the /content/drive path
|
| 7 |
+
model_repo = "MakD1227/afriberta-hsd-model"
|
| 8 |
+
classifier = pipeline("text-classification", model=model_repo)
|
| 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 |
+
label = results[0]['label']
|
| 17 |
+
score = results[0]['score']
|
| 18 |
+
|
| 19 |
+
return label_map.get(label, label), f"{round(score * 100, 2)}%"
|
| 20 |
+
|
| 21 |
+
# 3. Gradio Interface
|
| 22 |
+
interface = gr.Interface(
|
| 23 |
+
fn=predict_speech,
|
| 24 |
+
inputs=gr.Textbox(
|
| 25 |
+
lines=2,
|
| 26 |
+
label="Input Text",
|
| 27 |
+
placeholder="Enter Amharic or Afan Oromo text..."
|
| 28 |
+
),
|
| 29 |
+
outputs=[
|
| 30 |
+
gr.Label(label="Classification"),
|
| 31 |
+
gr.Text(label="Confidence")
|
| 32 |
+
],
|
| 33 |
+
title="Amharic & Afan Oromo Hate Speech Detector",
|
| 34 |
+
description="Classify text into Free, Offensive, or Hate Speech",
|
| 35 |
+
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>",
|
| 36 |
+
examples=[
|
| 37 |
+
["ኢትዮጵያ ለዘላለም ትኑር"],
|
| 38 |
+
["haatee sali shamtuu situ nuu beekaa waa ee baalee"]
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch (No 'share=True' needed on Hugging Face Spaces)
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
interface.launch()
|