Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -108,85 +108,5 @@ with gr.Blocks(title="Chat with PDF") as demo:
|
|
| 108 |
process_button.click(process_pdfs, inputs=[pdf_input], outputs=[status_output])
|
| 109 |
ask_button.click(query_pdf, inputs=[question_input], outputs=[answer_output])
|
| 110 |
|
| 111 |
-
if __name__ == "__main__":
|
| 112 |
-
demo.launch() try:
|
| 113 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=GOOGLE_API_KEY)
|
| 114 |
-
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
| 115 |
-
vector_store.save_local(INDEX_PATH)
|
| 116 |
-
return "PDFs processed successfully! Vector store saved. Now you can ask questions."
|
| 117 |
-
except Exception as e:
|
| 118 |
-
return f"Error creating vector store: {str(e)}"
|
| 119 |
-
|
| 120 |
-
def load_vector_store():
|
| 121 |
-
try:
|
| 122 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=GOOGLE_API_KEY)
|
| 123 |
-
if os.path.exists(INDEX_PATH):
|
| 124 |
-
return FAISS.load_local(INDEX_PATH, embeddings, allow_dangerous_deserialization=True)
|
| 125 |
-
return None
|
| 126 |
-
except Exception as e:
|
| 127 |
-
return None
|
| 128 |
-
|
| 129 |
-
def get_qa_chain():
|
| 130 |
-
# Modern stuff QA chain
|
| 131 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0.3, google_api_key=GOOGLE_API_KEY)
|
| 132 |
-
|
| 133 |
-
qa_prompt = ChatPromptTemplate.from_messages([
|
| 134 |
-
("system", """
|
| 135 |
-
Answer the question as detailed as possible from the provided context only.
|
| 136 |
-
If the answer is not in the provided context, respond with "answer is not available in the context".
|
| 137 |
-
Do not make up information.
|
| 138 |
-
|
| 139 |
-
Context: {context}
|
| 140 |
-
"""),
|
| 141 |
-
("human", "{input}"),
|
| 142 |
-
])
|
| 143 |
-
|
| 144 |
-
stuff_chain = create_stuff_documents_chain(llm, qa_prompt)
|
| 145 |
-
return stuff_chain
|
| 146 |
-
|
| 147 |
-
def query_pdf(user_question):
|
| 148 |
-
vector_store = load_vector_store()
|
| 149 |
-
if vector_store is None:
|
| 150 |
-
return "Please process a PDF first by uploading and submitting it."
|
| 151 |
-
|
| 152 |
-
try:
|
| 153 |
-
retriever = vector_store.as_retriever(search_kwargs={"k": 4}) # Retrieve top 4 docs
|
| 154 |
-
stuff_chain = get_qa_chain()
|
| 155 |
-
|
| 156 |
-
# Full retrieval QA chain
|
| 157 |
-
retrieval_chain = create_retrieval_chain(retriever, stuff_chain)
|
| 158 |
-
|
| 159 |
-
response = retrieval_chain.invoke({"input": user_question})
|
| 160 |
-
return response["answer"]
|
| 161 |
-
except Exception as e:
|
| 162 |
-
return f"Error querying the PDF: {str(e)}"
|
| 163 |
-
|
| 164 |
-
def process_pdfs(pdf_files):
|
| 165 |
-
if not pdf_files:
|
| 166 |
-
return "Please upload at least one PDF."
|
| 167 |
-
|
| 168 |
-
raw_text = get_pdf_text(pdf_files)
|
| 169 |
-
if "Error" in raw_text:
|
| 170 |
-
return raw_text
|
| 171 |
-
if not raw_text.strip():
|
| 172 |
-
return "No extractable text found in the uploaded PDFs."
|
| 173 |
-
|
| 174 |
-
text_chunks = get_text_chunks(raw_text)
|
| 175 |
-
result = create_vector_store(text_chunks)
|
| 176 |
-
return result
|
| 177 |
-
|
| 178 |
-
# Gradio UI
|
| 179 |
-
with gr.Blocks(title="Chat with PDF") as demo:
|
| 180 |
-
gr.Markdown("## Chat with PDF 💁")
|
| 181 |
-
pdf_input = gr.File(file_types=[".pdf"], label="Upload PDF(s)", file_count="multiple")
|
| 182 |
-
process_button = gr.Button("Submit & Process")
|
| 183 |
-
status_output = gr.Textbox(label="Status", placeholder="Status updates will appear here...")
|
| 184 |
-
question_input = gr.Textbox(label="Ask a Question from the PDF")
|
| 185 |
-
answer_output = gr.Textbox(label="Reply", placeholder="Answers will appear here...")
|
| 186 |
-
ask_button = gr.Button("Get Answer")
|
| 187 |
-
|
| 188 |
-
process_button.click(process_pdfs, inputs=[pdf_input], outputs=[status_output])
|
| 189 |
-
ask_button.click(query_pdf, inputs=[question_input], outputs=[answer_output])
|
| 190 |
-
|
| 191 |
if __name__ == "__main__":
|
| 192 |
demo.launch()
|
|
|
|
| 108 |
process_button.click(process_pdfs, inputs=[pdf_input], outputs=[status_output])
|
| 109 |
ask_button.click(query_pdf, inputs=[question_input], outputs=[answer_output])
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
if __name__ == "__main__":
|
| 112 |
demo.launch()
|