senaK-quasara's picture
Upload read_dataset.py
a252a0a verified
raw
history blame
1.69 kB
import pyarrow.parquet as pq
METADATA_FILEPATH = '/home/sena/plato_backend/scripts/huggingface_embedding_sharing/huggingface_embedding_sharing/save_backup/metadata_file.parquet'
EMBEDDINGS_FILEPATH = '/home/sena/plato_backend/scripts/huggingface_embedding_sharing/huggingface_embedding_sharing/embeddings_2.txt'
def read_embeddings(filepath):
# Read the lines from the file
with open(filepath, 'rb') as f:
lines = f.readlines()
embedding_dict = {}
for index, line in enumerate(lines):
line_as_list = line.split()
# Decode the byte string to a regular string and convert to float
float_list = [float(byte_string.decode('utf-8')) for byte_string in line_as_list]
# Save the float list (embeddings) to the dictionary with the index as the key
embedding_dict[index] = float_list
return embedding_dict
#Read the metadata file
metadata_df = pq.read_table(METADATA_FILEPATH).to_pandas()
## How to reach embeddings - example for a specific row
unique_id = '390U_535R_2021-08-21064631_69'
# Get the row with the unique_id
row = metadata_df.loc[metadata_df['unique_id'] == unique_id]
# Get the values for 'data_row' and 'embedding_file'
dat_row_value = row['dat_row'] # index of the row in the embedding file
embedding_file_value = row['embedding_file'] # specific embedding file for the row
# Read the embeddings in the embedding file
embeddings = read_embeddings(EMBEDDINGS_FILEPATH)
vector = embeddings[int(dat_row_value)]
print(f"Row: {row}, Dat Row: {dat_row_value}, Embedding File: {embedding_file_value}, Vector length: {len(vector)}")
#print(f"Vector: {vector}")