NimaKL commited on
Commit
25cba84
β€’
1 Parent(s): 00addfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -48
app.py CHANGED
@@ -11,53 +11,53 @@ with col1:
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
 
 
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