|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
import torch |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
MODEL_NAME = "Hate-speech-CNERG/dehatebert-mono-english" |
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
|
|
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) |
|
|
|
|
|
def detect_hate(text): |
|
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) |
|
|
outputs = model(**inputs) |
|
|
probs = torch.nn.functional.softmax(outputs.logits, dim=-1) |
|
|
labels = ["non-hate", "hate"] |
|
|
result = {labels[i]: float(probs[0][i]) for i in range(len(labels))} |
|
|
return result |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=detect_hate, |
|
|
inputs=gr.Textbox(label="Enter Text", placeholder="Type something..."), |
|
|
outputs=gr.Label(label="Prediction"), |
|
|
title="🧠 Hate Speech Detector", |
|
|
description="Classifies text as hate or non-hate using a fine-tuned BERT model.", |
|
|
) |
|
|
|
|
|
demo.launch() |