avengers27 commited on
Commit
ca2222c
1 Parent(s): c7d29d1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_community.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
+ load_dotenv()
15
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))#config api
16
+
17
+ def get_pdf_text(pdf_docs):
18
+ text=""
19
+ for pdf in pdf_docs:
20
+ pdf_reader=PdfReader(pdf)
21
+ for page in pdf_reader.pages:
22
+ text+=page.extract_text()
23
+ return text
24
+
25
+
26
+ def get_text_chunks(text):
27
+ text_splitter=RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
28
+ chunks=text_splitter.split_text(text)
29
+ return chunks
30
+
31
+ def get_vector_store(text_chunks):
32
+ embeddings=GoogleGenerativeAIEmbeddings(model="models/embedding-001")
33
+ vector_store=FAISS.from_texts(text_chunks,embedding=embeddings)
34
+ vector_store.save_local("faiss_index")
35
+
36
+ def get_conversational_chain():
37
+ prompt_template="""
38
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
39
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
40
+ Context:\n {context}?\n
41
+ Question: \n{question}\n
42
+
43
+ Answer:
44
+ """
45
+
46
+ model = ChatGoogleGenerativeAI(model="gemini-pro",
47
+ temperature=0.3)
48
+
49
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
50
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
51
+
52
+ return chain
53
+
54
+
55
+ def user_input(user_question):
56
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
57
+
58
+ new_db = FAISS.load_local("faiss_index", embeddings)
59
+ docs = new_db.similarity_search(user_question)
60
+
61
+ chain = get_conversational_chain()
62
+
63
+
64
+ response = chain(
65
+ {"input_documents":docs, "question": user_question}
66
+ , return_only_outputs=True)
67
+
68
+ print(response)
69
+ st.write("Reply: ", response["output_text"])
70
+
71
+
72
+
73
+
74
+ def main():
75
+ st.set_page_config("Chat PDF")
76
+ st.header("Chat with PDF using Gemini💁")
77
+
78
+ user_question = st.text_input("Ask a Question from the PDF Files")
79
+
80
+ if user_question:
81
+ user_input(user_question)
82
+
83
+ with st.sidebar:
84
+ st.title("Menu:")
85
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
86
+ if st.button("Submit & Process"):
87
+ with st.spinner("Processing..."):
88
+ raw_text = get_pdf_text(pdf_docs)
89
+ text_chunks = get_text_chunks(raw_text)
90
+ get_vector_store(text_chunks)
91
+ st.success("Done")
92
+
93
+
94
+
95
+ if __name__ == "__main__":
96
+ main()
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+