Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import numpy as np
|
|
3 |
import pandas as pd
|
4 |
import re
|
5 |
from tensorflow.keras.models import Sequential
|
|
|
6 |
from tensorflow.keras.layers import Dense
|
7 |
from transformers import BertTokenizer, TFBertModel
|
8 |
from sklearn.model_selection import train_test_split
|
@@ -18,9 +19,6 @@ nltk.download('stopwords')
|
|
18 |
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
19 |
bert_model = TFBertModel.from_pretrained("bert-base-uncased")
|
20 |
|
21 |
-
# Load dataset
|
22 |
-
file_path = "https://raw.githubusercontent.com/alexvatti/full-stack-data-science/main/NLP-Exercises/Movie-Review/IMDB%20Dataset.csv"
|
23 |
-
movies_df = pd.read_csv(file_path)
|
24 |
|
25 |
# Clean text
|
26 |
def remove_tags(txt):
|
@@ -33,65 +31,9 @@ def remove_stop_words(txt):
|
|
33 |
stop_words = set(stopwords.words('english'))
|
34 |
return ' '.join([word for word in txt.split() if word not in stop_words])
|
35 |
|
36 |
-
movies_df['review'] = movies_df['review'].apply(remove_tags)
|
37 |
-
movies_df['review'] = movies_df['review'].apply(remove_stop_words)
|
38 |
-
movies_df['Category'] = movies_df['sentiment'].apply(lambda x: 1 if x == 'positive' else 0)
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
# Convert labels to TensorFlow format
|
44 |
-
y_train = tf.convert_to_tensor(y_train.values, dtype=tf.float32)
|
45 |
-
y_test = tf.convert_to_tensor(y_test.values, dtype=tf.float32)
|
46 |
-
|
47 |
-
# Compute BERT embeddings
|
48 |
-
def bert_embeddings_batch(texts, batch_size=32, max_length=64):
|
49 |
-
embeddings = []
|
50 |
-
for i in range(0, len(texts), batch_size):
|
51 |
-
batch_texts = texts[i:i + batch_size]
|
52 |
-
inputs = tokenizer(
|
53 |
-
batch_texts.tolist(),
|
54 |
-
return_tensors="tf",
|
55 |
-
padding=True,
|
56 |
-
truncation=True,
|
57 |
-
max_length=max_length
|
58 |
-
)
|
59 |
-
outputs = bert_model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
|
60 |
-
cls_embeddings = outputs.last_hidden_state[:, 0, :]
|
61 |
-
embeddings.append(cls_embeddings.numpy())
|
62 |
-
return np.vstack(embeddings)
|
63 |
-
|
64 |
-
# Compute embeddings
|
65 |
-
X_train_embeddings = bert_embeddings_batch(X_train)
|
66 |
-
X_test_embeddings = bert_embeddings_batch(X_test)
|
67 |
-
|
68 |
-
# Define classifier
|
69 |
-
classifier = Sequential([
|
70 |
-
Dense(128, activation='relu', input_shape=(768,)),
|
71 |
-
Dense(1, activation='sigmoid')
|
72 |
-
])
|
73 |
-
|
74 |
-
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
75 |
-
|
76 |
-
# Train classifier
|
77 |
-
classifier.fit(X_train_embeddings, y_train, epochs=5, batch_size=32, validation_split=0.1)
|
78 |
-
|
79 |
-
# Evaluate
|
80 |
-
test_loss, test_accuracy = classifier.evaluate(X_test_embeddings, y_test)
|
81 |
-
print(f"Test Accuracy: {test_accuracy}")
|
82 |
-
|
83 |
-
# Predictions and confusion matrix
|
84 |
-
y_pred = (classifier.predict(X_test_embeddings) > 0.5).astype("int32")
|
85 |
-
conf_matrix = confusion_matrix(y_test.numpy(), y_pred)
|
86 |
-
class_report = classification_report(y_test.numpy(), y_pred)
|
87 |
-
|
88 |
-
print("Confusion Matrix:")
|
89 |
-
print(conf_matrix)
|
90 |
-
print("\nClassification Report:")
|
91 |
-
print(class_report)
|
92 |
-
|
93 |
-
# Save the trained model to a file
|
94 |
-
#classifier.save("movie_sentiment_model.h5")
|
95 |
|
96 |
# Single input BERT embeddings
|
97 |
def bert_embeddings(text, max_length=64):
|
|
|
3 |
import pandas as pd
|
4 |
import re
|
5 |
from tensorflow.keras.models import Sequential
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
from tensorflow.keras.layers import Dense
|
8 |
from transformers import BertTokenizer, TFBertModel
|
9 |
from sklearn.model_selection import train_test_split
|
|
|
19 |
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
20 |
bert_model = TFBertModel.from_pretrained("bert-base-uncased")
|
21 |
|
|
|
|
|
|
|
22 |
|
23 |
# Clean text
|
24 |
def remove_tags(txt):
|
|
|
31 |
stop_words = set(stopwords.words('english'))
|
32 |
return ' '.join([word for word in txt.split() if word not in stop_words])
|
33 |
|
|
|
|
|
|
|
34 |
|
35 |
+
# Load the trained model
|
36 |
+
classifier=load_model('movie_sentiment_model.h5')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Single input BERT embeddings
|
39 |
def bert_embeddings(text, max_length=64):
|