Views
No views yet
mBERT (Multilingual BERT). The model classifies text into two categories:DawitMelka/amharic-hate-speech-detection-mBERT1import requests
2
3API_URL = "https://api-inference.huggingface.co/models/DawitMelka/amharic-hate-speech-detection-mBERT"
4headers = {"Authorization": f"Bearer YOUR_HUGGINGFACE_API_TOKEN"}
5
6def query(payload):
7 response = requests.post(API_URL, headers=headers, json=payload)
8 return response.json()
9
10# Single text prediction
11result = query({"inputs": "ሰላም እንድት ነው"})
12print(result)
13
14# Batch text prediction
15batch_result = query({"inputs": ["Text 1", "Text 2", "Text 3"]})
16print(batch_result)1[
2 {"label": "free", "score": 0.999930739402771},
3 {"label": "hate", "score": 6.921886233612895e-05}
4]pip install fastapi uvicorn transformers torchmain.py:1from fastapi import FastAPI, HTTPException
2from pydantic import BaseModel
3from transformers import AutoTokenizer, AutoModelForSequenceClassification
4import torch
5
6app = FastAPI()
7
8MODEL_NAME = "DawitMelka/amharic-hate-speech-detection-mBERT"
9tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
11
12class TextRequest(BaseModel):
13 text: str
14
15@app.post("/predict/")
16async def predict(request: TextRequest):
17 text = request.text
18 if not text.strip():
19 raise HTTPException(status_code=400, detail="Text cannot be empty.")
20
21 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
22 outputs = model(**inputs)
23 logits = outputs.logits
24 prediction = torch.argmax(logits, dim=1).item()
25 labels = {0: "free", 1: "hate"}
26 return {"text": text, "prediction": labels.get(prediction, "unknown")}uvicorn main:app --reload1curl -X POST "http://127.0.0.1:8000/predict/" \
2-H "Content-Type: application/json" \
3-d '{"text": "ሰላም እንድት ነው"}'mBERT@misc{amharic-hate-speech-detection,
author = {Dawit Melka},
title = {Amharic Hate Speech Detection Model},
year = {2025},
publisher = {Hugging Face},
url = {https://huggingface.co/DawitMelka/amharic-hate-speech-detection-mBERT}
}