irfan4108 commited on
Commit
cdac0d2
·
verified ·
1 Parent(s): 90eb236

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +109 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #vector_store=""
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
+ print(type(chunks))
36
+ return chunks
37
+
38
+
39
+ def get_vector_store(text_chunks):
40
+ #global vector_store
41
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
42
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
43
+ vector_store.save_local("faiss_index")
44
+
45
+
46
+ def get_conversational_chain():
47
+
48
+ prompt_template = """
49
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
50
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
51
+ Context:\n {context}?\n
52
+ Question: \n{question}\n
53
+
54
+ Answer:
55
+ """
56
+
57
+ model = ChatGoogleGenerativeAI(model="gemini-pro",
58
+ temperature=0.3)
59
+
60
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
61
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
62
+
63
+ return chain
64
+
65
+
66
+
67
+ def user_input(user_question):
68
+ global vector_store
69
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
70
+
71
+ new_db = FAISS.load_local("faiss_index", embeddings)
72
+ docs = new_db.similarity_search(user_question)
73
+ #retriever = vector_store.as_retriever()
74
+ chain = get_conversational_chain()
75
+
76
+
77
+ response = chain(
78
+ {"input_documents":docs, "question": user_question}
79
+ , return_only_outputs=True)
80
+
81
+ print(response)
82
+ st.write("Reply: ", response["output_text"])
83
+
84
+
85
+
86
+
87
+ def main():
88
+ st.set_page_config("Chat PDF")
89
+ st.header("Chat with PDF using Gemini💁")
90
+
91
+ user_question = st.text_input("Ask a Question from the PDF Files")
92
+
93
+ if user_question:
94
+ user_input(user_question)
95
+
96
+ with st.sidebar:
97
+ st.title("Menu:")
98
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
99
+ if st.button("Submit & Process"):
100
+ with st.spinner("Processing..."):
101
+ raw_text = get_pdf_text(pdf_docs)
102
+ text_chunks = get_text_chunks(raw_text)
103
+ get_vector_store(text_chunks)
104
+ st.success("Done")
105
+
106
+
107
+
108
+ if __name__ == "__main__":
109
+ 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