Spaces:
Sleeping
Sleeping
Update pdf_processor.py
Browse files- pdf_processor.py +12 -6
pdf_processor.py
CHANGED
|
@@ -8,8 +8,15 @@ from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from embedding_functions import embed_text, embed_image
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def process_uploaded_pdf(pdf_bytes):
|
| 12 |
-
|
| 13 |
doc = fitz.open(stream = pdf_bytes, filetype = "pdf")
|
| 14 |
|
| 15 |
all_docs = []
|
|
@@ -47,8 +54,6 @@ def process_uploaded_pdf(pdf_bytes):
|
|
| 47 |
embedding = embed_image(pil_image)
|
| 48 |
all_embeddings.append(embedding)
|
| 49 |
|
| 50 |
-
# FIX: Add contextual textual clues (like the extracted text from the same page)
|
| 51 |
-
# so the similarity search can find it!
|
| 52 |
page_context_clue = text[:200] if text.strip() else ""
|
| 53 |
image_doc = Document(
|
| 54 |
page_content = f"Image illustration graphic chart on page {i}. Context: {page_context_clue}",
|
|
@@ -64,11 +69,12 @@ def process_uploaded_pdf(pdf_bytes):
|
|
| 64 |
|
| 65 |
# BUILD FAISS DATABASE
|
| 66 |
embeddings_array = np.array(all_embeddings)
|
|
|
|
|
|
|
| 67 |
vector_store = FAISS.from_embeddings(
|
| 68 |
text_embeddings = [(d.page_content, emb) for d, emb in zip(all_docs, embeddings_array)],
|
| 69 |
-
embedding =
|
| 70 |
metadatas = [d.metadata for d in all_docs]
|
| 71 |
)
|
| 72 |
|
| 73 |
-
return vector_store, image_data_store
|
| 74 |
-
|
|
|
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from embedding_functions import embed_text, embed_image
|
| 10 |
|
| 11 |
+
# 1. ADD THIS: A simple wrapper class so LangChain can call your CLIP model
|
| 12 |
+
class CLIPEmbeddingsWrapper:
|
| 13 |
+
def embed_query(self, text: str):
|
| 14 |
+
# Convert the numpy array from embed_text into a standard Python list of floats
|
| 15 |
+
return embed_text(text).tolist()
|
| 16 |
+
def embed_documents(self, texts: list[str]):
|
| 17 |
+
return [embed_text(t).tolist() for t in texts]
|
| 18 |
+
|
| 19 |
def process_uploaded_pdf(pdf_bytes):
|
|
|
|
| 20 |
doc = fitz.open(stream = pdf_bytes, filetype = "pdf")
|
| 21 |
|
| 22 |
all_docs = []
|
|
|
|
| 54 |
embedding = embed_image(pil_image)
|
| 55 |
all_embeddings.append(embedding)
|
| 56 |
|
|
|
|
|
|
|
| 57 |
page_context_clue = text[:200] if text.strip() else ""
|
| 58 |
image_doc = Document(
|
| 59 |
page_content = f"Image illustration graphic chart on page {i}. Context: {page_context_clue}",
|
|
|
|
| 69 |
|
| 70 |
# BUILD FAISS DATABASE
|
| 71 |
embeddings_array = np.array(all_embeddings)
|
| 72 |
+
|
| 73 |
+
# 2. FIX: Pass the new wrapper instance instead of None
|
| 74 |
vector_store = FAISS.from_embeddings(
|
| 75 |
text_embeddings = [(d.page_content, emb) for d, emb in zip(all_docs, embeddings_array)],
|
| 76 |
+
embedding = CLIPEmbeddingsWrapper(),
|
| 77 |
metadatas = [d.metadata for d in all_docs]
|
| 78 |
)
|
| 79 |
|
| 80 |
+
return vector_store, image_data_store
|
|
|