CoolFace
Apppublic

Orawan/Text_Classify

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py59 linesDownload Raw Back to root
1import streamlit as st2from transformers import pipeline3from textblob import TextBlob4from transformers import BertForSequenceClassification, AdamW, BertConfig5st.set_page_config(layout='wide', initial_sidebar_state='expanded')6col1, col2= st.columns(2)7with col2:8    text = st.text_input("Enter the text you'd like to analyze for spam.")9    aButton = st.button('Analyze') 10with col1:11    st.title("Spam Detector")12 13import torch14import numpy as np15from transformers import AutoTokenizer16tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")17from transformers import AutoModel18model = BertForSequenceClassification.from_pretrained("NimaKL/spamd_model")19token_id = []20attention_masks = []21def preprocessing(input_text, tokenizer):22    '''23                  Returns <class transformers.tokenization_utils_base.BatchEncoding> with the following fields:24                    - input_ids: list of token ids25                    - token_type_ids: list of token type ids26                    - attention_mask: list of indices (0,1) specifying which tokens should considered by the model (return_attention_mask = True).27    '''28    return tokenizer.encode_plus(29        input_text,30        add_special_tokens = True,31        max_length = 32,32        pad_to_max_length = True,33        return_attention_mask = True,34        return_tensors = 'pt'35            )36device = 'cpu'37    38def predict(new_sentence):39    # We need Token IDs and Attention Mask for inference on the new sentence40    test_ids = []41    test_attention_mask = []42    # Apply the tokenizer43    encoding = preprocessing(new_sentence, tokenizer)44    # Extract IDs and Attention Mask45    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 predictions50    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: '+ prediction54        return pred      55 56if text or aButton:57    with col2:58        with st.spinner('Wait for it...'):59            st.success(predict(text))