Files
doczyai-pipelines/fieldExtraction/scripts/create_code_embeddings.py
T

31 lines
1.5 KiB
Python
Raw Normal View History

import src.utils.embedding_utils as embedding_utils
from crosswalk.crosswalk_utils import CrosswalkBuilder
import src.codes.code_funcs as code_funcs
import os
import pandas as pd
from sentence_transformers import SentenceTransformer
# Load model
roberta_model = SentenceTransformer("all-roberta-large-v1")
mapping_dir = "crosswalk/mapping_csvs/proc_cd"
pkl_dir = "embeddings"
for filename in os.listdir(mapping_dir):
if filename.endswith(".csv") and "level" in filename:
print(f"Creating embeddings for {filename}")
stripped_filename = filename.replace(".csv", "")
mapping_df = pd.read_csv(os.path.join(mapping_dir, filename))
proc_crosswalk = CrosswalkBuilder().from_df(mapping_df, from_col="Code", to_col="Description")
proc_choices = [x for x in proc_crosswalk.mapping.values()]
os.makedirs(os.path.join(pkl_dir, stripped_filename), exist_ok=True)
proc_index = embedding_utils.create_faiss_index(choices=proc_choices,
model=roberta_model,
save_path=os.path.join(pkl_dir, stripped_filename, "faiss_index.bin"),
embedding_path=os.path.join(pkl_dir, stripped_filename, "embeddings.npy"),
choices_path=os.path.join(pkl_dir, stripped_filename, "choices.pkl")
)