NimaKL commited on
Commit
00addfe
β€’
1 Parent(s): 2ea92ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -54
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
- if st.button('Load Model', disabled=False):
14
- with st.spinner('Wait for it...'):
15
-
16
- import torch
17
- import numpy as np
18
-
19
- from transformers import AutoTokenizer
20
- tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")
21
- from transformers import AutoModel
22
- model = BertForSequenceClassification.from_pretrained("NimaKL/spamd_model")
23
-
24
- token_id = []
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
- return tokenizer.encode_plus(
35
- input_text,
36
- add_special_tokens = True,
37
- max_length = 32,
38
- pad_to_max_length = True,
39
- return_attention_mask = True,
40
- return_tensors = 'pt'
41
- )
42
-
43
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
44
- #Used for printing the name if the variables. Removing it will not intrupt the project.
45
-
46
- st.success("Model Loaded!")
47
- def predict(new_sentence):
48
- # We need Token IDs and Attention Mask for inference on the new sentence
49
- test_ids = []
50
- test_attention_mask = []
51
-
52
- # Apply the tokenizer
53
- encoding = preprocessing(new_sentence, tokenizer)
54
-
55
- # Extract IDs and Attention Mask
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
- prediction = 'Spam' if np.argmax(output.logits.cpu().numpy()).flatten().item() == 1 else 'Normal'
66
- pred = 'Predicted Class: '+ prediction
67
- with col2:
68
- st.header(pred)
69
- text = st.text_input("Enter the text you'd like to analyze for spam.")
70
- if text or st.button('Analyze'):
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