Spaces:
Running
on
Zero
Running
on
Zero
Enhance model loading in indexing functions for BiGemma3 and ColGemma3
Browse files
app.py
CHANGED
|
@@ -143,6 +143,10 @@ def unload_models():
|
|
| 143 |
@spaces.GPU
|
| 144 |
def index_bigemma_images(images: List[Image.Image]) -> torch.Tensor:
|
| 145 |
"""Index images with BiGemma3 model."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
model, processor = doc_index.bigemma_model, doc_index.bigemma_processor
|
| 147 |
batch_images = processor.process_images(images).to(device)
|
| 148 |
embeddings = model(**batch_images, embedding_dim=768)
|
|
@@ -151,6 +155,10 @@ def index_bigemma_images(images: List[Image.Image]) -> torch.Tensor:
|
|
| 151 |
@spaces.GPU
|
| 152 |
def index_colgemma_images(images: List[Image.Image]) -> torch.Tensor:
|
| 153 |
"""Index images with ColGemma3 model."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
model, processor = doc_index.colgemma_model, doc_index.colgemma_processor
|
| 155 |
batch_images = processor.process_images(images).to(device)
|
| 156 |
embeddings = model(**batch_images)
|
|
@@ -175,27 +183,13 @@ def index_document(pdf_file, model_choice: str):
|
|
| 175 |
|
| 176 |
# Index with BiGemma3
|
| 177 |
if model_choice in ["NetraEmbed (BiGemma3)", "Both"]:
|
| 178 |
-
|
| 179 |
-
status.append("β³ Loading BiGemma3 model...")
|
| 180 |
-
load_bigemma_model()
|
| 181 |
-
status.append("β BiGemma3 loaded")
|
| 182 |
-
else:
|
| 183 |
-
status.append("β Using cached BiGemma3 model")
|
| 184 |
-
|
| 185 |
-
status.append("β³ Encoding images with BiGemma3...")
|
| 186 |
doc_index.bigemma_embeddings = index_bigemma_images(doc_index.images)
|
| 187 |
status.append(f"β Indexed with BiGemma3 (shape: {doc_index.bigemma_embeddings.shape})")
|
| 188 |
|
| 189 |
# Index with ColGemma3
|
| 190 |
if model_choice in ["ColNetraEmbed (ColGemma3)", "Both"]:
|
| 191 |
-
|
| 192 |
-
status.append("β³ Loading ColGemma3 model...")
|
| 193 |
-
load_colgemma_model()
|
| 194 |
-
status.append("β ColGemma3 loaded")
|
| 195 |
-
else:
|
| 196 |
-
status.append("β Using cached ColGemma3 model")
|
| 197 |
-
|
| 198 |
-
status.append("β³ Encoding images with ColGemma3...")
|
| 199 |
doc_index.colgemma_embeddings = index_colgemma_images(doc_index.images)
|
| 200 |
status.append(f"β Indexed with ColGemma3 (shape: {doc_index.colgemma_embeddings.shape})")
|
| 201 |
|
|
@@ -293,6 +287,10 @@ def generate_colgemma_heatmap(
|
|
| 293 |
@spaces.GPU
|
| 294 |
def query_bigemma(query: str, top_k: int) -> Tuple[str, List]:
|
| 295 |
"""Query indexed documents with BiGemma3."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
model, processor = doc_index.bigemma_model, doc_index.bigemma_processor
|
| 297 |
|
| 298 |
# Encode query
|
|
@@ -322,6 +320,10 @@ def query_bigemma(query: str, top_k: int) -> Tuple[str, List]:
|
|
| 322 |
@spaces.GPU
|
| 323 |
def query_colgemma(query: str, top_k: int, show_heatmap: bool = False) -> Tuple[str, List]:
|
| 324 |
"""Query indexed documents with ColGemma3."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
model, processor = doc_index.colgemma_model, doc_index.colgemma_processor
|
| 326 |
|
| 327 |
# Encode query
|
|
|
|
| 143 |
@spaces.GPU
|
| 144 |
def index_bigemma_images(images: List[Image.Image]) -> torch.Tensor:
|
| 145 |
"""Index images with BiGemma3 model."""
|
| 146 |
+
# Ensure model is loaded
|
| 147 |
+
if doc_index.bigemma_model is None:
|
| 148 |
+
load_bigemma_model()
|
| 149 |
+
|
| 150 |
model, processor = doc_index.bigemma_model, doc_index.bigemma_processor
|
| 151 |
batch_images = processor.process_images(images).to(device)
|
| 152 |
embeddings = model(**batch_images, embedding_dim=768)
|
|
|
|
| 155 |
@spaces.GPU
|
| 156 |
def index_colgemma_images(images: List[Image.Image]) -> torch.Tensor:
|
| 157 |
"""Index images with ColGemma3 model."""
|
| 158 |
+
# Ensure model is loaded
|
| 159 |
+
if doc_index.colgemma_model is None:
|
| 160 |
+
load_colgemma_model()
|
| 161 |
+
|
| 162 |
model, processor = doc_index.colgemma_model, doc_index.colgemma_processor
|
| 163 |
batch_images = processor.process_images(images).to(device)
|
| 164 |
embeddings = model(**batch_images)
|
|
|
|
| 183 |
|
| 184 |
# Index with BiGemma3
|
| 185 |
if model_choice in ["NetraEmbed (BiGemma3)", "Both"]:
|
| 186 |
+
status.append("β³ Loading & encoding with BiGemma3...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
doc_index.bigemma_embeddings = index_bigemma_images(doc_index.images)
|
| 188 |
status.append(f"β Indexed with BiGemma3 (shape: {doc_index.bigemma_embeddings.shape})")
|
| 189 |
|
| 190 |
# Index with ColGemma3
|
| 191 |
if model_choice in ["ColNetraEmbed (ColGemma3)", "Both"]:
|
| 192 |
+
status.append("β³ Loading & encoding with ColGemma3...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
doc_index.colgemma_embeddings = index_colgemma_images(doc_index.images)
|
| 194 |
status.append(f"β Indexed with ColGemma3 (shape: {doc_index.colgemma_embeddings.shape})")
|
| 195 |
|
|
|
|
| 287 |
@spaces.GPU
|
| 288 |
def query_bigemma(query: str, top_k: int) -> Tuple[str, List]:
|
| 289 |
"""Query indexed documents with BiGemma3."""
|
| 290 |
+
# Ensure model is loaded
|
| 291 |
+
if doc_index.bigemma_model is None:
|
| 292 |
+
load_bigemma_model()
|
| 293 |
+
|
| 294 |
model, processor = doc_index.bigemma_model, doc_index.bigemma_processor
|
| 295 |
|
| 296 |
# Encode query
|
|
|
|
| 320 |
@spaces.GPU
|
| 321 |
def query_colgemma(query: str, top_k: int, show_heatmap: bool = False) -> Tuple[str, List]:
|
| 322 |
"""Query indexed documents with ColGemma3."""
|
| 323 |
+
# Ensure model is loaded
|
| 324 |
+
if doc_index.colgemma_model is None:
|
| 325 |
+
load_colgemma_model()
|
| 326 |
+
|
| 327 |
model, processor = doc_index.colgemma_model, doc_index.colgemma_processor
|
| 328 |
|
| 329 |
# Encode query
|