Spaces:
Configuration error
Configuration error
Upload 6 files
Browse files- .gitattributes +1 -0
- her2_faiss_db/index.faiss +3 -0
- her2_faiss_db/index.pkl +3 -0
- utils/__init__.py +0 -0
- utils/__pycache__/__init__.cpython-310.pyc +0 -0
- utils/__pycache__/pdf_vector_utils.cpython-310.pyc +0 -0
- utils/pdf_vector_utils.py +85 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
her2_faiss_db/index.faiss filter=lfs diff=lfs merge=lfs -text
|
her2_faiss_db/index.faiss
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:128bb737bb3397c970cde24772a305d88f1402a1d1387d9357647c3cf39ae783
|
| 3 |
+
size 113709
|
her2_faiss_db/index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c5e97c97a1f1b82dae54340cda46900b5f92d2eb5d7269771f59546f2afba484
|
| 3 |
+
size 53682
|
utils/__init__.py
ADDED
|
File without changes
|
utils/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (187 Bytes). View file
|
|
|
utils/__pycache__/pdf_vector_utils.cpython-310.pyc
ADDED
|
Binary file (3.95 kB). View file
|
|
|
utils/pdf_vector_utils.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import fitz
|
| 3 |
+
import spacy
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from langchain.docstore.document import Document
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 8 |
+
|
| 9 |
+
# Load SpaCy model once
|
| 10 |
+
nlp = spacy.load("en_core_web_sm")
|
| 11 |
+
|
| 12 |
+
def spacy_sentence_tokenize(text: str) -> list:
|
| 13 |
+
doc = nlp(text)
|
| 14 |
+
return [sent.text.strip() for sent in doc.sents]
|
| 15 |
+
|
| 16 |
+
def load_pdf_to_documents(pdf_path: str) -> list:
|
| 17 |
+
documents = []
|
| 18 |
+
with fitz.open(pdf_path) as doc:
|
| 19 |
+
for i, page in enumerate(doc):
|
| 20 |
+
text = page.get_text().replace("-\n", "").replace("\n", " ").strip()
|
| 21 |
+
if text:
|
| 22 |
+
documents.append(Document(page_content=text, metadata={"page": i}))
|
| 23 |
+
return documents
|
| 24 |
+
|
| 25 |
+
def sentence_overlap_chunk(text: str, max_tokens: int = 150, overlap_sent_count: int = 2) -> list:
|
| 26 |
+
sentences = spacy_sentence_tokenize(text)
|
| 27 |
+
chunks, current_chunk, current_len = [], [], 0
|
| 28 |
+
|
| 29 |
+
for sentence in sentences:
|
| 30 |
+
token_count = len(sentence.split())
|
| 31 |
+
if current_len + token_count <= max_tokens:
|
| 32 |
+
current_chunk.append(sentence)
|
| 33 |
+
current_len += token_count
|
| 34 |
+
else:
|
| 35 |
+
chunks.append(" ".join(current_chunk))
|
| 36 |
+
current_chunk = current_chunk[-overlap_sent_count:] + [sentence]
|
| 37 |
+
current_len = sum(len(s.split()) for s in current_chunk)
|
| 38 |
+
|
| 39 |
+
if current_chunk:
|
| 40 |
+
chunks.append(" ".join(current_chunk))
|
| 41 |
+
return chunks
|
| 42 |
+
|
| 43 |
+
def analyze_chunks(chunks: list):
|
| 44 |
+
token_lengths = [len(chunk.page_content.split()) for chunk in chunks]
|
| 45 |
+
|
| 46 |
+
print(f"Total Chunks: {len(token_lengths)}")
|
| 47 |
+
print(f"Avg Tokens per Chunk: {sum(token_lengths)/len(token_lengths):.2f}")
|
| 48 |
+
print(f"Min Tokens: {min(token_lengths)}")
|
| 49 |
+
print(f"Max Tokens: {max(token_lengths)}")
|
| 50 |
+
|
| 51 |
+
plt.hist(token_lengths, bins=20)
|
| 52 |
+
plt.title("Chunk Token Length Distribution")
|
| 53 |
+
plt.xlabel("Token Count")
|
| 54 |
+
plt.ylabel("Number of Chunks")
|
| 55 |
+
plt.show()
|
| 56 |
+
|
| 57 |
+
def build_vector_store(documents: list,
|
| 58 |
+
max_tokens: int = 250,
|
| 59 |
+
overlap_sent_count: int = 3,
|
| 60 |
+
model_name: str = "sentence-transformers/allenai-specter",
|
| 61 |
+
persist_directory: str = "./vector_db") -> FAISS:
|
| 62 |
+
|
| 63 |
+
all_chunks = []
|
| 64 |
+
for doc in documents:
|
| 65 |
+
chunks = sentence_overlap_chunk(doc.page_content, max_tokens=max_tokens, overlap_sent_count=overlap_sent_count)
|
| 66 |
+
all_chunks.extend([Document(page_content=chunk, metadata=doc.metadata) for chunk in chunks])
|
| 67 |
+
|
| 68 |
+
analyze_chunks(all_chunks)
|
| 69 |
+
|
| 70 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
| 71 |
+
vectorstore = FAISS.from_documents(all_chunks, embeddings)
|
| 72 |
+
vectorstore.save_local(persist_directory)
|
| 73 |
+
return vectorstore
|
| 74 |
+
|
| 75 |
+
def load_vector_store(persist_directory: str,
|
| 76 |
+
model_name: str = "sentence-transformers/allenai-specter") -> FAISS:
|
| 77 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
| 78 |
+
return FAISS.load_local(persist_directory, embeddings, allow_dangerous_deserialization=True)
|
| 79 |
+
|
| 80 |
+
def query_vector_store(vectorstore: FAISS, query: str, k: int = 3, show: bool = True):
|
| 81 |
+
results = vectorstore.similarity_search(query, k=k)
|
| 82 |
+
if show:
|
| 83 |
+
for i, doc in enumerate(results, 1):
|
| 84 |
+
print(f"\n--- Result {i} (Page {doc.metadata.get('page')}):\n{doc.page_content[:500]}...\n")
|
| 85 |
+
return results
|