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
|
|
|
|
|
|
|
|
|
|
import conditional_funcs
|
2025-01-06 15:39:29 +00:00
|
|
|
import config
|
|
|
|
|
import one_to_n_funcs
|
2024-10-28 23:39:36 +00:00
|
|
|
import postprocess
|
2025-01-06 15:39:29 +00:00
|
|
|
import preprocess
|
|
|
|
|
import smart_chunking_funcs
|
|
|
|
|
from regex_funcs import irs_hotfix, npi_hotfix
|
2024-12-06 18:19:58 +00:00
|
|
|
|
2024-10-28 23:39:36 +00:00
|
|
|
|
|
|
|
|
def run_ac_prompts(file_object):
|
|
|
|
|
|
2024-11-13 17:08:19 +00:00
|
|
|
ac_answers_dict = {}
|
|
|
|
|
log_data = []
|
|
|
|
|
chunk_data = []
|
|
|
|
|
|
2024-10-28 23:39:36 +00:00
|
|
|
################## INITIATE PROCESSING ##################
|
|
|
|
|
filename, contract_text = file_object
|
|
|
|
|
print(f"Processing AC for {filename}...")
|
2024-11-13 17:08:19 +00:00
|
|
|
|
|
|
|
|
################## PREPROCESS - SMART CHUNK ##################
|
2024-11-22 15:30:01 +00:00
|
|
|
|
|
|
|
|
# Top Sheet Addition
|
2024-12-06 18:19:58 +00:00
|
|
|
text_dict, top_sheet_dict, exhibit_pages, num_pages, ac_chunks = (
|
|
|
|
|
preprocess.preprocess(contract_text, filename, fields="ac")
|
2024-10-28 23:39:36 +00:00
|
|
|
)
|
|
|
|
|
print(f"AC Preprocessing Complete - {filename}")
|
|
|
|
|
|
2024-11-13 17:08:19 +00:00
|
|
|
################## RUN REGEX FUNCTIONALITY ##################
|
2024-12-02 19:50:41 +00:00
|
|
|
# no prompting for these fields
|
2024-12-06 18:19:58 +00:00
|
|
|
|
2024-12-02 23:03:46 +00:00
|
|
|
irs_answers = irs_hotfix(filename, text_dict, top_sheet_dict)
|
|
|
|
|
ac_answers_dict.update(irs_answers)
|
2024-12-02 19:50:41 +00:00
|
|
|
|
|
|
|
|
npi_answers = npi_hotfix(text_dict, top_sheet_dict)
|
2024-12-02 23:03:46 +00:00
|
|
|
ac_answers_dict.update(npi_answers)
|
2024-12-06 18:19:58 +00:00
|
|
|
################## RUN SMART CHUNKED PROMPTS ##################
|
|
|
|
|
ac_answers_dict, no_keyword_matches = (
|
2025-01-06 15:39:29 +00:00
|
|
|
smart_chunking_funcs.run_smart_chunk_prompts(
|
2024-12-06 18:19:58 +00:00
|
|
|
ac_answers_dict, ac_chunks, contract_text, filename, text_dict
|
2024-11-13 21:33:17 +00:00
|
|
|
)
|
2024-12-06 18:19:58 +00:00
|
|
|
)
|
2024-11-18 21:01:09 +00:00
|
|
|
|
2024-12-06 18:19:58 +00:00
|
|
|
################## GET FULL CONTEXT FIELDS ##################
|
|
|
|
|
# Process 'full_context' fields together
|
2025-01-06 15:39:29 +00:00
|
|
|
full_context_fields = smart_chunking_funcs.get_full_context_fields(
|
2024-12-06 18:19:58 +00:00
|
|
|
ac_answers_dict, no_keyword_matches
|
|
|
|
|
)
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2024-12-06 18:19:58 +00:00
|
|
|
################## RUN FULL CONTEXT PROMPTS ##################
|
2025-01-06 15:39:29 +00:00
|
|
|
ac_answers_dict = smart_chunking_funcs.run_full_context_prompts(
|
2024-12-06 18:19:58 +00:00
|
|
|
ac_answers_dict, full_context_fields, contract_text, filename
|
|
|
|
|
)
|
2024-11-18 21:01:09 +00:00
|
|
|
|
2024-10-28 23:39:36 +00:00
|
|
|
################## AC CONDITIONAL PROMPTS ##################
|
2025-01-06 15:39:29 +00:00
|
|
|
ac_answers_dict = smart_chunking_funcs.run_ac_conditional_prompts(
|
2024-12-06 18:19:58 +00:00
|
|
|
ac_answers_dict, contract_text, filename
|
|
|
|
|
)
|
2024-11-13 17:08:19 +00:00
|
|
|
|
2024-10-28 23:39:36 +00:00
|
|
|
################## CREATE OUTPUT DIRECTORIES ##################
|
|
|
|
|
base_filename = os.path.splitext(filename)[0].strip()
|
|
|
|
|
output_dir = os.path.join(config.OUTPUT_DIRECTORY, base_filename)
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
################## POSTPROCESS ##################
|
2024-11-04 22:54:31 +00:00
|
|
|
|
2024-11-13 17:08:19 +00:00
|
|
|
ac_df = pd.DataFrame([ac_answers_dict])
|
2024-12-06 18:19:58 +00:00
|
|
|
|
2024-12-05 22:46:41 +00:00
|
|
|
# print(f"Before ac_postprocess Unique values: {ac_df['CONTRACT_EFFECTIVE_DT'].unique()}")
|
2024-12-06 18:19:58 +00:00
|
|
|
|
2024-12-04 22:14:34 +00:00
|
|
|
ac_df = postprocess.ac_postprocess(ac_df, text_dict, filename, num_pages)
|
2024-10-28 23:39:36 +00:00
|
|
|
|
2024-12-05 22:46:41 +00:00
|
|
|
# print(f"After ac_postprocess Unique values: {ac_df['Contract Effective Date'].unique()}")
|
2024-12-06 18:19:58 +00:00
|
|
|
|
2024-10-28 23:39:36 +00:00
|
|
|
################## WRITE TO OUTPUT ##################
|
|
|
|
|
ac_df.to_csv(os.path.join(output_dir, config.AC_RESULTS_NAME), index=False)
|
|
|
|
|
|
2024-11-13 17:08:19 +00:00
|
|
|
chunk_file_path = "chunk_log.csv"
|
|
|
|
|
with open(chunk_file_path, "a", newline="", encoding="utf-8") as chunk_file:
|
|
|
|
|
fieldnames = [
|
|
|
|
|
"Contract name",
|
|
|
|
|
"Field group",
|
|
|
|
|
"Field list",
|
|
|
|
|
"Methodology",
|
|
|
|
|
"Case sensitivity",
|
|
|
|
|
"Page list",
|
|
|
|
|
"Chunk size",
|
|
|
|
|
"% Reduction",
|
|
|
|
|
]
|
|
|
|
|
writer = csv.DictWriter(chunk_file, fieldnames=fieldnames)
|
|
|
|
|
writer.writeheader()
|
|
|
|
|
writer.writerows(chunk_data)
|
|
|
|
|
|
|
|
|
|
print(f"Chunk log written to {chunk_file_path}")
|
|
|
|
|
|
|
|
|
|
return ac_answers_dict
|
2024-10-28 23:39:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_b_prompts(file_object):
|
|
|
|
|
################## INITIATE PROCESSING ##################
|
|
|
|
|
filename, contract_text = file_object
|
|
|
|
|
print(f"Processing B for {filename}...")
|
|
|
|
|
|
|
|
|
|
################## PREPROCESS ##################
|
2024-12-06 18:19:58 +00:00
|
|
|
text_dict, top_sheet_dict, exhibit_pages, num_pages, ac_chunks = (
|
|
|
|
|
preprocess.preprocess(contract_text, filename, fields="b")
|
2024-10-28 23:39:36 +00:00
|
|
|
)
|
|
|
|
|
print(f"B Preprocessing Complete - {filename}")
|
|
|
|
|
|
|
|
|
|
################## RUN BOTTOM UP PROMPTS ##################
|
2025-01-06 15:39:29 +00:00
|
|
|
bu_results = one_to_n_funcs.run_bottom_up(
|
2024-10-28 23:39:36 +00:00
|
|
|
filename, text_dict
|
|
|
|
|
) # Returns list of dictionaries
|
|
|
|
|
print(f"B Bottom Up Complete - {filename}")
|
|
|
|
|
|
|
|
|
|
################## RUN TOP DOWN PROMPTS ##################
|
2025-01-06 15:39:29 +00:00
|
|
|
combined_results = one_to_n_funcs.run_top_down(
|
2024-10-28 23:39:36 +00:00
|
|
|
filename, text_dict, bu_results, exhibit_pages
|
|
|
|
|
) # Returns list of dictionaries
|
|
|
|
|
print(f"B Top Down Complete - {filename}")
|
|
|
|
|
|
|
|
|
|
################## RUN CONDITIONAL PROMPTS ##################
|
|
|
|
|
conditional_results = conditional_funcs.run_conditional(
|
|
|
|
|
combined_results, text_dict, filename
|
|
|
|
|
) # List of dictionaries
|
|
|
|
|
print(f"B Conditional Prompts Complete - {filename}")
|
|
|
|
|
|
|
|
|
|
################## CREATE OUTPUT DIRECTORIES ##################
|
|
|
|
|
base_filename = os.path.splitext(filename)[0].strip()
|
|
|
|
|
output_dir = os.path.join(config.OUTPUT_DIRECTORY, base_filename)
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
################## WRITE UNPROCESSED OUTPUT ##################
|
|
|
|
|
combined_df = pd.DataFrame(conditional_results)
|
|
|
|
|
combined_df.to_csv(
|
|
|
|
|
os.path.join(output_dir, config.UNPROCESSED_RESULTS_NAME), index=False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
################## RUN POSTPROCESSING ##################
|
|
|
|
|
final_df = postprocess.b_postprocess(filename, combined_df, num_pages)
|
|
|
|
|
print(f"B Postprocessing Complete - {filename} ")
|
|
|
|
|
|
|
|
|
|
################## WRITE FINAL ##################
|
|
|
|
|
final_df.to_csv(os.path.join(output_dir, config.B_RESULTS_NAME), index=False)
|
|
|
|
|
print(f"B Output Complete - {filename} ")
|