Spaces:
Sleeping
Sleeping
Upload app (2).py
Browse files- app (2).py +62 -0
app (2).py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
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 |
+
with col2:
|
8 |
+
text = st.text_input("Enter the text you'd like to analyze for spam.")
|
9 |
+
aButton = st.button('Analyze')
|
10 |
+
with col1:
|
11 |
+
st.title("Spamd: Turkish Spam Detector")
|
12 |
+
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.")
|
13 |
+
st.markdown("Original file is located at")
|
14 |
+
st.markdown("https://colab.research.google.com/drive/1QuorqAuLsmomesZHsaQHEZgzbPEM8YTH")
|
15 |
+
|
16 |
+
import torch
|
17 |
+
import numpy as np
|
18 |
+
from transformers import AutoTokenizer
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")
|
20 |
+
from transformers import AutoModel
|
21 |
+
model = BertForSequenceClassification.from_pretrained("NimaKL/spamd_model")
|
22 |
+
token_id = []
|
23 |
+
attention_masks = []
|
24 |
+
def preprocessing(input_text, tokenizer):
|
25 |
+
'''
|
26 |
+
Returns <class transformers.tokenization_utils_base.BatchEncoding> with the following fields:
|
27 |
+
- input_ids: list of token ids
|
28 |
+
- token_type_ids: list of token type ids
|
29 |
+
- attention_mask: list of indices (0,1) specifying which tokens should considered by the model (return_attention_mask = True).
|
30 |
+
'''
|
31 |
+
return tokenizer.encode_plus(
|
32 |
+
input_text,
|
33 |
+
add_special_tokens = True,
|
34 |
+
max_length = 32,
|
35 |
+
pad_to_max_length = True,
|
36 |
+
return_attention_mask = True,
|
37 |
+
return_tensors = 'pt'
|
38 |
+
)
|
39 |
+
device = 'cpu'
|
40 |
+
|
41 |
+
def predict(new_sentence):
|
42 |
+
# We need Token IDs and Attention Mask for inference on the new sentence
|
43 |
+
test_ids = []
|
44 |
+
test_attention_mask = []
|
45 |
+
# Apply the tokenizer
|
46 |
+
encoding = preprocessing(new_sentence, tokenizer)
|
47 |
+
# Extract IDs and Attention Mask
|
48 |
+
test_ids.append(encoding['input_ids'])
|
49 |
+
test_attention_mask.append(encoding['attention_mask'])
|
50 |
+
test_ids = torch.cat(test_ids, dim = 0)
|
51 |
+
test_attention_mask = torch.cat(test_attention_mask, dim = 0)
|
52 |
+
# Forward pass, calculate logit predictions
|
53 |
+
with torch.no_grad():
|
54 |
+
output = model(test_ids.to(device), token_type_ids = None, attention_mask = test_attention_mask.to(device))
|
55 |
+
prediction = 'Spam' if np.argmax(output.logits.cpu().numpy()).flatten().item() == 1 else 'Normal'
|
56 |
+
pred = 'Predicted Class: '+ prediction
|
57 |
+
return pred
|
58 |
+
|
59 |
+
if text or aButton:
|
60 |
+
with col2:
|
61 |
+
with st.spinner('Wait for it...'):
|
62 |
+
st.success(predict(text))
|