shahed125238 commited on
Commit
0ae5d1e
·
verified ·
1 Parent(s): 8f3a1d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -42
app.py CHANGED
@@ -1,64 +1,106 @@
1
  import streamlit as st
2
  from PyPDF2 import PdfReader
3
- import google.generativeai as genai
4
  import os
 
 
 
 
 
 
5
  from dotenv import load_dotenv
6
 
7
- # Load API key from .env file
8
  load_dotenv()
 
9
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
10
 
 
 
 
 
 
11
  def get_pdf_text(pdf_docs):
12
- """Extracts text from uploaded PDF files."""
13
- text = ""
14
  for pdf in pdf_docs:
15
- pdf_reader = PdfReader(pdf)
16
  for page in pdf_reader.pages:
17
- text += page.extract_text()
18
- return text
19
-
20
- def get_gemini_response(question, context):
21
- """Sends question and context to Gemini for an answer."""
22
- try:
23
- prompt = f"Context:\n{context}\n\nQuestion:\n{question}\n\nAnswer:"
24
- st.write(f"Sending prompt to Gemini:\n{prompt}") # Debugging: Check prompt
25
- response = genai.ChatCompletion.create(
26
- model="gemini-pro",
27
- messages=[{"role": "user", "content": prompt}],
28
- )
29
- st.write(f"API Response:\n{response}") # Debugging: Check raw response
30
- if response and response['choices']:
31
- return response['choices'][0]['message']['content']
32
- else:
33
- return "No valid response from the API."
34
- except Exception as e:
35
- st.error(f"Error generating response: {e}")
36
- return "Error while generating response."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  def main():
39
- st.set_page_config(page_title="Chat with PDF")
40
- st.header("Chat with PDF using Gemini 💁")
41
 
42
- # Text input for user's question
43
  user_question = st.text_input("Ask a Question from the PDF Files")
44
 
45
- # PDF upload section
 
 
46
  with st.sidebar:
47
  st.title("Menu:")
48
- pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
49
-
50
- # Only process the PDFs if they are uploaded
51
- raw_text = ""
52
- if pdf_docs and st.button("Submit & Process"):
53
- with st.spinner("Processing PDFs..."):
54
  raw_text = get_pdf_text(pdf_docs)
55
- st.success("PDF content processed successfully!")
 
 
 
56
 
57
- # Ensure that both the question and PDF content are available before querying the model
58
- if user_question and raw_text:
59
- st.write("Fetching answer...")
60
- response = get_gemini_response(user_question, raw_text)
61
- st.write("Reply:", response)
62
 
63
  if __name__ == "__main__":
64
- main()
 
1
  import streamlit as st
2
  from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
  import os
5
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
6
+ import google.generativeai as genai
7
+ from langchain.vectorstores import FAISS
8
+ from langchain_google_genai import ChatGoogleGenerativeAI
9
+ from langchain.chains.question_answering import load_qa_chain
10
+ from langchain.prompts import PromptTemplate
11
  from dotenv import load_dotenv
12
 
 
13
  load_dotenv()
14
+ os.getenv("GOOGLE_API_KEY")
15
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
 
17
+
18
+
19
+
20
+
21
+
22
  def get_pdf_text(pdf_docs):
23
+ text=""
 
24
  for pdf in pdf_docs:
25
+ pdf_reader= PdfReader(pdf)
26
  for page in pdf_reader.pages:
27
+ text+= page.extract_text()
28
+ return text
29
+
30
+
31
+
32
+ def get_text_chunks(text):
33
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
34
+ chunks = text_splitter.split_text(text)
35
+ return chunks
36
+
37
+
38
+ def get_vector_store(text_chunks):
39
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
40
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
41
+ vector_store.save_local("faiss_index")
42
+
43
+
44
+ def get_conversational_chain():
45
+
46
+ prompt_template = """
47
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
48
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
49
+ Context:\n {context}?\n
50
+ Question: \n{question}\n
51
+
52
+ Answer:
53
+ """
54
+
55
+ model = ChatGoogleGenerativeAI(model="gemini-pro",
56
+ temperature=0.3)
57
+
58
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
59
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
60
+
61
+ return chain
62
+
63
+
64
+
65
+ def user_input(user_question):
66
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
67
+
68
+ new_db = FAISS.load_local("faiss_index", embeddings)
69
+ docs = new_db.similarity_search(user_question)
70
+
71
+ chain = get_conversational_chain()
72
+
73
+
74
+ response = chain(
75
+ {"input_documents":docs, "question": user_question}
76
+ , return_only_outputs=True)
77
+
78
+ print(response)
79
+ st.write("Reply: ", response["output_text"])
80
+
81
+
82
+
83
 
84
  def main():
85
+ st.set_page_config("Chat PDF")
86
+ st.header("Chat with PDF using Gemini💁")
87
 
 
88
  user_question = st.text_input("Ask a Question from the PDF Files")
89
 
90
+ if user_question:
91
+ user_input(user_question)
92
+
93
  with st.sidebar:
94
  st.title("Menu:")
95
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
96
+ if st.button("Submit & Process"):
97
+ with st.spinner("Processing..."):
 
 
 
98
  raw_text = get_pdf_text(pdf_docs)
99
+ text_chunks = get_text_chunks(raw_text)
100
+ get_vector_store(text_chunks)
101
+ st.success("Done")
102
+
103
 
 
 
 
 
 
104
 
105
  if __name__ == "__main__":
106
+ main()