File size: 936 Bytes
0aac553
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
from transformers import pipeline

# Load the hate speech detection model from Hugging Face
classifier = pipeline("text-classification", model="unitary/unbiased-toxic-roberta")

def detect_hate_speech(text):
    """Detect hate speech in the given text."""
    sentences = text.split(".")
    hate_speech = []

    for sentence in sentences:
        if sentence.strip():
            result = classifier(sentence.strip())[0]
            if result["label"] in ["toxic", "insult", "hate"]:
                hate_speech.append(sentence.strip())

    return hate_speech if hate_speech else "No hate speech detected"

# Create Gradio interface
iface = gr.Interface(
    fn=detect_hate_speech,
    inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
    outputs="json",
    title="Hate Speech Detection",
    description="Enter a sentence or paragraph to check for hate speech.",
)

# Launch the app
iface.launch()