Update app.py
Browse files
app.py
CHANGED
|
@@ -23,7 +23,7 @@ def load_pdf(pdf_path, chunk_size=500, chunk_overlap=100):
|
|
| 23 |
doc_splits = text_splitter.split_documents(pages)
|
| 24 |
return [doc.page_content for doc in doc_splits]
|
| 25 |
|
| 26 |
-
# --- Initialize hybrid
|
| 27 |
def create_hybrid_index():
|
| 28 |
global bm25, index, embed_model, document_embeddings
|
| 29 |
if not pdf_texts:
|
|
@@ -66,18 +66,19 @@ def generate_response(query):
|
|
| 66 |
memory.save_context({"user": query}, {"AI": response})
|
| 67 |
return response
|
| 68 |
|
| 69 |
-
# ---
|
| 70 |
-
def
|
| 71 |
global pdf_texts
|
| 72 |
-
|
| 73 |
if uploaded_file is not None:
|
| 74 |
pdf_texts = load_pdf(uploaded_file.name)
|
| 75 |
create_hybrid_index()
|
| 76 |
-
return "✅ PDF processed successfully.
|
|
|
|
| 77 |
|
|
|
|
|
|
|
| 78 |
if not pdf_texts:
|
| 79 |
return "❌ Please upload a PDF first."
|
| 80 |
-
|
| 81 |
return generate_response(query)
|
| 82 |
|
| 83 |
# --- Gradio UI ---
|
|
@@ -86,13 +87,17 @@ with gr.Blocks() as demo:
|
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
file_input = gr.File(label="Upload a PDF", file_types=[".pdf"])
|
|
|
|
|
|
|
|
|
|
| 89 |
query_input = gr.Textbox(label="Ask something from the document", lines=2, placeholder="Type your question here...")
|
| 90 |
submit_btn = gr.Button("Submit")
|
| 91 |
|
| 92 |
output = gr.Textbox(label="Answer", lines=4)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
demo.launch()
|
|
|
|
| 23 |
doc_splits = text_splitter.split_documents(pages)
|
| 24 |
return [doc.page_content for doc in doc_splits]
|
| 25 |
|
| 26 |
+
# --- Initialize hybrid retrieval ---
|
| 27 |
def create_hybrid_index():
|
| 28 |
global bm25, index, embed_model, document_embeddings
|
| 29 |
if not pdf_texts:
|
|
|
|
| 66 |
memory.save_context({"user": query}, {"AI": response})
|
| 67 |
return response
|
| 68 |
|
| 69 |
+
# --- Step 1: Process uploaded PDF (only indexing) ---
|
| 70 |
+
def handle_pdf_upload(uploaded_file):
|
| 71 |
global pdf_texts
|
|
|
|
| 72 |
if uploaded_file is not None:
|
| 73 |
pdf_texts = load_pdf(uploaded_file.name)
|
| 74 |
create_hybrid_index()
|
| 75 |
+
return "✅ PDF processed successfully. You can now ask your question."
|
| 76 |
+
return "⚠️ Please upload a valid PDF."
|
| 77 |
|
| 78 |
+
# --- Step 2: Handle query (only after PDF is uploaded) ---
|
| 79 |
+
def handle_query(query):
|
| 80 |
if not pdf_texts:
|
| 81 |
return "❌ Please upload a PDF first."
|
|
|
|
| 82 |
return generate_response(query)
|
| 83 |
|
| 84 |
# --- Gradio UI ---
|
|
|
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
file_input = gr.File(label="Upload a PDF", file_types=[".pdf"])
|
| 90 |
+
upload_status = gr.Textbox(label="Status", interactive=False)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
query_input = gr.Textbox(label="Ask something from the document", lines=2, placeholder="Type your question here...")
|
| 94 |
submit_btn = gr.Button("Submit")
|
| 95 |
|
| 96 |
output = gr.Textbox(label="Answer", lines=4)
|
| 97 |
|
| 98 |
+
# Handlers
|
| 99 |
+
file_input.change(fn=handle_pdf_upload, inputs=file_input, outputs=upload_status)
|
| 100 |
+
submit_btn.click(fn=handle_query, inputs=query_input, outputs=output)
|
| 101 |
+
query_input.submit(fn=handle_query, inputs=query_input, outputs=output)
|
| 102 |
|
| 103 |
demo.launch()
|