Upload 2 files
Browse files- app.py +7 -7
- dsbert_toxic_balanced.pt +3 -0
app.py
CHANGED
@@ -13,7 +13,7 @@ comment_input = []
|
|
13 |
comment_input.append(comment)
|
14 |
test_df = pd.DataFrame()
|
15 |
test_df['comment_text'] = comment_input
|
16 |
-
cols = {'toxic':[0], 'severe_toxic':[0], 'obscene':[0], 'threat':[0], 'insult':[0], 'identity_hate':[0]}
|
17 |
for key in cols.keys():
|
18 |
test_df[key] = cols[key]
|
19 |
test_df = test_df.reset_index()
|
@@ -90,7 +90,7 @@ Test_data = Toxic_Dataset(X_test, Y_test)
|
|
90 |
Test_Loader = DataLoader(Test_data, shuffle=False)
|
91 |
|
92 |
# Loading pre-trained weights of DistilBert model for sequence classification
|
93 |
-
# and changing classifiers output to
|
94 |
# DistilBERT
|
95 |
|
96 |
from transformers import DistilBertForSequenceClassification
|
@@ -98,7 +98,7 @@ from transformers import DistilBertForSequenceClassification
|
|
98 |
Distil_bert = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased")
|
99 |
|
100 |
Distil_bert.classifier = nn.Sequential(
|
101 |
-
nn.Linear(768,
|
102 |
nn.Sigmoid()
|
103 |
)
|
104 |
# print(Distil_bert)
|
@@ -106,7 +106,7 @@ Distil_bert.classifier = nn.Sequential(
|
|
106 |
# Instantiating the model and loading the weights
|
107 |
model = Distil_bert
|
108 |
model.to('cpu')
|
109 |
-
model = torch.load('
|
110 |
|
111 |
# Making Predictions
|
112 |
for comments, labels in Test_Loader:
|
@@ -119,7 +119,7 @@ for comments, labels in Test_Loader:
|
|
119 |
op = output.logits
|
120 |
|
121 |
res = []
|
122 |
-
for i in range(
|
123 |
res.append(op[0, i])
|
124 |
# print(res)
|
125 |
|
@@ -128,10 +128,10 @@ preds = []
|
|
128 |
for i in range(len(res)):
|
129 |
preds.append(res[i].tolist())
|
130 |
|
131 |
-
classes = ['Toxic', 'Severe Toxic', 'Obscene', 'Threat', 'Insult', 'Identity Hate']
|
132 |
|
133 |
if st.button('Classify'):
|
134 |
for i in range(len(res)):
|
135 |
st.write(f"{classes[i]} : {round(preds[i], 2)}\n")
|
136 |
st.success('These are the outputs')
|
137 |
-
|
|
|
13 |
comment_input.append(comment)
|
14 |
test_df = pd.DataFrame()
|
15 |
test_df['comment_text'] = comment_input
|
16 |
+
cols = {'toxic':[0], 'severe_toxic':[0], 'obscene':[0], 'threat':[0], 'insult':[0], 'identity_hate':[0], 'non_toxic': [0]}
|
17 |
for key in cols.keys():
|
18 |
test_df[key] = cols[key]
|
19 |
test_df = test_df.reset_index()
|
|
|
90 |
Test_Loader = DataLoader(Test_data, shuffle=False)
|
91 |
|
92 |
# Loading pre-trained weights of DistilBert model for sequence classification
|
93 |
+
# and changing classifiers output to 7 because we have 7 labels to classify.
|
94 |
# DistilBERT
|
95 |
|
96 |
from transformers import DistilBertForSequenceClassification
|
|
|
98 |
Distil_bert = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased")
|
99 |
|
100 |
Distil_bert.classifier = nn.Sequential(
|
101 |
+
nn.Linear(768,7),
|
102 |
nn.Sigmoid()
|
103 |
)
|
104 |
# print(Distil_bert)
|
|
|
106 |
# Instantiating the model and loading the weights
|
107 |
model = Distil_bert
|
108 |
model.to('cpu')
|
109 |
+
model = torch.load('dsbert_toxic_balanced.pt', map_location=torch.device('cpu'))
|
110 |
|
111 |
# Making Predictions
|
112 |
for comments, labels in Test_Loader:
|
|
|
119 |
op = output.logits
|
120 |
|
121 |
res = []
|
122 |
+
for i in range(7):
|
123 |
res.append(op[0, i])
|
124 |
# print(res)
|
125 |
|
|
|
128 |
for i in range(len(res)):
|
129 |
preds.append(res[i].tolist())
|
130 |
|
131 |
+
classes = ['Toxic', 'Severe Toxic', 'Obscene', 'Threat', 'Insult', 'Identity Hate', 'Non Toxic']
|
132 |
|
133 |
if st.button('Classify'):
|
134 |
for i in range(len(res)):
|
135 |
st.write(f"{classes[i]} : {round(preds[i], 2)}\n")
|
136 |
st.success('These are the outputs')
|
137 |
+
|
dsbert_toxic_balanced.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a3bb55eaba141c4c5582838e502074b3c9bcff689321d85b3a3eff211b274c93
|
3 |
+
size 267889455
|