Upload 4 files
Browse files- .env +1 -0
- app.py +94 -0
- faiss_index +0 -0
- requirements.txt +0 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GOOGLE_API_KEY="AIzaSyC61X0LJWGJLcZ8AiOvPqaA9mrl66hN5Xw"
|
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 the 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 |
+
st.set_page_config(page_title="Document Genie", layout="wide")
|
20 |
+
|
21 |
+
st.markdown("""
|
22 |
+
## Document Genie: Get instant insights from your Documents
|
23 |
+
|
24 |
+
This chatbot is built using the Retrieval-Augmented Generation (RAG) framework, leveraging Google's Generative AI model Gemini-PRO. It processes uploaded PDF documents by breaking them down into manageable chunks, creates a searchable vector store, and generates accurate answers to user queries. This advanced approach ensures high-quality, contextually relevant responses for an efficient and effective user experience.
|
25 |
+
|
26 |
+
### How It Works
|
27 |
+
|
28 |
+
Follow these simple steps to interact with the chatbot:
|
29 |
+
|
30 |
+
1. **Upload Your Documents**: The system accepts multiple PDF files at once, analyzing the content to provide comprehensive insights.
|
31 |
+
|
32 |
+
2. **Ask a Question**: After processing the documents, ask any question related to the content of your uploaded documents for a precise answer.
|
33 |
+
""")
|
34 |
+
|
35 |
+
def get_pdf_text(pdf_docs):
|
36 |
+
text = ""
|
37 |
+
for pdf in pdf_docs:
|
38 |
+
pdf_reader = PdfReader(pdf)
|
39 |
+
for page in pdf_reader.pages:
|
40 |
+
text += page.extract_text()
|
41 |
+
return text
|
42 |
+
|
43 |
+
def get_text_chunks(text):
|
44 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
45 |
+
chunks = text_splitter.split_text(text)
|
46 |
+
return chunks
|
47 |
+
|
48 |
+
def get_vector_store(text_chunks, api_key):
|
49 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
50 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
51 |
+
vector_store.save_local("faiss_index")
|
52 |
+
|
53 |
+
def get_conversational_chain():
|
54 |
+
prompt_template = """
|
55 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
56 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
57 |
+
Context:\n {context}?\n
|
58 |
+
Question: \n{question}\n
|
59 |
+
|
60 |
+
Answer:
|
61 |
+
"""
|
62 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3, google_api_key=api_key)
|
63 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
64 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
65 |
+
return chain
|
66 |
+
|
67 |
+
def user_input(user_question, api_key):
|
68 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
69 |
+
new_db = FAISS.load_local("faiss_index", embeddings)
|
70 |
+
docs = new_db.similarity_search(user_question)
|
71 |
+
chain = get_conversational_chain()
|
72 |
+
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
73 |
+
st.write("Reply: ", response["output_text"])
|
74 |
+
|
75 |
+
def main():
|
76 |
+
st.header("AI clone chatbot💁")
|
77 |
+
|
78 |
+
user_question = st.text_input("Ask a Question from the PDF Files", key="user_question")
|
79 |
+
|
80 |
+
if user_question: # Only check for the user question now
|
81 |
+
user_input(user_question, api_key)
|
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, key="pdf_uploader")
|
86 |
+
if st.button("Submit & Process", key="process_button"): # No need to check for API key here
|
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, api_key)
|
91 |
+
st.success("Done")
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
main()
|
faiss_index
ADDED
File without changes
|
requirements.txt
ADDED
Binary file (220 Bytes). View file
|
|