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