Ashoka74 commited on
Commit
159a8d2
Β·
verified Β·
1 Parent(s): 3685f30

Update gradio_demo.py

Browse files
Files changed (1) hide show
  1. gradio_demo.py +23 -12
gradio_demo.py CHANGED
@@ -1372,19 +1372,20 @@ with block:
1372
 
1373
  # return composite
1374
 
1375
- class BackgroundManager:
1376
  def __init__(self, mask_mover):
1377
  self.original_bg = None # To store the original background
1378
- self.current_mask = None # To store the current mask applied
1379
  self.mask_mover = mask_mover # Reference to the MaskMover instance
1380
 
1381
  def update_background(self, new_bg):
1382
- """Set a new background."""
1383
  if new_bg is not None:
1384
- self.original_bg = new_bg.copy()
1385
- self.current_mask = None # Reset current mask when background changes
 
1386
  print("Background updated successfully.") # Debugging
1387
-
1388
  def reset_background(self):
1389
  """Reset the background to its original state"""
1390
  if self.original_bg is not None:
@@ -1462,24 +1463,32 @@ with block:
1462
  # outputs=[input_bg],
1463
  # show_progress=False
1464
  # )
1465
-
1466
- # Update the slider change functions to apply the current mask
 
 
 
 
 
 
 
 
1467
  x_slider.change(
1468
  fn=lambda x_pos: bg_manager.update_position(x_pos, y_slider.value, fg_scale_slider.value),
1469
  inputs=[x_slider],
1470
- outputs=[input_bg]
1471
  )
1472
 
1473
  y_slider.change(
1474
  fn=lambda y_pos: bg_manager.update_position(x_slider.value, y_pos, fg_scale_slider.value),
1475
  inputs=[y_slider],
1476
- outputs=[input_bg]
1477
  )
1478
 
1479
  fg_scale_slider.change(
1480
  fn=lambda scale: bg_manager.update_position(x_slider.value, y_slider.value, scale),
1481
  inputs=[fg_scale_slider],
1482
- outputs=[input_bg]
1483
  )
1484
 
1485
  # Update inputs list to include fg_scale_slider
@@ -1585,13 +1594,15 @@ with block:
1585
  outputs=[result_gallery], show_progress=True
1586
  )
1587
 
 
 
1588
  reset_button.click(
1589
  fn=lambda: bg_manager.reset_background(), # Call the reset function
1590
  inputs=[], # No inputs needed for reset
1591
  outputs=[input_bg], # Update the displayed background
1592
  show_progress=True
1593
  )
1594
-
1595
  example_quick_prompts.click(lambda x, y: ', '.join(y.split(', ')[:2] + [x[0]]), inputs=[example_quick_prompts, prompt], outputs=prompt, show_progress=False, queue=False)
1596
  example_quick_subjects.click(lambda x: x[0], inputs=example_quick_subjects, outputs=prompt, show_progress=False, queue=False)
1597
 
 
1372
 
1373
  # return composite
1374
 
1375
+ class BackgroundManager:
1376
  def __init__(self, mask_mover):
1377
  self.original_bg = None # To store the original background
1378
+ self.current_bg = None # Store current background
1379
  self.mask_mover = mask_mover # Reference to the MaskMover instance
1380
 
1381
  def update_background(self, new_bg):
1382
+ """Update the current background without affecting the mask"""
1383
  if new_bg is not None:
1384
+ if self.original_bg is None: # Store the original background only once
1385
+ self.original_bg = new_bg.copy()
1386
+ self.current_bg = new_bg.copy() # Overwrite the current background
1387
  print("Background updated successfully.") # Debugging
1388
+
1389
  def reset_background(self):
1390
  """Reset the background to its original state"""
1391
  if self.original_bg is not None:
 
1463
  # outputs=[input_bg],
1464
  # show_progress=False
1465
  # )
1466
+
1467
+ # Update the input_bg.change function to reset the mask and update the background
1468
+ input_bg.change(
1469
+ fn=lambda new_bg: bg_manager.update_background(new_bg), # Update the background
1470
+ inputs=[input_bg],
1471
+ outputs=[input_bg], # You may want to update other outputs as needed
1472
+ show_progress=False
1473
+ )
1474
+
1475
+ # Slider change functions
1476
  x_slider.change(
1477
  fn=lambda x_pos: bg_manager.update_position(x_pos, y_slider.value, fg_scale_slider.value),
1478
  inputs=[x_slider],
1479
+ outputs=[input_bg] # This should not reset the background
1480
  )
1481
 
1482
  y_slider.change(
1483
  fn=lambda y_pos: bg_manager.update_position(x_slider.value, y_pos, fg_scale_slider.value),
1484
  inputs=[y_slider],
1485
+ outputs=[input_bg] # This should not reset the background
1486
  )
1487
 
1488
  fg_scale_slider.change(
1489
  fn=lambda scale: bg_manager.update_position(x_slider.value, y_slider.value, scale),
1490
  inputs=[fg_scale_slider],
1491
+ outputs=[input_bg] # This should not reset the background
1492
  )
1493
 
1494
  # Update inputs list to include fg_scale_slider
 
1594
  outputs=[result_gallery], show_progress=True
1595
  )
1596
 
1597
+
1598
+ # Connect the reset button to the reset functionality
1599
  reset_button.click(
1600
  fn=lambda: bg_manager.reset_background(), # Call the reset function
1601
  inputs=[], # No inputs needed for reset
1602
  outputs=[input_bg], # Update the displayed background
1603
  show_progress=True
1604
  )
1605
+
1606
  example_quick_prompts.click(lambda x, y: ', '.join(y.split(', ')[:2] + [x[0]]), inputs=[example_quick_prompts, prompt], outputs=prompt, show_progress=False, queue=False)
1607
  example_quick_subjects.click(lambda x: x[0], inputs=example_quick_subjects, outputs=prompt, show_progress=False, queue=False)
1608