NimaKL commited on
Commit
1309cb2
β€’
1 Parent(s): f201997

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -46
app.py CHANGED
@@ -3,68 +3,58 @@ from transformers import pipeline
3
  from textblob import TextBlob
4
  from transformers import BertForSequenceClassification, AdamW, BertConfig
5
  st.set_page_config(layout='wide', initial_sidebar_state='expanded')
6
- col1, col2= st.columns(2)
7
- placeholder = st.empty()
8
- placeholder2 = st.empty()
9
- with col2:
10
- text = placeholder.text_input("Enter the text you'd like to analyze for spam.", disabled=True, key="1")
11
- aButton = placeholder2.button('Analyze', disabled=True, key="1")
12
  with col1:
13
  st.title("Spamd: Turkish Spam Detector")
14
  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.")
15
 
16
- if st.button('Load Model', disabled=False):
17
- with st.spinner('Wait for it...'):
18
- import torch
19
- import numpy as np
20
- from transformers import AutoTokenizer
21
- tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")
22
- from transformers import AutoModel
23
- model = BertForSequenceClassification.from_pretrained("NimaKL/spamd_model")
24
-
25
- token_id = []
26
- attention_masks = []
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
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
43
- with col1:
44
- st.success("Model Loaded!")
45
- def predict(new_sentence):
46
- # We need Token IDs and Attention Mask for inference on the new sentence
47
- test_ids = []
48
- test_attention_mask = []
49
- # Apply the tokenizer
50
- encoding = preprocessing(new_sentence, tokenizer)
51
- #Extract IDs and Attention Mask
52
- test_ids.append(encoding['input_ids'])
53
- test_attention_mask.append(encoding['attention_mask'])
54
- test_ids = torch.cat(test_ids, dim = 0)
55
- test_attention_mask = torch.cat(test_attention_mask, dim = 0)
56
- #Forward pass, calculate logit predictions
57
- with torch.no_grad():
58
- output = model(test_ids.to(device), token_type_ids = None, attention_mask = test_attention_mask.to(device))
59
- prediction = 'Spam' if np.argmax(output.logits.cpu().numpy()).flatten().item() == 1 else 'Normal'
60
- pred = 'Predicted Class: '+ prediction
61
- return pred
62
- with col2:
63
- placeholder.text_input("Enter the text you'd like to analyze for spam.", disabled=False, key="2")
64
- placeholder2.button('Analyze', disabled=False, key="2")
65
  if text or aButton:
66
- placeholder.text_input("Enter the text you'd like to analyze for spam.", disabled=False, key="3")
67
- placeholder2.button('Analyze', disabled=False, key="3")
68
  with col2:
69
  st.header(predict(text))
70
 
 
3
  from textblob import TextBlob
4
  from transformers import BertForSequenceClassification, AdamW, BertConfig
5
  st.set_page_config(layout='wide', initial_sidebar_state='expanded')
6
+
 
 
 
 
 
7
  with col1:
8
  st.title("Spamd: Turkish Spam Detector")
9
  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.")
10
 
11
+
12
+ import torch
13
+ import numpy as np
14
+ from transformers import AutoTokenizer
15
+ tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")
16
+ from transformers import AutoModel
17
+ model = BertForSequenceClassification.from_pretrained("NimaKL/spamd_model")
18
+ token_id = []
19
+ attention_masks = []
20
+ def preprocessing(input_text, tokenizer):
 
 
21
  '''
22
  Returns <class transformers.tokenization_utils_base.BatchEncoding> with the following fields:
23
  - input_ids: list of token ids
24
  - token_type_ids: list of token type ids
25
  - attention_mask: list of indices (0,1) specifying which tokens should considered by the model (return_attention_mask = True).
26
  '''
27
+ return tokenizer.encode_plus(
28
+ input_text,
29
+ add_special_tokens = True,
30
  max_length = 32,
31
  pad_to_max_length = True,
32
  return_attention_mask = True,
33
  return_tensors = 'pt'
34
  )
35
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
36
+ with col1:
37
+ st.success("Model Loaded!")
38
+ def predict(new_sentence):
39
+ # We need Token IDs and Attention Mask for inference on the new sentence
40
+ test_ids = []
41
+ test_attention_mask = []
42
+ # Apply the tokenizer
43
+ encoding = preprocessing(new_sentence, tokenizer)
44
+ #Extract IDs and Attention Mask
45
+ test_ids.append(encoding['input_ids'])
46
+ test_attention_mask.append(encoding['attention_mask'])
47
+ test_ids = torch.cat(test_ids, dim = 0)
48
+ test_attention_mask = torch.cat(test_attention_mask, dim = 0)
49
+ #Forward pass, calculate logit predictions
50
+ with torch.no_grad():
51
+ output = model(test_ids.to(device), token_type_ids = None, attention_mask = test_attention_mask.to(device))
52
+ prediction = 'Spam' if np.argmax(output.logits.cpu().numpy()).flatten().item() == 1 else 'Normal'
53
+ pred = 'Predicted Class: '+ prediction
54
+ return pred
 
 
 
55
  if text or aButton:
56
+ st.text_input("Enter the text you'd like to analyze for spam.")
57
+ st.button('Analyze')
58
  with col2:
59
  st.header(predict(text))
60