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): 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() pages_to_be_loaded =[] if relevant_pages: page_numbers = relevant_pages.split(",") if len(page_numbers) != 0: for page_number in page_numbers: if page_number.isdigit(): pageIndex = int(page_number)-1 if pageIndex >=0 and pageIndex

Chatbot for PDFs - GPT-4

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

""" with gr.Blocks(css=css,theme=gr.themes.Monochrome()) as demo: with gr.Column(elem_id="col-container"): gr.HTML(title) with gr.Column(): openai_key = gr.Textbox(label="Your GPT-4 OpenAI API key", type="password") pdf_doc = gr.File(label="Load a pdf",file_types=['.pdf'],type='file') relevant_pages = gr.Textbox(label="*Optional - List comma separated page numbers to load or leave this field blank to use the entire PDF") with gr.Row(): status = gr.Textbox(label="Status", placeholder="", interactive=False) load_pdf = gr.Button("Upload PDF and generate embeddings").style(full_width=False) with gr.Row(): document_type = gr.Radio(['Deed of Trust', 'Transmittal Summary'], label="Select the Document Type") answers = gr.Dataframe(label="Answers to Predefined Question set") answers_for_predefined_question_set = gr.Button("Get gpt-4 answers to pre-defined question set").style(full_width=False) with gr.Row(): input = gr.Textbox(label="Type in your question") output = gr.Textbox(label="Answer") submit_query = gr.Button("Submit your own question to gpt-4").style(full_width=False) load_pdf.click(load_pdf_and_generate_embeddings, inputs=[pdf_doc, openai_key, relevant_pages], outputs=status) answers_for_predefined_question_set.click(answer_predefined_questions, document_type, answers) submit_query.click(answer_query,input,output) demo.launch()