File size: 1,776 Bytes
890bd4b 378a259 890bd4b 378a259 890bd4b 378a259 |
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 50 51 52 53 54 55 56 |
import gradio as gr
import torch
from transformers import DistilBertForSequenceClassification, DistilBertTokenizer
# Load the trained model and tokenizer
model = DistilBertForSequenceClassification.from_pretrained('best_model')
tokenizer = DistilBertTokenizer.from_pretrained('best_model')
# Define the prediction function
def predict_hate_speech(text):
inputs = tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=512,
padding='max_length',
truncation=True,
return_tensors='pt'
)
input_ids = inputs['input_ids']
attention_mask = inputs['attention_mask']
with torch.no_grad():
outputs = model(input_ids, attention_mask=attention_mask)
logits = outputs.logits
probabilities = torch.nn.functional.softmax(logits, dim=-1)
prediction = torch.argmax(probabilities, dim=1).item()
labels = {0: 'Neutral', 1: 'Offensive', 2: 'Hateful'}
predicted_label = labels[prediction]
confidence_scores = {labels[i]: prob for i, prob in enumerate(probabilities[0].tolist())}
return predicted_label, confidence_scores
# Define the Gradio interface
interface = gr.Interface(
fn=predict_hate_speech,
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
outputs=[
gr.Textbox(label="Prediction"),
gr.Label(label="Confidence Scores")
],
title="Hate Speech Detection System using a Deep Active Learning Approach",
description="Enter a text to predict whether it is Neutral, Offensive, or Hateful.",
examples=[
["I love this product!"],
["You are so stupid!"],
["I hate this!"]
],
allow_flagging="manual",
flagging_dir="flagged_data"
)
# Launch the interface
interface.launch() |