Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the hate speech detection model from Hugging Face
|
| 5 |
+
classifier = pipeline("text-classification", model="unitary/unbiased-toxic-roberta")
|
| 6 |
+
|
| 7 |
+
def detect_hate_speech(text):
|
| 8 |
+
"""Detect hate speech in the given text."""
|
| 9 |
+
sentences = text.split(".")
|
| 10 |
+
hate_speech = []
|
| 11 |
+
|
| 12 |
+
for sentence in sentences:
|
| 13 |
+
if sentence.strip():
|
| 14 |
+
result = classifier(sentence.strip())[0]
|
| 15 |
+
if result["label"] in ["toxic", "insult", "hate"]:
|
| 16 |
+
hate_speech.append(sentence.strip())
|
| 17 |
+
|
| 18 |
+
return hate_speech if hate_speech else "No hate speech detected"
|
| 19 |
+
|
| 20 |
+
# Create Gradio interface
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=detect_hate_speech,
|
| 23 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
|
| 24 |
+
outputs="json",
|
| 25 |
+
title="Hate Speech Detection",
|
| 26 |
+
description="Enter a sentence or paragraph to check for hate speech.",
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Launch the app
|
| 30 |
+
iface.launch()
|