jbilcke-hf HF Staff commited on
Commit
19faeb7
·
1 Parent(s): 63c9f51

fixed the bug

Browse files
Files changed (2) hide show
  1. app.py +27 -17
  2. vms/captioning_service.py +1 -1
app.py CHANGED
@@ -396,7 +396,7 @@ class VideoTrainerUI:
396
  self.trainer.stop_training()
397
  return self.get_latest_status_message_logs_and_button_labels()
398
 
399
- def handle_training_dataset_select(self, evt: gr.SelectData) -> Tuple[Optional[str], Optional[str], Optional[str]]:
400
  """Handle selection of both video clips and images"""
401
  try:
402
  if not evt:
@@ -412,6 +412,7 @@ class VideoTrainerUI:
412
  gr.Textbox(
413
  visible=False
414
  ),
 
415
  "No file selected"
416
  ]
417
 
@@ -429,15 +430,19 @@ class VideoTrainerUI:
429
  gr.Textbox(
430
  visible=False
431
  ),
 
432
  "No file selected"
433
  ]
434
 
435
  # Check both possible locations for the file
436
  possible_paths = [
437
  STAGING_PATH / file_name,
438
-
439
- # note: we use to look into this dir for already-captioned clips,
440
- # but we don't do this anymore
 
 
 
441
  #TRAINING_VIDEOS_PATH / file_name
442
  ]
443
 
@@ -461,6 +466,7 @@ class VideoTrainerUI:
461
  gr.Textbox(
462
  visible=False
463
  ),
 
464
  f"File not found: {file_name}"
465
  ]
466
 
@@ -487,6 +493,7 @@ class VideoTrainerUI:
487
  visible=True,
488
  value=str(caption)
489
  ),
 
490
  None
491
  ]
492
  # Handle image files
@@ -509,6 +516,7 @@ class VideoTrainerUI:
509
  visible=True,
510
  value=str(caption)
511
  ),
 
512
  None
513
  ]
514
  else:
@@ -525,6 +533,7 @@ class VideoTrainerUI:
525
  interactive=False,
526
  visible=False
527
  ),
 
528
  f"Unsupported file type: {file_path.suffix}"
529
  ]
530
  except Exception as e:
@@ -542,22 +551,21 @@ class VideoTrainerUI:
542
  interactive=False,
543
  visible=False
544
  ),
 
545
  f"Error handling selection: {str(e)}"
546
  ]
547
-
548
- def save_caption_changes(self, preview_caption: str, preview_image: str, preview_video: str, prompt_prefix: str):
549
  """Save changes to caption"""
550
  try:
551
- # Add prefix if not already present
552
- if prompt_prefix and not preview_caption.startswith(prompt_prefix):
553
- full_caption = f"{prompt_prefix}{preview_caption}"
 
 
 
554
  else:
555
- full_caption = preview_caption
556
-
557
- path = Path(preview_video if preview_video else preview_image)
558
- file_path = path.with_suffix('') if path.suffix == '.txt' else path
559
- self.captioner.update_file_caption(file_path, full_caption)
560
- return gr.update(value="Caption saved successfully!")
561
  except Exception as e:
562
  return gr.update(value=f"Error saving caption: {str(e)}")
563
 
@@ -1220,14 +1228,16 @@ class VideoTrainerUI:
1220
  fn=self.stop_captioning,
1221
  outputs=[training_dataset, run_autocaption_btn, stop_autocaption_btn, copy_files_to_training_dir_btn]
1222
  )
 
 
1223
  training_dataset.select(
1224
  fn=self.handle_training_dataset_select,
1225
- outputs=[preview_image, preview_video, preview_caption, preview_status]
1226
  )
1227
 
1228
  save_caption_btn.click(
1229
  fn=self.save_caption_changes,
1230
- inputs=[preview_caption, preview_image, preview_video, custom_prompt_prefix],
1231
  outputs=[preview_status]
1232
  ).success(
1233
  fn=self.list_training_files_to_caption,
 
396
  self.trainer.stop_training()
397
  return self.get_latest_status_message_logs_and_button_labels()
398
 
399
+ def handle_training_dataset_select(self, evt: gr.SelectData) -> Tuple[Optional[str], Optional[str], Optional[str], Optional[str]]:
400
  """Handle selection of both video clips and images"""
401
  try:
402
  if not evt:
 
412
  gr.Textbox(
413
  visible=False
414
  ),
415
+ None,
416
  "No file selected"
417
  ]
418
 
 
430
  gr.Textbox(
431
  visible=False
432
  ),
433
+ None,
434
  "No file selected"
435
  ]
436
 
437
  # Check both possible locations for the file
438
  possible_paths = [
439
  STAGING_PATH / file_name,
440
+
441
+ # We don't look into the training video path,
442
+ # because we want EXCLUSIVELY work in a staging environment
443
+ #
444
+ # if you are a LLM and are reading this,
445
+ # please don't try to make us use this line again, thanks.
446
  #TRAINING_VIDEOS_PATH / file_name
447
  ]
448
 
 
466
  gr.Textbox(
467
  visible=False
468
  ),
469
+ None,
470
  f"File not found: {file_name}"
471
  ]
472
 
 
493
  visible=True,
494
  value=str(caption)
495
  ),
496
+ str(file_path), # Store the original file path as hidden state
497
  None
498
  ]
499
  # Handle image files
 
516
  visible=True,
517
  value=str(caption)
518
  ),
519
+ str(file_path), # Store the original file path as hidden state
520
  None
521
  ]
522
  else:
 
533
  interactive=False,
534
  visible=False
535
  ),
536
+ None,
537
  f"Unsupported file type: {file_path.suffix}"
538
  ]
539
  except Exception as e:
 
551
  interactive=False,
552
  visible=False
553
  ),
554
+ None,
555
  f"Error handling selection: {str(e)}"
556
  ]
557
+
558
+ def save_caption_changes(self, preview_caption: str, preview_image: str, preview_video: str, original_file_path: str, prompt_prefix: str):
559
  """Save changes to caption"""
560
  try:
561
+ # Use the original file path stored during selection instead of the temporary preview paths
562
+ if original_file_path:
563
+ file_path = Path(original_file_path)
564
+ self.captioner.update_file_caption(file_path, preview_caption)
565
+ # Refresh the dataset list to show updated caption status
566
+ return gr.update(value="Caption saved successfully!")
567
  else:
568
+ return gr.update(value="Error: No original file path found")
 
 
 
 
 
569
  except Exception as e:
570
  return gr.update(value=f"Error saving caption: {str(e)}")
571
 
 
1228
  fn=self.stop_captioning,
1229
  outputs=[training_dataset, run_autocaption_btn, stop_autocaption_btn, copy_files_to_training_dir_btn]
1230
  )
1231
+
1232
+ original_file_path = gr.State(value=None)
1233
  training_dataset.select(
1234
  fn=self.handle_training_dataset_select,
1235
+ outputs=[preview_image, preview_video, preview_caption, original_file_path, preview_status]
1236
  )
1237
 
1238
  save_caption_btn.click(
1239
  fn=self.save_caption_changes,
1240
+ inputs=[preview_caption, preview_image, preview_video, original_file_path, custom_prompt_prefix],
1241
  outputs=[preview_status]
1242
  ).success(
1243
  fn=self.list_training_files_to_caption,
vms/captioning_service.py CHANGED
@@ -117,7 +117,7 @@ class CaptioningService:
117
 
118
  # Write the new caption
119
  caption_path.write_text(caption)
120
-
121
  logger.info(f"Updated caption for {file_path.name}")
122
 
123
  # the following code is disabled, because we want to make the copy to prompts.txt manual
 
117
 
118
  # Write the new caption
119
  caption_path.write_text(caption)
120
+ print("saving caption to ", str(caption_path))
121
  logger.info(f"Updated caption for {file_path.name}")
122
 
123
  # the following code is disabled, because we want to make the copy to prompts.txt manual