File size: 13,812 Bytes
56696a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import streamlit as st
st.set_page_config(layout="wide")
from annotated_text import annotated_text, annotation
import fitz
import os
import chromadb
import uuid
from pathlib import Path
import os
os.environ['OPENAI_API_KEY'] = os.environ['OPEN_API_KEY']
st.title("Contracts Multiple File Search ")
import pandas as pd

from langchain.retrievers import BM25Retriever, EnsembleRetriever
from langchain.schema import Document
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
embedding = HuggingFaceEmbeddings(model_name='BAAI/bge-base-en-v1.5')
from FlagEmbedding import FlagReranker
reranker = FlagReranker('BAAI/bge-reranker-base')
import spacy
# Load the English model from SpaCy
nlp = spacy.load("en_core_web_md")

def util_upload_file_and_return_list_docs(uploaded_files):
    #util_del_cwd()
    list_docs = []
    list_save_path = []
    for uploaded_file in uploaded_files:
        save_path = Path(os.getcwd(), uploaded_file.name)
        with open(save_path, mode='wb') as w:
            w.write(uploaded_file.getvalue())
        #print('save_path:', save_path)
        docs = fitz.open(save_path) 
        list_docs.append(docs)
        list_save_path.append(save_path)
    return(list_docs, list_save_path)
#### Helper Functions to Split using Rolling Window (recomm : use smaller rolling window )
def split_txt_file_synthetic_sentence_rolling(ctxt, sentence_size_in_chars, sliding_size_in_chars,debug=False):
    sliding_size_in_chars = sentence_size_in_chars - sliding_size_in_chars
    pos_start = 0
    pos_end = len(ctxt)
    final_return = []
    if(debug):
        print('pos_start : ',pos_start)
        print('pos_end : ',pos_end)
    if(pos_end<sentence_size_in_chars):
        return([{'section_org_text':ctxt[pos_start:pos_end],'section_char_start':pos_start,'section_char_end':pos_end}])
    if(sentence_size_in_chars<sliding_size_in_chars):
        return(None)
    stop_condition = False
    start = pos_start
    end = start + sentence_size_in_chars
    mydict = {}
    mydict['section_org_text'] = ctxt[start:end]
    mydict['section_char_start'] = start
    mydict['section_char_end'] = end
    final_return.append(mydict)
    #### First Time ENDS
    while(stop_condition==False):
        start = end - sliding_size_in_chars
        end = start + sentence_size_in_chars
        if(end>pos_end):
            if(start<pos_end):
                end = pos_end
                mydict = {}
                mydict['section_org_text'] = ctxt[start:end]
                mydict['section_char_start'] = start
                mydict['section_char_end'] = end
                final_return.append(mydict)
                stop_condition=True
            else:
                stop_condition=True
        else:
            mydict = {}
            mydict['section_org_text'] = ctxt[start:end]
            mydict['section_char_start'] = start
            mydict['section_char_end'] = end
            final_return.append(mydict)
        if(debug):
            print('start : ', start)
            print('end : ', end)
    return(final_return)
### helper to make string out of iw_status
# def util_get_list_page_and_passage(docs):
#     page_documents = []
#     passage_documents = []
#     for txt_index, txt_page in enumerate(docs):
#         page_document = txt_page.get_text()##.encode("utf8") # get plain text (is in UTF-8)
#         page_documents.append(page_document)
#         sections = split_txt_file_synthetic_sentence_rolling(page_document,700,200) 
#         for sub_sub_index, sub_sub_item in enumerate(sections):
#             sub_text=sub_sub_item['section_org_text']
#             passage_document = Document(page_content=sub_text, metadata={"page_index": txt_index}) 
#             passage_documents.append(passage_document) 
#     return(page_documents,passage_documents)

def split_into_sentences_with_offsets(text):
    """
    Splits a paragraph into sentences and returns them along with their start and end offsets.
    :param text: The input text to be split into sentences.
    :return: A list of tuples, each containing a sentence and its start and end offsets.
    """
    doc = nlp(text)
    return [(sent.text, sent.start_char, sent.end_char) for sent in doc.sents]
    
def util_get_list_page_and_passage(list_docs, list_save_path):
    #page_documents = []
    passage_documents = []
    for ind_doc, docs in enumerate(list_docs):
        for txt_index, txt_page in enumerate(docs):
            page_document = txt_page.get_text()##.encode("utf8") # get plain text (is in UTF-8)
            #page_documents.append(page_document)
            sections = split_into_sentences_with_offsets(page_document) 
            for sub_sub_index, sub_sub_item in enumerate(sections):
                sub_text=sub_sub_item[0]
                passage_document = Document(page_content=sub_text, metadata={"page_content": page_document,"page_index": txt_index, "file_name" : str(list_save_path[ind_doc])}) 
                passage_documents.append(passage_document) 
    return(passage_documents)
    
# def util_index_chromadb_passages():
#     ##### PROCESSING
#     # create client and a new collection
#     collection_name = str(uuid.uuid4().hex)
#     chroma_client = chromadb.EphemeralClient()
#     chroma_collection = chroma_client.get_or_create_collection(collection_name)
#     # define embedding function
#     embed_model = LangchainEmbedding(HuggingFaceEmbeddings(model_name="BAAI/bge-small-en"))
#     vector_store = ChromaVectorStore(chroma_collection=chroma_collection)   
#     return(chroma_client,chroma_collection,collection_name,vector_store,embed_model)

def util_get_only_content_inside_loop(page_no,page_documents):
    for index, item in enumerate(page_documents):
        if(page_documents[index].metadata['txt_page_index']==page_no):
            return(page_documents[index].get_content())  
    return(None)
# def util_get_list_pageno_and_contents(page_documents,passage_documents,passage_nodes):
#     ''' page no starts with index 1 '''
#     return_value = []
#     for index, item in enumerate(passage_nodes):
#         page_no = passage_nodes[index].metadata['txt_page_index']
#         page_content = util_get_only_content_inside_loop(page_no,page_documents)
#         return_value.append((page_no+1,page_content))
#     return(return_value)

def util_get_list_pageno_and_contents(some_query_passage,passage_documents,passage_nodes):
    ''' page no starts with index 1 '''

    return_value = []
    rescore = reranker.compute_score([[some_query_passage , x.page_content] for x in passage_nodes])
    print('rescore :: ',rescore)
    tmp_array = []
    for i, x in enumerate(passage_nodes):
        tmp_dict = {"passage_content":x.page_content, 
                    "page_no":int(x.metadata['page_index'])+1, 
                    "page_content":str(x.metadata['page_content']), 
                    "file_name": str(x.metadata['file_name']), 
                    "score" : float(rescore[i])}
        tmp_array.append(tmp_dict)
    df = pd.DataFrame(tmp_array)
    df = df.sort_values(by='score', ascending=False)
    df = df.drop_duplicates(subset=['file_name'], keep='first')
    df = df[["passage_content","file_name","page_no","page_content","score"]]
    return(df)
    
# # def util_openai_extract_entity(example_passage, example_entity, page_content):
# #     import openai
# #     openai.api_key = os.environ['OPENAI_API_KEY']
    
# #     content = """Find the Entity based on Text . Return empty string if Entity does not exists. Here is one example below
# #     Text: """ + example_passage + """
# #     Entity: """ + example_entity + """
    
# #     Text: """ + page_content + """
# #     Entity: """
    
# #     return_value = openai.ChatCompletion.create(model="gpt-4",temperature=0.0001,messages=[{"role": "user", "content": content},])
# #     return(str(return_value['choices'][0]['message']['content']))
def util_openai_extract_clause(example_prompt, page_content):
    import openai
    openai.api_key = os.environ['OPENAI_API_KEY']
    content = example_prompt
    content = content + "\n Answer precisely; do not add anything extra, and try to locate the answer in the below context   \n context: "
    return_value = openai.ChatCompletion.create(model="gpt-3.5-turbo",temperature=0.0001,messages=[{"role": "user", "content": content + "\n" + page_content},])
    return(str(return_value['choices'][0]['message']['content']))


def util_openai_hyde(example_prompt):
    import openai
    openai.api_key = os.environ['OPENAI_API_KEY']
    content = example_prompt
    return_value = openai.ChatCompletion.create(model="gpt-3.5-turbo",temperature=0.0001,messages=[
        {"role": "system", "content": "You are a legal contract lawyer. generate a summary from below text " + "\n"},
        {"role": "user", "content": example_prompt + "\n"},
    
    ]
                                               )
    return(str(return_value['choices'][0]['message']['content']))

    
def util_openai_format (example_passage, page_content):
    '''
    annotated_text("  ",annotation("ENTITY : ", str(page_no)),)
    '''
    if(True):
        found_value = util_openai_extract_clause(example_passage, page_content)
        if(len(found_value)>0):
            found_value = found_value.strip()
            first_index = page_content.find(found_value)
            if(first_index!=-1):
                print('first_index : ',first_index)
                print('found_value : ',found_value)
                return(annotated_text(page_content[0:first_index-1],annotation(found_value, " FOUND ENTITY "),page_content[first_index+len(found_value):]))        
    return(annotated_text(page_content))
def util_openai_modify_prompt(example_prompt, page_content):
    import openai
    openai.api_key = os.environ['OPENAI_API_KEY']
    my_prompt = """Expand the original Query to show exact resuls for extraction\n
    Query: """ + example_prompt # + """\nDocument: """ + page_content + """ """
    return_value = openai.ChatCompletion.create(model="gpt-4",temperature=0.0001,messages=[{"role": "user", "content": my_prompt},])
    return(str(return_value['choices'][0]['message']['content']))    

# def create_bm25_page_rank(page_list_retrieve, page_query):
#     """ page_corpus : array of page text , page_query is user query """
#     from operator import itemgetter
#     from rank_bm25 import BM25Okapi
#     tokenized_corpus = [doc.split(" ") for x, doc in page_list_retrieve]
#     tokenized_query = page_query.split(" ")
#     bm25 = BM25Okapi(tokenized_corpus)
#     doc_scores = bm25.get_scores(tokenized_query).tolist()
#     tmp_list = []
#     for index, item in enumerate(page_list_retrieve):
#         tmp_list.append((item[0], item[1],doc_scores[index]))
#     tmp_list = sorted(tmp_list, key=itemgetter(2), reverse=True)
#     return(tmp_list)
    

passage_documents = []

if(True):
    with st.form("my_form"):
        multi = '''1. Download and Upload Multiple contracts (PDF)
        
        e.g. https://www.barc.gov.in/tenders/GCC-LPS.pdf
        
        e.g. https://www.montrosecounty.net/DocumentCenter/View/823/Sample-Construction-Contract
        '''
        st.markdown(multi)
        multi = '''2. Insert Query to search or find similar language '''
        st.markdown(multi)
        multi = '''3. Press Index.'''
        st.markdown(multi)
        multi = '''
        ** Attempt is made for appropriate page and passage retrieval ** \n
        '''
        st.markdown(multi)
        #uploaded_file = st.file_uploader("Choose a file")  
    
        list_docs = []
        list_save_path = []
        uploaded_files = st.file_uploader("Choose file(s)", accept_multiple_files=True)
        print('uploaded_files ', uploaded_files)
        
        
        single_example_passage = st.text_area('Enter Query or similar passage Here and press Chat',"What is Governing Law?")
        submitted = st.form_submit_button("Index and Answer")
        
        if submitted and (uploaded_files is not None):
            list_docs, list_save_path = util_upload_file_and_return_list_docs(uploaded_files)
            passage_documents = util_get_list_page_and_passage(list_docs, list_save_path)

        
        # st.button("Chat")
        # if st.button('Chat'):
            bm25_retriever = BM25Retriever.from_documents(passage_documents)
            bm25_retriever.k = 2
            chroma_vectorstore = Chroma.from_documents(passage_documents, embedding)
            chroma_retriever = chroma_vectorstore.as_retriever(search_kwargs={"k": 2})
            ensemble_retriever = EnsembleRetriever(retrievers=[bm25_retriever, chroma_retriever],weights=[0.25, 0.75])
            passage_nodes = ensemble_retriever.get_relevant_documents(single_example_passage)
            print('len(passage_nodes):', len(passage_nodes))
            df = util_get_list_pageno_and_contents(single_example_passage,passage_documents,passage_nodes)
            st.write(df)
        # print('len(page_list_retrieve):', len(page_list_retrieve))
        # if(len(page_list_retrieve)>0):
        #     page_list_retrieve = list(set(page_list_retrieve))
        #     for iindex in page_list_retrieve:
        #         page_no = iindex[0]
        #         page_content = iindex[1]
        #         annotated_text("  ",annotation("RELEVANT PAGENO : ", str(page_no), font_family="Comic Sans MS", border="2px dashed red"),)
        #         util_openai_format(single_example_passage, page_content)
        #         annotated_text("  ",annotation("RELEVANT PASSAGE : ", "", font_family="Comic Sans MS", border="2px dashed red"),)
        #         st.write(found_passage)
        # pchroma_client = chromadb.Client()
        # for citem in pchroma_client.list_collections():
        #     print(citem.name)