Spaces:
Sleeping
Sleeping
File size: 1,825 Bytes
107a7e6 0c3ac93 107a7e6 0c3ac93 107a7e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import string
from transformers import BertTokenizer, TFBertForSequenceClassification
import tensorflow as tf
# Download NLTK resources (one-time step)
nltk.download('punkt')
nltk.download('stopwords')
# Define stopwords and punctuation
stop_words = set(stopwords.words('english'))
punctuations = set(string.punctuation)
# Function to preprocess text
def preprocess_text(text):
text = str(text)
# Lowercase the text
text = text.lower()
# Tokenize the text
tokens = word_tokenize(text)
# Remove stopwords and punctuation
tokens = [token for token in tokens if token not in stop_words and token not in punctuations]
# Reconstruct the text
preprocessed_text = ' '.join(tokens)
return preprocessed_text
bert_tokenizer = BertTokenizer.from_pretrained('../Tokenizer')
# Load model
bert_model = TFBertForSequenceClassification.from_pretrained('../Model')
label = {
1: 'positive',
0: 'Negative'
}
def Get_sentiment(Review, Tokenizer=bert_tokenizer, Model=bert_model):
# Convert Review to a list if it's not already a list
if not isinstance(Review, list):
Review = [Review]
Input_ids, Token_type_ids, Attention_mask = Tokenizer.batch_encode_plus(Review,
padding=True,
truncation=True,
max_length=128,
return_tensors='tf').values()
prediction = Model.predict([Input_ids, Token_type_ids, Attention_mask])
# Use argmax along the appropriate axis to get the predicted labels
pred_labels = tf.argmax(prediction.logits, axis=1)
# Convert the TensorFlow tensor to a NumPy array and then to a list to get the predicted sentiment labels
pred_labels = [label[i] for i in pred_labels.numpy().tolist()]
return pred_labels |