Spaces:
Runtime error
Runtime error
Rzhishchev
commited on
Commit
•
5a4923b
1
Parent(s):
d6defb5
Upload toxic.py
Browse files
toxic.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
model_checkpoint = 'cointegrated/rubert-tiny-toxicity'
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)
|
8 |
+
if torch.cuda.is_available():
|
9 |
+
model.cuda()
|
10 |
+
|
11 |
+
def text2toxicity(text, aggregate=True):
|
12 |
+
""" Calculate toxicity of a text (if aggregate=True) or a vector of toxicity aspects (if aggregate=False)"""
|
13 |
+
with torch.no_grad():
|
14 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True).to(model.device)
|
15 |
+
proba = torch.sigmoid(model(**inputs).logits).cpu().numpy()
|
16 |
+
if isinstance(text, str):
|
17 |
+
proba = proba[0]
|
18 |
+
if aggregate:
|
19 |
+
return 1 - proba.T[0] * (1 - proba.T[-1])
|
20 |
+
return proba
|
21 |
+
|
22 |
+
st.title("Toxicity Detector")
|
23 |
+
|
24 |
+
user_input = st.text_area("Enter text to check for toxicity:", "Капец ты гнида")
|
25 |
+
if st.button("Analyze"):
|
26 |
+
toxicity_score = text2toxicity(user_input, True)
|
27 |
+
st.write(f"Toxicity Score: {toxicity_score:.4f}")
|
28 |
+
if toxicity_score > 0.5:
|
29 |
+
st.write("Warning: The text seems to be toxic!")
|