Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("Sk1306/student_chat_toxicity_classifier_model")
|
5 |
+
model = AutoModelForSequenceClassification.from_pretrained("Sk1306/student_chat_toxicity_classifier_model")
|
6 |
+
def predict_toxicity(text):
|
7 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
8 |
+
outputs = model(**inputs)
|
9 |
+
logits = outputs.logits
|
10 |
+
|
11 |
+
# Apply softmax to get probabilities
|
12 |
+
probabilities = torch.nn.functional.softmax(logits, dim=-1)
|
13 |
+
|
14 |
+
# Get the predicted class (index 0 for non-toxic, index 1 for toxic)
|
15 |
+
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
16 |
+
|
17 |
+
# Map the prediction to the label (0 = Non-toxic, 1 = Toxic)
|
18 |
+
if predicted_class == 0:
|
19 |
+
return "Non-toxic"
|
20 |
+
else:
|
21 |
+
return "Toxic"
|
22 |
+
|
23 |
+
interface = gr.Interface(
|
24 |
+
fn=predict_toxicity,
|
25 |
+
inputs="text", # Text input from the user
|
26 |
+
outputs="text", # Text output for the prediction
|
27 |
+
title="Student Chat Toxicity Classifier",
|
28 |
+
description="Enter a message",
|
29 |
+
theme="dark",
|
30 |
+
examples=[
|
31 |
+
"You can copy in exam to pass!",
|
32 |
+
"Study well.Hardwork pays off!",
|
33 |
+
"Take these drugs.It will boost your memory",
|
34 |
+
],
|
35 |
+
|
36 |
+
)
|
37 |
+
interface.launch(inline=False)
|