2025-01-06 15:39:29 +00:00
|
|
|
import csv
|
2024-10-28 23:39:36 +00:00
|
|
|
import os
|
2025-01-06 15:39:29 +00:00
|
|
|
|
2024-10-28 23:39:36 +00:00
|
|
|
import pandas as pd
|
|
|
|
|
|
2025-01-08 21:59:47 +00:00
|
|
|
import src.investment.one_to_n_funcs as one_to_n_funcs
|
2025-01-27 19:39:23 +00:00
|
|
|
import src.investment.one_to_one_funcs as one_to_one_funcs
|
2025-01-08 21:59:47 +00:00
|
|
|
import src.investment.postprocess as postprocess
|
|
|
|
|
import src.investment.preprocess as preprocess
|
2025-01-28 20:51:19 +00:00
|
|
|
import src.investment.aarete_derived as aarete_derived
|
2025-01-27 19:39:23 +00:00
|
|
|
import src.investment.smart_chunking_funcs as smart_chunking_funcs
|
|
|
|
|
from src.prompts.investment_prompts import Field, FieldSet
|
|
|
|
|
from src import config
|
2025-01-08 21:59:47 +00:00
|
|
|
from src.regex.regex_utils import irs_hotfix, npi_hotfix
|
2025-01-27 19:39:23 +00:00
|
|
|
import src.utils.string_utils as string_utils
|
|
|
|
|
import src.utils.io_utils as io_utils
|
2025-01-27 21:27:15 +00:00
|
|
|
from src.config import FIELD_JSON_PATH
|
2024-12-06 18:19:58 +00:00
|
|
|
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
def process_file(file_object, run_timestamp):
|
2024-10-28 23:39:36 +00:00
|
|
|
filename, contract_text = file_object
|
2025-01-27 19:39:23 +00:00
|
|
|
print(f"Processing {filename}...")
|
2024-11-13 17:08:19 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
################## PREPROCESS ##################
|
2025-01-10 17:24:44 +00:00
|
|
|
contract_text = preprocess.clean_text(contract_text)
|
2025-01-27 19:39:23 +00:00
|
|
|
text_dict, top_sheet_dict = preprocess.split_text(contract_text)
|
|
|
|
|
text_dict = preprocess.clean_tables(text_dict, filename) # TODO: Optimize later to not run a bunch of extra prompts for the extra table pages
|
|
|
|
|
exhibit_pages, exhibit_chunk_mapping = preprocess.one_to_n_exhibit_chunking(text_dict, filename)
|
|
|
|
|
print(f"Preprocessing Complete - {filename}")
|
|
|
|
|
|
|
|
|
|
################## ONE TO ONE ##################
|
|
|
|
|
one_to_one_results = run_one_to_one_prompts(filename, contract_text, text_dict, top_sheet_dict) # Return df
|
|
|
|
|
one_to_one_results['CONTRACT_FILE_NAME'] = filename
|
|
|
|
|
|
|
|
|
|
################## ONE TO N ##################
|
|
|
|
|
if string_utils.contains_reimbursement(contract_text):
|
|
|
|
|
one_to_n_results = run_one_to_n_prompts(filename, text_dict, exhibit_pages, exhibit_chunk_mapping) # Return df
|
|
|
|
|
one_to_n_results['CONTRACT_FILE_NAME'] = filename
|
2025-01-28 20:51:19 +00:00
|
|
|
contract_results = pd.merge(one_to_one_results, one_to_n_results, how='right', on='CONTRACT_FILE_NAME')
|
2025-01-27 19:39:23 +00:00
|
|
|
else:
|
2025-01-28 20:51:19 +00:00
|
|
|
contract_results = one_to_n_results
|
|
|
|
|
|
|
|
|
|
################## AARETE-DERIVED ##################
|
|
|
|
|
final_results = aarete_derived.get_aarete_derived(contract_results)
|
2025-01-27 19:39:23 +00:00
|
|
|
|
|
|
|
|
################## POSTPROCESS ##################
|
|
|
|
|
final_df = postprocess.postprocess(final_results)
|
|
|
|
|
|
|
|
|
|
################## WRITE INDIVIDUAL ##################
|
|
|
|
|
if config.WRITE_TO_S3:
|
|
|
|
|
io_utils.write_s3(final_df, filename, run_timestamp, "individual")
|
|
|
|
|
else:
|
|
|
|
|
io_utils.write_local(final_df, filename, "", "individual")
|
|
|
|
|
|
|
|
|
|
return final_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_one_to_one_prompts(filename, contract_text, text_dict, top_sheet_dict):
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
################## INITIALIZE FIELDS ##################
|
|
|
|
|
one_to_one_fields = FieldSet(relationship="one_to_one", file_path=config.FIELD_JSON_PATH)
|
2024-12-02 19:50:41 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
################## RUN REGEX ##################
|
|
|
|
|
regex_answers_dict = one_to_one_funcs.run_regex_fields(one_to_one_fields, contract_text, filename)
|
2025-01-10 17:24:44 +00:00
|
|
|
|
2024-12-06 18:19:58 +00:00
|
|
|
################## RUN SMART CHUNKED PROMPTS ##################
|
2025-01-27 19:39:23 +00:00
|
|
|
smart_chunked_answers_dict = one_to_one_funcs.run_smart_chunked_fields(
|
|
|
|
|
one_to_one_fields, contract_text, filename
|
2024-12-06 18:19:58 +00:00
|
|
|
)
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2024-12-06 18:19:58 +00:00
|
|
|
################## RUN FULL CONTEXT PROMPTS ##################
|
2025-01-27 19:39:23 +00:00
|
|
|
full_context_answers_dict = one_to_one_funcs.run_full_context_fields(
|
|
|
|
|
one_to_one_fields, contract_text, filename
|
2024-12-06 18:19:58 +00:00
|
|
|
)
|
2024-11-18 21:01:09 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
################## CONVERT TO DF ##################
|
|
|
|
|
final_answers_dict = {**regex_answers_dict, **smart_chunked_answers_dict, **full_context_answers_dict}
|
|
|
|
|
final_df = pd.DataFrame([final_answers_dict])
|
2024-11-13 17:08:19 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
return final_df
|
2024-10-28 23:39:36 +00:00
|
|
|
|
|
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
def run_one_to_n_prompts(filename, text_dict, exhibit_pages, exhibit_chunk_mapping):
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
################## RUN PROMPTS ##################
|
|
|
|
|
one_to_n_results = one_to_n_funcs.get_one_to_n(text_dict, filename, exhibit_pages, exhibit_chunk_mapping)
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
one_to_n_df = pd.DataFrame(one_to_n_results)
|
|
|
|
|
return one_to_n_df
|