i want to add a sanity check to this code: # Check if the file with embeddings exists…
- User
i want to add a sanity check to this code: # Check if the file with embeddings exists and load it try: with open("encoded_embeddings.pkl", "rb") as f: existing_embeddings = pickle.load(f) except FileNotFoundError: existing_embeddings = ''
Find the number of already embedded rows
existing_rows = len(existing_embeddings)
Filter out the new rows from the closed_inq dataframe
new_rows = closed_inq.iloc[existing_rows:]
Encode new rows if needed
if len(new_rows) > 0: new_embeddings = encoder.encode(closed_inq.iloc[:, 1].tolist(), show_progress_bar=True, device=torch_device,) if len(existing_embeddings)>0: existing_embeddings = np.concatenate((existing_embeddings, new_embeddings)) else: existing_embeddings = new_embeddings
Save the encoded embeddings to a file
with open("encoded_embeddings.pkl", "wb") as f: pickle.dump(existing_embeddings, f)
if existing_embedding len is longer then closed_inq i want to delete "encoded_embeddings.pkl" and rerun the cell
- Assistant
To add a sanity check to the code, you can modify it as follows:
import os # Check if the file with embeddings exists and load it try: with open("encoded_embeddings.pkl", "rb") as f: existing_embeddings = pickle.load(f) except FileNotFoundError: existing_embeddings = '' # Find the number of already embedded rows existing_rows = len(existing_embeddings) # Filter out the new rows from the closed_inq dataframe new_rows = closed_inq.iloc[existing_rows:] # Encode new rows if needed if len(new_rows) > 0: new_embeddings = encoder.encode(closed_inq.iloc[:, 1].tolist(), show_progress_bar=True, device=torch_device,) if len(existing_embeddings) > 0: existing_embeddings = np.concatenate((existing_embeddings, new_embeddings)) else: existing_embeddings = new_embeddings # Save the encoded embeddings to a file with open("encoded_embeddings.pkl", "wb") as f: pickle.dump(existing_embeddings, f) # Check if existing embeddings are longer than closed_inq if len(existing_embeddings) > len(closed_inq): os.remove("encoded_embeddings.pkl") # Delete existing embeddings file # Rerun the cell # You can add the code block to rerun the cell here by directly running the necessary functions or reloading modules.