shahed125238 commited on
Commit
b864758
·
verified ·
1 Parent(s): 022e8d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -1,21 +1,15 @@
1
  import streamlit as st
2
  from PyPDF2 import PdfReader
3
- from langchain.text_splitter import RecursiveCharacterTextSplitter
4
  import os
5
  from dotenv import load_dotenv
6
 
7
- # Lazy import for other large libraries
8
- def load_dependencies():
9
- import google.generativeai as genai
10
- from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
11
- from langchain.vectorstores import FAISS
12
- from langchain.chains.question_answering import load_qa_chain
13
- from langchain.prompts import PromptTemplate
14
-
15
  load_dotenv()
16
- 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)
@@ -23,18 +17,33 @@ def get_pdf_text(pdf_docs):
23
  text += page.extract_text()
24
  return text
25
 
26
- def user_input(user_question):
27
- load_dependencies() # Load the necessary libraries only when needed
28
- from langchain_google_genai import GoogleGenerativeAIEmbeddings
29
- from langchain.vectorstores import FAISS
 
 
 
 
 
 
 
 
30
 
31
- embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
32
- new_db = FAISS.load_local("faiss_index", embeddings)
33
- docs = new_db.similarity_search(user_question)
 
34
 
35
- chain = get_conversational_chain()
 
 
 
36
 
37
- response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
38
- st.write("Reply: ", response["output_text"])
 
 
39
 
40
- # The rest of the code remains the same
 
 
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)
 
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
+ prompt = f"Context:\n{context}\n\nQuestion:\n{question}\n\nAnswer:"
23
+ response = genai.generate_text(prompt=prompt)
24
+ return response.generations[0].text
25
+
26
+ def main():
27
+ st.set_page_config(page_title="Chat with PDF")
28
+ st.header("Chat with PDF using Gemini 💁")
29
+
30
+ # Text input for user's question
31
+ user_question = st.text_input("Ask a Question from the PDF Files")
32
 
33
+ # PDF upload section
34
+ with st.sidebar:
35
+ st.title("Menu:")
36
+ pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
37
 
38
+ if pdf_docs and st.button("Submit & Process"):
39
+ with st.spinner("Processing PDFs..."):
40
+ raw_text = get_pdf_text(pdf_docs)
41
+ st.success("PDF content processed successfully!")
42
 
43
+ if user_question and pdf_docs:
44
+ st.write("Fetching answer...")
45
+ response = get_gemini_response(user_question, raw_text)
46
+ st.write("Reply:", response)
47
 
48
+ if __name__ == "__main__":
49
+ main()