Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,6 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
| 17 |
|
| 18 |
# --- Load and split PDF ---
|
| 19 |
def load_pdf(uploaded_file, chunk_size=500, chunk_overlap=100):
|
| 20 |
-
"""Loads a PDF and splits it into chunks."""
|
| 21 |
loader = PyPDFLoader(str(uploaded_file)) # Ensure path is string
|
| 22 |
pages = loader.load()
|
| 23 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
|
@@ -58,7 +57,7 @@ def hybrid_search(query, top_n=3):
|
|
| 58 |
final_indices = [top_bm25_indices[i] for i in sub_ranked[0]]
|
| 59 |
return [pdf_texts[i] for i in final_indices]
|
| 60 |
|
| 61 |
-
# --- Generate GPT response
|
| 62 |
def generate_response(query):
|
| 63 |
relevant_chunks = hybrid_search(query)
|
| 64 |
memory_context = memory.load_memory_variables({})
|
|
@@ -71,11 +70,11 @@ def generate_response(query):
|
|
| 71 |
)
|
| 72 |
|
| 73 |
llm = OpenAI(model_name="gpt-4", openai_api_key=openai_api_key)
|
| 74 |
-
response = llm
|
| 75 |
memory.save_context({"user": query}, {"AI": response})
|
| 76 |
return response
|
| 77 |
|
| 78 |
-
# ---
|
| 79 |
def handle_pdf_upload(uploaded_file):
|
| 80 |
global pdf_texts
|
| 81 |
try:
|
|
@@ -85,7 +84,32 @@ def handle_pdf_upload(uploaded_file):
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"❌ Failed to process PDF: {str(e)}"
|
| 87 |
|
| 88 |
-
# ---
|
| 89 |
def handle_query(query):
|
|
|
|
| 90 |
if not pdf_texts:
|
| 91 |
-
return "❌ No PDF uploaded. Please upload."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# --- Load and split PDF ---
|
| 19 |
def load_pdf(uploaded_file, chunk_size=500, chunk_overlap=100):
|
|
|
|
| 20 |
loader = PyPDFLoader(str(uploaded_file)) # Ensure path is string
|
| 21 |
pages = loader.load()
|
| 22 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
|
|
|
| 57 |
final_indices = [top_bm25_indices[i] for i in sub_ranked[0]]
|
| 58 |
return [pdf_texts[i] for i in final_indices]
|
| 59 |
|
| 60 |
+
# --- Generate GPT response ---
|
| 61 |
def generate_response(query):
|
| 62 |
relevant_chunks = hybrid_search(query)
|
| 63 |
memory_context = memory.load_memory_variables({})
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
llm = OpenAI(model_name="gpt-4", openai_api_key=openai_api_key)
|
| 73 |
+
response = llm(prompt) # ✅ FIXED: use __call__ instead of .predict
|
| 74 |
memory.save_context({"user": query}, {"AI": response})
|
| 75 |
return response
|
| 76 |
|
| 77 |
+
# --- Upload PDF ---
|
| 78 |
def handle_pdf_upload(uploaded_file):
|
| 79 |
global pdf_texts
|
| 80 |
try:
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
return f"❌ Failed to process PDF: {str(e)}"
|
| 86 |
|
| 87 |
+
# --- Handle user query ---
|
| 88 |
def handle_query(query):
|
| 89 |
+
global pdf_texts
|
| 90 |
if not pdf_texts:
|
| 91 |
+
return "❌ No PDF uploaded. Please upload a PDF first."
|
| 92 |
+
return generate_response(query)
|
| 93 |
+
|
| 94 |
+
# --- Gradio UI ---
|
| 95 |
+
with gr.Blocks() as demo:
|
| 96 |
+
gr.Markdown("## 🤖 Chat with Your PDF (BM25 + FAISS + GPT-4)")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
file_input = gr.File(label="Upload a PDF", file_types=[".pdf"])
|
| 100 |
+
upload_output = gr.Textbox(label="Upload Status", interactive=False)
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
query_input = gr.Textbox(label="Ask a Question", placeholder="What is this document about?", lines=2)
|
| 104 |
+
submit_btn = gr.Button("Submit")
|
| 105 |
+
|
| 106 |
+
response_output = gr.Textbox(label="Response", lines=6)
|
| 107 |
+
|
| 108 |
+
# Event Bindings
|
| 109 |
+
file_input.change(fn=handle_pdf_upload, inputs=file_input, outputs=upload_output)
|
| 110 |
+
submit_btn.click(fn=handle_query, inputs=query_input, outputs=response_output)
|
| 111 |
+
query_input.submit(fn=handle_query, inputs=query_input, outputs=response_output)
|
| 112 |
+
|
| 113 |
+
# --- Run App ---
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
demo.launch()
|