convert this to feather instead of pickle: import os import pickle import torch from…
- User
convert this to feather instead of pickle: import os import pickle import torch from sentence_transformers import SentenceTransformer import pandas as pd
Initialize your encoder (use the appropriate model name)
encoder = SentenceTransformer('model_name')
Initialize your DataFrame (replace this with your actual data)
For demonstration, let's assume you have a DataFrame called query_unlinked_closed_inq
query_unlinked_closed_inq = pd.DataFrame({'text_column' ['Text 1', 'Text 2', 'Text 3']})
Your existing embeddings (initialize as an empty list)
existing_embeddings = []
Function to encode new rows and append to existing embeddings
def encode_and_append(new_rows) new_embeddings = encoder.encode( new_rows['text_column'].tolist(), show_progress_bar=True, device=torch.device(cuda if torch.cuda.is_available() else cpu), # Use GPU if available ) existing_embeddings.extend(new_embeddings)
Check if the file with embeddings exists and load it
if os.path.exists(encoded_embeddings.pkl) with open(encoded_embeddings.pkl, rb) as f existing_embeddings = pickle.load(f)
Example usage
Encode and append new rows to existing embeddings
new_rows_to_encode = query_unlinked_closed_inq.iloc[len(existing_embeddings)] # Encode only new rows encode_and_append(new_rows_to_encode)
Now, existing_embeddings contains embeddings for all rows (both old and new)
Save the encoded embeddings to a file
with open(encoded_embeddings.pkl, wb) as f pickle.dump(existing_embeddings, f)
- Assistant
import os import pandas as pd import torch from sentence_transformers import SentenceTransformer import pyarrow.feather as feather
Initialize your encoder (use the appropriate model name)
encoder = SentenceTransformer('model_name')
Initialize your DataFrame (replace this with your actual data)
For demonstration, let's assume you have a DataFrame called query_unlinked_closed_inq
query_unlinked_closed_inq = pd.DataFrame({'text_column': ['Text 1', 'Text 2', 'Text 3']})
Your existing embeddings (initialize as an empty list)
existing_embeddings = []
Function to encode new rows and append to existing embeddings
def encode_and_append(new_rows): new_embeddings = encoder.encode( new_rows['text_column'].tolist(), show_progress_bar=True, device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'), # Use GPU if available ) existing_embeddings.extend(new_embeddings)