thomasjacob04 commited on
Commit
3c81ae5
·
verified ·
1 Parent(s): a28a5d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
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 = "vol1.pdf" # test paper
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
- # Step 1: Extract text from PDF
107
- text = extract_text_from_pdf(pdf_path)
 
 
 
 
108
 
109
- # Step 2: Generate embeddings for the text
110
- embeddings = generate_embeddings(text)
111
 
112
- # Step 3: Insert embeddings into Milvus
113
- insert_into_milvus(embeddings)
 
 
 
 
 
 
 
 
 
 
114
 
115
- # Step 4: Generate embeddings for the query
116
- query_embedding = generate_embeddings(query)
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
- pdf_path = "test.pdf"
131
- main(pdf_path, question)
132
 
133
- iface = gr.Interface(fn=ask_question, inputs="text", outputs="text")
 
 
 
 
 
 
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()