DA1212 commited on
Commit
c041fcf
·
verified ·
1 Parent(s): a7f1bf6

create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ import os
5
+
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
13
+
14
+
15
+ load_dotenv()
16
+
17
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
18
+
19
+
20
+ #we read the pages from the pdf and extract the information
21
+ def get_pdf_text(pdf_docs):
22
+ text=""
23
+ for pdf in pdf_docs:
24
+ pdf_reader= PdfReader(pdf)
25
+ for page in pdf_reader.pages: ##taking the pages from the pdf readen
26
+ text+= page.extract_text() ##text=text+page.extract_text
27
+ return text
28
+
29
+
30
+ def get_text_chunks(text):
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
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
37
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
38
+ vector_store.save_local("faiss_index")
39
+
40
+ ##chain_type=stuff is internal text summarization
41
+ def get_conversational_chain():
42
+
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
46
+ Context:\n {context}?\n
47
+ Question: \n{question}\n
48
+
49
+ Answer:
50
+ """
51
+
52
+ model = ChatGoogleGenerativeAI(model="gemini-pro",
53
+ temperature=0.3)
54
+
55
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
56
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
57
+
58
+ return chain
59
+
60
+
61
+
62
+ def user_input(user_question):
63
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
64
+
65
+ new_db = FAISS.load_local("faiss_index", embeddings)
66
+ docs = new_db.similarity_search(user_question)
67
+
68
+ chain = get_conversational_chain()
69
+
70
+
71
+ response = chain(
72
+ {"input_documents":docs, "question": user_question}
73
+ , return_only_outputs=True)
74
+
75
+ print(response)
76
+ st.write("Reply: ", response["output_text"])
77
+
78
+
79
+ def main():
80
+ st.set_page_config("Chat PDF")
81
+ st.header("Chat with PDF using Gemini💁")
82
+
83
+ user_question = st.text_input("Ask a Question from the PDF Files")
84
+
85
+ if user_question:
86
+ user_input(user_question)
87
+
88
+ with st.sidebar:
89
+ st.title("Menu:")
90
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
91
+ if st.button("Submit & Process"):
92
+ with st.spinner("Processing..."):
93
+ raw_text = get_pdf_text(pdf_docs)
94
+ text_chunks = get_text_chunks(raw_text)
95
+ get_vector_store(text_chunks)
96
+ st.success("Done")
97
+
98
+
99
+
100
+ if __name__ == "__main__":
101
+ main()