Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ class DoclingPDFLoader(BaseLoader):
|
|
| 26 |
text = dl_doc.export_to_markdown()
|
| 27 |
yield LCDocument(page_content=text)
|
| 28 |
|
| 29 |
-
FILE_PATH = "
|
| 30 |
|
| 31 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 32 |
|
|
@@ -102,33 +102,44 @@ rag_chain = (
|
|
| 102 |
# return rag_chain.invoke(query)
|
| 103 |
|
| 104 |
# Main function
|
| 105 |
-
def main(pdf_path, query):
|
| 106 |
-
#
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
# Step 5: Query Milvus for similar embeddings
|
| 119 |
-
results = query_milvus(query_embedding)
|
| 120 |
-
|
| 121 |
-
# Step 6: Generate response using Llama
|
| 122 |
-
context = " ".join([result for result in results])
|
| 123 |
-
response = generate_response(query, context)
|
| 124 |
-
|
| 125 |
-
print(response)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
# Initialize Gradio interface
|
| 129 |
-
def ask_question(question):
|
| 130 |
-
|
| 131 |
-
main(pdf_path, question)
|
| 132 |
|
| 133 |
-
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
iface.launch()
|
|
|
|
| 26 |
text = dl_doc.export_to_markdown()
|
| 27 |
yield LCDocument(page_content=text)
|
| 28 |
|
| 29 |
+
FILE_PATH = "test.pdf" # test paper
|
| 30 |
|
| 31 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 32 |
|
|
|
|
| 102 |
# return rag_chain.invoke(query)
|
| 103 |
|
| 104 |
# Main function
|
| 105 |
+
def main(pdf_path: str, query: str) -> str:
|
| 106 |
+
# Use existing components for PDF processing and RAG
|
| 107 |
+
loader = DoclingPDFLoader(file_path=pdf_path)
|
| 108 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 109 |
+
chunk_size=1000,
|
| 110 |
+
chunk_overlap=200,
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
docs = loader.load()
|
| 114 |
+
splits = text_splitter.split_documents(docs)
|
| 115 |
|
| 116 |
+
# Use the existing vectorstore
|
| 117 |
+
vectorstore = Milvus.from_documents(
|
| 118 |
+
splits,
|
| 119 |
+
embeddings,
|
| 120 |
+
connection_args={"uri": MILVUS_URI},
|
| 121 |
+
index_params={
|
| 122 |
+
"index_type": "IVF_FLAT",
|
| 123 |
+
"metric_type": "L2",
|
| 124 |
+
"params": {"nlist": 128}
|
| 125 |
+
},
|
| 126 |
+
drop_old=True,
|
| 127 |
+
)
|
| 128 |
|
| 129 |
+
# Use the existing RAG chain
|
| 130 |
+
response = rag_chain.invoke(query)
|
| 131 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
# Initialize Gradio interface
|
| 135 |
+
def ask_question(question: str) -> str:
|
| 136 |
+
return main("vol1.pdf", question)
|
|
|
|
| 137 |
|
| 138 |
+
iface = gr.Interface(
|
| 139 |
+
fn=ask_question,
|
| 140 |
+
inputs="text",
|
| 141 |
+
outputs="text",
|
| 142 |
+
title="PDF Question Answering",
|
| 143 |
+
description="Ask questions about the PDF content"
|
| 144 |
+
)
|
| 145 |
iface.launch()
|