|
import gradio as gr |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
tokenizer = AutoTokenizer.from_pretrained("Sk1306/student_chat_toxicity_classifier_model") |
|
model = AutoModelForSequenceClassification.from_pretrained("Sk1306/student_chat_toxicity_classifier_model") |
|
def predict_toxicity(text): |
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128) |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
|
|
|
|
probabilities = torch.nn.functional.softmax(logits, dim=-1) |
|
|
|
|
|
predicted_class = torch.argmax(probabilities, dim=-1).item() |
|
|
|
|
|
if predicted_class == 0: |
|
return "Non-toxic" |
|
else: |
|
return "Toxic" |
|
|
|
interface = gr.Interface( |
|
fn=predict_toxicity, |
|
inputs="text", |
|
outputs="text", |
|
title="Student Chat Toxicity Classifier", |
|
description="Enter a message", |
|
theme="dark", |
|
examples=[ |
|
"You can copy in exam to pass!", |
|
"Study well.Hardwork pays off!", |
|
"Take these drugs.It will boost your memory", |
|
], |
|
|
|
) |
|
interface.launch(inline=False) |