Fix HfFolder ImportError: make chromadb imports lazy, pin sentence-transformers>=3.0.0
Browse files- Move chromadb and embedding_functions imports inside functions in
vector_store.py so they don't execute at module import time
- Also lazy-load PdfReader and RecursiveCharacterTextSplitter in the
same file for consistency
- Pin sentence-transformers>=3.0.0 to avoid older versions that still
import the removed HfFolder from huggingface_hub
- Fix setup_demo.py run command (streamlit → gradio)"
- setup_demo.py +1 -1
- src/db/vector_store.py +8 -9
setup_demo.py
CHANGED
|
@@ -63,7 +63,7 @@ def main():
|
|
| 63 |
print(" --unzip -p docs/drug_safety/")
|
| 64 |
|
| 65 |
print()
|
| 66 |
-
print("To run the app: uv run
|
| 67 |
print()
|
| 68 |
|
| 69 |
|
|
|
|
| 63 |
print(" --unzip -p docs/drug_safety/")
|
| 64 |
|
| 65 |
print()
|
| 66 |
+
print("To run the app: uv run gradio app.py")
|
| 67 |
print()
|
| 68 |
|
| 69 |
|
src/db/vector_store.py
CHANGED
|
@@ -1,14 +1,9 @@
|
|
| 1 |
"""ChromaDB vector store for unstructured document RAG."""
|
| 2 |
|
| 3 |
-
import chromadb
|
| 4 |
-
from chromadb.utils import embedding_functions
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Optional
|
| 7 |
import hashlib
|
| 8 |
|
| 9 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 10 |
-
from pypdf import PdfReader
|
| 11 |
-
|
| 12 |
from ..config import get_settings
|
| 13 |
|
| 14 |
# Project paths
|
|
@@ -16,8 +11,9 @@ PROJECT_ROOT = Path(__file__).parent.parent.parent
|
|
| 16 |
DOCS_DIR = PROJECT_ROOT / "docs"
|
| 17 |
|
| 18 |
|
| 19 |
-
def get_chroma_client()
|
| 20 |
"""Get ChromaDB persistent client."""
|
|
|
|
| 21 |
chroma_dir = get_settings().chroma_db_dir
|
| 22 |
chroma_dir.mkdir(parents=True, exist_ok=True)
|
| 23 |
return chromadb.PersistentClient(path=str(chroma_dir))
|
|
@@ -25,6 +21,7 @@ def get_chroma_client() -> chromadb.PersistentClient:
|
|
| 25 |
|
| 26 |
def get_embedding_function():
|
| 27 |
"""Get the embedding function for ChromaDB."""
|
|
|
|
| 28 |
return embedding_functions.SentenceTransformerEmbeddingFunction(
|
| 29 |
model_name="all-MiniLM-L6-v2"
|
| 30 |
)
|
|
@@ -32,6 +29,7 @@ def get_embedding_function():
|
|
| 32 |
|
| 33 |
def extract_pdf_text(pdf_path: Path) -> str:
|
| 34 |
"""Extract text from PDF file."""
|
|
|
|
| 35 |
reader = PdfReader(pdf_path)
|
| 36 |
text = ""
|
| 37 |
for page in reader.pages:
|
|
@@ -41,6 +39,7 @@ def extract_pdf_text(pdf_path: Path) -> str:
|
|
| 41 |
|
| 42 |
def chunk_text(text: str, chunk_size: int = 1000, chunk_overlap: int = 200) -> list[str]:
|
| 43 |
"""Split text into chunks for embedding."""
|
|
|
|
| 44 |
splitter = RecursiveCharacterTextSplitter(
|
| 45 |
chunk_size=chunk_size,
|
| 46 |
chunk_overlap=chunk_overlap,
|
|
@@ -55,7 +54,7 @@ def generate_doc_id(text: str, index: int) -> str:
|
|
| 55 |
return hashlib.md5(hash_input.encode()).hexdigest()
|
| 56 |
|
| 57 |
|
| 58 |
-
def init_idsa_guidelines_collection()
|
| 59 |
"""Initialize the IDSA treatment guidelines collection."""
|
| 60 |
client = get_chroma_client()
|
| 61 |
ef = get_embedding_function()
|
|
@@ -79,7 +78,7 @@ def init_idsa_guidelines_collection() -> chromadb.Collection:
|
|
| 79 |
return collection
|
| 80 |
|
| 81 |
|
| 82 |
-
def init_mic_reference_collection()
|
| 83 |
"""Initialize the MIC reference documentation collection."""
|
| 84 |
client = get_chroma_client()
|
| 85 |
ef = get_embedding_function()
|
|
@@ -219,7 +218,7 @@ def import_mic_reference() -> int:
|
|
| 219 |
return len(documents)
|
| 220 |
|
| 221 |
|
| 222 |
-
def get_collection(name: str) -> Optional[
|
| 223 |
"""Get a collection by name."""
|
| 224 |
client = get_chroma_client()
|
| 225 |
ef = get_embedding_function()
|
|
|
|
| 1 |
"""ChromaDB vector store for unstructured document RAG."""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import Optional
|
| 5 |
import hashlib
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
from ..config import get_settings
|
| 8 |
|
| 9 |
# Project paths
|
|
|
|
| 11 |
DOCS_DIR = PROJECT_ROOT / "docs"
|
| 12 |
|
| 13 |
|
| 14 |
+
def get_chroma_client():
|
| 15 |
"""Get ChromaDB persistent client."""
|
| 16 |
+
import chromadb
|
| 17 |
chroma_dir = get_settings().chroma_db_dir
|
| 18 |
chroma_dir.mkdir(parents=True, exist_ok=True)
|
| 19 |
return chromadb.PersistentClient(path=str(chroma_dir))
|
|
|
|
| 21 |
|
| 22 |
def get_embedding_function():
|
| 23 |
"""Get the embedding function for ChromaDB."""
|
| 24 |
+
from chromadb.utils import embedding_functions
|
| 25 |
return embedding_functions.SentenceTransformerEmbeddingFunction(
|
| 26 |
model_name="all-MiniLM-L6-v2"
|
| 27 |
)
|
|
|
|
| 29 |
|
| 30 |
def extract_pdf_text(pdf_path: Path) -> str:
|
| 31 |
"""Extract text from PDF file."""
|
| 32 |
+
from pypdf import PdfReader
|
| 33 |
reader = PdfReader(pdf_path)
|
| 34 |
text = ""
|
| 35 |
for page in reader.pages:
|
|
|
|
| 39 |
|
| 40 |
def chunk_text(text: str, chunk_size: int = 1000, chunk_overlap: int = 200) -> list[str]:
|
| 41 |
"""Split text into chunks for embedding."""
|
| 42 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 43 |
splitter = RecursiveCharacterTextSplitter(
|
| 44 |
chunk_size=chunk_size,
|
| 45 |
chunk_overlap=chunk_overlap,
|
|
|
|
| 54 |
return hashlib.md5(hash_input.encode()).hexdigest()
|
| 55 |
|
| 56 |
|
| 57 |
+
def init_idsa_guidelines_collection():
|
| 58 |
"""Initialize the IDSA treatment guidelines collection."""
|
| 59 |
client = get_chroma_client()
|
| 60 |
ef = get_embedding_function()
|
|
|
|
| 78 |
return collection
|
| 79 |
|
| 80 |
|
| 81 |
+
def init_mic_reference_collection():
|
| 82 |
"""Initialize the MIC reference documentation collection."""
|
| 83 |
client = get_chroma_client()
|
| 84 |
ef = get_embedding_function()
|
|
|
|
| 218 |
return len(documents)
|
| 219 |
|
| 220 |
|
| 221 |
+
def get_collection(name: str) -> Optional[object]:
|
| 222 |
"""Get a collection by name."""
|
| 223 |
client = get_chroma_client()
|
| 224 |
ef = get_embedding_function()
|