Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,78 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
submit_btn = st.button("Submit")
|
29 |
|
|
|
|
|
|
|
|
|
30 |
if submit_btn and text_input:
|
31 |
-
result =
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
import pandas as pd
|
5 |
+
import random
|
6 |
+
|
7 |
+
classifiers = ['toxic', 'severe_toxic', 'obscene',
|
8 |
+
'threat', 'insult', 'identity_hate']
|
9 |
+
|
10 |
+
|
11 |
+
def reset_scores():
|
12 |
+
global scores_df
|
13 |
+
scores_df = pd.DataFrame(columns=['Comment'] + classifiers)
|
14 |
+
|
15 |
+
|
16 |
+
def get_score(model_base, text):
|
17 |
+
if model_base == "bert-base-cased":
|
18 |
+
model_dir = "./bert/_bert_model"
|
19 |
+
elif model_base == "distilbert-base-cased":
|
20 |
+
model_dir = "./distilbert/_distilbert_model"
|
21 |
+
else:
|
22 |
+
model_dir = "./roberta/_roberta_model"
|
23 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_dir)
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_base)
|
25 |
+
inputs = tokenizer.encode_plus(
|
26 |
+
text, max_length=512, truncation=True, padding=True, return_tensors='pt')
|
27 |
+
outputs = model(**inputs)
|
28 |
+
predictions = torch.sigmoid(outputs.logits)
|
29 |
+
return predictions
|
30 |
+
|
31 |
+
|
32 |
+
# Ask user for input, return scores
|
33 |
+
st.title("Toxic Comment Classifier")
|
34 |
+
text_input = st.text_input("Enter text for toxicity classification",
|
35 |
+
"I hope you die")
|
36 |
submit_btn = st.button("Submit")
|
37 |
|
38 |
+
# Drop down menu for model selection, default is roberta
|
39 |
+
model_base = st.selectbox("Select a pretrained model",
|
40 |
+
["roberta-base", "bert-base-cased", "distilbert-base-cased"])
|
41 |
+
|
42 |
if submit_btn and text_input:
|
43 |
+
result = get_score(model_base, text_input)
|
44 |
+
|
45 |
+
df = pd.DataFrame([result[0].tolist()], columns=classifiers)
|
46 |
+
df = df.round(2) # Round the values to 2 decimal places
|
47 |
+
# Format the values as percentages
|
48 |
+
df = df.applymap(lambda x: '{:.0%}'.format(x))
|
49 |
+
|
50 |
+
st.table(df)
|
51 |
+
|
52 |
+
# Read the test dataset
|
53 |
+
test_df = pd.read_csv(
|
54 |
+
"./jigsaw-toxic-comment-classification-challenge/test.csv")
|
55 |
+
|
56 |
+
# Select 10 random comments from the test dataset
|
57 |
+
sample_df = test_df.sample(n=3)
|
58 |
+
|
59 |
+
# Create an empty DataFrame to store the scores
|
60 |
+
reset_scores()
|
61 |
+
|
62 |
+
# Calculate the scores for each comment and add them to the DataFrame
|
63 |
+
for index, row in sample_df.iterrows():
|
64 |
+
result = get_score(model_base, row['comment_text'])
|
65 |
+
scores = result[0].tolist()
|
66 |
+
scores_df.loc[len(scores_df)] = [row['comment_text']] + scores
|
67 |
+
|
68 |
+
# Round the values to 2 decimal places
|
69 |
+
scores_df = scores_df.round(2)
|
70 |
+
|
71 |
+
|
72 |
+
st.subheader("Toxicity Scores for Random Comments")
|
73 |
+
st.table(scores_df)
|
74 |
+
|
75 |
+
# Create a button to reset the scores
|
76 |
+
if st.button("Refresh Random Tweets"):
|
77 |
+
reset_scores()
|
78 |
+
st.success("New tweets have been loaded!")
|