import gradio as gr import os import time import pandas as pd 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, relevant_pages='all'): 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() global vectordb if relevant_pages == 'all': #To create a vector store, we use the Chroma class, which takes the documents (pages in our case) and the embeddings instance vectordb = Chroma.from_documents(pages, embedding=embeddings) #Finally, we create the bot using the RetrievalQA 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 N/A. If you encounter a date, return it in mm/dd/yyyy format. {context} Question: {question} Return just 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(search_kwargs={"k": 5}), chain_type_kwargs=chain_type_kwargs, return_source_documents=False) return "Ready" else: return "Please provide an OpenAI gpt-4 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 query1 = "what is the Loan Number?" field1 = "Loan Number" query2 = "who is the Borrower?" field2 = "Borrower" query3 = "what is the Case Number?" field3 = "Case Number" query4 = "what is the Mortgage Identification number?" field4 = "MIN Number" query5 = "DOT signed date?" field5 = "Signed Date" elif document_type == "Transmittal Summary": #Create a list of questions around the relevant fields of a TRANSMITTAL SUMMARY document query1 = "who is the Borrower?" field1 = "Borrower" query2 = "what is the Property Address?" field2 = "Property Address" query3 = "what is the Loan Term?" field3 = "Loan Term" query4 = "What is the Base Income?" field4 = "Base Income" query5 = "what is the Borrower's SSN?" field5 = "Borrower SSN" else: return "Please choose your Document Type" queryList = [query1, query2, query3, query4, query5] fieldList = [field1, field2, field3, field4, field5] responseList =[] i = 0 while i < len(queryList): question = queryList[i] responseList.append(pdf_qa.run(question)) i = i+1 return pd.DataFrame({"Field": [fieldList[0], fieldList[1], fieldList[2], fieldList[3], fieldList[4]], "Question to gpt-4": [queryList[0], queryList[1], queryList[2], queryList[3], queryList[4]], "Response from gpt-4": [responseList[0],responseList[1],responseList[2],responseList[3],responseList[4]]}) def answer_query(query): question = query return pdf_qa.run(question) css=""" #col-container {max-width: 700px; margin-left: auto; margin-right: auto;} """ title = """
Upload a .PDF, click the "Upload PDF and generate embeddings" button,
Wait for the Status to show Ready. You can chose to get answers to the pre-defined question set OR ask your own question
The app is built on GPT-4 and leverages PromptTemplate