thongle commited on
Commit
eef9db5
·
verified ·
1 Parent(s): 09ed457

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ MODEL_NAME = "s-nlp/roberta_toxicity_classifier"
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
9
+
10
+ labels = [
11
+ "toxic",
12
+ "severe_toxic",
13
+ "obscene",
14
+ "threat",
15
+ "insult",
16
+ "identity_hate"
17
+ ]
18
+
19
+ def classify(text):
20
+ inputs = tokenizer(text, return_tensors="pt", truncation=True)
21
+ with torch.no_grad():
22
+ outputs = model(**inputs)
23
+ scores = torch.sigmoid(outputs.logits)[0].tolist()
24
+
25
+ result = {labels[i]: float(scores[i]) for i in range(len(labels))}
26
+ return result
27
+
28
+ demo = gr.Interface(
29
+ fn=classify,
30
+ inputs=gr.Textbox(label="Enter English text"),
31
+ outputs=gr.JSON(label="Toxicity scores"),
32
+ title="English Toxicity Detection",
33
+ description="Model: s-nlp/roberta_toxicity_classifier"
34
+ )
35
+
36
+ demo.launch()