adi-123 commited on
Commit
ad57ebd
1 Parent(s): d684981

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports
2
+ import streamlit as st
3
+ from PyPDF2 import PdfReader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ import os
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 environment variables
15
+ load_dotenv()
16
+ os.getenv("GOOGLE_API_KEY")
17
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) # Configure Google Generative AI
18
+
19
+ # Extracts text from all pages of provided PDF documents
20
+ def get_pdf_text(pdf_docs):
21
+ text = ""
22
+ for pdf in pdf_docs:
23
+ pdf_reader = PdfReader(pdf)
24
+ for page in pdf_reader.pages:
25
+ text += page.extract_text()
26
+ return text
27
+
28
+ # Splits text into chunks of 10,000 characters with 1,000 character overlap
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
+ # Creates and saves a FAISS vector store from text chunks
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
+ # Creates and returns a conversational chain for question answering
41
+ def get_conversational_chain():
42
+ prompt_template = """
43
+ Answer the question concisely, focusing on the most relevant and important details from the PDF context.
44
+ Refrain from mentioning any mathematical equations, even if they are present in provided context.
45
+ Focus on the textual information available. Please provide direct quotations or references from PDF
46
+ to back up your response. If the answer is not found within the PDF,
47
+ please state "answer is not available in the context.\n\n
48
+
49
+ Context:\n {context}?\n
50
+ Question: \n{question}\n
51
+
52
+ Example response format:
53
+ - Overview: (brief summary or introduction)
54
+ - Key points:
55
+ (point 1: paragraph for main details)
56
+ (point 2: paragraph for main details)
57
+ ...
58
+
59
+ Use a mix of paragraphs and points to effectively convey the information.
60
+ """
61
+
62
+ # Adjust temperature parameter to lower value to:
63
+ # reduce model creativity & focus on factual accuracy
64
+ model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.2)
65
+
66
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
67
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
68
+
69
+ return chain
70
+
71
+ # Processes user question and provides a response
72
+ def user_input(user_question):
73
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
74
+
75
+ new_db = FAISS.load_local("faiss_index", embeddings)
76
+ docs = new_db.similarity_search(user_question)
77
+
78
+ chain = get_conversational_chain()
79
+
80
+ response = chain.invoke(
81
+ {"input_documents": docs, "question": user_question},
82
+ return_only_outputs=True
83
+ )
84
+
85
+ st.write("Reply: ", response["output_text"])
86
+
87
+ # Streamlit UI
88
+ def main():
89
+ st.set_page_config(page_title="Chat with PDFs", page_icon="")
90
+ st.header("Chat with multiple PDFs using AI 💬")
91
+
92
+ user_question = st.text_input("Ask a Question from PDF file(s)")
93
+
94
+ if user_question:
95
+ user_input(user_question)
96
+
97
+ with st.sidebar:
98
+ st.title("Menu ✨")
99
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button ",
100
+ accept_multiple_files=True)
101
+
102
+ if st.button("Submit & Process"):
103
+ with st.spinner("Processing..."):
104
+ raw_text = get_pdf_text(pdf_docs)
105
+ text_chunks = get_text_chunks(raw_text)
106
+ get_vector_store(text_chunks)
107
+ st.success("Done ✨")
108
+
109
+ if __name__ == "__main__":
110
+ main()