Spaces:
Sleeping
Sleeping
Chandranshu Jain
commited on
Commit
•
cfcca1d
1
Parent(s):
29a0d03
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
4 |
+
import os
|
5 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
6 |
+
from langchain_community.vectorstores import Chroma
|
7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
+
from langchain.chains.question_answering import load_qa_chain
|
9 |
+
from langchain.prompts import PromptTemplate
|
10 |
+
|
11 |
+
st.set_page_config(page_title="Document Genie", layout="wide")
|
12 |
+
|
13 |
+
st.markdown("""
|
14 |
+
## Document Genie: Get instant insights from your Documents
|
15 |
+
|
16 |
+
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.
|
17 |
+
|
18 |
+
### How It Works
|
19 |
+
|
20 |
+
Follow these simple steps to interact with the chatbot:
|
21 |
+
|
22 |
+
1. **Upload Your Documents**: The system accepts multiple PDF files at once, analyzing the content to provide comprehensive insights.
|
23 |
+
|
24 |
+
2. **Ask a Question**: After processing the documents, ask any question related to the content of your uploaded documents for a precise answer.
|
25 |
+
""")
|
26 |
+
|
27 |
+
def get_pdf(pdf_docs):
|
28 |
+
text = ""
|
29 |
+
for pdf in pdf_docs:
|
30 |
+
pdf_reader = PdfReader(pdf)
|
31 |
+
for page in pdf_reader.pages:
|
32 |
+
text += page.extract_text()
|
33 |
+
return text
|
34 |
+
|
35 |
+
def text_splitter(text):
|
36 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
37 |
+
# Set a really small chunk size, just to show.
|
38 |
+
chunk_size=500,
|
39 |
+
chunk_overlap=20,
|
40 |
+
separators=["\n\n","\n"," ",".",","])
|
41 |
+
chunks=text_splitter.split_text(text)
|
42 |
+
return chunks
|
43 |
+
|
44 |
+
from google.colab import userdata
|
45 |
+
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
46 |
+
|
47 |
+
def embedding(chunk):
|
48 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
49 |
+
vector = Chroma.from_documents(chunk, embeddings)
|
50 |
+
db = Chroma.from_documents(vector, embeddings, persist_directory="./chroma_db")
|
51 |
+
|
52 |
+
def get_conversational_chain():
|
53 |
+
prompt_template = """
|
54 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
55 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
56 |
+
Context:\n {context}?\n
|
57 |
+
Question: \n{question}\n
|
58 |
+
|
59 |
+
Answer:
|
60 |
+
"""
|
61 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3, google_api_key=api_key)
|
62 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
63 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
64 |
+
return chain
|
65 |
+
|
66 |
+
def user_call(query):
|
67 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
68 |
+
db3 = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
|
69 |
+
docs = db3.similarity_search(query)
|
70 |
+
chain = get_conversational_chain()
|
71 |
+
response = chain({"input_documents": docs, "question": query}, return_only_outputs=True)
|
72 |
+
st.write("Reply: ", response["output_text"])
|
73 |
+
|
74 |
+
def main():
|
75 |
+
st.header("Chat with your pdf💁")
|
76 |
+
|
77 |
+
query = st.text_input("Ask a Question from the PDF Files", key="query")
|
78 |
+
|
79 |
+
if query:
|
80 |
+
user_call(query)
|
81 |
+
|
82 |
+
with st.sidebar:
|
83 |
+
st.title("Menu:")
|
84 |
+
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True, key="pdf_uploader")
|
85 |
+
if st.button("Submit & Process", key="process_button"):
|
86 |
+
with st.spinner("Processing..."):
|
87 |
+
raw_text = get_pdf(pdf_docs)
|
88 |
+
text_chunks = text_splitter(raw_text)
|
89 |
+
embedding(text_chunks)
|
90 |
+
st.success("Done")
|