sagar-g commited on
Commit
a9f2414
·
verified ·
1 Parent(s): fb46333

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -1,11 +1,12 @@
 
1
  import streamlit as st
2
  from PyPDF2 import PdfReader
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
4
  import os
5
- import google.generativeai as genai
6
  from langchain_google_genai import GoogleGenerativeAIEmbeddings
7
- from langchain_google_genai import ChatGoogleGenerativeAI
8
  from langchain.vectorstores import FAISS
 
9
  from langchain.chains.question_answering import load_qa_chain
10
  from langchain.prompts import PromptTemplate
11
  from dotenv import load_dotenv
@@ -14,14 +15,13 @@ load_dotenv()
14
  os.getenv("GOOGLE_API_KEY")
15
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
 
17
-
18
  def get_pdf_text(pdf_docs):
19
- text = ""
20
  for pdf in pdf_docs:
21
- pdf_reader = PdfReader(pdf)
22
  for page in pdf_reader.pages:
23
- text += page.extract_text()
24
- return text
25
 
26
 
27
  def get_text_chunks(text):
@@ -31,12 +31,13 @@ def get_text_chunks(text):
31
 
32
 
33
  def get_vector_store(text_chunks):
34
- embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
35
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
36
  vector_store.save_local("faiss_index")
37
 
38
 
39
  def get_conversational_chain():
 
40
  prompt_template = """
41
  Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
42
  provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
@@ -46,32 +47,33 @@ def get_conversational_chain():
46
  Answer:
47
  """
48
 
49
- model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
50
- prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
51
  chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
 
52
  return chain
53
 
54
 
55
- def user_input(user_question):
56
- embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
57
-
58
- faiss_index_path = "faiss_index/index.faiss"
59
- if os.path.exists(faiss_index_path):
60
- new_db = FAISS.load_local("faiss_index", embeddings)
61
- else:
62
- st.error(f"Error: Could not find FAISS index file at {faiss_index_path}")
63
- return
64
 
 
 
 
 
65
  docs = new_db.similarity_search(user_question)
66
 
67
  chain = get_conversational_chain()
68
 
69
- response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
 
 
 
70
 
71
  print(response)
72
  st.write("Reply: ", response["output_text"])
73
 
74
 
 
 
75
  def main():
76
  st.set_page_config("Chat PDF")
77
  st.header("Chat with PDF 💁")
@@ -88,13 +90,11 @@ def main():
88
  with st.spinner("Processing..."):
89
  raw_text = get_pdf_text(pdf_docs)
90
  text_chunks = get_text_chunks(raw_text)
91
-
92
- if not os.path.exists("faiss_index"):
93
- os.makedirs("faiss_index")
94
-
95
  get_vector_store(text_chunks)
96
  st.success("Done")
97
 
98
 
 
99
  if __name__ == "__main__":
100
  main()
 
 
1
+
2
  import streamlit as st
3
  from PyPDF2 import PdfReader
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  import os
 
6
  from langchain_google_genai import GoogleGenerativeAIEmbeddings
7
+ import google.generativeai as genai
8
  from langchain.vectorstores import FAISS
9
+ from langchain_google_genai import ChatGoogleGenerativeAI
10
  from langchain.chains.question_answering import load_qa_chain
11
  from langchain.prompts import PromptTemplate
12
  from dotenv import load_dotenv
 
15
  os.getenv("GOOGLE_API_KEY")
16
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
17
 
 
18
  def get_pdf_text(pdf_docs):
19
+ text=""
20
  for pdf in pdf_docs:
21
+ pdf_reader= PdfReader(pdf)
22
  for page in pdf_reader.pages:
23
+ text+= page.extract_text()
24
+ return text
25
 
26
 
27
  def get_text_chunks(text):
 
31
 
32
 
33
  def get_vector_store(text_chunks):
34
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
35
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
36
  vector_store.save_local("faiss_index")
37
 
38
 
39
  def get_conversational_chain():
40
+
41
  prompt_template = """
42
  Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
43
  provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
 
47
  Answer:
48
  """
49
 
50
+ model = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.3)
51
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
52
  chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
53
+
54
  return chain
55
 
56
 
 
 
 
 
 
 
 
 
 
57
 
58
+ def user_input(user_question):
59
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
60
+
61
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
62
  docs = new_db.similarity_search(user_question)
63
 
64
  chain = get_conversational_chain()
65
 
66
+
67
+ response = chain(
68
+ {"input_documents":docs, "question": user_question}
69
+ , return_only_outputs=True)
70
 
71
  print(response)
72
  st.write("Reply: ", response["output_text"])
73
 
74
 
75
+
76
+
77
  def main():
78
  st.set_page_config("Chat PDF")
79
  st.header("Chat with PDF 💁")
 
90
  with st.spinner("Processing..."):
91
  raw_text = get_pdf_text(pdf_docs)
92
  text_chunks = get_text_chunks(raw_text)
 
 
 
 
93
  get_vector_store(text_chunks)
94
  st.success("Done")
95
 
96
 
97
+
98
  if __name__ == "__main__":
99
  main()
100
+