Spaces:
Sleeping
Sleeping
Update process.py
Browse files- process.py +10 -21
process.py
CHANGED
|
@@ -2,7 +2,7 @@ import nltk
|
|
| 2 |
from nltk.tokenize import word_tokenize
|
| 3 |
from nltk.corpus import stopwords
|
| 4 |
import string
|
| 5 |
-
from transformers import BertTokenizer, TFBertForSequenceClassification
|
| 6 |
import tensorflow as tf
|
| 7 |
|
| 8 |
# Download NLTK resources (one-time step)
|
|
@@ -26,30 +26,19 @@ def preprocess_text(text):
|
|
| 26 |
preprocessed_text = ' '.join(tokens)
|
| 27 |
return preprocessed_text
|
| 28 |
|
| 29 |
-
bert_tokenizer = BertTokenizer.from_pretrained('
|
| 30 |
|
| 31 |
# Load model
|
| 32 |
-
bert_model = TFBertForSequenceClassification.from_pretrained('
|
| 33 |
-
|
| 34 |
-
1: 'positive',
|
| 35 |
-
0: 'Negative'
|
| 36 |
-
}
|
| 37 |
|
| 38 |
def Get_sentiment(Review, Tokenizer=bert_tokenizer, Model=bert_model):
|
| 39 |
# Convert Review to a list if it's not already a list
|
| 40 |
if not isinstance(Review, list):
|
| 41 |
Review = [Review]
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
prediction = Model.predict([Input_ids, Token_type_ids, Attention_mask])
|
| 49 |
-
|
| 50 |
-
# Use argmax along the appropriate axis to get the predicted labels
|
| 51 |
-
pred_labels = tf.argmax(prediction.logits, axis=1)
|
| 52 |
-
|
| 53 |
-
# Convert the TensorFlow tensor to a NumPy array and then to a list to get the predicted sentiment labels
|
| 54 |
-
pred_labels = [label[i] for i in pred_labels.numpy().tolist()]
|
| 55 |
-
return pred_labels
|
|
|
|
| 2 |
from nltk.tokenize import word_tokenize
|
| 3 |
from nltk.corpus import stopwords
|
| 4 |
import string
|
| 5 |
+
from transformers import BertTokenizer, TFBertForSequenceClassification, TextClassificationPipeline
|
| 6 |
import tensorflow as tf
|
| 7 |
|
| 8 |
# Download NLTK resources (one-time step)
|
|
|
|
| 26 |
preprocessed_text = ' '.join(tokens)
|
| 27 |
return preprocessed_text
|
| 28 |
|
| 29 |
+
bert_tokenizer = BertTokenizer.from_pretrained('mainakhf/bert-base-uncased-sentiment-analysis')
|
| 30 |
|
| 31 |
# Load model
|
| 32 |
+
bert_model = TFBertForSequenceClassification.from_pretrained('mainakhf/bert-base-uncased-sentiment-analysis')
|
| 33 |
+
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def Get_sentiment(Review, Tokenizer=bert_tokenizer, Model=bert_model):
|
| 36 |
# Convert Review to a list if it's not already a list
|
| 37 |
if not isinstance(Review, list):
|
| 38 |
Review = [Review]
|
| 39 |
+
model = bert_model
|
| 40 |
+
model.config.id2label = {0: "Negative", 1: "Positive"}
|
| 41 |
+
tokenizer = bert_tokenizer
|
| 42 |
+
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer)
|
| 43 |
+
pred_labels=pipe(Review)
|
| 44 |
+
return [pred_labels[0]['label']]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|