Spaces:
Runtime error
Runtime error
Commit
·
e8c2dc9
1
Parent(s):
5d4b507
Update toxic.py
Browse files
toxic.py
CHANGED
@@ -6,29 +6,30 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
6 |
def app():
|
7 |
st.title('Toxic Comment Classifier')
|
8 |
st.write('This is the toxic comment classifier page.')
|
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 |
-
|
|
|
|
6 |
def app():
|
7 |
st.title('Toxic Comment Classifier')
|
8 |
st.write('This is the toxic comment classifier page.')
|
9 |
+
|
10 |
+
model_checkpoint = 'cointegrated/rubert-tiny-toxicity'
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
12 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)
|
13 |
+
|
14 |
+
if torch.cuda.is_available():
|
15 |
+
model.cuda()
|
16 |
+
|
17 |
+
def text2toxicity(text, aggregate=True):
|
18 |
+
""" Calculate toxicity of a text (if aggregate=True) or a vector of toxicity aspects (if aggregate=False)"""
|
19 |
+
with torch.no_grad():
|
20 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True).to(model.device)
|
21 |
+
proba = torch.sigmoid(model(**inputs).logits).cpu().numpy()
|
22 |
+
if isinstance(text, str):
|
23 |
+
proba = proba[0]
|
24 |
+
if aggregate:
|
25 |
+
return 1 - proba.T[0] * (1 - proba.T[-1])
|
26 |
+
return proba
|
27 |
+
|
28 |
+
st.title("Toxicity Detector")
|
29 |
+
|
30 |
+
user_input = st.text_area("Enter text to check for toxicity:", "Капец ты гнида")
|
31 |
+
if st.button("Analyze"):
|
32 |
+
toxicity_score = text2toxicity(user_input, True)
|
33 |
+
st.write(f"Toxicity Score: {toxicity_score:.4f}")
|
34 |
+
if toxicity_score > 0.5:
|
35 |
+
st.write("Warning: The text seems to be toxic!")
|