ByteBrewer commited on
Commit
881eef4
·
verified ·
1 Parent(s): 5005b95

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_community.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
+ # Extracting the PDFs content...
18
+ # We go through each page of each PDF and extract the text of the pages...
19
+ def get_pdf_text(pdf_docs):
20
+ text=""
21
+ for pdf in pdf_docs:
22
+ doc = PdfReader(pdf)
23
+ for page in doc.pages:
24
+ text += page.extract_text()
25
+ return text
26
+
27
+
28
+ # Now the next step is to divide the texts to smaller chunks...
29
+ def get_text_chunks(text):
30
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
31
+ chunks = text_splitter.split_text(text)
32
+ return chunks
33
+
34
+
35
+ # Nextly, convert these chunks into vectors...
36
+ def get_vector_stores(text_chunks):
37
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
38
+ vector_stores = FAISS.from_texts(text_chunks, embedding=embeddings)
39
+ vector_stores.save_local("faiss-index")
40
+
41
+
42
+ def get_conversational_chain():
43
+
44
+ prompt_template = """
45
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
46
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
47
+ Context:\n {context}?\n
48
+ Question: \n{question}\n
49
+
50
+ Answer:
51
+ """
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
+
59
+ def user_input(user_question):
60
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
61
+
62
+ new_db = FAISS.load_local("faiss-index", embeddings, allow_dangerous_deserialization="True")
63
+ docs = new_db.similarity_search(user_question)
64
+
65
+ chain = get_conversational_chain()
66
+
67
+ response = chain({"input_documents":docs, "question": user_question} , return_only_outputs=True)
68
+
69
+ print(response)
70
+ st.write("Reply: ", response["output_text"])
71
+
72
+
73
+ def main():
74
+ st.set_page_config("Chat PDF")
75
+ st.header("Chat with PDF using Gemini💁")
76
+
77
+ user_question = st.text_input("Ask a Question from the PDF Files")
78
+
79
+ if user_question:
80
+ user_input(user_question)
81
+
82
+ with st.sidebar:
83
+ st.title("Menu:")
84
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
85
+ if st.button("Submit & Process"):
86
+ with st.spinner("Processing..."):
87
+ raw_text = get_pdf_text(pdf_docs)
88
+ text_chunks = get_text_chunks(raw_text)
89
+ get_vector_stores(text_chunks)
90
+ st.success("Done")
91
+
92
+
93
+ if __name__ == "__main__":
94
+ main()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ langchain_google_genai