JS6969 commited on
Commit
290985d
·
verified ·
1 Parent(s): 305fb2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -15
app.py CHANGED
@@ -359,9 +359,11 @@ def _build_inputs(im: Image.Image, instr: str, dtype) -> Dict[str, Any]:
359
  inputs["pixel_values"] = inputs["pixel_values"].to(dtype)
360
  return inputs
361
 
362
- @gpu
363
  @torch.no_grad()
364
  def caption_once(im: Image.Image, instr: str, temp: float, top_p: float, max_tokens: int) -> str:
 
 
 
365
  model, device, dtype = get_model()
366
  inputs = _build_inputs(im, instr, dtype)
367
  inputs = {k: (v.to(device) if hasattr(v, "to") else v) for k, v in inputs.items()}
@@ -401,7 +403,7 @@ def run_batch(
401
  time_budget_s: float | None = None, # respects Zero-GPU window
402
  progress: gr.Progress = gr.Progress(track_tqdm=True), # drives the progress bar
403
  ) -> Tuple[List[dict], list, list, str, List[str], int, int]:
404
- """
405
  Returns:
406
  session_rows, gallery_pairs, table_rows, status_text,
407
  leftover_files, processed_in_this_call, total_in_this_call
@@ -419,7 +421,7 @@ def run_batch(
419
 
420
  start = time.time()
421
  leftover: List[str] = []
422
-
423
  # Progress bar shows inside the GPU worker
424
  for idx, path in enumerate(progress.tqdm(files, desc="Captioning")):
425
  try:
@@ -436,10 +438,10 @@ def run_batch(
436
  session_rows.append({"filename": filename, "caption": cap, "path": path, "thumb_path": thumb})
437
  processed += 1
438
 
439
- # Time-slice to avoid Zero GPU timeouts
440
  if time_budget_s and (time.time() - start) >= float(time_budget_s):
441
  leftover = files[idx+1:]
442
- break
443
 
444
  save_session(session_rows)
445
  gallery_pairs = [((r.get("thumb_path") or r.get("path")), r.get("caption",""))
@@ -455,7 +457,7 @@ def run_batch(
455
  total,
456
  )
457
 
458
- # Ensure Spaces detects at least one GPU function at startup
459
  @gpu
460
  @torch.no_grad()
461
  def _gpu_startup_warm():
@@ -689,7 +691,7 @@ setTimeout(() => {{
689
  # ---- Results (UNCHANGED POSITION): Gallery left, Table right
690
  rows_state = gr.State(load_session())
691
  autosave_md = gr.Markdown("Ready.")
692
- progress_md = gr.Markdown("")
693
  remaining_state = gr.State([]) # for manual step mode
694
 
695
  with gr.Row():
@@ -802,7 +804,7 @@ setTimeout(() => {{
802
  prog = f"Batch progress: {done}/{total} processed in this step • Remaining overall: {len(remaining)}"
803
  return new_rows, gal, tbl, stamp, remaining, panel_vis, gr.update(value=msg), gr.update(value=prog)
804
 
805
- # Auto / All-at-once (still obey time budget)
806
  new_rows, gal, tbl, stamp, leftover, done, total = run_batch(
807
  files, rows or [], instr, t, p, m, int(ms), float(budget_s)
808
  )
@@ -811,7 +813,6 @@ setTimeout(() => {{
811
  prog = f"Batch progress: {done}/{total} processed in this call • Remaining: {len(leftover)}"
812
  return new_rows, gal, tbl, stamp, leftover, panel_vis, gr.update(value=msg), gr.update(value=prog)
813
 
814
-
815
  run_button.click(
816
  _run_click,
817
  inputs=[input_files, rows_state, instruction_preview, max_side, chunk_mode, chunk_size, gpu_budget],
@@ -835,15 +836,10 @@ setTimeout(() => {{
835
  prog = f"Batch progress: {done}/{total} processed in this step • Remaining overall: {len(leftover)}"
836
  return new_rows, msg, panel_vis, leftover, gal, tbl, stamp, gr.update(value=prog)
837
 
838
- # hookup (with gpu_budget slider)
839
  step_next.click(
840
  _step_next,
841
  inputs=[remaining_state, rows_state, instruction_preview, max_side, chunk_size, gpu_budget],
842
  outputs=[rows_state, step_msg, step_panel, remaining_state, gallery, table, autosave_md, progress_md]
843
- )
844
-
845
- # if you don't have gpu_budget: wrap with a lambda and pass 55 as above
846
-
847
  )
848
 
849
  def _step_finish():
@@ -884,7 +880,6 @@ setTimeout(() => {{
884
  alias_table = gr.Dataframe(
885
  headers=["shape (token or synonyms)", "name to insert"],
886
  value=init_rows,
887
- col_count=(2, "fixed"),
888
  row_count=(max(1, len(init_rows)), "dynamic"),
889
  datatype=["str","str"],
890
  type="array",
 
359
  inputs["pixel_values"] = inputs["pixel_values"].to(dtype)
360
  return inputs
361
 
 
362
  @torch.no_grad()
363
  def caption_once(im: Image.Image, instr: str, temp: float, top_p: float, max_tokens: int) -> str:
364
+ """
365
+ NOTE: Not GPU-decorated on purpose; call this only from within a @gpu function.
366
+ """
367
  model, device, dtype = get_model()
368
  inputs = _build_inputs(im, instr, dtype)
369
  inputs = {k: (v.to(device) if hasattr(v, "to") else v) for k, v in inputs.items()}
 
403
  time_budget_s: float | None = None, # respects Zero-GPU window
404
  progress: gr.Progress = gr.Progress(track_tqdm=True), # drives the progress bar
405
  ) -> Tuple[List[dict], list, list, str, List[str], int, int]:
406
+ """
407
  Returns:
408
  session_rows, gallery_pairs, table_rows, status_text,
409
  leftover_files, processed_in_this_call, total_in_this_call
 
421
 
422
  start = time.time()
423
  leftover: List[str] = []
424
+
425
  # Progress bar shows inside the GPU worker
426
  for idx, path in enumerate(progress.tqdm(files, desc="Captioning")):
427
  try:
 
438
  session_rows.append({"filename": filename, "caption": cap, "path": path, "thumb_path": thumb})
439
  processed += 1
440
 
441
+ # Time-slice to avoid Zero GPU timeouts
442
  if time_budget_s and (time.time() - start) >= float(time_budget_s):
443
  leftover = files[idx+1:]
444
+ break
445
 
446
  save_session(session_rows)
447
  gallery_pairs = [((r.get("thumb_path") or r.get("path")), r.get("caption",""))
 
457
  total,
458
  )
459
 
460
+ # Ensure Spaces detects at least one GPU function at startup (without CUDA in main proc)
461
  @gpu
462
  @torch.no_grad()
463
  def _gpu_startup_warm():
 
691
  # ---- Results (UNCHANGED POSITION): Gallery left, Table right
692
  rows_state = gr.State(load_session())
693
  autosave_md = gr.Markdown("Ready.")
694
+ progress_md = gr.Markdown("")
695
  remaining_state = gr.State([]) # for manual step mode
696
 
697
  with gr.Row():
 
804
  prog = f"Batch progress: {done}/{total} processed in this step • Remaining overall: {len(remaining)}"
805
  return new_rows, gal, tbl, stamp, remaining, panel_vis, gr.update(value=msg), gr.update(value=prog)
806
 
807
+ # Auto / All-at-once (still obey time budget)
808
  new_rows, gal, tbl, stamp, leftover, done, total = run_batch(
809
  files, rows or [], instr, t, p, m, int(ms), float(budget_s)
810
  )
 
813
  prog = f"Batch progress: {done}/{total} processed in this call • Remaining: {len(leftover)}"
814
  return new_rows, gal, tbl, stamp, leftover, panel_vis, gr.update(value=msg), gr.update(value=prog)
815
 
 
816
  run_button.click(
817
  _run_click,
818
  inputs=[input_files, rows_state, instruction_preview, max_side, chunk_mode, chunk_size, gpu_budget],
 
836
  prog = f"Batch progress: {done}/{total} processed in this step • Remaining overall: {len(leftover)}"
837
  return new_rows, msg, panel_vis, leftover, gal, tbl, stamp, gr.update(value=prog)
838
 
 
839
  step_next.click(
840
  _step_next,
841
  inputs=[remaining_state, rows_state, instruction_preview, max_side, chunk_size, gpu_budget],
842
  outputs=[rows_state, step_msg, step_panel, remaining_state, gallery, table, autosave_md, progress_md]
 
 
 
 
843
  )
844
 
845
  def _step_finish():
 
880
  alias_table = gr.Dataframe(
881
  headers=["shape (token or synonyms)", "name to insert"],
882
  value=init_rows,
 
883
  row_count=(max(1, len(init_rows)), "dynamic"),
884
  datatype=["str","str"],
885
  type="array",