Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chatmultipledocuments/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_API_KEY=
98 changes: 60 additions & 38 deletions chatmultipledocuments/chatpdf1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,34 @@
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv

# Load environment variables and configure Google API
load_dotenv()
os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))






def get_pdf_text(pdf_docs):
text=""
"""Extract text from uploaded PDF documents"""
text = ""
for pdf in pdf_docs:
pdf_reader= PdfReader(pdf)
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text+= page.extract_text()
return text


text += page.extract_text()
return text

def get_text_chunks(text):
"""Split text into manageable chunks"""
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
chunks = text_splitter.split_text(text)
return chunks


def get_vector_store(text_chunks):
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
"""Create and save FAISS vector store"""
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
vector_store.save_local("faiss_index")


def get_conversational_chain():

"""Create conversation chain with Gemini model"""
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
Expand All @@ -52,55 +47,82 @@ def get_conversational_chain():
Answer:
"""

model = ChatGoogleGenerativeAI(model="gemini-pro",
temperature=0.3)

prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)

return chain



def user_input(user_question):
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
"""Process user questions and generate responses"""
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")

new_db = FAISS.load_local("faiss_index", embeddings)
# Load the vector store with safe deserialization
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
docs = new_db.similarity_search(user_question)

chain = get_conversational_chain()


response = chain(
{"input_documents":docs, "question": user_question}
, return_only_outputs=True)

print(response)
st.write("Reply: ", response["output_text"])

{"input_documents": docs, "question": user_question},
return_only_outputs=True
)

return response["output_text"]

def initialize_session_state():
"""Initialize session state variables"""
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'processing_done' not in st.session_state:
st.session_state.processing_done = False

def main():
"""Main Streamlit application"""
initialize_session_state()

st.set_page_config("Chat PDF")
st.header("Chat with PDF using Gemini💁")

user_question = st.text_input("Ask a Question from the PDF Files")

if user_question:
user_input(user_question)

with st.sidebar:
st.title("Menu:")
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
pdf_docs = st.file_uploader(
"Upload your PDF Files and Click on the Submit & Process Button",
accept_multiple_files=True
)
if st.button("Submit & Process"):
with st.spinner("Processing..."):
raw_text = get_pdf_text(pdf_docs)
text_chunks = get_text_chunks(raw_text)
get_vector_store(text_chunks)
st.session_state.processing_done = True
st.success("Done")


# Chat interface
if 'processing_done' in st.session_state and st.session_state.processing_done:
# Display chat history
for message in st.session_state.chat_history:
with st.chat_message(message["role"]):
st.markdown(message["content"])

# Chat input
if prompt := st.chat_input("Ask a question about your documents"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)

# Add user message to chat history
st.session_state.chat_history.append({"role": "user", "content": prompt})

# Display assistant response in chat message container
with st.chat_message("assistant"):
response = user_input(prompt)
st.markdown(response)

# Add assistant response to chat history
st.session_state.chat_history.append({"role": "assistant", "content": response})
else:
st.info("Please upload PDF files and click 'Submit & Process' to start the conversation.")

if __name__ == "__main__":
main()
main()
Binary file added chatmultipledocuments/faiss_index/index.faiss
Binary file not shown.
Binary file added chatmultipledocuments/faiss_index/index.pkl
Binary file not shown.