-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrompt Injection Py Script.py
More file actions
570 lines (474 loc) · 19.8 KB
/
Prompt Injection Py Script.py
File metadata and controls
570 lines (474 loc) · 19.8 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
from google.colab import drive
drive.mount('/content/drive/')
import pandas as pd
import numpy as np
df_train = pd.read_csv('/content/drive/MyDrive/LLM_Injcetion/train.csv')
df_train.head()
df_test = pd.read_csv('/content/drive/MyDrive/LLM_Injcetion/test.csv')
df_test.head()
df = pd.concat([df_train, df_test], ignore_index=True)
#df.to_csv('/content/drive/MyDrive/LLM_Injcetion/combined.csv', index=False)
df.head()
import re
def remove_between_square_brackets(text):
return re.sub(r'\[[^]]*\]', '', text)
def denoise_text(text):
text = remove_between_square_brackets(text)
return text
def remove_special_characters(text, remove_digits=True):
pattern = r'[^a-zA-Z0-9\s]' if remove_digits else r'[^a-zA-Z\s]'
text = re.sub(pattern, '', text)
return text
def preprocess_text(text):
text = denoise_text(text)
text = remove_special_characters(text)
return text
df['preprocessed_text'] = df['text'].apply(preprocess_text)
df.head()
import matplotlib.pyplot as plt
label_counts = df['label'].value_counts()
plt.figure(figsize=(8, 6))
ax = label_counts.plot(kind='bar')
plt.title('Distribution of Labels')
plt.xlabel('Label')
plt.ylabel('Count')
plt.xticks(rotation=0)
for i, count in enumerate(label_counts):
ax.text(i, count + 0.5, str(count), ha='center', va='bottom')
from sklearn.model_selection import train_test_split
X = df['preprocessed_text'].values
y = df['label'].values
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.2, random_state=42)
from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer()
vec.fit(X_train)
x_train=vec.transform(X_train)
x_test=vec.transform(X_test)
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
import xgboost as xgb
models = {
"Logistic Regression": LogisticRegression(),
"Multinomial Naive Bayes": MultinomialNB(),
"Support Vector Machine": SVC(),
"Random Forest": RandomForestClassifier(),
"AdaBoost": AdaBoostClassifier(),
"XGBoost": xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss'),
"K-Nearest Neighbors": KNeighborsClassifier(),
"Decision Tree": DecisionTreeClassifier(),
"Gradient Boosting": GradientBoostingClassifier()
}
# Initialize a list to store results
results = []
# Evaluate each model
for model_name, model in models.items():
model.fit(x_train, Y_train)
Y_pred = model.predict(x_test)
accuracy = accuracy_score(Y_test, Y_pred)
precision = precision_score(Y_test, Y_pred, average='binary')
recall = recall_score(Y_test, Y_pred, average='binary')
f1 = f1_score(Y_test, Y_pred, average='binary')
results.append({
"Model": model_name,
"Accuracy": accuracy,
"Precision": precision,
"Recall": recall,
"F1 Score": f1
})
# Convert results to DataFrame
results_df = pd.DataFrame(results)
# Save results to a CSV file
#results_df.to_csv('/content/drive/MyDrive/LLM_Injcetion/model_results.csv', index=False)
# Display the results DataFrame
print(results_df)
from gensim.models import Word2Vec
import nltk
nltk.download('punkt')
# Tokenize the text data
tokenized_text = [nltk.word_tokenize(text) for text in X_train]
# Train Word2Vec model
word2vec_model = Word2Vec(tokenized_text, vector_size=100, window=5, min_count=1, workers=4)
# Save Word2Vec model
word2vec_model.save('/content/drive/MyDrive/LLM_Injcetion/word2vec.model')
tokenized_text_train = [nltk.word_tokenize(text) for text in X_train]
tokenized_text_test = [nltk.word_tokenize(text) for text in X_test]
def text_to_word_embeddings(tokenized_text):
embeddings = [word2vec_model.wv[word] for word in tokenized_text if word in word2vec_model.wv]
if embeddings:
return np.mean(embeddings, axis=0)
else:
return np.zeros(word2vec_model.vector_size)
x_train = np.array([text_to_word_embeddings(text) for text in tokenized_text_train])
x_test = np.array([text_to_word_embeddings(text) for text in tokenized_text_test])
# Define models to evaluate
models = {
"Logistic Regression": LogisticRegression(),
"Support Vector Machine": SVC(),
"Random Forest": RandomForestClassifier(),
"AdaBoost": AdaBoostClassifier(),
"XGBoost": xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss'),
"K-Nearest Neighbors": KNeighborsClassifier(),
"Decision Tree": DecisionTreeClassifier(),
"Gradient Boosting": GradientBoostingClassifier()
}
# Initialize a list to store results
results = []
# Evaluate each model
for model_name, model in models.items():
model.fit(x_train, Y_train)
Y_pred = model.predict(x_test)
accuracy = accuracy_score(Y_test, Y_pred)
precision = precision_score(Y_test, Y_pred, average='binary')
recall = recall_score(Y_test, Y_pred, average='binary')
f1 = f1_score(Y_test, Y_pred, average='binary')
results.append({
"Model": model_name,
"Accuracy": accuracy,
"Precision": precision,
"Recall": recall,
"F1 Score": f1
})
# Convert results to DataFrame
results_df = pd.DataFrame(results)
# Save results to a CSV file
results_df.to_csv('/content/drive/MyDrive/LLM_Injcetion/word2vec_model_results.csv', index=False)
# Display the results DataFrame
print(results_df)
df.head()
from sklearn.feature_extraction.text import CountVectorizer
# Extract features and labels
X = df['preprocessed_text'].values
Y = df['label'].values
# Split the dataset into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# Transform the text data using CountVectorizer (Bag of Words)
vectorizer = CountVectorizer()
x_train = vectorizer.fit_transform(X_train)
x_test = vectorizer.transform(X_test)
# Define models to evaluate
models = {
"Logistic Regression": LogisticRegression(),
"Multinomial Naive Bayes": MultinomialNB(),
"Support Vector Machine": SVC(),
"Random Forest": RandomForestClassifier(),
"AdaBoost": AdaBoostClassifier(),
"XGBoost": xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss'),
"K-Nearest Neighbors": KNeighborsClassifier(),
"Decision Tree": DecisionTreeClassifier(),
"Gradient Boosting": GradientBoostingClassifier()
}
# Initialize a list to store results
results = []
# Evaluate each model
for model_name, model in models.items():
model.fit(x_train, Y_train)
Y_pred = model.predict(x_test)
accuracy = accuracy_score(Y_test, Y_pred)
precision = precision_score(Y_test, Y_pred, average='binary')
recall = recall_score(Y_test, Y_pred, average='binary')
f1 = f1_score(Y_test, Y_pred, average='binary')
results.append({
"Model": model_name,
"Accuracy": accuracy,
"Precision": precision,
"Recall": recall,
"F1 Score": f1
})
# Convert results to DataFrame
results_df = pd.DataFrame(results)
# Save results to a CSV file
results_df.to_csv('/content/drive/MyDrive/LLM_Injcetion/bow_model_results.csv', index=False)
# Display the results DataFrame
print(results_df)
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Dense, Dropout, Conv1D, MaxPooling1D, GlobalMaxPooling1D
from tensorflow.keras.optimizers import Adam
X = df['preprocessed_text'].values
Y = df['label'].values
# Split the dataset into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# Tokenize the text data and create sequences
tokenizer = Tokenizer(num_words=10000)
tokenizer.fit_on_texts(X_train)
x_train = tokenizer.texts_to_sequences(X_train)
x_test = tokenizer.texts_to_sequences(X_test)
# Pad the sequences
vocab = len(tokenizer.word_index) + 1
maxlen = 100
x_train = pad_sequences(x_train, padding='post', maxlen=maxlen)
x_test = pad_sequences(x_test, padding='post', maxlen=maxlen)
# Build a more complex model
emb_dim = 100
model = Sequential()
model.add(Embedding(input_dim=vocab, output_dim=emb_dim, input_length=maxlen))
model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(x_train, Y_train, epochs=10, verbose=1, validation_split=0.2, batch_size=64)
# Evaluate the model
loss, accuracy = model.evaluate(x_test, Y_test, verbose=1)
print(f'Test Accuracy: {accuracy}')
# Plot the training and validation accuracy and loss
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Dense, Dropout, Conv1D, MaxPooling1D, GlobalMaxPooling1D
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
import nltk
import tensorflow as tf
from transformers import BertTokenizer, TFBertForSequenceClassification
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv1D, MaxPooling1D, GlobalMaxPooling1D
from sklearn.model_selection import train_test_split
from transformers import BertTokenizer, TFBertModel
X = df['preprocessed_text'].values
Y = df['label'].values
# Split the dataset into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# Load BERT tokenizer and BERT model for embeddings
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
bert_model = TFBertModel.from_pretrained('bert-base-uncased')
# Tokenize and pad sequences for BERT
def tokenize_bert(texts, max_len=100):
input_ids, attention_masks = [], []
for text in texts:
encoded = bert_tokenizer.encode_plus(text, add_special_tokens=True, max_length=max_len, pad_to_max_length=True, return_attention_mask=True)
input_ids.append(encoded['input_ids'])
attention_masks.append(encoded['attention_mask'])
return np.array(input_ids), np.array(attention_masks)
x_train_ids, x_train_masks = tokenize_bert(X_train, max_len=100)
x_test_ids, x_test_masks = tokenize_bert(X_test, max_len=100)
# Create BERT embeddings
def create_bert_embeddings(input_ids, attention_masks):
embeddings = bert_model(input_ids, attention_mask=attention_masks)[0]
return embeddings
train_embeddings = create_bert_embeddings(x_train_ids, x_train_masks)
test_embeddings = create_bert_embeddings(x_test_ids, x_test_masks)
# Build a more complex model using BERT embeddings
emb_dim = train_embeddings.shape[-1]
maxlen = train_embeddings.shape[1]
model = Sequential()
model.add(tf.keras.layers.Input(shape=(maxlen, emb_dim)))
model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(train_embeddings, Y_train, epochs=10, batch_size=32, validation_split=0.2)
# Evaluate the model
loss, accuracy = model.evaluate(test_embeddings, Y_test, verbose=1)
print(f'Test Accuracy: {accuracy}')
# Plot the training and validation accuracy and loss
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv1D, MaxPooling1D, GlobalMaxPooling1D, SimpleRNN, LSTM, Bidirectional, Embedding, Input
from sklearn.model_selection import train_test_split
from transformers import BertTokenizer, TFBertModel
# Define function to build and train model
def build_and_train_model(model_name, model):
print(f'Training {model_name} model...')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(train_embeddings, Y_train, epochs=10, batch_size=32, validation_split=0.2)
loss, accuracy = model.evaluate(test_embeddings, Y_test, verbose=1)
print(f'{model_name} Test Accuracy: {accuracy}')
# Plot the training and validation accuracy and loss
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title(f'{model_name} Accuracy')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.title(f'{model_name} Loss')
plt.show()
# CNN Model
cnn_model = Sequential()
cnn_model.add(Input(shape=(train_embeddings.shape[1], train_embeddings.shape[2])))
cnn_model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
cnn_model.add(MaxPooling1D(pool_size=2))
cnn_model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
cnn_model.add(GlobalMaxPooling1D())
cnn_model.add(Dense(64, activation='relu'))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(32, activation='relu'))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('CNN', cnn_model)
# RNN Model
rnn_model = Sequential()
rnn_model.add(Input(shape=(train_embeddings.shape[1], train_embeddings.shape[2])))
rnn_model.add(SimpleRNN(128, return_sequences=True))
rnn_model.add(GlobalMaxPooling1D())
rnn_model.add(Dense(64, activation='relu'))
rnn_model.add(Dropout(0.5))
rnn_model.add(Dense(32, activation='relu'))
rnn_model.add(Dropout(0.5))
rnn_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('RNN', rnn_model)
# LSTM Model
lstm_model = Sequential()
lstm_model.add(Input(shape=(train_embeddings.shape[1], train_embeddings.shape[2])))
lstm_model.add(LSTM(128, return_sequences=True))
lstm_model.add(GlobalMaxPooling1D())
lstm_model.add(Dense(64, activation='relu'))
lstm_model.add(Dropout(0.5))
lstm_model.add(Dense(32, activation='relu'))
lstm_model.add(Dropout(0.5))
lstm_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('LSTM', lstm_model)
# Bi-LSTM Model
bi_lstm_model = Sequential()
bi_lstm_model.add(Input(shape=(train_embeddings.shape[1], train_embeddings.shape[2])))
bi_lstm_model.add(Bidirectional(LSTM(128, return_sequences=True)))
bi_lstm_model.add(GlobalMaxPooling1D())
bi_lstm_model.add(Dense(64, activation='relu'))
bi_lstm_model.add(Dropout(0.5))
bi_lstm_model.add(Dense(32, activation='relu'))
bi_lstm_model.add(Dropout(0.5))
bi_lstm_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('Bi-LSTM', bi_lstm_model)
X = df['preprocessed_text'].values
Y = df['label'].values
# Split the dataset into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# Tokenize the text data
tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>')
tokenizer.fit_on_texts(X_train)
x_train_seq = tokenizer.texts_to_sequences(X_train)
x_test_seq = tokenizer.texts_to_sequences(X_test)
# Pad the sequences
maxlen = 100
x_train_pad = pad_sequences(x_train_seq, padding='post', maxlen=maxlen)
x_test_pad = pad_sequences(x_test_seq, padding='post', maxlen=maxlen)
# Create the embedding matrix
vocab_size = len(tokenizer.word_index) + 1
# Define function to build and train model
def build_and_train_model(model_name, model):
print(f'Training {model_name} model...')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(x_train_pad, Y_train, epochs=10, batch_size=32, validation_split=0.2)
loss, accuracy = model.evaluate(x_test_pad, Y_test, verbose=1)
print(f'{model_name} Test Accuracy: {accuracy}')
# Plot the training and validation accuracy and loss
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title(f'{model_name} Accuracy')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.title(f'{model_name} Loss')
plt.show()
# CNN Model
cnn_model = Sequential()
cnn_model.add(Embedding(input_dim=vocab_size, output_dim=100, input_length=maxlen))
cnn_model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
cnn_model.add(MaxPooling1D(pool_size=2))
cnn_model.add(Conv1D(filters=128, kernel_size=5, activation='relu'))
cnn_model.add(GlobalMaxPooling1D())
cnn_model.add(Dense(64, activation='relu'))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(32, activation='relu'))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('CNN', cnn_model)
# RNN Model
rnn_model = Sequential()
rnn_model.add(Embedding(input_dim=vocab_size, output_dim=100, input_length=maxlen))
rnn_model.add(SimpleRNN(128, return_sequences=True))
rnn_model.add(GlobalMaxPooling1D())
rnn_model.add(Dense(64, activation='relu'))
rnn_model.add(Dropout(0.5))
rnn_model.add(Dense(32, activation='relu'))
rnn_model.add(Dropout(0.5))
rnn_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('RNN', rnn_model)
# LSTM Model
lstm_model = Sequential()
lstm_model.add(Embedding(input_dim=vocab_size, output_dim=100, input_length=maxlen))
lstm_model.add(LSTM(128, return_sequences=True))
lstm_model.add(GlobalMaxPooling1D())
lstm_model.add(Dense(64, activation='relu'))
lstm_model.add(Dropout(0.5))
lstm_model.add(Dense(32, activation='relu'))
lstm_model.add(Dropout(0.5))
lstm_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('LSTM', lstm_model)
# Bi-LSTM Model
bi_lstm_model = Sequential()
bi_lstm_model.add(Embedding(input_dim=vocab_size, output_dim=100, input_length=maxlen))
bi_lstm_model.add(Bidirectional(LSTM(128, return_sequences=True)))
bi_lstm_model.add(GlobalMaxPooling1D())
bi_lstm_model.add(Dense(64, activation='relu'))
bi_lstm_model.add(Dropout(0.5))
bi_lstm_model.add(Dense(32, activation='relu'))
bi_lstm_model.add(Dropout(0.5))
bi_lstm_model.add(Dense(1, activation='sigmoid'))
build_and_train_model('Bi-LSTM', bi_lstm_model)