adannaned's picture
Update app.py
378a259 verified
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()