import gradio as gr import os import time from langchain.document_loaders import OnlinePDFLoader #for laoding the pdf from langchain.embeddings import OpenAIEmbeddings # for creating embeddings from langchain.vectorstores import Chroma # for the vectorization part from langchain.chains import RetrievalQA # for conversing with chatGPT from langchain.chat_models import ChatOpenAI # the LLM model we'll use (ChatGPT) from langchain import PromptTemplate def load_pdf_and_generate_embeddings(pdf_doc, open_ai_key): if openai_key is not None: os.environ['OPENAI_API_KEY'] = open_ai_key #Load the pdf file loader = OnlinePDFLoader(pdf_doc.name) pages = loader.load_and_split() #Create an instance of OpenAIEmbeddings, which is responsible for generating embeddings for text embeddings = OpenAIEmbeddings() #To create a vector store, we use the Chroma class, which takes the documents (pages in our case), the embeddings instance, and a directory to store the vector data vectordb = Chroma.from_documents(pages, embedding=embeddings) #Finally, we create the bot using the RetrievalQAChain class global pdf_qa prompt_template = """Use the following pieces of context to answer the question at the end. If you do not know the answer, just return the question followed by N/A. If you encounter a date, return it in mm/dd/yyyy format. {context} Question: {question} Return the key fields from the question followed by : and the answer :""" PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) chain_type_kwargs = {"prompt": PROMPT} pdf_qa = RetrievalQA.from_chain_type(llm=ChatOpenAI(temperature=0, model_name="gpt-4"),chain_type="stuff", retriever=vectordb.as_retriever(), chain_type_kwargs=chain_type_kwargs, return_source_documents=False) return "Ready" else: return "Please provide an OpenAI API key" def answer_predefined_questions(document_type): if document_type == "Deed of Trust": #Create a list of questions around the relevant fields of a Deed of Trust(DOT) document query0 = "what is the Lender's Name?" field0 = "Lender" query1 = "what is the Loan Number?" field1 = "Loan Number" query2 = "what is the Case Number?" field2 = "Case Number" query3 = "what is the VA/FHA Number?" field3 = "VA/FHA Number" query4 = "who is the Borrower?" field4 = "Borrower" query5 = "who is the Co-Borrower?" field5 = "Co-Borrower" query6 = "what is the Mortgage Identification number?" field6 = "MIN Number" query7 = "what is the property type - single fmaily, multi family?" field7 = "Property Type" query8 = "what is the Property Address?" field8 = "Property Address" query9 = "On what date was the Deed of Trust signed?" field9 = "Signed Date" query10 = "What is the Property County?" field10 = "Property County" query11 = "what is the Electronically recorded date" field11 = "Electronic Recording Date" queryList = [query0, query1, query2, query3, query4, query5, query6, query7, query8, query9, query10, query11] fieldList= [field0, field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11] elif document_type == "Transmittal Summary": #Create a list of questions around the relevant fields of a TRANSMITTAL SUMMARY document queryA0 = "who is the Borrower?" fieldA0 = "Borrower" queryA1 = "what is the Property Address?" fieldA1 = "Property Address" queryA2 = "who is the Co-Borrower?" fieldA2 = "Co-Borrower" queryA3 = "what is the loan term?" fieldA3 = "Loan Term" queryA4 = "What is the base income?" fieldA4 = "Base Income" queryA5 = "what is the original loan amount?" fieldA5 = "Original Loan Amount" queryA6 = "what is the Initial P&I Payment?" fieldA6 = "Initial P&I Payment" queryA7 = "what is the borrower's SSN?" fieldA7 = "Borrower SSN" queryA8 = "what is the co-borrower's SSN?" fieldA8 = "C0-Borrower SSN" queryA9 = "Number of units?" fieldA9 = "Number of units" queryA10 = "who is the seller?" fieldA10 = "Seller" queryA11 = "Document signed date?" fieldA11 = "Singed Date" queryList = [queryA0, queryA1, queryA2, queryA3, queryA4, queryA5, queryA6, queryA7, queryA8, queryA9, queryA10, queryA11] fieldList = [fieldA0, fieldA1, fieldA2, fieldA3, fieldA4, fieldA5, fieldA6, fieldA7, fieldA8, fieldA9, fieldA10, fieldA11] response="" i = 0 while i < len(queryList): question = queryList[i] field = fieldList[i] fieldInfo = "Field Name:", field response += fieldInfo questionInfo = "; Question sent to gpt-4: ", question response += questionInfo answer = pdf_qa.run(question) gptResponse = "; Response from gpt-4:", answer response += gptResponse return response def answer_query(query): question = query response = "Field Name: Location; Question sent to gpt-4: ", question, "Response from gpt-4:",pdf_qa.run(question) return response css=""" #col-container {max-width: 700px; margin-left: auto; margin-right: auto;} """ title = """
Upload a .PDF, click the "Load PDF" button,
Wait for the Status to show Ready, start typing your questions.
The app is built on GPT-4 and leverages PromptTemplate