190 lines
7.2 KiB
Python
190 lines
7.2 KiB
Python
import json
|
|
|
|
import boto3
|
|
from langchain.prompts import PromptTemplate
|
|
from langchain.embeddings.bedrock import BedrockEmbeddings
|
|
from langchain.llms.bedrock import Bedrock
|
|
from langchain_community.vectorstores import Chroma
|
|
from constants import CHROMA_SETTINGS, EMBEDDING_MODEL_NAME, PERSIST_DIRECTORY, MODEL_ID, MODEL_BASENAME, SOURCE_DIRECTORY
|
|
from langchain.chains import RetrievalQA
|
|
|
|
import streamlit as st
|
|
from streamlit_extras.add_vertical_space import add_vertical_space
|
|
import os
|
|
import pandas as pd
|
|
import numpy as np
|
|
import util
|
|
import anthropic
|
|
from pydantic import BaseModel
|
|
from typing import List
|
|
import re
|
|
|
|
REDIRECT_URI = 'https://doczy.aarete.com:8502'
|
|
user_list = ['maamseek@aarete.com', 'smahdavian@aarete.com', 'ahinge@aarete.com', 'akadam@aarete.com', 'pkatariya@aarete.com'
|
|
, 'piragavarapu@aarete.com', 'umistry@aarete.com', 'ahutchison@aarete.com', 'bgrunst@aarete.com', 'ddimeglio@aarete.com'
|
|
, 'vnair@aarete.com', 'kminhas@aarete.com','dculotta@aarete.com','cbull@aarete.com','sclark@aarete.com', 'fmohiuddin@aarete.com', 'hupreti@aarete.com']
|
|
|
|
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")
|
|
|
|
_,c1= st.columns([5,1])
|
|
try:
|
|
util.setup_page(REDIRECT_URI)
|
|
except:
|
|
st.write("SSO Failed")
|
|
st.session_state['user_info'] = {'mail': 'maamseek@aarete.com', 'displayName': 'Mayank Aamseek'}
|
|
try:
|
|
c1.write(f"User: **{st.session_state.user_info['displayName']}**")
|
|
user_mail = st.session_state.user_info['mail']
|
|
except KeyError as e:
|
|
st.write("Session Expired.")
|
|
st.stop()
|
|
|
|
# remove below try except statement if comparison with actual vales is not required
|
|
try:
|
|
sf_secrets = json.loads(get_secret())
|
|
conn = snowflake.connector.connect(
|
|
user=sf_secrets.get('user'),
|
|
password=sf_secrets.get('password'),
|
|
account="aarete-doczyai",
|
|
role = "DEVADMIN",
|
|
warehouse="DEV_XS",
|
|
database="DOCZY_DEV",
|
|
schema="STG"
|
|
)
|
|
cur = conn.cursor()
|
|
query = 'select * from "TRAINING_DATA_RAW"'
|
|
cur.execute(query)
|
|
field_values = pd.DataFrame.from_records(iter(cur), columns=[x[0] for x in cur.description])
|
|
field_values['Document_Name'] = field_values['DOCUMENT_NAME']
|
|
except:
|
|
field_values = pd.read_csv('contract_field_values.csv', encoding='unicode_escape', skipinitialspace=True)
|
|
field_values = field_values.loc[:, ~field_values.columns.str.contains('Unnamed:')]
|
|
st.write("conn failed")
|
|
|
|
try:
|
|
query = 'select * from "PROMPT_CONFIG"'
|
|
cur.execute(query)
|
|
fields = pd.DataFrame(cur.fetchall())
|
|
fields.rename(columns={'FIELD_DESC': 'Field Name'}, inplace = True)
|
|
fields.rename(columns={'PROMPT': 'Interrogation Question?'}, inplace = True)
|
|
fields.rename(columns={'GROUP_ID': 'PRIORITY'}, inplace = True)
|
|
fields.rename(columns={'FIELD_NAME': 'SF_DB_COL_NAME'}, inplace = True)
|
|
fields.rename(columns={'FM_MODEL_ID': 'llm_selected'}, inplace = True)
|
|
error('table is empty')
|
|
except:
|
|
fields = pd.read_csv('contract_fields.csv', encoding='unicode_escape', skipinitialspace=True)
|
|
fields = fields[~fields['SF_COL_NAME'].str.endswith('_PG', na=None)]
|
|
|
|
# change the code below if contract list is fetched from snowflake
|
|
s3_client = boto3.client('s3',
|
|
region_name="us-east-2"
|
|
)
|
|
bucket = 'doczy-dev-infra-textract'
|
|
objects = s3_client.list_objects_v2(Bucket=bucket, Prefix="training-data/")
|
|
file_list = []
|
|
for obj in objects['Contents']:
|
|
if not obj['Key'].endswith('/'):
|
|
file_list.append(obj['Key'])
|
|
|
|
contract_list = sorted(file_list)
|
|
|
|
if user_mail in user_list:
|
|
|
|
file_row = st.columns([0.2, 0.7, 0.1])
|
|
with file_row[0]:
|
|
st.write("**Contract Name**")
|
|
with file_row[1]:
|
|
file_name = st.selectbox('Select a file', contract_list + ['All'], label_visibility = "collapsed")
|
|
|
|
field_row = st.columns([0.2, 0.7, 0.1])
|
|
with field_row[0]:
|
|
st.write("**Field Group**")
|
|
with field_row[1]:
|
|
field_group = st.selectbox('Field Group',('Unique Key', 'Contract Related', 'Pricing Before Carveouts - I'
|
|
, 'Pricing Before Carveouts - II', 'Carveout Indicator, Code Type and Code #s - I'
|
|
, 'Carveout Indicator, Code Type and Code #s - II', 'Carveout Indicator, Code Type and Code #s - III'
|
|
, 'Optimize Carving Indic.', 'Carveout Method - I', 'Carveout Method - II', 'Provider'
|
|
, 'Timeline'), label_visibility = "collapsed")
|
|
|
|
if field_group == 'Unique Key':
|
|
fields = fields[fields['PRIORITY'] == 'A']
|
|
elif field_group == 'Contract Related':
|
|
fields = fields[fields['PRIORITY'] == 'C']
|
|
elif field_group == 'Pricing Before Carveouts - I':
|
|
fields = fields[fields['PRIORITY'] == 'B']
|
|
fields = np.array_split(fields, 2)[0]
|
|
elif field_group == 'Pricing Before Carveouts - II':
|
|
fields = fields[fields['PRIORITY'] == 'B']
|
|
fields = np.array_split(fields, 2)[1]
|
|
elif field_group == 'Carveout Indicator, Code Type and Code #s - I':
|
|
fields = fields[fields['PRIORITY'] == 'F']
|
|
fields = np.array_split(fields, 3)[0]
|
|
elif field_group == 'Carveout Indicator, Code Type and Code #s - II':
|
|
fields = fields[fields['PRIORITY'] == 'F']
|
|
fields = np.array_split(fields, 3)[1]
|
|
elif field_group == 'Carveout Indicator, Code Type and Code #s - III':
|
|
fields = fields[fields['PRIORITY'] == 'F']
|
|
fields = np.array_split(fields, 3)[2]
|
|
elif field_group == 'Carveout Methodology - I':
|
|
fields = fields[fields['PRIORITY'] == 'G']
|
|
fields = np.array_split(fields, 4)[0]
|
|
elif field_group == 'Carveout Methodology - II':
|
|
fields = fields[fields['PRIORITY'] == 'G']
|
|
fields = np.array_split(fields, 4)[1]
|
|
elif field_group == 'Carveout Method - III':
|
|
fields = fields[fields['PRIORITY'] == 'G']
|
|
fields = np.array_split(fields, 4)[2]
|
|
elif field_group == 'Carveout Method - IV':
|
|
fields = fields[fields['PRIORITY'] == 'G']
|
|
fields = np.array_split(fields, 4)[3]
|
|
elif field_group == 'Provider':
|
|
fields = fields[fields['PRIORITY'] == 'D']
|
|
elif field_group == 'Timeline':
|
|
fields = fields[fields['PRIORITY'] == 'E']
|
|
|
|
if st.button("Show Results"):
|
|
|
|
# get this dataframe from snowflake table
|
|
df2 = pd.DataFrame(columns=['Contract Name','Field Name', 'SF_DB_COL_NAME', 'Snippet','Page Number'
|
|
, 'Field Extracted Value', 'Actual Value','Imputed Value'])
|
|
df2.to_csv('temp2.csv', index=False)
|
|
|
|
df2 = pd.read_csv('temp2.csv')
|
|
df2['Imputed Value'] = ''
|
|
edited_df = st.data_editor(df2)
|
|
|
|
@st.cache_data
|
|
def convert_df(df):
|
|
return df.to_csv(index=False).encode('utf-8')
|
|
|
|
csv = convert_df(edited_df)
|
|
|
|
buttons = st.columns(3)
|
|
with buttons[0]:
|
|
# st.button("Save All Imputations")
|
|
st.download_button("Download Table", csv, "file.csv", "text/csv", key='download-csv')
|
|
with buttons[1]:
|
|
# st.download_button("Download Table", csv, "file.csv", "text/csv", key='download-csv')
|
|
st.write("")
|
|
with buttons[2]:
|
|
if st.button("Kickoff Database Integration"):
|
|
st.write("Store in DB")
|
|
|
|
else:
|
|
st.write("Access Denied")
|
|
|
|
|
|
|