Spaces:
Sleeping
Sleeping
Update utils/DocsLoader.py
Browse files- utils/DocsLoader.py +15 -17
utils/DocsLoader.py
CHANGED
|
@@ -66,13 +66,23 @@ from langchain.schema import Document
|
|
| 66 |
from langchain_text_splitters.sentence_transformers import SentenceTransformersTokenTextSplitter # better but slower
|
| 67 |
|
| 68 |
MODEL_DIR = os.path.join("/tmp", "e5-large-v2")
|
| 69 |
-
CACHE_DIR = "/
|
| 70 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 71 |
|
| 72 |
def load_and_chunk(url: str) -> list[Document]:
|
| 73 |
-
print(f"[Loader]
|
| 74 |
|
| 75 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
head = requests.head(url)
|
| 77 |
file_size = int(head.headers.get("Content-Length", 0))
|
| 78 |
USE_STREAM = file_size > 30 * 1024 * 1024 # > 30MB
|
|
@@ -81,20 +91,7 @@ def load_and_chunk(url: str) -> list[Document]:
|
|
| 81 |
if resp.status_code != 200:
|
| 82 |
raise HTTPException(400, "Could not download document")
|
| 83 |
|
| 84 |
-
|
| 85 |
-
hasher = hashlib.md5()
|
| 86 |
-
content = b""
|
| 87 |
-
for chunk in resp.iter_content(8192):
|
| 88 |
-
hasher.update(chunk)
|
| 89 |
-
content += chunk
|
| 90 |
-
content_hash = hasher.hexdigest()
|
| 91 |
-
cache_file = os.path.join(CACHE_DIR, f"{content_hash}.pkl")
|
| 92 |
-
|
| 93 |
-
# Return cached version if exists
|
| 94 |
-
if os.path.exists(cache_file):
|
| 95 |
-
print(f"✅ Loaded chunks from disk cache (key={content_hash})")
|
| 96 |
-
with open(cache_file, "rb") as f:
|
| 97 |
-
return pickle.load(f)
|
| 98 |
|
| 99 |
# Determine content type
|
| 100 |
content_type = resp.headers.get("Content-Type", "").lower()
|
|
@@ -139,3 +136,4 @@ def load_and_chunk(url: str) -> list[Document]:
|
|
| 139 |
|
| 140 |
return chunks
|
| 141 |
|
|
|
|
|
|
| 66 |
from langchain_text_splitters.sentence_transformers import SentenceTransformersTokenTextSplitter # better but slower
|
| 67 |
|
| 68 |
MODEL_DIR = os.path.join("/tmp", "e5-large-v2")
|
| 69 |
+
CACHE_DIR = os.path.join(os.path.dirname(__file__), "../chunk_data")
|
| 70 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 71 |
|
| 72 |
def load_and_chunk(url: str) -> list[Document]:
|
| 73 |
+
print(f"[Loader] URL: {url}")
|
| 74 |
|
| 75 |
+
# Use hash of the URL to avoid download if cached
|
| 76 |
+
content_hash = hashlib.md5(url.encode()).hexdigest()
|
| 77 |
+
cache_file = os.path.join(CACHE_DIR, f"{content_hash}.pkl")
|
| 78 |
+
|
| 79 |
+
# Return cached version if exists
|
| 80 |
+
if os.path.exists(cache_file):
|
| 81 |
+
print(f"✅ Loaded chunks from disk cache (key={content_hash})")
|
| 82 |
+
with open(cache_file, "rb") as f:
|
| 83 |
+
return pickle.load(f)
|
| 84 |
+
|
| 85 |
+
# Download content
|
| 86 |
head = requests.head(url)
|
| 87 |
file_size = int(head.headers.get("Content-Length", 0))
|
| 88 |
USE_STREAM = file_size > 30 * 1024 * 1024 # > 30MB
|
|
|
|
| 91 |
if resp.status_code != 200:
|
| 92 |
raise HTTPException(400, "Could not download document")
|
| 93 |
|
| 94 |
+
content = resp.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Determine content type
|
| 97 |
content_type = resp.headers.get("Content-Type", "").lower()
|
|
|
|
| 136 |
|
| 137 |
return chunks
|
| 138 |
|
| 139 |
+
|