Datasets:
File size: 1,689 Bytes
984890f a252a0a 984890f a252a0a 984890f |
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 |
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}")
|