Spaces:
Sleeping
Sleeping
File size: 1,568 Bytes
f655d24 a9aeaee 3082536 db6254a a9aeaee 3082536 a9aeaee 3082536 db6254a 3082536 db6254a 3082536 11853c6 db6254a f655d24 3082536 | 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import gradio as gr
import joblib
import re
import string
# 1. Load the model and vectorizer
# Ensure 'hate_speech_model.joblib' is in the same directory
checkpoint = joblib.load('hate_speech_model.joblib')
model = checkpoint['model']
tfidf = checkpoint['tfidf']
# 2. Pre-processing function (must match the one used during training)
def clean_text(text):
text = str(text).lower()
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
text = re.sub(r'\@\w+|\#','', text)
text = text.translate(str.maketrans('', '', string.punctuation))
text = ' '.join(text.split())
return text
# 3. Prediction function
def predict(text):
if not text:
return "Please enter some text."
cleaned_text = clean_text(text)
vectorized_text = tfidf.transform([cleaned_text])
prediction = model.predict(vectorized_text)[0]
# Map numerical class to label
labels = {0: "Hate Speech", 1: "Offensive Language", 2: "Neither"}
return labels.get(prediction, "Unknown")
# 4. Build Gradio Interface
demo = gr.Interface(
fn=predict,
inputs=gr.Textbox(lines=2, placeholder="Enter text here...", label="Input Text"),
outputs=gr.Label(label="Classification Result"),
title="Hate Speech Detector",
description="This model classifies text into Hate Speech, Offensive Language, or Neither.",
examples=[
["I hope you have a wonderful day!"],
["You are so stupid and I hate you."],
["That person is a complete idiot."]
]
)
if __name__ == "__main__":
demo.launch() |