import gradio as gr import os import time import pandas as pd import sqlite3 import ocrmypdf from langchain.document_loaders import OnlinePDFLoader #for laoding the pdf from langchain.embeddings import OpenAIEmbeddings # for creating embeddings from langchain.text_splitter import CharacterTextSplitter 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 open_ai_key is not None: os.environ['OPENAI_API_KEY'] = open_ai_key #OCR Conversion - skips conversion of pages that already contain text pdf_doc = ocr_converter(pdf_doc) #Load the pdf file loader = OnlinePDFLoader(pdf_doc) pages = loader.load_and_split() print('pages loaded:', len(pages)) #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

AskMoli - Chatbot for PDFs

Upload a .PDF, click the "Upload PDF and generate embeddings" button,
Wait for the Status to show Ready. You can choose to get answers to the pre-defined question set OR ask your own question
The app is built on GPT-4 and leverages the magic of PromptTemplate

""" with gr.Blocks(css=css,theme=gr.themes.Monochrome()) as demo: with gr.Column(elem_id="col-container"): gr.HTML(title) with gr.Tab("Chatbot"): with gr.Column(): open_ai_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(): summary = gr.Textbox(label="Summary") summarize_pdf = gr.Button("Have Moli Summarize the Contents").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 AskMoli").style(full_width=False) with gr.Row(): questionsets = gr.Dropdown(label="Pre-defined Question Sets stored in the DB", choices=[]) load_questionsets = gr.Button("Retrieve Pre-defined Question Sets from DB").style(full_width=False) fields_and_questions = gr.Dataframe(label="Fields and Questions in the chosen Question Set") load_fields_and_questions = gr.Button("Retrieve Pre-defined Questions from the DB for the chosen QuestionSet").style(full_width=False) with gr.Row(): answers = gr.Dataframe(label="Answers to Predefined Question set") answers_for_predefined_question_set = gr.Button("Get answers to the chosen pre-defined question set").style(full_width=False) with gr.Tab("OCR Converter"): with gr.Column(): image_pdf = gr.File(label="Load the pdf to be converted",file_types=['.pdf'],type='file') with gr.Row(): ocr_pdf = gr.File(label="OCR'd pdf", file_types=['.pdf'],type='file',file_count="single") convert_into_ocr = gr.Button("Convert").style(full_width=False) with gr.Tab("Upload Question Set"): with gr.Column(): document_types =["Mortgage 1040 US Individual Tax Returns 8453 Elec Form", "Mortgage 1098", "Mortgage 1099", "Mortgage Abstract", "Mortgage ACH Authorization Form", "Mortgage Advance Fee Agreement", "Mortgage Affidavit", "Mortgage Affidavit of Suspense Funds", "Mortgage Agreement Documents", "Mortgage Sales Contract", "Mortgage Loan Estimate", "Mortgage Alimony Or Child Support", "Mortgage Amended Proof Of Claim", "Mortgage Amortization Schedule", "Mortgage Flood Insurance", "Mortgage Appraisal Report", "Mortgage Appraisal Disclosure", "Mortgage ARM Letter", "Mortgage Arms Length Affidavit", "Mortgage Assignment-Recorded", "Mortgage Assignment-Unrecorded", "Mortgage Assignment of Rent or Lease", "Mortgage Automated Value Model", "Mortgage Award Letters", "Mortgage Bailee Letter", "Mortgage Balloon Disclosure", "Mortgage Bank Statement", "Mortgage Bankruptcy Documents", "Mortgage Bill of Sale", "Mortgage Billing Statement", "Mortgage Birth-Marriage-Death Certificate", "Mortgage Borrower Certification Authorization", "Mortgage Borrower Response Package", "Mortgage Brokers Price Opinion", "Mortgage Business Plan", "Mortgage Buydown Agreement", "Mortgage Bylaws Covenants Conditions Restrictions", "Mortgage Cash for Keys", "Mortgage Certificate of Redemption", "Mortgage Certificate of Sale", "Mortgage Certificate of Title", "Mortgage Certification of Amount Due Payoff Reinstatement", "Mortgage Checks-Regular or Cashiers", "Mortgage Closing Disclosure", "Mortgage Closing Protection Letter", "Mortgage Closing Other", "Mortgage Code Violations", "Mortgage Request for Release", "Mortgage Certificate of Liability Insurance", "Mortgage Commitment Letter", "Mortgage Complaint", "Mortgage Complaint Answer Counter Claim", "Mortgage Conditional Approval Letter", "Mortgage Conditional Commitment", "Mortgage Consent Order", "Mortgage Consolidated Mortgage CEMA", "Mortgage Conveyance Claims", "Mortgage Correction and Revision Agreement", "Mortgage Correspondence", "Mortgage Court Order Settlement Divorce Decree", "Mortgage Credit Report", "Mortgage Customer Signature Authorization", "Mortgage Debt Validation", "Mortgage Deed", "Mortgage Default Notices", "Mortgage Direct Debit Authorization Form", "Mortgage Disclosure Documents", "Mortgage Document Checklist", "Mortgage Document Correction and Fee Due Agreement", "Mortgage Dodd Frank Certification", "Mortgage Drivers License", "Mortgage Request for VOE", "Mortgage Environmental Indemnity Agreement", "Mortgage Equal Credit Opportunity Act Notice", "Mortgage Escrow Agreement", "Mortgage Escrow Analysis Trial Balance Worksheet", "Mortgage Instructions to Escrow Agent", "Mortgage Escrow Letters", "Mortgage Executed Deeds", "Mortgage Fair Lending Notice", "Mortgage Foreclosure Complaint", "Mortgage Foreclosure Judgement", "Mortgage Foreclosure Sale", "Mortgage FHA Neighborhood Watch", "Mortgage Truth-In-Lending Disclosure Statement", "Mortgage Financial Form", "Mortgage Financing Agreement", "Mortgage First Payment Letter", "Mortgage Forced Place Insurance Documents", "Mortgage Foreclosure Documents", "Mortgage Good Faith Estimate", "Mortgage Guaranty", "Mortgage HAMP Certifications", "Mortgage HOA-Condo Covenants and Dues", "Mortgage Exemption Hold Harmless Letter", "Mortgage Home Equity Signature Verification Card", "Mortgage Home Inspection", "Mortgage Property Liability Insurance", "Mortgage Homeowners Insurance Notice", "Mortgage HUD-1 Settlement Statement", "Mortgage Income Other", "Mortgage Indemnity Agreement", "Mortgage Informed Consumer Choice Disclosure Notice", "Mortgage Initial Escrow Account Disclosure Statement", "Mortgage Invoices", "Mortgage Land Lease or Land Trust", "Mortgage Land Title Adjustment", "Mortgage Last Will and Testament", "Mortgage Legal Description", "Mortgage Letters Of Administration", "Mortgage Letters of Testamentary", "Mortgage Listing Agreement", "Mortgage Litigation Guarantee", "Mortgage DIL Closing", "Mortgage Hardship Letter", "Mortgage Hardship Affidavit", "Mortgage Home Affordable Modification Agreement", "Mortgage Profit And Loss", "Mortgage Earnest Money Promissory Note", "Mortgage Rental Agreement", "Mortgage Repayment Plan", "Mortgage Short Sale Miscellaneous", "Mortgage LM - Trial Offer Letter or Plan", "Mortgage Errors and Omissions Agreement", "Mortgage Custom Type 2", "Mortgage Custom Type 1", "Mortgage Loan Agreement", "Mortgage Loan Closing Information Summary", "Mortgage Loan Modification", "Mortgage Loan Summary Report", "Mortgage Lock Confirmation", "Mortgage Loss Drafts", "Mortgage Loss Mitigation", "Mortgage Lost Assignment Affidavit", "Mortgage Mech Lien", "Mortgage Mediation", "Mortgage MI Claim Explanation of Benefits", "Mortgage MI Policy Cancellation Document", "Mortgage MI Repurchase Document", "Mortgage Miscellaneous Lien Release", "Mortgage Mobile Home Documentation", "Mortgage Monthly Activity Report", "Mortgage Deed of Trust-Recorded", "Mortgage PMI Disclosure", "Mortgage Payments", "Mortgage Deed of Trust-Unrecorded", "Mortgage Motion For Relief", "Mortgage Note", "Mortgage Note Affidavit", "Mortgage Note Endorsements", "Mortgage Notice Of Appearance", "Mortgage Notice of Default Filedrecorded", "Mortgage Notice of Final Cure", "Mortgage Notice of Levy", "Mortgage Notice of Payment Change", "Mortgage Notice of Right to Cancel", "Mortgage Notice of Sale", "Mortgage Notice of Second Lien", "Mortgage Notice of Servicing Transfer-Transferee", "Mortgage Notice of Servicing Transfer-Transferor", "Mortgage Notice of Termination", "Mortgage Notice to Quit", "Mortgage Objection to Claim", "Mortgage Processing and Underwriting Doc Set", "Mortgage Objection to Motion for Relief", "Mortgage Affidavit of Occupancy", "Mortgage Occupancy Agreement", "Mortgage Occupancy Termination Agreement", "Mortgage Ombudsman Documents", "Mortgage Owner Affidavit", "Mortgage Ownership and Encumbrances Report", "Mortgage Pay History External", "Mortgage Paystub", "Mortgage Payoff Demand Statement", "Mortgage PMI Certificate", "Mortgage Post Petition Fee Notices", "Mortgage Post Sale Documents", "Mortgage Power of Attorney-Recorded", "Mortgage Power of Attorney-Unrecorded", "Mortgage Closing Instructions", "Mortgage Preliminary Modification", "Mortgage Merged-Privacy Policy Notice-Title Policy - Privacy Policy-1098 Privacy Policy", "Mortgage Probate Court Order", "Mortgage Proof of Claim", "Mortgage Property Legal and Vesting Report", "Mortgage Property Management Agreement", "Mortgage Property Notices", "Mortgage Public Assistance", "Mortgage Record Owner and Lien Certificate", "Mortgage Recorded Satisfaction", "Mortgage Regfore Affidavit Executed", "Mortgage Release of Lis Pendens", "Mortgage REO Bids", "Mortgage REO Other", "Mortgage Form 26-1820 Report and Certificate of Loan Disbursement", "Mortgage Request for Verification of Rent or Mortgage", "Mortgage Request for Waiver of R.E. Tax Escrow Requirements", "Mortgage 1003", "Mortgage RMA Package", "Mortgage Sale Postponement", "Mortgage Sale or Milestone Rescission", "Mortgage Satisfaction of Judgement Tax Mortgage Liens", "Mortgage Security Agreement", "Mortgage Separation Agreement", "Mortgage Servicing Acquisition", "Mortgage Servicing Disclosure Statement", "Mortgage Short Payoffs", "Mortgage Signature-Name Affidavit", "Mortgage Assumption of Mortgage", "Mortgage SCRA Related Documents", "Mortgage Social Security Card or Customer ID", "Mortgage Soft Delete", "Mortgage Flood Hazard Determination Form", "Mortgage Stipulated Agreement", "Mortgage Subordination Agreement", "Mortgage Subordination Request Form", "Mortgage Appointment of Substitute Trustee", "Mortgage Merged-Real Estate Taxes-Tax Bill-Tax Certificate", "Mortgage Tax Certificate", "Mortgage Tax Record Information Sheet", "Mortgage Tax Liens", "Mortgage Tax Search", "Mortgage Third Party Authorization", "Mortgage Title Commitment-Equity or Property Report", "Mortgage Title Policy", "Mortgage Title Policy Endorsement", "Mortgage Title Search", "Mortgage Title Insurance Other", "Mortgage Transfer of Claim", "Mortgage Uniform Underwriting and Transmittal Summary", "Mortgage Trustee Sale Guarantee", "Mortgage UCC-1 Financing Statement", "Mortgage Others", "Mortgage Unknown", "Mortgage Utility Bill", "Mortgage Valuation Orders", "Mortgage Verification Document Set", "Mortgage Verification of Service for Military Home Buyers", "Mortgage W2", "Mortgage W9", "Mortgage Wire Transfer Instructions", "Mortgage Workmens Compensation", "Mortgage Writ of Possession", "Mortgage Cover Page", "Mortgage Barcode Page", "Mortgage Wisconsin Tax Escrow Option Notice", "Mortgage Hazard Insurance Declaration", "Mortgage Flood Insurance Declaration", "Mortgage Quitclaim Deed", "Mortgage Tax Deed", "Mortgage Warranty Deed", "Mortgage ALTA Settlement Statement", "Mortgage Home Inspection Waiver", "Mortgage Insurance Disclosure"] document_type_for_questionset = gr.Dropdown(choices=document_types, label="Select the Document Type") tag_for_questionset = gr.Textbox(label="Please provide a name for the question set. Ex: rwikd-dot-basic-questionset-20230707.") csv_file = gr.File(label="Load a csv - 2 columns with the headers as field, question",file_types=['.csv'],type='file') with gr.Row(): status_for_loading_csv = gr.Textbox(label="Status", placeholder="", interactive=False) load_csv = gr.Button("Upload data into the database").style(full_width=False) load_pdf.click(load_pdf_and_generate_embeddings, inputs=[pdf_doc, open_ai_key, relevant_pages], outputs=status) summarize_pdf.click(summarize_contents,outputs=summary) load_csv.click(load_csv_and_store_questionset_into_sqlite, inputs=[csv_file, document_type_for_questionset, tag_for_questionset], outputs=status_for_loading_csv) load_questionsets.click(retrieve_document_type_and_questionsettag_from_sqlite,outputs=questionsets) load_fields_and_questions.click(retrieve_fields_and_questions,questionsets,fields_and_questions) answers_for_predefined_question_set.click(answer_predefined_questions, questionsets, answers) convert_into_ocr.click(ocr_converter,image_pdf, ocr_pdf) submit_query.click(answer_query,input,output) #Use this flavor of demo.launch if you need the app to have an admin page. The credentials to login in this case #would be admin/lm0R!Rm0#97r #demo.launch(auth=("admin", "lm0R!Rm0#97r")) demo.launch(debug=True)