Mike0021 commited on
Commit
9049aa0
·
verified ·
1 Parent(s): d2e7054

Fix Server API progress signature

Browse files
Files changed (1) hide show
  1. app.py +2 -14
app.py CHANGED
@@ -277,16 +277,13 @@ def _run_search(
277
  custom_candidates: list[Candidate],
278
  include_showcase: bool,
279
  dimension: int,
280
- progress: gr.Progress,
281
  ) -> tuple[torch.Tensor, torch.Tensor, list[Candidate], float, bool]:
282
  global _SHOWCASE_EMBEDDINGS
283
 
284
  started = time.perf_counter()
285
  was_cold = include_showcase and _SHOWCASE_EMBEDDINGS is None
286
- progress(0.04, desc="Allocating the 9B model on GPU")
287
  MODEL.to("cuda")
288
  try:
289
- progress(0.16, desc=f"Encoding the {query_kind} query")
290
  query_embedding = _encode([query_payload], query=True)
291
  document_blocks: list[torch.Tensor] = []
292
  candidates: list[Candidate] = []
@@ -295,7 +292,6 @@ def _run_search(
295
  with _CACHE_LOCK:
296
  cached = _SHOWCASE_EMBEDDINGS
297
  if cached is None:
298
- progress(0.30, desc="Mapping the curated multimodal universe")
299
  encoded = _encode([item.payload for item in SHOWCASE], query=False)
300
  with _CACHE_LOCK:
301
  if _SHOWCASE_EMBEDDINGS is None:
@@ -305,7 +301,6 @@ def _run_search(
305
  candidates.extend(SHOWCASE)
306
 
307
  if custom_candidates:
308
- progress(0.72, desc="Encoding your candidate collection")
309
  custom_embeddings = _encode([item.payload for item in custom_candidates], query=False)
310
  document_blocks.append(custom_embeddings)
311
  candidates.extend(custom_candidates)
@@ -314,7 +309,6 @@ def _run_search(
314
  raise gr.Error("Include the showcase universe or add at least one candidate.")
315
 
316
  documents = torch.cat(document_blocks, dim=0)
317
- progress(0.92, desc=f"Ranking in {dimension:,} dimensions")
318
  return query_embedding, documents, candidates, time.perf_counter() - started, was_cold
319
  finally:
320
  MODEL.to("cpu")
@@ -335,15 +329,11 @@ def _pair_duration(*args: Any, **kwargs: Any) -> int:
335
  def _run_pair(
336
  query_payload: Any,
337
  candidate_payload: Any,
338
- progress: gr.Progress,
339
  ) -> tuple[torch.Tensor, torch.Tensor, float]:
340
  started = time.perf_counter()
341
- progress(0.08, desc="Allocating the model on GPU")
342
  MODEL.to("cuda")
343
  try:
344
- progress(0.35, desc="Encoding the query")
345
  query = _encode([query_payload], query=True)
346
- progress(0.68, desc="Encoding the candidate")
347
  candidate = _encode([candidate_payload], query=False)
348
  return query, candidate, time.perf_counter() - started
349
  finally:
@@ -384,7 +374,6 @@ def search_api(
384
  candidate_media: list[FileData] | None = None,
385
  include_showcase: bool = True,
386
  dimension: int = 1024,
387
- progress: gr.Progress = gr.Progress(track_tqdm=True),
388
  ) -> dict[str, Any]:
389
  """Search a mixed media collection and return structured visualization data."""
390
  dimension = int(dimension)
@@ -395,7 +384,7 @@ def search_api(
395
  custom_candidates = _parse_text_candidates(custom_texts) + _parse_media_candidates(candidate_media)
396
  with _INFERENCE_LOCK:
397
  query, documents, candidates, elapsed, was_cold = _run_search(
398
- query_payload, query_kind, custom_candidates, bool(include_showcase), dimension, progress,
399
  )
400
 
401
  dimension_map = _dimension_scores(query, documents)
@@ -456,7 +445,6 @@ def compare_api(
456
  candidate_image: FileData | None = None,
457
  candidate_video: FileData | None = None,
458
  dimension: int = 1024,
459
- progress: gr.Progress = gr.Progress(track_tqdm=True),
460
  ) -> dict[str, Any]:
461
  """Compare two supported inputs and return structured visualization data."""
462
  dimension = int(dimension)
@@ -468,7 +456,7 @@ def compare_api(
468
  candidate_text, candidate_image, candidate_video
469
  )
470
  with _INFERENCE_LOCK:
471
- query, candidate, elapsed = _run_pair(query_payload, candidate_payload, progress)
472
 
473
  scores = {
474
  dim: float((_truncate_normalize(query, dim) @ _truncate_normalize(candidate, dim).T).item())
 
277
  custom_candidates: list[Candidate],
278
  include_showcase: bool,
279
  dimension: int,
 
280
  ) -> tuple[torch.Tensor, torch.Tensor, list[Candidate], float, bool]:
281
  global _SHOWCASE_EMBEDDINGS
282
 
283
  started = time.perf_counter()
284
  was_cold = include_showcase and _SHOWCASE_EMBEDDINGS is None
 
285
  MODEL.to("cuda")
286
  try:
 
287
  query_embedding = _encode([query_payload], query=True)
288
  document_blocks: list[torch.Tensor] = []
289
  candidates: list[Candidate] = []
 
292
  with _CACHE_LOCK:
293
  cached = _SHOWCASE_EMBEDDINGS
294
  if cached is None:
 
295
  encoded = _encode([item.payload for item in SHOWCASE], query=False)
296
  with _CACHE_LOCK:
297
  if _SHOWCASE_EMBEDDINGS is None:
 
301
  candidates.extend(SHOWCASE)
302
 
303
  if custom_candidates:
 
304
  custom_embeddings = _encode([item.payload for item in custom_candidates], query=False)
305
  document_blocks.append(custom_embeddings)
306
  candidates.extend(custom_candidates)
 
309
  raise gr.Error("Include the showcase universe or add at least one candidate.")
310
 
311
  documents = torch.cat(document_blocks, dim=0)
 
312
  return query_embedding, documents, candidates, time.perf_counter() - started, was_cold
313
  finally:
314
  MODEL.to("cpu")
 
329
  def _run_pair(
330
  query_payload: Any,
331
  candidate_payload: Any,
 
332
  ) -> tuple[torch.Tensor, torch.Tensor, float]:
333
  started = time.perf_counter()
 
334
  MODEL.to("cuda")
335
  try:
 
336
  query = _encode([query_payload], query=True)
 
337
  candidate = _encode([candidate_payload], query=False)
338
  return query, candidate, time.perf_counter() - started
339
  finally:
 
374
  candidate_media: list[FileData] | None = None,
375
  include_showcase: bool = True,
376
  dimension: int = 1024,
 
377
  ) -> dict[str, Any]:
378
  """Search a mixed media collection and return structured visualization data."""
379
  dimension = int(dimension)
 
384
  custom_candidates = _parse_text_candidates(custom_texts) + _parse_media_candidates(candidate_media)
385
  with _INFERENCE_LOCK:
386
  query, documents, candidates, elapsed, was_cold = _run_search(
387
+ query_payload, query_kind, custom_candidates, bool(include_showcase), dimension,
388
  )
389
 
390
  dimension_map = _dimension_scores(query, documents)
 
445
  candidate_image: FileData | None = None,
446
  candidate_video: FileData | None = None,
447
  dimension: int = 1024,
 
448
  ) -> dict[str, Any]:
449
  """Compare two supported inputs and return structured visualization data."""
450
  dimension = int(dimension)
 
456
  candidate_text, candidate_image, candidate_video
457
  )
458
  with _INFERENCE_LOCK:
459
+ query, candidate, elapsed = _run_pair(query_payload, candidate_payload)
460
 
461
  scores = {
462
  dim: float((_truncate_normalize(query, dim) @ _truncate_normalize(candidate, dim).T).item())