Update app.py
Browse files
app.py
CHANGED
@@ -9,66 +9,55 @@ with col1:
|
|
9 |
st.title("Spamd: Turkish Spam Detector")
|
10 |
st.markdown("Message spam detection tool for Turkish language. Due the small size of the dataset, I decided to go with transformers technology Google BERT. Using the Turkish pre-trained model BERTurk, I imporved the accuracy of the tool by 18 percent compared to the previous model which used fastText.")
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
attention_masks = []
|
26 |
-
|
27 |
-
def preprocessing(input_text, tokenizer):
|
28 |
-
'''
|
29 |
Returns <class transformers.tokenization_utils_base.BatchEncoding> with the following fields:
|
30 |
- input_ids: list of token ids
|
31 |
- token_type_ids: list of token type ids
|
32 |
- attention_mask: list of indices (0,1) specifying which tokens should considered by the model (return_attention_mask = True).
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
test_ids.append(encoding['input_ids'])
|
57 |
-
test_attention_mask.append(encoding['attention_mask'])
|
58 |
-
test_ids = torch.cat(test_ids, dim = 0)
|
59 |
-
test_attention_mask = torch.cat(test_attention_mask, dim = 0)
|
60 |
-
|
61 |
-
# Forward pass, calculate logit predictions
|
62 |
with torch.no_grad():
|
63 |
output = model(test_ids.to(device), token_type_ids = None, attention_mask = test_attention_mask.to(device))
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
predict(text)
|
72 |
|
73 |
|
74 |
|
|
|
9 |
st.title("Spamd: Turkish Spam Detector")
|
10 |
st.markdown("Message spam detection tool for Turkish language. Due the small size of the dataset, I decided to go with transformers technology Google BERT. Using the Turkish pre-trained model BERTurk, I imporved the accuracy of the tool by 18 percent compared to the previous model which used fastText.")
|
11 |
|
12 |
+
if st.button('Load Model', disabled=False):
|
13 |
+
with st.spinner('Wait for it...'):
|
14 |
+
import torch
|
15 |
+
import numpy as np
|
16 |
+
from transformers import AutoTokenizer
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")
|
18 |
+
from transformers import AutoModel
|
19 |
+
model = BertForSequenceClassification.from_pretrained("NimaKL/spamd_model")
|
20 |
+
|
21 |
+
token_id = []
|
22 |
+
attention_masks = []
|
23 |
+
def preprocessing(input_text, tokenizer):
|
24 |
+
'''
|
|
|
|
|
|
|
|
|
25 |
Returns <class transformers.tokenization_utils_base.BatchEncoding> with the following fields:
|
26 |
- input_ids: list of token ids
|
27 |
- token_type_ids: list of token type ids
|
28 |
- attention_mask: list of indices (0,1) specifying which tokens should considered by the model (return_attention_mask = True).
|
29 |
+
'''
|
30 |
+
return tokenizer.encode_plus(
|
31 |
+
input_text,
|
32 |
+
add_special_tokens = True,
|
33 |
+
max_length = 32,
|
34 |
+
pad_to_max_length = True,
|
35 |
+
return_attention_mask = True,
|
36 |
+
return_tensors = 'pt'
|
37 |
+
)
|
38 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
39 |
+
st.success("Model Loaded!")
|
40 |
+
def predict(new_sentence):
|
41 |
+
# We need Token IDs and Attention Mask for inference on the new sentence
|
42 |
+
test_ids = []
|
43 |
+
test_attention_mask = []
|
44 |
+
# Apply the tokenizer
|
45 |
+
encoding = preprocessing(new_sentence, tokenizer)
|
46 |
+
# Extract IDs and Attention Mask
|
47 |
+
test_ids.append(encoding['input_ids'])
|
48 |
+
test_attention_mask.append(encoding['attention_mask'])
|
49 |
+
test_ids = torch.cat(test_ids, dim = 0)
|
50 |
+
test_attention_mask = torch.cat(test_attention_mask, dim = 0)
|
51 |
+
# Forward pass, calculate logit predictions
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
with torch.no_grad():
|
53 |
output = model(test_ids.to(device), token_type_ids = None, attention_mask = test_attention_mask.to(device))
|
54 |
+
prediction = 'Spam' if np.argmax(output.logits.cpu().numpy()).flatten().item() == 1 else 'Normal'
|
55 |
+
pred = 'Predicted Class: '+ prediction
|
56 |
+
with col2:
|
57 |
+
st.header(pred)
|
58 |
+
text = st.text_input("Enter the text you'd like to analyze for spam.")
|
59 |
+
if text or st.button('Analyze'):
|
60 |
+
predict(text)
|
|
|
61 |
|
62 |
|
63 |
|