from transformers import BertTokenizer, TFBertModel
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report

# Load Quora dataset
quora_data = pd.read_csv('questions.csv', nrows=6000)

# Select a subset of the dataset for training
#train_data = quora_data.sample(n=80)  # Adjust the number of samples based on your requirements

# Get the minimum length between question1 and question2
min_length = min(len(quora_data['question1']), len(quora_data['question2']))

# Adjust the number of samples based on the minimum length
train_data = quora_data.sample(n=min_length)

# Convert the question pairs to list format
question1_list = list(train_data['question1'])
question2_list = list(train_data['question2'])
is_duplicate = list(train_data['is_duplicate'])


# Create DataFrame for the first pair
data1 = pd.DataFrame({'Sentences': question1_list, 'Labels': is_duplicate})

# Create DataFrame for the second pair
data2 = pd.DataFrame({'Sentences': question2_list, 'Labels': is_duplicate})

# Concatenate the two DataFrames
data = pd.concat([data1, data2], ignore_index=True)


# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(data['Sentences'], data['Labels'], test_size=0.2, random_state=42)

# Initialize the TF-IDF vectorizer
vectorizer = TfidfVectorizer()

# Vectorize the sentences in train set
X_train_vectors = vectorizer.fit_transform(X_train)

# Vectorize the sentences in test set
X_test_vectors = vectorizer.transform(X_test)

# Initialize the SVM classifier
svm = SVC()

# Fit the classifier to the training data
svm.fit(X_train_vectors, y_train)

# Predict labels for the test data
y_pred = svm.predict(X_test_vectors)

# Print classification report
print(classification_report(y_test, y_pred))


how to take output from this code and input in to the below code 

from transformers import BertTokenizer, TFBertModel
import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Bidirectional, LSTM, Dense

# Load BERT tokenizer and model
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
bert_model = TFBertModel.from_pretrained('bert-base-uncased')

# Load Quora dataset
quora_data = pd.read_csv('questions_test.csv',nrows=100)

# Select a subset of the dataset for training
train_data = quora_data.sample(n=80)  # Adjust the number of samples based on your requirements

# Convert the question pairs to list format
question1_list = list(train_data['question1'])
question2_list = list(train_data['question2'])


# Tokenize and convert sentences to BERT inputs
max_len = 25  # Define the maximum length for padding/truncating
encoded_inputs = tokenizer(question1_list, question2_list, padding=True, truncation=True, max_length=max_len, return_tensors='tf' )
#attention_mask = encoded_inputs['attention_mask']

# Get BERT embeddings
#outputs = bert_model(input_ids, attention_mask=attention_mask)
#embeddings = outputs[0]  # Shape: (num_samples, max_len, 768)
#outputs = bert_model(encoded_inputs.input_ids, attention_mask=encoded_inputs.attention_mask)
#embeddings = outputs.last_hidden_state  # Shape: (num_samples, max_len, 768)

inputs = {
    'input_ids': encoded_inputs['input_ids'],
    'attention_mask': encoded_inputs['attention_mask']
}

outputs = bert_model(inputs)
embeddings = outputs.last_hidden_state  # Shape: (num_samples, max_len, 768)

# Define Bi-LSTM model
model = Sequential()
model.add(Bidirectional(LSTM(64, return_sequences=True), input_shape=(max_len, 768)))
model.add(Bidirectional(LSTM(64)))
model.add(Dense(1, activation='sigmoid'))

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Prepare labels for training
labels = np.array(train_data['is_duplicate'])  # Assuming the dataset has a “is_duplicate” column indicating similarity/dissimilarity

# Train the model
model.fit(embeddings, labels, epochs=10)

#question1_list = list(test_data['question1'])
#question2_list = list(test_data['question2'])

# Load test data and preprocess
test_data = quora_data.sample(n=20)  # Adjust the number of samples for testing
encoded_test = tokenizer(list(test_data['question1']), list(test_data['question2']), padding=True, truncation=True, max_length=max_len, return_tensors='tf')
test_input_ids = encoded_test['input_ids']
test_attention_mask = encoded_test['attention_mask']

# Get BERT embeddings for test data
test_outputs = bert_model(test_input_ids, attention_mask=test_attention_mask)
test_embeddings = test_outputs[0]  # Shape: (num_samples, max_len, 768)

# Make predictions on test data
predictions = model.predict(test_embeddings)

# Print test predictions
for i, pred in enumerate(predictions):
    print(f"Question 1: {test_data.iloc[i]['question1']}")
    print(f"Question 2: {test_data.iloc[i]['question2']}")
    print(f"Similarity Prediction: {pred}")
    print()