|
import streamlit as st |
|
from PyPDF2 import PdfReader |
|
from langchain_text_splitters import RecursiveCharacterTextSplitter |
|
import os |
|
from langchain_google_genai import GoogleGenerativeAIEmbeddings |
|
from langchain_community.vectorstores import Chroma |
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
from langchain.chains.question_answering import load_qa_chain |
|
from langchain.prompts import PromptTemplate |
|
from langchain_community.document_loaders import PyPDFLoader |
|
from langchain_chroma import Chroma |
|
import tempfile |
|
|
|
st.set_page_config(page_title="Document Genie", layout="wide") |
|
|
|
st.markdown(""" |
|
## Document Genie: Get instant insights from your Documents |
|
|
|
This chatbot is built using the Retrieval-Augmented Generation (RAG) framework, leveraging Google's Generative AI model Gemini-PRO. It processes uploaded PDF documents by breaking them down into manageable chunks, creates a searchable vector store, and generates accurate answers to user queries. This advanced approach ensures high-quality, contextually relevant responses for an efficient and effective user experience. |
|
|
|
### How It Works |
|
|
|
Follow these simple steps to interact with the chatbot: |
|
|
|
1. **Upload Your Documents**: The system accepts multiple PDF files at once, analyzing the content to provide comprehensive insights. |
|
|
|
2. **Ask a Question**: After processing the documents, ask any question related to the content of your uploaded documents for a precise answer. |
|
""") |
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_pdf(pdf_file): |
|
if pdf_file: |
|
temp_dir = tempfile.mkdtemp() |
|
path = os.path.join(temp_dir, pdf_file.name) |
|
|
|
loader = PyPDFLoader(path) |
|
docs = loader.load() |
|
return docs |
|
|
|
def text_splitter(text): |
|
text_splitter = RecursiveCharacterTextSplitter( |
|
|
|
chunk_size=500, |
|
chunk_overlap=20, |
|
separators=["\n\n","\n"," ",".",","]) |
|
chunks=text_splitter.split_text(text) |
|
return chunks |
|
|
|
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") |
|
|
|
def get_conversational_chain(): |
|
prompt_template = """ |
|
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in |
|
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n |
|
Context:\n {context}?\n |
|
Question: \n{question}\n |
|
|
|
Answer: |
|
""" |
|
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3, google_api_key=GOOGLE_API_KEY) |
|
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) |
|
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt) |
|
return chain |
|
|
|
def embedding(chunk,query): |
|
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001") |
|
db = Chroma.from_documents(chunk,embeddings, persist_directory="./chroma_db") |
|
docs = db3.similarity_search(query) |
|
chain = get_conversational_chain() |
|
response = chain({"input_documents": docs, "question": query}, return_only_outputs=True) |
|
st.write("Reply: ", response["output_text"]) |
|
|
|
|
|
|
|
|
|
def main(): |
|
st.header("Chat with your pdf💁") |
|
st.title("Menu:") |
|
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=False, key="pdf_uploader") |
|
query = st.text_input("Ask a Question from the PDF Files", key="query") |
|
if st.button("Submit & Process", key="process_button"): |
|
with st.spinner("Processing..."): |
|
raw_text = get_pdf(pdf_docs) |
|
text_chunks = text_splitter(raw_text) |
|
if query: |
|
embedding(text_chunks,query) |
|
st.success("Done") |
|
|
|
if __name__ == "__main__": |
|
main() |