2025-08-27 16:37:48 +00:00
|
|
|
import logging
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-08-29 14:38:15 +00:00
|
|
|
import pandas as pd
|
2025-08-05 20:46:19 +00:00
|
|
|
import src.codes.code_funcs as code_funcs
|
2025-04-25 20:28:52 +00:00
|
|
|
import src.investment.aarete_derived as aarete_derived
|
|
|
|
|
import src.investment.dynamic_funcs as dynamic_funcs
|
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
|
2025-08-05 20:46:19 +00:00
|
|
|
import src.investment.postprocessing_funcs as postprocessing_funcs
|
2025-01-08 21:59:47 +00:00
|
|
|
import src.investment.preprocess as preprocess
|
2025-11-05 15:20:33 +00:00
|
|
|
import src.investment.hybrid_smart_chunking_funcs as hybrid_smart_chunking_funcs
|
2025-06-09 21:49:45 +00:00
|
|
|
import src.investment.row_funcs as row_funcs
|
2025-08-05 20:46:19 +00:00
|
|
|
import src.investment.tin_npi_funcs as tin_npi_funcs
|
2025-01-27 19:39:23 +00:00
|
|
|
import src.utils.io_utils as io_utils
|
2025-08-29 14:38:15 +00:00
|
|
|
import src.utils.logging_utils as logging_utils
|
2025-04-25 20:28:52 +00:00
|
|
|
import src.utils.string_utils as string_utils
|
2025-08-05 20:46:19 +00:00
|
|
|
from constants.constants import Constants
|
2025-04-25 20:28:52 +00:00
|
|
|
from src import config
|
2025-08-11 20:47:52 +00:00
|
|
|
from src.prompts.fieldset import FieldSet
|
2025-04-25 20:28:52 +00:00
|
|
|
from src.utils.string_utils import datetime_str
|
|
|
|
|
|
2025-04-08 18:29:39 +00:00
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
def process_file(file_object, constants: Constants, run_timestamp):
|
2024-10-28 23:39:36 +00:00
|
|
|
filename, contract_text = file_object
|
2025-08-29 14:38:15 +00:00
|
|
|
|
2025-09-24 21:31:38 +00:00
|
|
|
# Set per-file logging context:
|
|
|
|
|
# With this, all logging calls in this thread will now route to logs/{filename}.log
|
|
|
|
|
# This includes logging from all called functions (preprocess, one_to_n_funcs, etc.)
|
2025-08-29 14:38:15 +00:00
|
|
|
logging_utils.set_current_file(filename)
|
2025-08-27 16:37:48 +00:00
|
|
|
logging.info(f"{datetime_str()} Processing {filename}...")
|
2024-11-13 17:08:19 +00:00
|
|
|
|
2025-10-23 19:57:11 +00:00
|
|
|
# Set default values
|
|
|
|
|
dynamic_one_to_one_fields = FieldSet()
|
|
|
|
|
process_one_to_n = config.FIELDS in ['all', 'one_to_n']
|
|
|
|
|
process_one_to_one = config.FIELDS in ['all', 'one_to_one']
|
|
|
|
|
# Initialize default fallback for final results
|
|
|
|
|
final_results = pd.DataFrame([{"FILE_NAME": filename}])
|
|
|
|
|
|
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)
|
2025-08-27 17:42:35 +00:00
|
|
|
text_dict, header, footer = preprocess.find_headers_and_footers(text_dict)
|
2025-08-05 20:46:19 +00:00
|
|
|
text_dict = preprocess.clean_tables(
|
|
|
|
|
text_dict, constants.EXHIBIT_HEADER_MARKERS, filename
|
|
|
|
|
)
|
2025-10-13 18:54:10 +00:00
|
|
|
|
|
|
|
|
# ONE TO N PROCESSING
|
2025-10-27 16:22:43 +00:00
|
|
|
one_to_n_results = pd.DataFrame() # Initialize empty DataFrame for one_to_n_results
|
2025-10-13 18:54:10 +00:00
|
|
|
if process_one_to_n:
|
|
|
|
|
exhibit_dict, all_exhibit_headers = preprocess.one_to_n_exhibit_chunking(
|
|
|
|
|
text_dict, constants.EXHIBIT_HEADER_MARKERS, filename
|
|
|
|
|
)
|
|
|
|
|
logging.info(f"{datetime_str()} Preprocessing Complete - {filename}")
|
|
|
|
|
|
|
|
|
|
one_to_n_results = pd.DataFrame([{"FILE_NAME": filename}]) # Initialize here
|
|
|
|
|
|
|
|
|
|
if string_utils.contains_reimbursement(contract_text):
|
|
|
|
|
one_to_n_results, dynamic_one_to_one_fields = run_one_to_n_prompts(
|
|
|
|
|
filename, exhibit_dict, all_exhibit_headers, constants
|
|
|
|
|
)
|
2025-10-23 19:57:11 +00:00
|
|
|
if not one_to_n_results.empty:
|
|
|
|
|
one_to_n_results["FILE_NAME"] = filename
|
|
|
|
|
one_to_n_results = postprocessing_funcs.generate_reimb_ids(one_to_n_results)
|
2025-10-13 18:54:10 +00:00
|
|
|
logging.info(f"{datetime_str()} One to N Complete - {filename}")
|
|
|
|
|
else:
|
|
|
|
|
logging.info(f"{datetime_str()} No Reimbursement Found, Skipping - {filename}")
|
|
|
|
|
|
|
|
|
|
final_results = one_to_n_results # Set as final results
|
2025-01-27 19:39:23 +00:00
|
|
|
else:
|
2025-10-13 18:54:10 +00:00
|
|
|
logging.info(f"{datetime_str()} Fields not configured for One to N, Skipping - {filename}")
|
|
|
|
|
|
|
|
|
|
# ONE TO ONE PROCESSING
|
|
|
|
|
if process_one_to_one:
|
|
|
|
|
one_to_one_results = run_one_to_one_prompts(
|
|
|
|
|
filename,
|
|
|
|
|
contract_text,
|
|
|
|
|
text_dict,
|
|
|
|
|
top_sheet_dict,
|
|
|
|
|
dynamic_one_to_one_fields,
|
|
|
|
|
constants,
|
|
|
|
|
)
|
|
|
|
|
one_to_one_results["FILE_NAME"] = filename
|
|
|
|
|
logging.info(f"{datetime_str()} One to One Complete - {filename}")
|
|
|
|
|
|
|
|
|
|
# Decide how to handle one_to_one results
|
2025-10-23 19:57:11 +00:00
|
|
|
if not one_to_n_results.empty:
|
2025-10-13 18:54:10 +00:00
|
|
|
# BOTH processed - merge into one_to_n
|
|
|
|
|
final_results = row_funcs.merge_one_to_one_into_one_to_n(
|
|
|
|
|
one_to_n_results, one_to_one_results, constants
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
# ONLY one_to_one processed - convert dict to DataFrame
|
|
|
|
|
final_results = pd.DataFrame([one_to_one_results])
|
|
|
|
|
else:
|
|
|
|
|
logging.info(f"{datetime_str()} Fields not configured for One to One, Skipping - {filename}")
|
2025-08-01 22:36:01 +00:00
|
|
|
|
2025-10-13 18:54:10 +00:00
|
|
|
# APPLY CODES IF ONE_TO_N WAS PROCESSED
|
2025-10-23 19:57:11 +00:00
|
|
|
if not one_to_n_results.empty:
|
2025-10-13 18:54:10 +00:00
|
|
|
results_with_code = code_funcs.code_breakout(final_results, constants)
|
|
|
|
|
final_results = code_funcs.grouper_breakout(results_with_code)
|
|
|
|
|
logging.info(f"{datetime_str()} Codes Complete - {filename}")
|
2025-08-26 15:43:53 +00:00
|
|
|
|
2025-10-13 18:54:10 +00:00
|
|
|
# POSTPROCESS
|
2025-08-05 20:46:19 +00:00
|
|
|
final_df = postprocess.postprocess(final_results, constants)
|
2025-08-27 16:37:48 +00:00
|
|
|
logging.info(f"{datetime_str()} Postprocessing Complete - {filename}")
|
2025-01-27 19:39:23 +00:00
|
|
|
|
|
|
|
|
################## 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")
|
|
|
|
|
|
2025-08-27 16:37:48 +00:00
|
|
|
logging.info(f"{datetime_str()} Writing Complete - {filename}")
|
2025-01-27 19:39:23 +00:00
|
|
|
return final_df
|
|
|
|
|
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
def run_one_to_one_prompts(
|
2025-08-14 16:46:28 +00:00
|
|
|
filename,
|
|
|
|
|
contract_text,
|
|
|
|
|
text_dict,
|
|
|
|
|
top_sheet_dict,
|
|
|
|
|
dynamic_one_to_one_fields,
|
|
|
|
|
constants,
|
2025-08-05 20:46:19 +00:00
|
|
|
):
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2025-01-27 19:39:23 +00:00
|
|
|
################## INITIALIZE FIELDS ##################
|
2025-08-05 20:46:19 +00:00
|
|
|
one_to_one_fields = FieldSet(
|
|
|
|
|
relationship="one_to_one", file_path=config.FIELD_JSON_PATH
|
|
|
|
|
).combine(dynamic_one_to_one_fields)
|
2024-12-02 19:50:41 +00:00
|
|
|
|
2025-06-26 19:35:32 +00:00
|
|
|
################## RUN PROVIDER INFO ##################
|
2025-08-05 20:46:19 +00:00
|
|
|
one_to_one_results, one_to_one_fields = tin_npi_funcs.run_provider_info_fields(
|
|
|
|
|
contract_text, one_to_one_fields, text_dict, filename
|
|
|
|
|
)
|
2025-11-05 15:20:33 +00:00
|
|
|
|
|
|
|
|
################## RUN HYBRID SMART CHUNKED PROMPTS ##################
|
|
|
|
|
# RAG function loads retrieval questions internally and matches with investment_prompts.json
|
|
|
|
|
hybrid_smart_chunked_answers_dict = hybrid_smart_chunking_funcs.run_hybrid_smart_chunked_fields(
|
|
|
|
|
one_to_one_fields, constants, contract_text, filename, text_dict
|
2024-12-06 18:19:58 +00:00
|
|
|
)
|
2025-11-05 15:20:33 +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(
|
2025-08-11 20:47:52 +00:00
|
|
|
one_to_one_fields, contract_text, constants, filename
|
2024-12-06 18:19:58 +00:00
|
|
|
)
|
2025-01-31 22:36:11 +00:00
|
|
|
################## COMBINE ANSWERS ################
|
2025-11-05 15:20:33 +00:00
|
|
|
# Prefer HSC answers over full_context when HSC value is not N/A
|
|
|
|
|
for key, value in full_context_answers_dict.items():
|
|
|
|
|
one_to_one_results[key] = value
|
|
|
|
|
|
|
|
|
|
for key, value in hybrid_smart_chunked_answers_dict.items():
|
|
|
|
|
if not string_utils.is_empty(value):
|
|
|
|
|
one_to_one_results[key] = value
|
|
|
|
|
elif key not in one_to_one_results:
|
|
|
|
|
# Add HSC's N/A if field doesn't exist yet
|
|
|
|
|
one_to_one_results[key] = value
|
2025-01-31 22:36:11 +00:00
|
|
|
|
2025-10-30 15:28:39 +00:00
|
|
|
################## ADD AD FIELDS ################
|
|
|
|
|
one_to_one_results = one_to_one_funcs.get_aarete_derived_dates(one_to_one_results, text_dict, filename)
|
2025-11-05 15:20:33 +00:00
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
################## Crosswalk Fields ##################
|
2025-09-24 15:56:27 +00:00
|
|
|
one_to_one_results = aarete_derived.get_crosswalk_fields(
|
|
|
|
|
[one_to_one_results], constants
|
|
|
|
|
)
|
2024-11-13 17:08:19 +00:00
|
|
|
|
2025-04-08 18:29:39 +00:00
|
|
|
################## Fill NA Mapping ##################
|
|
|
|
|
one_to_one_results = aarete_derived.fill_na_mapping(one_to_one_results)
|
|
|
|
|
|
|
|
|
|
return one_to_one_results[0]
|
2024-10-28 23:39:36 +00:00
|
|
|
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
def run_one_to_n_prompts(filename, exhibit_dict, all_exhibit_headers, constants):
|
|
|
|
|
|
2025-01-31 22:36:11 +00:00
|
|
|
one_to_n_results = []
|
2025-09-24 15:56:27 +00:00
|
|
|
seen_pairs = set()
|
|
|
|
|
|
|
|
|
|
for exhibit_page, exhibit_text in exhibit_dict.items():
|
|
|
|
|
|
|
|
|
|
############################### Initialize Fields ###############################
|
|
|
|
|
exhibit_level_fields = FieldSet(
|
|
|
|
|
config.FIELD_JSON_PATH, field_type="exhibit_level"
|
|
|
|
|
)
|
|
|
|
|
reimbursement_level_fields = FieldSet(
|
|
|
|
|
config.FIELD_JSON_PATH, field_type="reimbursement_level"
|
|
|
|
|
)
|
|
|
|
|
dynamic_primary_fields = FieldSet(
|
|
|
|
|
config.FIELD_JSON_PATH, field_type="dynamic_primary"
|
|
|
|
|
)
|
|
|
|
|
dynamic_reimb_info_fields = FieldSet(
|
|
|
|
|
config.FIELD_JSON_PATH, field_type="dynamic_reimb_info"
|
|
|
|
|
)
|
|
|
|
|
dynamic_code_fields = FieldSet(
|
|
|
|
|
config.FIELD_JSON_PATH, field_type="dynamic_code"
|
|
|
|
|
)
|
|
|
|
|
special_case_primary_fields = FieldSet(
|
|
|
|
|
config.FIELD_JSON_PATH, field_type="special_case_primary"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
############################### Get Exhibit Header ###############################
|
|
|
|
|
exhibit_header = one_to_n_funcs.get_exhibit_header(
|
|
|
|
|
all_exhibit_headers, exhibit_page
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
############################### Get Exhibit Level ###############################
|
|
|
|
|
exhibit_level_answers, reimbursement_level_fields = (
|
|
|
|
|
one_to_n_funcs.exhibit_level(
|
|
|
|
|
exhibit_text,
|
|
|
|
|
exhibit_header,
|
|
|
|
|
exhibit_page,
|
|
|
|
|
exhibit_level_fields,
|
|
|
|
|
dynamic_primary_fields,
|
|
|
|
|
dynamic_code_fields,
|
|
|
|
|
dynamic_reimb_info_fields,
|
|
|
|
|
reimbursement_level_fields,
|
|
|
|
|
constants,
|
|
|
|
|
filename,
|
2025-08-05 20:46:19 +00:00
|
|
|
)
|
|
|
|
|
)
|
2025-10-22 15:21:43 +00:00
|
|
|
logging.debug(f"Exhibit Level Answers for {filename}, Page {exhibit_page}: {exhibit_level_answers}")
|
2025-06-19 17:20:39 +00:00
|
|
|
|
2025-09-24 15:56:27 +00:00
|
|
|
############################### Get Reimbursement-Level ###############################
|
|
|
|
|
reimbursement_primary_answers, special_case_primary_answers = (
|
|
|
|
|
one_to_n_funcs.reimbursement_level(
|
|
|
|
|
exhibit_text,
|
|
|
|
|
reimbursement_level_fields,
|
|
|
|
|
special_case_primary_fields,
|
|
|
|
|
exhibit_level_answers["EXHIBIT_LESSER_OF_STATEMENT"],
|
|
|
|
|
seen_pairs,
|
|
|
|
|
exhibit_page,
|
|
|
|
|
filename,
|
|
|
|
|
constants,
|
|
|
|
|
)
|
|
|
|
|
)
|
2025-10-22 15:21:43 +00:00
|
|
|
logging.debug(f"Reimbursement Primary Answers for {filename}, Page {exhibit_page}: {reimbursement_primary_answers}")
|
2025-09-04 18:22:35 +00:00
|
|
|
|
2025-09-24 15:56:27 +00:00
|
|
|
################################ Get Breakouts ###############################
|
|
|
|
|
reimbursement_level_answers, special_case_breakout_answers = (
|
|
|
|
|
one_to_n_funcs.breakout(
|
|
|
|
|
reimbursement_primary_answers,
|
|
|
|
|
special_case_primary_answers,
|
|
|
|
|
special_case_primary_fields,
|
|
|
|
|
filename,
|
|
|
|
|
constants,
|
|
|
|
|
)
|
2025-08-05 20:46:19 +00:00
|
|
|
)
|
2025-05-13 20:22:43 +00:00
|
|
|
|
2025-09-24 15:56:27 +00:00
|
|
|
################################ Combine Answers ###############################
|
|
|
|
|
all_exhibit_rows = row_funcs.combine_one_to_n(
|
2025-08-05 20:46:19 +00:00
|
|
|
exhibit_text,
|
2025-09-24 15:56:27 +00:00
|
|
|
reimbursement_level_answers,
|
|
|
|
|
exhibit_level_answers,
|
|
|
|
|
special_case_breakout_answers,
|
2025-08-05 20:46:19 +00:00
|
|
|
filename,
|
2025-09-24 15:56:27 +00:00
|
|
|
) # returns list of dicts
|
|
|
|
|
|
2025-10-07 15:06:10 +00:00
|
|
|
################################ Mapping and Cleaning ###############################
|
|
|
|
|
all_exhibit_rows = one_to_n_funcs.one_to_n_cleaning(all_exhibit_rows, exhibit_text, constants, filename)
|
2025-04-08 18:29:39 +00:00
|
|
|
|
2025-09-24 15:56:27 +00:00
|
|
|
################################ Add to Total ###############################
|
|
|
|
|
one_to_n_results += all_exhibit_rows
|
2025-05-07 23:03:16 +00:00
|
|
|
|
2025-04-08 18:29:39 +00:00
|
|
|
################## Add N/A Dynamic or Exhibit to One-to-One ##################
|
2025-08-05 20:46:19 +00:00
|
|
|
dynamic_one_to_one_fields = dynamic_funcs.get_dynamic_one_to_one_fields(
|
|
|
|
|
one_to_n_results
|
|
|
|
|
)
|
2025-04-08 18:29:39 +00:00
|
|
|
|
2025-01-31 22:36:11 +00:00
|
|
|
################## CONVERT TO DF ##################
|
2025-01-27 19:39:23 +00:00
|
|
|
one_to_n_df = pd.DataFrame(one_to_n_results)
|
2025-02-21 14:44:36 +00:00
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
return one_to_n_df, dynamic_one_to_one_fields
|