shahed125238 commited on
Commit
01d6d5b
·
verified ·
1 Parent(s): 7025a91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
app.py CHANGED
@@ -1,45 +1,37 @@
1
  import gradio as gr
2
- import fitz # PyMuPDF
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 environment variables from .env file
14
- load_dotenv()
15
 
16
- # Configure the Google Generative AI
 
17
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
18
 
19
- def get_pdf_text(pdf_docs):
20
- """Extracts text from PDF files."""
21
  text = ""
22
- for pdf in pdf_docs:
23
- pdf_reader = fitz.open(pdf.name)
24
- for page in pdf_reader:
25
- text += page.get_text()
26
- pdf_reader.close()
27
  return text
28
 
29
  def get_text_chunks(text):
30
- """Splits the extracted text into chunks."""
31
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
32
  chunks = text_splitter.split_text(text)
33
  return chunks
34
 
35
  def get_vector_store(text_chunks):
36
- """Creates and saves a FAISS vector store from text chunks."""
37
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
38
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
39
  vector_store.save_local("faiss_index")
40
 
41
  def get_conversational_chain():
42
- """Returns a conversational chain with a prompt template."""
43
  prompt_template = """
44
  Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
45
  provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
@@ -52,41 +44,43 @@ def get_conversational_chain():
52
  model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
53
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
54
  chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
55
-
56
  return chain
57
 
58
- def user_input(user_question, pdf_docs):
59
- """Processes user input and returns a response."""
60
- raw_text = get_pdf_text(pdf_docs)
61
  text_chunks = get_text_chunks(raw_text)
62
  get_vector_store(text_chunks)
63
 
64
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
65
  new_db = FAISS.load_local("faiss_index", embeddings)
66
  docs = new_db.similarity_search(user_question)
67
-
68
  chain = get_conversational_chain()
69
  response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
70
 
71
  return response["output_text"]
72
 
73
  def main():
74
- """Main function to run the Gradio app."""
75
- with gr.Blocks() as demo:
76
- gr.Markdown("# Chat with PDF using Gemini💁")
77
 
78
  with gr.Row():
79
  with gr.Column():
80
- pdf_upload = gr.File(label="Upload PDF Files", type="file", file_count="multiple")
81
- question_input = gr.Textbox(label="Ask a Question from the PDF Files")
82
  submit_button = gr.Button("Submit & Process")
83
-
84
  with gr.Column():
85
- output_text = gr.Textbox(label="Reply", interactive=False)
 
 
 
 
 
86
 
87
- submit_button.click(fn=user_input, inputs=[question_input, pdf_upload], outputs=output_text)
88
 
89
- demo.launch()
90
 
91
  if __name__ == "__main__":
92
  main()
 
1
  import gradio as gr
2
+ from PyPDF2 import PdfReader
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
4
  from langchain_google_genai import GoogleGenerativeAIEmbeddings
5
  import google.generativeai as genai
6
  from langchain.vectorstores import FAISS
7
  from langchain_google_genai import ChatGoogleGenerativeAI
8
  from langchain.chains.question_answering import load_qa_chain
9
  from langchain.prompts import PromptTemplate
10
+ import os
 
 
 
11
 
12
+ # Set up environment variables
13
+ os.environ["GOOGLE_API_KEY"] = "your_google_api_key_here"
14
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
15
 
16
+ def get_pdf_text(pdf_files):
 
17
  text = ""
18
+ for pdf in pdf_files:
19
+ pdf_reader = PdfReader(pdf)
20
+ for page in pdf_reader.pages:
21
+ text += page.extract_text()
 
22
  return text
23
 
24
  def get_text_chunks(text):
 
25
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
26
  chunks = text_splitter.split_text(text)
27
  return chunks
28
 
29
  def get_vector_store(text_chunks):
 
30
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
31
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
32
  vector_store.save_local("faiss_index")
33
 
34
  def get_conversational_chain():
 
35
  prompt_template = """
36
  Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
37
  provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
 
44
  model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
45
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
46
  chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
 
47
  return chain
48
 
49
+ def process_pdf(pdf_files, user_question):
50
+ raw_text = get_pdf_text(pdf_files)
 
51
  text_chunks = get_text_chunks(raw_text)
52
  get_vector_store(text_chunks)
53
 
54
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
55
  new_db = FAISS.load_local("faiss_index", embeddings)
56
  docs = new_db.similarity_search(user_question)
57
+
58
  chain = get_conversational_chain()
59
  response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
60
 
61
  return response["output_text"]
62
 
63
  def main():
64
+ with gr.Blocks() as app:
65
+ gr.Markdown("## Chat with PDF using Gemini 💁")
 
66
 
67
  with gr.Row():
68
  with gr.Column():
69
+ pdf_files = gr.File(label="Upload your PDF Files", file_count="multiple", type="file")
70
+ user_question = gr.Textbox(label="Ask a Question from the PDF Files")
71
  submit_button = gr.Button("Submit & Process")
72
+
73
  with gr.Column():
74
+ output_text = gr.Textbox(label="Reply", lines=10)
75
+
76
+ def update_output(files, question):
77
+ if files and question:
78
+ return process_pdf(files, question)
79
+ return "Please upload PDFs and ask a question."
80
 
81
+ submit_button.click(fn=update_output, inputs=[pdf_files, user_question], outputs=output_text)
82
 
83
+ app.launch()
84
 
85
  if __name__ == "__main__":
86
  main()