jarif commited on
Commit
c191ddc
·
verified ·
1 Parent(s): 0d69822

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -128
app.py CHANGED
@@ -1,128 +1,130 @@
1
- import streamlit as st
2
- from PyPDF2 import PdfReader
3
- from langchain.text_splitter import RecursiveCharacterTextSplitter
4
- from langchain_google_genai import GoogleGenerativeAIEmbeddings
5
- import google.generativeai as genai
6
- from langchain.vectorstores import FAISS
7
- from langchain_google_genai import ChatGoogleGenerativeAI
8
- from langchain.chains.question_answering import load_qa_chain
9
- from langchain.prompts import PromptTemplate
10
- from dotenv import load_dotenv
11
- import os
12
-
13
- # Load environment variables from .env file
14
- load_dotenv()
15
-
16
- # Fetch the Google API key from the .env file
17
- api_key = os.getenv("GOOGLE_API_KEY")
18
-
19
- # Set the page configuration for the Streamlit app
20
- st.set_page_config(page_title="Document Genie", layout="wide")
21
-
22
- # Header and Instructions
23
- st.markdown("""
24
- ## Document Genie: Get Instant Insights from Your Documents
25
-
26
- This chatbot utilizes the Retrieval-Augmented Generation (RAG) framework with Google's Generative AI model Gemini-PRO. It processes uploaded PDF documents by segmenting them into chunks, creating a searchable vector store, and generating precise answers to your questions. This method ensures high-quality, contextually relevant responses for an efficient user experience.
27
-
28
- ### How It Works
29
-
30
- 1. **Upload Your Documents**: You can upload multiple PDF files simultaneously for comprehensive analysis.
31
- 2. **Ask a Question**: After processing the documents, type your question related to the content of your uploaded documents for a detailed answer.
32
- """)
33
-
34
- def get_pdf_text(pdf_docs):
35
- """
36
- Extract text from uploaded PDF documents.
37
- """
38
- text = ""
39
- for pdf in pdf_docs:
40
- pdf_reader = PdfReader(pdf)
41
- for page in pdf_reader.pages:
42
- page_text = page.extract_text()
43
- if page_text:
44
- text += page_text
45
- return text
46
-
47
- def get_text_chunks(text):
48
- """
49
- Split text into manageable chunks for processing.
50
- """
51
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
52
- chunks = text_splitter.split_text(text)
53
- return chunks
54
-
55
- def get_vector_store(text_chunks, api_key):
56
- """
57
- Create and save a FAISS vector store from text chunks.
58
- """
59
- try:
60
- embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
61
- vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
62
- vector_store.save_local("faiss_index")
63
- st.success("FAISS index created and saved successfully.")
64
- except Exception as e:
65
- st.error(f"Error creating FAISS index: {e}")
66
-
67
- def get_conversational_chain(api_key):
68
- """
69
- Set up the conversational chain using the Gemini-PRO model.
70
- """
71
- prompt_template = """
72
- Answer the question as detailed as possible from the provided context. If the answer is not in the provided context,
73
- say "Answer is not available in the context". Do not provide incorrect information.\n\n
74
- Context:\n{context}\n
75
- Question:\n{question}\n
76
- Answer:
77
- """
78
- model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3, google_api_key=api_key)
79
- prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
80
- chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
81
- return chain
82
-
83
- def user_input(user_question, api_key):
84
- """
85
- Handle user input and generate a response from the chatbot.
86
- """
87
- embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
88
-
89
- try:
90
- new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
91
- docs = new_db.similarity_search(user_question)
92
- chain = get_conversational_chain(api_key)
93
- response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
94
- st.write("Reply:", response["output_text"])
95
- except ValueError as e:
96
- st.error(f"Error loading FAISS index or generating response: {e}")
97
-
98
- def main():
99
- """
100
- Main function to run the Streamlit app.
101
- """
102
- st.header("AI Chatbot 💁")
103
-
104
- user_question = st.text_input("Ask a Question from the PDF Files", key="user_question")
105
-
106
- if user_question: # Trigger user input function only if there's a question
107
- user_input(user_question, api_key)
108
-
109
- with st.sidebar:
110
- st.title("Menu:")
111
- pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True, key="pdf_uploader")
112
-
113
- if st.button("Submit & Process", key="process_button"):
114
- if not api_key:
115
- st.error("Google API key is missing. Please add it to the .env file.")
116
- return
117
-
118
- if pdf_docs:
119
- with st.spinner("Processing..."):
120
- raw_text = get_pdf_text(pdf_docs)
121
- text_chunks = get_text_chunks(raw_text)
122
- get_vector_store(text_chunks, api_key)
123
- st.success("Processing complete. You can now ask questions based on the uploaded documents.")
124
- else:
125
- st.error("No PDF files uploaded. Please upload at least one PDF file to proceed.")
126
-
127
- if __name__ == "__main__":
128
- main()
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
5
+ import google.generativeai as genai
6
+ from langchain.vectorstores import FAISS
7
+ from langchain_google_genai import ChatGoogleGenerativeAI
8
+ from langchain.chains.question_answering import load_qa_chain
9
+ from langchain.prompts import PromptTemplate
10
+ from dotenv import load_dotenv
11
+ import os
12
+
13
+ # Load environment variables from .env file
14
+ load_dotenv()
15
+
16
+ # Fetch the Google API key from the .env file
17
+ api_key = os.getenv("GOOGLE_API_KEY")
18
+
19
+ # Set the page configuration for the Streamlit app
20
+ st.set_page_config(page_title="DocWizard Instant Insights and Analysis", layout="wide")
21
+
22
+ # Header and Instructions
23
+ st.markdown("""
24
+ ## Document Intelligence Explorer: Real-Time Insights and Analysis
25
+
26
+ This chatbot utilizes the Retrieval-Augmented Generation (RAG) framework with Google's Generative AI model Gemini-PRO. It processes uploaded PDF documents by segmenting them into chunks, creating a searchable vector store, and generating precise answers to your questions. This method ensures high-quality, contextually relevant responses for an efficient user experience.
27
+
28
+ ### How It Works
29
+
30
+ 1. **Upload Your Documents**: You can upload multiple PDF files simultaneously for comprehensive analysis.
31
+ 2. **Ask a Question**: After processing the documents, type your question related to the content of your uploaded documents for a detailed answer.
32
+ """)
33
+
34
+ def get_pdf_text(pdf_docs):
35
+ """
36
+ Extract text from uploaded PDF documents.
37
+ """
38
+ text = ""
39
+ for pdf in pdf_docs:
40
+ pdf_reader = PdfReader(pdf)
41
+ for page in pdf_reader.pages:
42
+ page_text = page.extract_text()
43
+ if page_text:
44
+ text += page_text
45
+ return text
46
+
47
+ def get_text_chunks(text):
48
+ """
49
+ Split text into manageable chunks for processing.
50
+ """
51
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
52
+ chunks = text_splitter.split_text(text)
53
+ return chunks
54
+
55
+ def get_vector_store(text_chunks, api_key):
56
+ """
57
+ Create and save a FAISS vector store from text chunks.
58
+ """
59
+ try:
60
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
61
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
62
+ vector_store.save_local("faiss_index")
63
+ st.success("FAISS index created and saved successfully.")
64
+ except Exception as e:
65
+ st.error(f"Error creating FAISS index: {e}")
66
+
67
+ def get_conversational_chain(api_key):
68
+ """
69
+ Set up the conversational chain using the Gemini-PRO model.
70
+ """
71
+ prompt_template = """
72
+ Answer the question as detailed as possible from the provided context. If the answer is not in the provided context,
73
+ say "Answer is not available in the context". Do not provide incorrect information.\n\n
74
+ Context:\n{context}\n
75
+ Question:\n{question}\n
76
+ Answer:
77
+ """
78
+ model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3, google_api_key=api_key)
79
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
80
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
81
+ return chain
82
+
83
+ def user_input(user_question, api_key):
84
+ """
85
+ Handle user input and generate a response from the chatbot.
86
+ """
87
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
88
+
89
+ try:
90
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
91
+ docs = new_db.similarity_search(user_question)
92
+ chain = get_conversational_chain(api_key)
93
+ response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
94
+ st.write("Reply:", response["output_text"])
95
+ except ValueError as e:
96
+ st.error(f"Error loading FAISS index or generating response: {e}")
97
+
98
+ def main():
99
+ """
100
+ Main function to run the Streamlit app.
101
+ """
102
+ st.header("AI Chatbot 💁")
103
+
104
+ user_question = st.text_input("Ask a Question from the PDF Files", key="user_question")
105
+
106
+ if st.button("Generate Text", key="generate_button"): # Add a button to generate text
107
+ if user_question: # Trigger user input function only if there's a question
108
+ with st.spinner("Generating result..."): # Display spinner while generating
109
+ user_input(user_question, api_key)
110
+
111
+ with st.sidebar:
112
+ st.title("Menu:")
113
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True, key="pdf_uploader")
114
+
115
+ if st.button("Submit & Process", key="process_button"):
116
+ if not api_key:
117
+ st.error("Google API key is missing. Please add it to the .env file.")
118
+ return
119
+
120
+ if pdf_docs:
121
+ with st.spinner("Processing..."):
122
+ raw_text = get_pdf_text(pdf_docs)
123
+ text_chunks = get_text_chunks(raw_text)
124
+ get_vector_store(text_chunks, api_key)
125
+ st.success("Processing complete. You can now ask questions based on the uploaded documents.")
126
+ else:
127
+ st.error("No PDF files uploaded. Please upload at least one PDF file to proceed.")
128
+
129
+ if __name__ == "__main__":
130
+ main()