afb6d5185d
Feature/lesser table caching refactor hybrid * chore: Remove unused duplicate main.py from shared pipeline * fix: Correct crosswalk paths in aarete_derived.py * chore: Remove unused documentation files from fieldExtraction * docs: Add documentation files to documentation folder * docs: Update README with uv setup, expanded project structure, and branching conventions * docs: Add uv installation steps with Ubuntu/WSL emphasis * Enable prompt caching for all remaining LLM calls - Add _INSTRUCTION() functions for: EXHIBIT_HEADER, EXHIBIT_LINKAGE, EXHIBIT_TITLE_MATCH, DATE_FIX, DERIVED_TERM_DATE, CHECK_PROVIDER_NAME_MATCH, SPECIAL_CASE_ASSIGNMENT - Update all invoke_claude() calls in saas and clover pipelines to use cache=True with corresponding _INSTRUCTION() functions - Add new instructions to get_cacheable_instructions() for cache warming - Update tests for new instruction functions Functions now using caching: - prompt_exhibit_level - prompt_exhibit_lesser (EXHIBIT_LEVEL_LESSER_OF) - prompt_fee_schedule_breakout - prompt_grouper_breakout - prompt_special_case_assignment - prompt_exhibit_linkage - prompt_exhibit_header - prompt_smart_chunked (ONE_TO_ONE templates) - prompt_date_fix - prompt_derived_term_date - prompt_exhibit_title_match - provider_name_match_check 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Reorder * feat: Add bcbs_promise client pipeline with OFFSET_TERM extraction - Add new bcbs_promise client with HSC-based OFFSET_TERM field extraction - Extract full paragraph text of offset/recoupment provisions from contracts - Derive OFFSET_INDICATOR (Y/N) from OFFSET_TERM presence - Fix reorder_columns to preserve extra columns not in COLUMN_ORDER - Update QC/QA output path to outputs/qc_qa/ * fix: Update dev deps and test assertions for QC/QA output path - Add pytest/pytest-mock to dev dependencies for mypy type checking - Update test assertions to expect outputs/qc_qa instead of qa_qc_output * style: Apply black formatting to prompt_templates.py * Merge main, move scripts * Archive some scripts * update py version * remove .py version file * Remove ASCII characters * Restore testbed code * restore tracking * Update testbed metrics * Enable prompt caching for CODE_LAST_CHECK, FILL_BILL_TYPE, DUAL_LOB_CHECK, and GROUPER_BREAKOUT - Add CODE_LAST_CHECK_INSTRUCTION() for service specificity classification - Add FILL_BILL_TYPE_INSTRUCTION() for bill type code determination - Add DUAL_LOB_CHECK_INSTRUCTION() for Medicare/Medicaid classification - Update code_funcs.py to use caching for CODE_LAST_CHECK, FILL_BILL_TYPE, GROUPER_BREAKOUT - Update postprocessing_funcs.py to use caching for DUAL_LOB_CHECK - Add new instructions to get_cacheable_instructions() for cache warming - Add unit tests for new instruction functions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix postprocessing_funcs to remove invalid columns * Merge branch 'main' into feature/lesser-table-caching-refactor-hybrid * Revert prompt caching changes from aed1b73c * update formatting * Update imports Approved-by: Sha Brown Approved-by: Praneel Panchigar
292 lines
9.9 KiB
Python
292 lines
9.9 KiB
Python
import json
|
|
import security
|
|
import streamlit as st
|
|
from streamlit_extras.add_vertical_space import add_vertical_space
|
|
import os
|
|
import streamlit as st
|
|
import pandas as pd
|
|
from io import StringIO
|
|
from datetime import datetime
|
|
import boto3
|
|
import util
|
|
import requests
|
|
from sf_conn import get_secret, save_to_sf
|
|
from io import StringIO, BytesIO
|
|
import time
|
|
from sf_conn import get_client_names, insert_upload_logs
|
|
from constants import USER_LIST
|
|
from util import logger
|
|
|
|
(REDIRECT_URI, create_batch_url, doczy_pipeline) = util.load_page_details(0)
|
|
|
|
user_list = USER_LIST
|
|
if "uploading" not in st.session_state:
|
|
st.session_state.uploading = False
|
|
if "upload_key" not in st.session_state:
|
|
st.session_state.upload_key = 0
|
|
if "file_list" not in st.session_state:
|
|
st.session_state.file_list = []
|
|
if "show_batchID" not in st.session_state:
|
|
st.session_state.show_batchID = False
|
|
if "landing_zone" not in st.session_state:
|
|
st.session_state.landing_zone = "contracts-landing-zone"
|
|
if "batch_id" not in st.session_state:
|
|
st.session_state.batch_id = "failed_cases"
|
|
if "client_bucket" not in st.session_state:
|
|
st.session_state.client_bucket = "default_bucket"
|
|
|
|
|
|
def insert_upload_logs(batch_id, client_name, file_name, upload_datetime, upload_user):
|
|
"""
|
|
Input: batch_id, client_name, file_name, upload_datetime, upload_user
|
|
Output: status of the insert query
|
|
"""
|
|
try:
|
|
return "Log inserted successfully"
|
|
except Exception as e:
|
|
return e
|
|
|
|
|
|
st.set_page_config(layout="wide")
|
|
# # # Sidebar contents
|
|
with st.sidebar:
|
|
st.title("Doczy.AI ™")
|
|
st.markdown(
|
|
"""
|
|
## About
|
|
This app extracts data from contracts
|
|
|
|
"""
|
|
)
|
|
add_vertical_space(15)
|
|
# st.write("Doczy")
|
|
|
|
# AARETE LOGO
|
|
x, y, z = st.columns([15, 2, 15])
|
|
with y:
|
|
st.image("aaretelogo.png")
|
|
|
|
hide_img_fs = """
|
|
<style>
|
|
button[title="View fullscreen"]{
|
|
visibility: hidden;}
|
|
</style>
|
|
"""
|
|
st.markdown(hide_img_fs, unsafe_allow_html=True)
|
|
|
|
_, c1 = st.columns([5, 1])
|
|
try:
|
|
util.setup_page(REDIRECT_URI)
|
|
except Exception as e:
|
|
st.write(f"SSO Failed = {e}")
|
|
st.session_state["user_info"] = {
|
|
"mail": "maamseek@aarete.com",
|
|
"displayName": "Mayank Aamseek",
|
|
} # RECOMMENDATION - Remove Line after development.
|
|
try:
|
|
c1.write(f"User: **{st.session_state.user_info['displayName']}**")
|
|
user_mail = st.session_state.user_info["mail"]
|
|
except (
|
|
KeyError
|
|
) as e: # RECOMMENDATION - Not Working as Expected. Unreachable Code because the above Exception handles everything
|
|
st.write("Session Expired.")
|
|
auth_url = security.get_auth_url(REDIRECT_URI)
|
|
st.markdown(
|
|
f"<a href='{auth_url}' target='_self'>Sign In</a>", unsafe_allow_html=True
|
|
)
|
|
st.stop()
|
|
|
|
s3_client = boto3.client(
|
|
"s3", # RECOMMENDATION - We can make use of AWS Access Key for enabling this in local testing. Will help a lot.
|
|
region_name="us-east-2",
|
|
)
|
|
|
|
client_list = [
|
|
"doczy-ai-client-1",
|
|
"Delaware First Health, Inc.",
|
|
"Community Health Choice, Inc",
|
|
"CareSource Network Partners LLC",
|
|
"HealthNet of Cali",
|
|
"Oklahoma Complete Health, Inc",
|
|
"HealthFirst",
|
|
"Molina Healthcare of TX",
|
|
"AvMed",
|
|
"Arizona Care1st",
|
|
"WellCare New Jersey",
|
|
]
|
|
|
|
# This is the list of client fetched from Snowflake
|
|
# TODO: Need to update the streamlit code to use the client names from this list
|
|
# And use the s3 paths to save the objects for the respective client
|
|
client_list, s3_paths = get_client_names()
|
|
logger.info(f"client_list={client_list}")
|
|
logger.info(f"s3_paths={s3_paths}")
|
|
client_s3_paths = dict(zip(client_list, s3_paths))
|
|
|
|
client_row = st.columns([0.1, 0.8])
|
|
with client_row[0]:
|
|
st.write("**Client Name**")
|
|
with client_row[1]:
|
|
client = st.selectbox(
|
|
"Client Name", (client_list), label_visibility="collapsed", index=None
|
|
)
|
|
|
|
client_bucket = client_s3_paths.get(client)
|
|
|
|
# to be deleted when client buckets are created
|
|
# client_bucket = 'doczyai-use2-d-cn1-s3-textract-processing-001'
|
|
|
|
file_row = st.columns([0.1, 0.8])
|
|
with file_row[0]:
|
|
st.write("**Upload Files**")
|
|
with file_row[1]:
|
|
file_list = st.file_uploader(
|
|
"Upload",
|
|
type=["docx", "tiff", "pdf"],
|
|
accept_multiple_files=True,
|
|
label_visibility="collapsed",
|
|
help="Only PDF, TIFF and DOCX file formats are supported.",
|
|
disabled=st.session_state.uploading,
|
|
key=st.session_state.upload_key,
|
|
)
|
|
|
|
add_vertical_space(2)
|
|
df = pd.DataFrame(columns=["Contract Name"])
|
|
df["Contract Name"] = file_list
|
|
file_names = []
|
|
buttons = st.columns([0.4, 0.4, 0.2])
|
|
|
|
|
|
def set_uploading_state():
|
|
if not client == None and not len(file_list) == 0:
|
|
st.session_state.file_list = file_list
|
|
st.session_state.upload_key += 1
|
|
st.session_state.uploading = True
|
|
|
|
|
|
with buttons[1]:
|
|
if st.button("Create Batch", on_click=set_uploading_state):
|
|
file_list = st.session_state.file_list
|
|
if client == None:
|
|
st.error("No Client Name Selected.")
|
|
elif len(file_list) == 0:
|
|
st.error("No Files Selected.")
|
|
else:
|
|
with st.spinner("Running..."):
|
|
myobj = {"client-bucket-name": client_bucket}
|
|
response = requests.post(create_batch_url, json=myobj)
|
|
if response.status_code >= 200 and response.status_code < 300:
|
|
try:
|
|
batch_id = json.loads(json.loads(response.text)["body"])[
|
|
"batch_id"
|
|
]
|
|
landing_zone = json.loads(json.loads(response.text)["body"])[
|
|
"landing_zone"
|
|
]
|
|
landing_zone = "contracts-landing-zone/"
|
|
except:
|
|
# st.write(myobj)
|
|
# st.write(response.text)
|
|
st.write("Internal Error. Reach out to Doczy.AI Team.")
|
|
batch_id = "failed_cases"
|
|
landing_zone = "contracts-landing-zone/"
|
|
else:
|
|
st.error("Failed")
|
|
for uploaded_file in file_list:
|
|
stringio = BytesIO(uploaded_file.getvalue())
|
|
stringio.seek(0)
|
|
s3_client.put_object(
|
|
Bucket=client_bucket,
|
|
Body=stringio.getvalue(),
|
|
Key=landing_zone + batch_id + "/" + str(uploaded_file.name),
|
|
)
|
|
s3_client.put_object_tagging(
|
|
Bucket=client_bucket,
|
|
Key=landing_zone + batch_id + "/" + str(uploaded_file.name),
|
|
Tagging={"TagSet": [{"Key": "BatchId", "Value": batch_id}]},
|
|
)
|
|
|
|
# TODO: Test this insert function with snowflake
|
|
upload_log = insert_upload_logs(
|
|
batch_id,
|
|
client,
|
|
str(uploaded_file.name),
|
|
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
user_mail,
|
|
)
|
|
st.write(upload_log)
|
|
|
|
file_names.append(str(uploaded_file.name))
|
|
st.session_state.uploading = False
|
|
st.session_state.show_batchID = True
|
|
st.session_state.client_bucket = client_bucket
|
|
st.session_state.batch_id = batch_id
|
|
st.session_state.landing_zone = landing_zone
|
|
st.rerun()
|
|
if st.session_state.show_batchID:
|
|
batch_id = st.session_state.batch_id
|
|
client_bucket = st.session_state.client_bucket
|
|
landing_zone = st.session_state.landing_zone
|
|
st.write("Batch ID Created - ")
|
|
st.code(f"{batch_id}")
|
|
st.write(f"Files uploaded to s3://{client_bucket}/{landing_zone}{batch_id}")
|
|
st.session_state.show_batchID = False
|
|
st.session_state.file_list = []
|
|
st.session_state.batch_id = "failed_cases"
|
|
st.session_state.client_bucket = "default_bucket"
|
|
st.session_state.landing_zone = "contracts-landing-zone"
|
|
|
|
# RECOMMENDATION - Rethink about how to best manage cache
|
|
# @st.cache_data
|
|
# def convert_df(df):
|
|
# return df.to_csv(index=False).encode('utf-8')
|
|
|
|
# csv = convert_df(df)
|
|
|
|
# df = df.reset_index() # make sure indexes pair with number of rows
|
|
|
|
# contract_list = []
|
|
# for index, row in df.iterrows():
|
|
# group_list = []
|
|
# if row['Unique Key']:
|
|
# group_list.append('Unique Key')
|
|
# if row['Pricing Before Carveouts']:
|
|
# group_list.append('Pricing Before Carveouts')
|
|
# if row['Contract Related']:
|
|
# group_list.append('Contract Related')
|
|
# if row['Provider']:
|
|
# group_list.append('Provider')
|
|
# if row['Timeline']:
|
|
# group_list.append('Timeline')
|
|
# if row['Carveout Indicator']:
|
|
# group_list.append('Carveout Indicator')
|
|
# if row['Carveout Methodology']:
|
|
# group_list.append('Carveout Methodology')
|
|
# entry_dict = {
|
|
# "contract_name": row['Contract Name'],
|
|
# "groups": group_list,
|
|
# "contract_source_path": "batches/batch_1/"+client+"/"+row['Contract Name']
|
|
# }
|
|
# contract_list.append(entry_dict)
|
|
# contract_list = list(df['Contract Name'])
|
|
|
|
# myobj = {
|
|
# "s3_bucket": 'doczy-dev-infra-textract',
|
|
# "batch_id": "1",
|
|
# "client_name": client,
|
|
# "username": user_mail,
|
|
# "contract_list": contract_list
|
|
# }
|
|
|
|
# buttons = st.columns([0.8, 0.2])
|
|
# with buttons[0]:
|
|
# st.download_button("Download Table", csv, "file.csv", "text/csv", key='download-csv')
|
|
# with buttons[1]:
|
|
# if st.button("Upload to DB"):
|
|
# response = requests.post(doczy_pipeline, json = myobj)
|
|
# if response.status_code >= 200 and response.status_code < 300:
|
|
# st.write("Success")
|
|
# else:
|
|
# st.write("Failed")
|
|
# # st.write(response.text)
|