NimaKL commited on
Commit
799ec3f
Β·
1 Parent(s): dc256c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -37,27 +37,27 @@ if st.button('Load Model', disabled=False):
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
 
 
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