princexoleo / ask_your_pdf

A streamlit based web application where you can upload a PDF and question & answering features added using LangChain

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Into to Gen AI Application

Tutorial: Chat with a PDF using Streamlit and Generative AI

This tutorial demonstrates how to build a web application using Streamlit that allows users to ask questions and receive answers derived from uploaded PDF documents. The application leverages the power of Google's Generative AI technology to process and understand the content within the PDFs.

Prerequisites:

Step-by-Step Guide:

  1. Set up the Environment:

    • Install the required libraries using pip install streamlit pypdf2 langchain genai faiss dotenv.

    • Create a Google Cloud project and enable the Generative AI API. Obtain an API key and store it securely using a .env file with the following content:

      GOOGLE_API_KEY=<YOUR_API_KEY_HERE>
      
  2. Code Breakdown:

    • The code imports necessary libraries for PDF processing, text chunking, vectorization, generative AI interaction, and building a question-answering chain.
    • The get_pdf_text function extracts text from uploaded PDF files.
    • The get_text_chunks function splits the extracted text into smaller chunks for efficient processing.
    • The get_vector_store function creates a vector store to represent the text chunks using embeddings from a pre-trained generative AI model. This step builds an index for faster information retrieval.
    • The get_conversational_chain function defines a prompt template and loads a question-answering chain using the generative AI model. This chain is responsible for answering user questions based on the provided context (PDF content).
    • The user_input function handles user queries. It retrieves relevant information from the vector store based on the user's question and feeds it along with the question to the question-answering chain. The chain then generates a response based on the retrieved context.
    • The main function creates a Streamlit web app interface. It allows users to upload PDF files, enter questions, and displays the generated answers.
  3. Running the Application:

    • Save the code as a Python script (e.g., pdf_chat.py).
    • Make sure the .env file is in the same directory.
    • Run the script using streamlit run pdf_chat.py.
    • This will launch the Streamlit app in your web browser.
  4. Interaction:

    • Upload one or more PDF files using the file uploader in the sidebar.
    • Click the "Submit & Process" button. This will process the uploaded PDFs and create the internal data structures.
    • In the main area, type your question related to the content of the uploaded PDFs and press Enter.
    • The application will analyze the question and provide an answer derived from the processed PDF documents.

Note:

  • This is a basic example and can be extended to support more features like highlighting relevant passages in the PDFs or providing different answer formats.
  • The accuracy of the answers depends on the quality of the uploaded PDFs and the chosen generative AI model.
  • Ensure proper handling of sensitive information within the PDFs.

About

A streamlit based web application where you can upload a PDF and question & answering features added using LangChain


Languages

Language:Python 100.0%