Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
we want to build RAG base application in which 3 to 4 different PDF documents will be placed at github . Application will be deployed on hugging face using streamlit interface. our objective is user will inquire the query and our application will give pin point answer and very short but comprehensive summary upto maximum 250 words about that particular question. i already developed an application but it asks to upload document on run time means on user interface and then extract information. similarly we need only query block and it should extract required info from different Pdfs and give pin point answer. Sample of code is given below nad amend its as per requirement:
|
2 |
+
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from langchain.chains.question_answering import load_qa_chain
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
+
import google.generativeai as genai
|
10 |
+
import streamlit as st
|
11 |
+
|
12 |
+
# Configure Google Generative AI API key
|
13 |
+
GOOGLE_API_KEY = st.secrets["GOOGLE_API_KEY"]
|
14 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
15 |
+
|
16 |
+
|
17 |
+
@st.cache_data
|
18 |
+
def get_pdf_text(pdf_docs):
|
19 |
+
text = ""
|
20 |
+
for pdf in pdf_docs:
|
21 |
+
pdf_reader = PdfReader(pdf)
|
22 |
+
if pdf_reader.is_encrypted:
|
23 |
+
try:
|
24 |
+
pdf_reader.decrypt("")
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"Failed to decrypt PDF: {e}")
|
27 |
+
return ""
|
28 |
+
for page in pdf_reader.pages:
|
29 |
+
page_text = page.extract_text()
|
30 |
+
if page_text:
|
31 |
+
text += page_text
|
32 |
+
return text
|
33 |
+
|
34 |
+
|
35 |
+
@st.cache_data
|
36 |
+
def get_text_chunks(text):
|
37 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
38 |
+
chunks = text_splitter.split_text(text)
|
39 |
+
return chunks
|
40 |
+
|
41 |
+
|
42 |
+
@st.cache_resource
|
43 |
+
def load_or_create_vector_store(text_chunks):
|
44 |
+
if not text_chunks:
|
45 |
+
raise ValueError("Text chunks are empty. Cannot create vector store.")
|
46 |
+
|
47 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=GOOGLE_API_KEY)
|
48 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
49 |
+
vector_store.save_local("faiss_index")
|
50 |
+
return vector_store
|
51 |
+
|
52 |
+
|
53 |
+
def get_conversational_chain():
|
54 |
+
prompt_template = """
|
55 |
+
Answer the question as detailed as possible from the provided context. If the answer is not in the provided context, say "answer is not available in the context".
|
56 |
+
|
57 |
+
Context:\n {context}?\n
|
58 |
+
Question: \n{question}\n
|
59 |
+
|
60 |
+
Answer:
|
61 |
+
"""
|
62 |
+
model = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0.3, google_api_key=GOOGLE_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 |
+
|
68 |
+
def user_input(user_question, chain, vector_store):
|
69 |
+
docs = vector_store.similarity_search(user_question)
|
70 |
+
response = chain(
|
71 |
+
{"input_documents": docs, "question": user_question},
|
72 |
+
return_only_outputs=True
|
73 |
+
)
|
74 |
+
return response['output_text']
|
75 |
+
|
76 |
+
|
77 |
+
def main():
|
78 |
+
st.set_page_config(page_title="Chat with PDF", layout="centered", page_icon="π")
|
79 |
+
st.title("π Chat with Your PDF Document")
|
80 |
+
|
81 |
+
# Initialize chat history and vector store in session state
|
82 |
+
if 'chat_history' not in st.session_state:
|
83 |
+
st.session_state.chat_history = []
|
84 |
+
if 'vector_store' not in st.session_state:
|
85 |
+
st.session_state.vector_store = None
|
86 |
+
|
87 |
+
# Sidebar for PDF upload
|
88 |
+
st.sidebar.title("Upload PDF")
|
89 |
+
pdf_docs = st.sidebar.file_uploader("Upload your PDF Files", type=["pdf"], accept_multiple_files=True)
|
90 |
+
|
91 |
+
if pdf_docs:
|
92 |
+
if st.sidebar.button("Process PDFs"):
|
93 |
+
with st.spinner("Processing..."):
|
94 |
+
raw_text = get_pdf_text(pdf_docs)
|
95 |
+
text_chunks = get_text_chunks(raw_text)
|
96 |
+
|
97 |
+
if not text_chunks:
|
98 |
+
st.error("No text extracted from the PDFs. Please check the content.")
|
99 |
+
else:
|
100 |
+
st.session_state.vector_store = load_or_create_vector_store(text_chunks)
|
101 |
+
st.success("PDF processed successfully!")
|
102 |
+
|
103 |
+
st.sidebar.markdown("---")
|
104 |
+
st.sidebar.markdown(
|
105 |
+
"<footer style='text-align: center;'>Created by <span style='font-weight: 600;'>Ghulam Mahiyudin</span></footer>",
|
106 |
+
unsafe_allow_html=True)
|
107 |
+
|
108 |
+
# Create conversational chain
|
109 |
+
chain = get_conversational_chain()
|
110 |
+
|
111 |
+
# User question input
|
112 |
+
user_question = st.text_input("Ask a Question:", placeholder="Type your question here...")
|
113 |
+
|
114 |
+
if st.button("Get Response"):
|
115 |
+
# Validate user input
|
116 |
+
if not user_question:
|
117 |
+
st.warning("Please enter a question before submitting.")
|
118 |
+
elif st.session_state.vector_store is None:
|
119 |
+
st.warning("Please upload and process a PDF document before asking a question.")
|
120 |
+
else:
|
121 |
+
with st.spinner("Generating response..."):
|
122 |
+
answer = user_input(user_question, chain, st.session_state.vector_store)
|
123 |
+
# Add question and answer to chat history
|
124 |
+
st.session_state.chat_history.insert(0, (user_question, answer)) # Insert at the top
|
125 |
+
|
126 |
+
# Display chat history with styling
|
127 |
+
if st.session_state.chat_history:
|
128 |
+
for question, answer in st.session_state.chat_history:
|
129 |
+
st.write("---")
|
130 |
+
st.markdown(f"**π€ You:** _{question}_")
|
131 |
+
st.markdown(f"**π€ AI:** {answer}")
|
132 |
+
|
133 |
+
if __name__ == "__main__":
|
134 |
+
main()
|