Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Load pretrained hate-speech model
|
| 6 |
+
MODEL_NAME = "Hate-speech-CNERG/dehatebert-mono-english"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 9 |
+
|
| 10 |
+
def detect_hate(text):
|
| 11 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 12 |
+
outputs = model(**inputs)
|
| 13 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 14 |
+
labels = ["non-hate", "hate"]
|
| 15 |
+
result = {labels[i]: float(probs[0][i]) for i in range(len(labels))}
|
| 16 |
+
return result
|
| 17 |
+
|
| 18 |
+
# Gradio interface
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=detect_hate,
|
| 21 |
+
inputs=gr.Textbox(label="Enter Text", placeholder="Type something..."),
|
| 22 |
+
outputs=gr.Label(label="Prediction"),
|
| 23 |
+
title="🧠 Hate Speech Detector",
|
| 24 |
+
description="Classifies text as hate or non-hate using a fine-tuned BERT model.",
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
demo.launch()
|