Spaces:
Runtime error
Runtime error
Commit
·
eaa94d7
1
Parent(s):
c0bdf68
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
2 |
+
import torch
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
model_path = "berkaysahiin/bert-base-uncased-jigsaw-toxic-classifier"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
8 |
+
|
9 |
+
classifier = pipeline(task='text-classification', model=model, tokenizer=tokenizer, return_all_scores=True)
|
10 |
+
|
11 |
+
label_to_format = {
|
12 |
+
'toxic': 'Toxic',
|
13 |
+
'severe_toxic': 'Severe Toxic',
|
14 |
+
'obscene' : 'Obscene',
|
15 |
+
'threat' : 'Threat',
|
16 |
+
'identity_hate' : 'Identity Hate',
|
17 |
+
'insult' : 'Insult'
|
18 |
+
}
|
19 |
+
|
20 |
+
@st.cache_resource
|
21 |
+
def test_sentence(sentence: str, thresh_hold=0.2, debug=False):
|
22 |
+
results = classifier(sentence)
|
23 |
+
|
24 |
+
if debug:
|
25 |
+
st.text('Debug is enabled, threshold value will be ignored\n')
|
26 |
+
for result in results[0]:
|
27 |
+
st.text(result)
|
28 |
+
return
|
29 |
+
|
30 |
+
may_include = []
|
31 |
+
|
32 |
+
for result in results[0]:
|
33 |
+
if result['score'] > thresh_hold:
|
34 |
+
formatted_score = "{:.2f}".format(result['score'])
|
35 |
+
formatted_label = label_to_format[result['label']]
|
36 |
+
may_include.append((formatted_label, formatted_score))
|
37 |
+
|
38 |
+
if may_include:
|
39 |
+
for label, score in may_include:
|
40 |
+
capitalized_label = label.capitalize()
|
41 |
+
st.info(f'{capitalized_label} ({score})')
|
42 |
+
else:
|
43 |
+
st.info('Your sentence is totally fine')
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
st.title('Hate Speech Labeler')
|
47 |
+
user_sentence = st.text_input('Enter your sentence to test it', value='I love NLP')
|
48 |
+
user_threshold = st.slider('Select threshold value', min_value=0.0, max_value=1.0, value=0.4)
|
49 |
+
|
50 |
+
if st.button('Test your sentence'):
|
51 |
+
if user_sentence:
|
52 |
+
st.success('Testing complete!')
|
53 |
+
test_sentence(sentence=user_sentence, thresh_hold=user_threshold)
|
54 |
+
else:
|
55 |
+
st.error('Please enter a sentence before you test it!')
|