Spaces:
Sleeping
Sleeping
app.py
CHANGED
|
@@ -1,22 +1,153 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
os.environ["TRITON_CACHE_DIR"] = "/tmp/triton_cache"
|
| 16 |
os.environ["TORCH_COMPILE_DISABLE"] = "1"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
from pylate import models, indexes, retrieve
|
| 20 |
|
| 21 |
# Global variables for PyLate components
|
| 22 |
model = None
|
|
@@ -27,483 +158,577 @@ metadata_db = None
|
|
| 27 |
# ===== DOCUMENT PROCESSING FUNCTIONS =====
|
| 28 |
|
| 29 |
def extract_text_from_pdf(file_path: str) -> str:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def extract_text_from_docx(file_path: str) -> str:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def extract_text_from_txt(file_path: str) -> str:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def chunk_text(text: str, chunk_size: int = 1000, overlap: int = 100) -> List[Dict[str, Any]]:
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
# ===== METADATA DATABASE =====
|
| 112 |
|
| 113 |
def init_metadata_db():
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
|
| 141 |
def add_document_metadata(doc_id: str, filename: str, file_hash: str,
|
| 142 |
original_text: str, chunk_info: Dict[str, Any], total_chunks: int):
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
def get_document_metadata(doc_id: str) -> Dict[str, Any]:
|
| 159 |
-
|
| 160 |
-
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
|
| 172 |
# ===== PYLATE INITIALIZATION =====
|
| 173 |
|
| 174 |
-
@spaces.GPU
|
| 175 |
def initialize_pylate(model_name: str = "colbert-ir/colbertv2.0") -> str:
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
# ===== DOCUMENT PROCESSING =====
|
| 208 |
|
| 209 |
-
@spaces.GPU
|
| 210 |
def process_documents(files, chunk_size: int = 1000, overlap: int = 100) -> str:
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
# ===== SEARCH FUNCTION =====
|
| 298 |
|
| 299 |
-
@spaces.GPU
|
| 300 |
def search_documents(query: str, k: int = 5, show_chunks: bool = True) -> str:
|
| 301 |
-
|
| 302 |
-
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
|
| 317 |
-
|
| 318 |
-
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
| 329 |
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
-
|
| 342 |
|
| 343 |
-
|
|
|
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
| 347 |
|
| 348 |
# ===== GRADIO INTERFACE =====
|
| 349 |
|
| 350 |
def create_interface():
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
|
| 501 |
# ===== MAIN =====
|
| 502 |
|
| 503 |
if __name__ == "__main__":
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
PyLate ZeroGPU Document Search with Runtime Package Installation
|
| 4 |
+
Complete version that installs all dependencies at startup if needed.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import subprocess
|
| 8 |
+
import sys
|
| 9 |
import os
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
print("π Starting PyLate ZeroGPU Document Search...")
|
| 13 |
+
print("π§ Checking and installing required packages...")
|
| 14 |
+
|
| 15 |
+
# ===== RUNTIME PACKAGE INSTALLATION =====
|
| 16 |
+
def install_package(package, quiet=True):
|
| 17 |
+
"""Install a package at runtime."""
|
| 18 |
+
try:
|
| 19 |
+
if quiet:
|
| 20 |
+
subprocess.check_call([
|
| 21 |
+
sys.executable, '-m', 'pip', 'install', package,
|
| 22 |
+
'--quiet', '--disable-pip-version-check'
|
| 23 |
+
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 24 |
+
else:
|
| 25 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
|
| 26 |
+
return True
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"β οΈ Failed to install {package}: {e}")
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
+
def check_and_install_packages():
|
| 32 |
+
"""Check and install all required packages."""
|
| 33 |
+
|
| 34 |
+
# Define packages with their import names and pip names
|
| 35 |
+
packages_to_check = [
|
| 36 |
+
# (import_name, pip_package, test_import)
|
| 37 |
+
('gradio', 'gradio==4.44.0', lambda: __import__('gradio')),
|
| 38 |
+
('spaces', 'spaces', lambda: __import__('spaces')),
|
| 39 |
+
('torch', 'torch', lambda: __import__('torch')),
|
| 40 |
+
('torchvision', 'torchvision', lambda: __import__('torchvision')),
|
| 41 |
+
('torchaudio', 'torchaudio', lambda: __import__('torchaudio')),
|
| 42 |
+
('transformers', 'transformers==4.48.2', lambda: __import__('transformers')),
|
| 43 |
+
('sentence_transformers', 'sentence-transformers', lambda: __import__('sentence_transformers')),
|
| 44 |
+
('docx', 'python-docx', lambda: __import__('docx')),
|
| 45 |
+
('fitz', 'pymupdf', lambda: __import__('fitz')),
|
| 46 |
+
('unstructured', 'unstructured', lambda: __import__('unstructured')),
|
| 47 |
+
('pandas', 'pandas', lambda: __import__('pandas')),
|
| 48 |
+
('numpy', 'numpy', lambda: __import__('numpy')),
|
| 49 |
+
('huggingface_hub', 'huggingface_hub', lambda: __import__('huggingface_hub')),
|
| 50 |
+
('accelerate', 'accelerate', lambda: __import__('accelerate')),
|
| 51 |
+
('pylate', 'pylate==1.2.0', lambda: __import__('pylate')),
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
installed_count = 0
|
| 55 |
+
failed_packages = []
|
| 56 |
+
|
| 57 |
+
for import_name, pip_package, test_func in packages_to_check:
|
| 58 |
+
try:
|
| 59 |
+
test_func()
|
| 60 |
+
print(f"β
{import_name} - already installed")
|
| 61 |
+
installed_count += 1
|
| 62 |
+
except ImportError:
|
| 63 |
+
print(f"π¦ Installing {pip_package}...")
|
| 64 |
+
success = install_package(pip_package, quiet=False)
|
| 65 |
+
if success:
|
| 66 |
+
try:
|
| 67 |
+
# Test import after installation
|
| 68 |
+
test_func()
|
| 69 |
+
print(f"β
{import_name} - installed successfully")
|
| 70 |
+
installed_count += 1
|
| 71 |
+
except ImportError:
|
| 72 |
+
print(f"β {import_name} - installation failed (import test failed)")
|
| 73 |
+
failed_packages.append(import_name)
|
| 74 |
+
else:
|
| 75 |
+
print(f"β {import_name} - installation failed")
|
| 76 |
+
failed_packages.append(import_name)
|
| 77 |
+
|
| 78 |
+
print(f"\nπ Installation Summary:")
|
| 79 |
+
print(f" β
Successfully installed/verified: {installed_count}/{len(packages_to_check)}")
|
| 80 |
+
|
| 81 |
+
if failed_packages:
|
| 82 |
+
print(f" β Failed packages: {', '.join(failed_packages)}")
|
| 83 |
+
print(f" β οΈ App may not work correctly with missing packages")
|
| 84 |
+
else:
|
| 85 |
+
print(f" π All packages ready!")
|
| 86 |
+
|
| 87 |
+
return len(failed_packages) == 0
|
| 88 |
+
|
| 89 |
+
# Install packages before importing anything else
|
| 90 |
+
installation_success = check_and_install_packages()
|
| 91 |
+
|
| 92 |
+
# Now import everything
|
| 93 |
+
print("\nπ Loading modules...")
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
import gradio as gr
|
| 97 |
+
import spaces
|
| 98 |
+
import torch
|
| 99 |
+
import tempfile
|
| 100 |
+
import sqlite3
|
| 101 |
+
import json
|
| 102 |
+
import hashlib
|
| 103 |
+
from pathlib import Path
|
| 104 |
+
from typing import List, Dict, Any, Tuple
|
| 105 |
+
print("β
Core modules loaded")
|
| 106 |
+
except ImportError as e:
|
| 107 |
+
print(f"β Failed to import core modules: {e}")
|
| 108 |
+
sys.exit(1)
|
| 109 |
+
|
| 110 |
+
# Import document processing modules with fallbacks
|
| 111 |
+
try:
|
| 112 |
+
import docx
|
| 113 |
+
print("β
python-docx loaded")
|
| 114 |
+
except ImportError:
|
| 115 |
+
print("β οΈ python-docx not available - DOCX processing will be disabled")
|
| 116 |
+
docx = None
|
| 117 |
+
|
| 118 |
+
try:
|
| 119 |
+
import fitz # pymupdf
|
| 120 |
+
print("β
PyMuPDF loaded")
|
| 121 |
+
except ImportError:
|
| 122 |
+
print("β οΈ PyMuPDF not available - PDF processing will be limited")
|
| 123 |
+
fitz = None
|
| 124 |
+
|
| 125 |
+
try:
|
| 126 |
+
from unstructured.partition.auto import partition
|
| 127 |
+
print("β
Unstructured loaded")
|
| 128 |
+
except ImportError:
|
| 129 |
+
print("β οΈ Unstructured not available - fallback text extraction disabled")
|
| 130 |
+
partition = None
|
| 131 |
+
|
| 132 |
+
try:
|
| 133 |
+
from pylate import models, indexes, retrieve
|
| 134 |
+
print("β
PyLate loaded")
|
| 135 |
+
except ImportError as e:
|
| 136 |
+
print(f"β PyLate failed to load: {e}")
|
| 137 |
+
print("π Attempting to install PyLate...")
|
| 138 |
+
install_package('pylate==1.2.0', quiet=False)
|
| 139 |
+
try:
|
| 140 |
+
from pylate import models, indexes, retrieve
|
| 141 |
+
print("β
PyLate loaded after installation")
|
| 142 |
+
except ImportError:
|
| 143 |
+
print("β PyLate installation failed - core functionality unavailable")
|
| 144 |
+
sys.exit(1)
|
| 145 |
+
|
| 146 |
+
# Set environment variables
|
| 147 |
os.environ["TRITON_CACHE_DIR"] = "/tmp/triton_cache"
|
| 148 |
os.environ["TORCH_COMPILE_DISABLE"] = "1"
|
| 149 |
|
| 150 |
+
print("π― All modules loaded successfully!\n")
|
|
|
|
| 151 |
|
| 152 |
# Global variables for PyLate components
|
| 153 |
model = None
|
|
|
|
| 158 |
# ===== DOCUMENT PROCESSING FUNCTIONS =====
|
| 159 |
|
| 160 |
def extract_text_from_pdf(file_path: str) -> str:
|
| 161 |
+
"""Extract text from PDF file using PyMuPDF and unstructured as fallback."""
|
| 162 |
+
text = ""
|
| 163 |
+
|
| 164 |
+
if not fitz:
|
| 165 |
+
return "Error: PyMuPDF not available for PDF processing"
|
| 166 |
+
|
| 167 |
+
try:
|
| 168 |
+
# Use PyMuPDF (fitz) - more reliable than PyPDF2
|
| 169 |
+
doc = fitz.open(file_path)
|
| 170 |
+
for page in doc:
|
| 171 |
+
text += page.get_text() + "\n"
|
| 172 |
+
doc.close()
|
| 173 |
+
|
| 174 |
+
# If no text extracted, try unstructured
|
| 175 |
+
if not text.strip() and partition:
|
| 176 |
+
elements = partition(filename=file_path)
|
| 177 |
+
text = "\n".join([str(element) for element in elements])
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
# Final fallback to unstructured
|
| 181 |
+
if partition:
|
| 182 |
+
try:
|
| 183 |
+
elements = partition(filename=file_path)
|
| 184 |
+
text = "\n".join([str(element) for element in elements])
|
| 185 |
+
except:
|
| 186 |
+
text = f"Error: Could not extract text from PDF: {str(e)}"
|
| 187 |
+
else:
|
| 188 |
+
text = f"Error: Could not extract text from PDF: {str(e)}"
|
| 189 |
+
|
| 190 |
+
return text.strip()
|
| 191 |
|
| 192 |
def extract_text_from_docx(file_path: str) -> str:
|
| 193 |
+
"""Extract text from DOCX file."""
|
| 194 |
+
if not docx:
|
| 195 |
+
return "Error: python-docx not available for DOCX processing"
|
| 196 |
+
|
| 197 |
+
try:
|
| 198 |
+
doc = docx.Document(file_path)
|
| 199 |
+
text = ""
|
| 200 |
+
for paragraph in doc.paragraphs:
|
| 201 |
+
text += paragraph.text + "\n"
|
| 202 |
+
return text.strip()
|
| 203 |
+
except Exception as e:
|
| 204 |
+
return f"Error: Could not extract text from DOCX: {str(e)}"
|
| 205 |
|
| 206 |
def extract_text_from_txt(file_path: str) -> str:
|
| 207 |
+
"""Extract text from TXT file."""
|
| 208 |
+
try:
|
| 209 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 210 |
+
return file.read().strip()
|
| 211 |
+
except UnicodeDecodeError:
|
| 212 |
+
try:
|
| 213 |
+
with open(file_path, 'r', encoding='latin1') as file:
|
| 214 |
+
return file.read().strip()
|
| 215 |
+
except Exception as e:
|
| 216 |
+
return f"Error: Could not read text file: {str(e)}"
|
| 217 |
+
except Exception as e:
|
| 218 |
+
return f"Error: Could not read text file: {str(e)}"
|
| 219 |
|
| 220 |
def chunk_text(text: str, chunk_size: int = 1000, overlap: int = 100) -> List[Dict[str, Any]]:
|
| 221 |
+
"""Chunk text with overlap and return metadata."""
|
| 222 |
+
chunks = []
|
| 223 |
+
start = 0
|
| 224 |
+
chunk_index = 0
|
| 225 |
+
|
| 226 |
+
while start < len(text):
|
| 227 |
+
end = start + chunk_size
|
| 228 |
+
chunk_text = text[start:end]
|
| 229 |
+
|
| 230 |
+
# Try to break at sentence boundary
|
| 231 |
+
if end < len(text):
|
| 232 |
+
last_period = chunk_text.rfind('.')
|
| 233 |
+
last_newline = chunk_text.rfind('\n')
|
| 234 |
+
break_point = max(last_period, last_newline)
|
| 235 |
+
|
| 236 |
+
if break_point > chunk_size * 0.7:
|
| 237 |
+
chunk_text = chunk_text[:break_point + 1]
|
| 238 |
+
end = start + break_point + 1
|
| 239 |
+
|
| 240 |
+
if chunk_text.strip():
|
| 241 |
+
chunks.append({
|
| 242 |
+
'text': chunk_text.strip(),
|
| 243 |
+
'start': start,
|
| 244 |
+
'end': end,
|
| 245 |
+
'index': chunk_index,
|
| 246 |
+
'length': len(chunk_text.strip())
|
| 247 |
+
})
|
| 248 |
+
chunk_index += 1
|
| 249 |
+
|
| 250 |
+
start = max(start + 1, end - overlap)
|
| 251 |
+
|
| 252 |
+
return chunks
|
| 253 |
|
| 254 |
# ===== METADATA DATABASE =====
|
| 255 |
|
| 256 |
def init_metadata_db():
|
| 257 |
+
"""Initialize SQLite database for metadata."""
|
| 258 |
+
global metadata_db
|
| 259 |
+
|
| 260 |
+
db_path = "metadata.db"
|
| 261 |
+
metadata_db = sqlite3.connect(db_path, check_same_thread=False)
|
| 262 |
+
|
| 263 |
+
metadata_db.execute("""
|
| 264 |
+
CREATE TABLE IF NOT EXISTS documents (
|
| 265 |
+
doc_id TEXT PRIMARY KEY,
|
| 266 |
+
filename TEXT NOT NULL,
|
| 267 |
+
file_hash TEXT NOT NULL,
|
| 268 |
+
original_text TEXT NOT NULL,
|
| 269 |
+
chunk_index INTEGER NOT NULL,
|
| 270 |
+
total_chunks INTEGER NOT NULL,
|
| 271 |
+
chunk_start INTEGER NOT NULL,
|
| 272 |
+
chunk_end INTEGER NOT NULL,
|
| 273 |
+
chunk_size INTEGER NOT NULL,
|
| 274 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 275 |
+
)
|
| 276 |
+
""")
|
| 277 |
+
|
| 278 |
+
metadata_db.execute("""
|
| 279 |
+
CREATE INDEX IF NOT EXISTS idx_filename ON documents(filename);
|
| 280 |
+
""")
|
| 281 |
+
|
| 282 |
+
metadata_db.commit()
|
| 283 |
|
| 284 |
def add_document_metadata(doc_id: str, filename: str, file_hash: str,
|
| 285 |
original_text: str, chunk_info: Dict[str, Any], total_chunks: int):
|
| 286 |
+
"""Add document metadata to database."""
|
| 287 |
+
global metadata_db
|
| 288 |
+
|
| 289 |
+
metadata_db.execute("""
|
| 290 |
+
INSERT OR REPLACE INTO documents
|
| 291 |
+
(doc_id, filename, file_hash, original_text, chunk_index, total_chunks,
|
| 292 |
+
chunk_start, chunk_end, chunk_size)
|
| 293 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 294 |
+
""", (
|
| 295 |
+
doc_id, filename, file_hash, original_text,
|
| 296 |
+
chunk_info['index'], total_chunks,
|
| 297 |
+
chunk_info['start'], chunk_info['end'], chunk_info['length']
|
| 298 |
+
))
|
| 299 |
+
metadata_db.commit()
|
| 300 |
|
| 301 |
def get_document_metadata(doc_id: str) -> Dict[str, Any]:
|
| 302 |
+
"""Get document metadata by ID."""
|
| 303 |
+
global metadata_db
|
| 304 |
|
| 305 |
+
cursor = metadata_db.execute(
|
| 306 |
+
"SELECT * FROM documents WHERE doc_id = ?", (doc_id,)
|
| 307 |
+
)
|
| 308 |
+
row = cursor.fetchone()
|
| 309 |
|
| 310 |
+
if row:
|
| 311 |
+
columns = [desc[0] for desc in cursor.description]
|
| 312 |
+
return dict(zip(columns, row))
|
| 313 |
+
return {}
|
| 314 |
|
| 315 |
# ===== PYLATE INITIALIZATION =====
|
| 316 |
|
| 317 |
+
@spaces.GPU(duration=120) # Allow 2 minutes for initialization
|
| 318 |
def initialize_pylate(model_name: str = "colbert-ir/colbertv2.0") -> str:
|
| 319 |
+
"""Initialize PyLate components on ZeroGPU H200."""
|
| 320 |
+
global model, index, retriever
|
| 321 |
+
|
| 322 |
+
try:
|
| 323 |
+
# Initialize metadata database
|
| 324 |
+
init_metadata_db()
|
| 325 |
+
|
| 326 |
+
# Load ColBERT model
|
| 327 |
+
model = models.ColBERT(model_name_or_path=model_name)
|
| 328 |
+
|
| 329 |
+
# Move to GPU - ZeroGPU provides CUDA access
|
| 330 |
+
device_info = "CPU"
|
| 331 |
+
if torch.cuda.is_available():
|
| 332 |
+
model = model.to('cuda')
|
| 333 |
+
device_name = torch.cuda.get_device_name()
|
| 334 |
+
device_info = f"GPU: {device_name}"
|
| 335 |
+
|
| 336 |
+
# Initialize PLAID index with optimized settings for ZeroGPU
|
| 337 |
+
index = indexes.PLAID(
|
| 338 |
+
index_folder="./pylate_index",
|
| 339 |
+
index_name="documents",
|
| 340 |
+
override=True,
|
| 341 |
+
kmeans_niters=1, # Reduce k-means iterations for faster setup
|
| 342 |
+
nbits=2 # Optimized for memory efficiency
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
# Initialize retriever
|
| 346 |
+
retriever = retrieve.ColBERT(index=index)
|
| 347 |
+
|
| 348 |
+
return f"β
PyLate initialized successfully on ZeroGPU!\nπ₯ Model: {model_name}\nπ― Device: {device_info}\nπΎ VRAM: ~70GB available\nπ Ready for document processing!"
|
| 349 |
+
|
| 350 |
+
except Exception as e:
|
| 351 |
+
return f"β Error initializing PyLate: {str(e)}\n\nPlease check the logs for more details."
|
| 352 |
|
| 353 |
# ===== DOCUMENT PROCESSING =====
|
| 354 |
|
| 355 |
+
@spaces.GPU(duration=300) # Allow 5 minutes for processing
|
| 356 |
def process_documents(files, chunk_size: int = 1000, overlap: int = 100) -> str:
|
| 357 |
+
"""Process uploaded documents and add to index using ZeroGPU."""
|
| 358 |
+
global model, index, metadata_db
|
| 359 |
+
|
| 360 |
+
if not model or not index:
|
| 361 |
+
return "β Please initialize PyLate first!"
|
| 362 |
+
|
| 363 |
+
if not files:
|
| 364 |
+
return "β No files uploaded!"
|
| 365 |
+
|
| 366 |
+
try:
|
| 367 |
+
all_documents = []
|
| 368 |
+
all_doc_ids = []
|
| 369 |
+
processed_files = []
|
| 370 |
+
skipped_files = []
|
| 371 |
+
|
| 372 |
+
for file in files:
|
| 373 |
+
# Get file info
|
| 374 |
+
filename = Path(file.name).name
|
| 375 |
+
file_path = file.name
|
| 376 |
+
|
| 377 |
+
# Calculate file hash
|
| 378 |
+
with open(file_path, 'rb') as f:
|
| 379 |
+
file_hash = hashlib.md5(f.read()).hexdigest()
|
| 380 |
+
|
| 381 |
+
# Extract text based on file type
|
| 382 |
+
text = ""
|
| 383 |
+
if filename.lower().endswith('.pdf'):
|
| 384 |
+
if fitz:
|
| 385 |
+
text = extract_text_from_pdf(file_path)
|
| 386 |
+
else:
|
| 387 |
+
skipped_files.append(f"{filename}: PDF processing not available")
|
| 388 |
+
continue
|
| 389 |
+
elif filename.lower().endswith('.docx'):
|
| 390 |
+
if docx:
|
| 391 |
+
text = extract_text_from_docx(file_path)
|
| 392 |
+
else:
|
| 393 |
+
skipped_files.append(f"{filename}: DOCX processing not available")
|
| 394 |
+
continue
|
| 395 |
+
elif filename.lower().endswith('.txt'):
|
| 396 |
+
text = extract_text_from_txt(file_path)
|
| 397 |
+
else:
|
| 398 |
+
skipped_files.append(f"{filename}: Unsupported file type")
|
| 399 |
+
continue
|
| 400 |
+
|
| 401 |
+
if not text or text.startswith("Error:"):
|
| 402 |
+
skipped_files.append(f"{filename}: Failed to extract text")
|
| 403 |
+
continue
|
| 404 |
+
|
| 405 |
+
# Chunk the text
|
| 406 |
+
chunks = chunk_text(text, chunk_size, overlap)
|
| 407 |
+
|
| 408 |
+
if not chunks:
|
| 409 |
+
skipped_files.append(f"{filename}: No valid chunks created")
|
| 410 |
+
continue
|
| 411 |
+
|
| 412 |
+
# Process each chunk
|
| 413 |
+
for chunk in chunks:
|
| 414 |
+
doc_id = f"{filename}_chunk_{chunk['index']}"
|
| 415 |
+
all_documents.append(chunk['text'])
|
| 416 |
+
all_doc_ids.append(doc_id)
|
| 417 |
+
|
| 418 |
+
# Store metadata
|
| 419 |
+
add_document_metadata(
|
| 420 |
+
doc_id=doc_id,
|
| 421 |
+
filename=filename,
|
| 422 |
+
file_hash=file_hash,
|
| 423 |
+
original_text=chunk['text'],
|
| 424 |
+
chunk_info=chunk,
|
| 425 |
+
total_chunks=len(chunks)
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
processed_files.append(f"{filename}: {len(chunks)} chunks")
|
| 429 |
+
|
| 430 |
+
if not all_documents:
|
| 431 |
+
return "β No text could be extracted from uploaded files!\n" + "\n".join(skipped_files)
|
| 432 |
+
|
| 433 |
+
# Encode documents with PyLate on H200 GPU
|
| 434 |
+
document_embeddings = model.encode(
|
| 435 |
+
all_documents,
|
| 436 |
+
batch_size=32, # Optimized batch size for H200's 70GB VRAM
|
| 437 |
+
is_query=False,
|
| 438 |
+
show_progress_bar=True
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
# Add to PLAID index
|
| 442 |
+
index.add_documents(
|
| 443 |
+
documents_ids=all_doc_ids,
|
| 444 |
+
documents_embeddings=document_embeddings
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
result = f"β
Successfully processed {len([f for f in files if not any(f.name in skip for skip in skipped_files)])} files on ZeroGPU H200:\n"
|
| 448 |
+
result += f"π Total chunks indexed: {len(all_documents)}\n"
|
| 449 |
+
result += f"π Documents processed:\n"
|
| 450 |
+
for file_info in processed_files:
|
| 451 |
+
result += f" β’ {file_info}\n"
|
| 452 |
+
|
| 453 |
+
if skipped_files:
|
| 454 |
+
result += f"\nβ οΈ Skipped files:\n"
|
| 455 |
+
for skip_info in skipped_files:
|
| 456 |
+
result += f" β’ {skip_info}\n"
|
| 457 |
+
|
| 458 |
+
result += f"\nπ Document index ready for search!"
|
| 459 |
+
return result
|
| 460 |
+
|
| 461 |
+
except Exception as e:
|
| 462 |
+
return f"β Error processing documents: {str(e)}\n\nPlease check your files and try again."
|
| 463 |
|
| 464 |
# ===== SEARCH FUNCTION =====
|
| 465 |
|
| 466 |
+
@spaces.GPU(duration=60) # 1 minute for search
|
| 467 |
def search_documents(query: str, k: int = 5, show_chunks: bool = True) -> str:
|
| 468 |
+
"""Search documents using PyLate on ZeroGPU."""
|
| 469 |
+
global model, retriever, metadata_db
|
| 470 |
|
| 471 |
+
if not model or not retriever:
|
| 472 |
+
return "β Please initialize PyLate and process documents first!"
|
| 473 |
|
| 474 |
+
if not query.strip():
|
| 475 |
+
return "β Please enter a search query!"
|
| 476 |
|
| 477 |
+
try:
|
| 478 |
+
# Encode query on GPU
|
| 479 |
+
query_embedding = model.encode([query], is_query=True)
|
| 480 |
|
| 481 |
+
# Search
|
| 482 |
+
results = retriever.retrieve(query_embedding, k=k)[0]
|
| 483 |
|
| 484 |
+
if not results:
|
| 485 |
+
return "π No results found for your query.\n\nTry:\nβ’ Different keywords\nβ’ Broader search terms\nβ’ Check if documents were processed correctly"
|
| 486 |
|
| 487 |
+
# Format results with metadata
|
| 488 |
+
formatted_results = [f"π **Search Results for:** '{query}' (powered by ZeroGPU H200)\n"]
|
| 489 |
|
| 490 |
+
for i, result in enumerate(results):
|
| 491 |
+
doc_id = result['id']
|
| 492 |
+
score = result['score']
|
| 493 |
|
| 494 |
+
# Get metadata
|
| 495 |
+
metadata = get_document_metadata(doc_id)
|
| 496 |
|
| 497 |
+
formatted_results.append(f"## Result {i+1} (Relevance: {score:.3f})")
|
| 498 |
+
formatted_results.append(
|
| 499 |
+
f"**π File:** {metadata.get('filename', 'Unknown')}")
|
| 500 |
+
formatted_results.append(
|
| 501 |
+
f"**π Chunk:** {metadata.get('chunk_index', 0) + 1}/{metadata.get('total_chunks', 1)}")
|
| 502 |
|
| 503 |
+
if show_chunks:
|
| 504 |
+
text = metadata.get('original_text', '')
|
| 505 |
+
if len(text) > 400:
|
| 506 |
+
preview = text[:400] + "..."
|
| 507 |
+
else:
|
| 508 |
+
preview = text
|
| 509 |
+
formatted_results.append(f"**π¬ Text:** {preview}")
|
| 510 |
|
| 511 |
+
formatted_results.append("---")
|
| 512 |
|
| 513 |
+
formatted_results.append(f"\nπ― Found {len(results)} relevant results using ColBERT semantic search")
|
| 514 |
+
return "\n".join(formatted_results)
|
| 515 |
|
| 516 |
+
except Exception as e:
|
| 517 |
+
return f"β Error searching: {str(e)}\n\nPlease try again or check if PyLate is properly initialized."
|
| 518 |
|
| 519 |
# ===== GRADIO INTERFACE =====
|
| 520 |
|
| 521 |
def create_interface():
|
| 522 |
+
"""Create the Gradio interface for ZeroGPU."""
|
| 523 |
+
|
| 524 |
+
with gr.Blocks(title="PyLate ZeroGPU Document Search", theme=gr.themes.Soft()) as demo:
|
| 525 |
+
gr.Markdown("""
|
| 526 |
+
# π PyLate ZeroGPU Document Search
|
| 527 |
+
### Powered by ColBERT and NVIDIA H200 (70GB VRAM)
|
| 528 |
+
|
| 529 |
+
Upload documents, process them with PyLate on ZeroGPU, and perform lightning-fast semantic search!
|
| 530 |
+
|
| 531 |
+
**π₯ ZeroGPU Features:**
|
| 532 |
+
- π― NVIDIA H200 GPU with 70GB VRAM
|
| 533 |
+
- β‘ Dynamic GPU allocation (only when needed)
|
| 534 |
+
- π Free for HF Pro subscribers
|
| 535 |
+
- π Optimized for PyTorch/ColBERT workloads
|
| 536 |
+
- π Automatic package installation
|
| 537 |
+
""")
|
| 538 |
+
|
| 539 |
+
# Status indicator
|
| 540 |
+
with gr.Row():
|
| 541 |
+
gr.Markdown(f"""
|
| 542 |
+
**π System Status:**
|
| 543 |
+
- β
PyLate: Ready
|
| 544 |
+
- β
Document Processing: {"PDF β
" if fitz else "PDF β"} | {"DOCX β
" if docx else "DOCX β"} | TXT β
|
| 545 |
+
- β
ZeroGPU: Available
|
| 546 |
+
""")
|
| 547 |
+
|
| 548 |
+
with gr.Tab("π Setup"):
|
| 549 |
+
gr.Markdown("### Initialize PyLate System on ZeroGPU H200")
|
| 550 |
+
|
| 551 |
+
model_choice = gr.Dropdown(
|
| 552 |
+
choices=[
|
| 553 |
+
"colbert-ir/colbertv2.0",
|
| 554 |
+
"sentence-transformers/all-MiniLM-L6-v2"
|
| 555 |
+
],
|
| 556 |
+
value="colbert-ir/colbertv2.0",
|
| 557 |
+
label="Select ColBERT Model",
|
| 558 |
+
info="ColBERT v2.0 is recommended for best performance"
|
| 559 |
+
)
|
| 560 |
+
|
| 561 |
+
init_btn = gr.Button("π Initialize PyLate on ZeroGPU", variant="primary", size="lg")
|
| 562 |
+
init_status = gr.Textbox(label="Initialization Status", lines=6, max_lines=10)
|
| 563 |
+
|
| 564 |
+
init_btn.click(
|
| 565 |
+
initialize_pylate,
|
| 566 |
+
inputs=model_choice,
|
| 567 |
+
outputs=init_status
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
with gr.Tab("π Document Upload"):
|
| 571 |
+
gr.Markdown("### Upload and Process Documents on H200 GPU")
|
| 572 |
+
|
| 573 |
+
with gr.Row():
|
| 574 |
+
with gr.Column():
|
| 575 |
+
file_upload = gr.File(
|
| 576 |
+
file_count="multiple",
|
| 577 |
+
file_types=[".pdf", ".docx", ".txt"],
|
| 578 |
+
label="Upload Documents",
|
| 579 |
+
info="Supported: PDF, DOCX, TXT files"
|
| 580 |
+
)
|
| 581 |
+
|
| 582 |
+
with gr.Row():
|
| 583 |
+
chunk_size = gr.Slider(
|
| 584 |
+
minimum=500,
|
| 585 |
+
maximum=3000,
|
| 586 |
+
value=1000,
|
| 587 |
+
step=100,
|
| 588 |
+
label="Chunk Size (characters)",
|
| 589 |
+
info="Larger chunks = more context, smaller chunks = more precise"
|
| 590 |
+
)
|
| 591 |
+
|
| 592 |
+
overlap = gr.Slider(
|
| 593 |
+
minimum=0,
|
| 594 |
+
maximum=500,
|
| 595 |
+
value=100,
|
| 596 |
+
step=50,
|
| 597 |
+
label="Chunk Overlap (characters)",
|
| 598 |
+
info="Overlap helps maintain context between chunks"
|
| 599 |
+
)
|
| 600 |
+
|
| 601 |
+
process_btn = gr.Button(
|
| 602 |
+
"β‘ Process Documents on ZeroGPU", variant="primary", size="lg")
|
| 603 |
+
|
| 604 |
+
with gr.Column():
|
| 605 |
+
process_status = gr.Textbox(
|
| 606 |
+
label="Processing Status",
|
| 607 |
+
lines=15,
|
| 608 |
+
max_lines=20,
|
| 609 |
+
info="Processing status and results will appear here"
|
| 610 |
+
)
|
| 611 |
+
|
| 612 |
+
process_btn.click(
|
| 613 |
+
process_documents,
|
| 614 |
+
inputs=[file_upload, chunk_size, overlap],
|
| 615 |
+
outputs=process_status
|
| 616 |
+
)
|
| 617 |
+
|
| 618 |
+
with gr.Tab("π Search"):
|
| 619 |
+
gr.Markdown("### Search Your Documents with H200 Power")
|
| 620 |
+
|
| 621 |
+
with gr.Row():
|
| 622 |
+
with gr.Column():
|
| 623 |
+
search_query = gr.Textbox(
|
| 624 |
+
label="Search Query",
|
| 625 |
+
placeholder="Enter your search query... (e.g., 'machine learning algorithms', 'financial projections')",
|
| 626 |
+
lines=2,
|
| 627 |
+
info="Use natural language - ColBERT understands semantic meaning"
|
| 628 |
+
)
|
| 629 |
+
|
| 630 |
+
with gr.Row():
|
| 631 |
+
num_results = gr.Slider(
|
| 632 |
+
minimum=1,
|
| 633 |
+
maximum=20,
|
| 634 |
+
value=5,
|
| 635 |
+
step=1,
|
| 636 |
+
label="Number of Results",
|
| 637 |
+
info="How many relevant chunks to return"
|
| 638 |
+
)
|
| 639 |
+
|
| 640 |
+
show_chunks = gr.Checkbox(
|
| 641 |
+
value=True,
|
| 642 |
+
label="Show Text Chunks",
|
| 643 |
+
info="Display the actual text content"
|
| 644 |
+
)
|
| 645 |
+
|
| 646 |
+
search_btn = gr.Button("π Search with ZeroGPU", variant="primary", size="lg")
|
| 647 |
+
|
| 648 |
+
with gr.Column():
|
| 649 |
+
search_results = gr.Textbox(
|
| 650 |
+
label="Search Results",
|
| 651 |
+
lines=18,
|
| 652 |
+
max_lines=25,
|
| 653 |
+
info="Semantic search results will appear here"
|
| 654 |
+
)
|
| 655 |
+
|
| 656 |
+
search_btn.click(
|
| 657 |
+
search_documents,
|
| 658 |
+
inputs=[search_query, num_results, show_chunks],
|
| 659 |
+
outputs=search_results
|
| 660 |
+
)
|
| 661 |
+
|
| 662 |
+
with gr.Tab("βΉοΈ ZeroGPU Info"):
|
| 663 |
+
gr.Markdown("""
|
| 664 |
+
### About ZeroGPU PyLate Search
|
| 665 |
+
|
| 666 |
+
**π₯ Powered by NVIDIA H200 Tensor Core GPU**
|
| 667 |
+
|
| 668 |
+
#### π ZeroGPU Features:
|
| 669 |
+
- **70GB HBM3 Memory** - Massive capacity for large document collections
|
| 670 |
+
- **Dynamic Allocation** - GPU assigned only when functions need it
|
| 671 |
+
- **Optimized for PyTorch** - Perfect for ColBERT/PyLate workloads
|
| 672 |
+
- **Free for Pro Users** - No additional charges beyond HF Pro
|
| 673 |
+
- **Auto Scaling** - Efficient resource usage and queue management
|
| 674 |
+
|
| 675 |
+
#### π§ How ColBERT Works:
|
| 676 |
+
1. **Late Interaction** - Processes queries and documents separately
|
| 677 |
+
2. **Token-level Matching** - Fine-grained semantic understanding
|
| 678 |
+
3. **Efficient Retrieval** - Fast search with high-quality results
|
| 679 |
+
4. **GPU Acceleration** - Leverages H200 for rapid inference
|
| 680 |
+
|
| 681 |
+
#### π Performance Benefits:
|
| 682 |
+
- **10-100x faster** than CPU-based search
|
| 683 |
+
- **Large batch processing** - 32+ documents simultaneously
|
| 684 |
+
- **Real-time search** - Sub-second query responses
|
| 685 |
+
- **Massive scale** - 70GB VRAM handles huge document sets
|
| 686 |
+
|
| 687 |
+
#### π οΏ½οΏ½ Technical Details:
|
| 688 |
+
- **Runtime Package Installation** - Automatically installs dependencies
|
| 689 |
+
- **Gradio SDK Required** - ZeroGPU doesn't support Docker
|
| 690 |
+
- **Smart Chunking** - Intelligent text segmentation with overlap
|
| 691 |
+
- **Metadata Tracking** - SQLite database for chunk information
|
| 692 |
+
|
| 693 |
+
#### π― Usage Tips:
|
| 694 |
+
1. **Initialize first** - Required before processing documents
|
| 695 |
+
2. **Natural language queries** - ColBERT understands meaning, not just keywords
|
| 696 |
+
3. **Adjust chunk size** - Larger for context, smaller for precision
|
| 697 |
+
4. **Multiple file types** - Mix PDFs, DOCX, and TXT files
|
| 698 |
+
5. **Semantic search** - Try "concepts similar to X" type queries
|
| 699 |
+
|
| 700 |
+
#### π Privacy & Security:
|
| 701 |
+
- Documents processed in-memory only
|
| 702 |
+
- No permanent storage of your content
|
| 703 |
+
- Processing happens on HF infrastructure
|
| 704 |
+
- Automatic cleanup after session ends
|
| 705 |
+
|
| 706 |
+
---
|
| 707 |
+
|
| 708 |
+
**Built with β€οΈ using:**
|
| 709 |
+
- π€ PyLate & ColBERT for semantic search
|
| 710 |
+
- β‘ ZeroGPU H200 for GPU acceleration
|
| 711 |
+
- π¨ Gradio for the interface
|
| 712 |
+
- π Python ecosystem for document processing
|
| 713 |
+
""")
|
| 714 |
+
|
| 715 |
+
return demo
|
| 716 |
|
| 717 |
# ===== MAIN =====
|
| 718 |
|
| 719 |
if __name__ == "__main__":
|
| 720 |
+
print("π Launching PyLate ZeroGPU Document Search interface...")
|
| 721 |
+
|
| 722 |
+
# Check if running on ZeroGPU
|
| 723 |
+
if torch.cuda.is_available():
|
| 724 |
+
print(f"π₯ GPU detected: {torch.cuda.get_device_name()}")
|
| 725 |
+
else:
|
| 726 |
+
print("π» Running on CPU (GPU will be allocated when @spaces.GPU functions are called)")
|
| 727 |
+
|
| 728 |
+
demo = create_interface()
|
| 729 |
+
demo.launch(
|
| 730 |
+
share=False,
|
| 731 |
+
server_name="0.0.0.0",
|
| 732 |
+
server_port=7860,
|
| 733 |
+
show_error=True
|
| 734 |
+
)
|