Update app.py

#8
by linoyts HF Staff - opened
Files changed (1) hide show
  1. app.py +24 -6
app.py CHANGED
@@ -13,6 +13,9 @@ import imageio
13
  from controlnet_aux import CannyDetector
14
  from PIL import Image
15
  import cv2
 
 
 
16
 
17
  FPS = 24
18
  dtype = torch.bfloat16
@@ -52,9 +55,25 @@ pipeline.load_lora_weights(
52
  )
53
  pipeline.set_adapters([CONTROL_LORAS["canny"]["adapter_name"]], adapter_weights=[1.0])
54
 
55
-
56
  canny_processor = CannyDetector()
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  @spaces.GPU()
59
  def read_video(video) -> torch.Tensor:
60
  """
@@ -136,7 +155,6 @@ def process_input_video(reference_video, width, height,
136
  # Process video for canny edges
137
  processed_video = process_video_for_canny(video, width, height)
138
 
139
- # Create a preview video file for display
140
  output_filename = "control.mp4"
141
  video_uint8_frames = [(frame * 255).astype(np.uint8) for frame in processed_video]
142
  with imageio.get_writer(output_filename, fps=FPS, quality=8, macro_block_size=1) as writer:
@@ -287,7 +305,7 @@ def generate_video(
287
  return None, seed
288
 
289
  # Create Gradio interface
290
- with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"), "sans-serif"])) as demo:
291
  gr.Markdown(
292
  """
293
  # Canny Control LTX Video Distilled
@@ -298,9 +316,6 @@ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"),
298
  """
299
  )
300
 
301
- # State variables
302
- #current_lora_state = gr.State(value=None)
303
-
304
  with gr.Row():
305
  with gr.Column(scale=1):
306
 
@@ -454,5 +469,8 @@ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"),
454
  show_progress=True
455
  )
456
 
 
 
 
457
  if __name__ == "__main__":
458
  demo.launch()
 
13
  from controlnet_aux import CannyDetector
14
  from PIL import Image
15
  import cv2
16
+ import shutil
17
+ import glob
18
+ from pathlib import Path
19
 
20
  FPS = 24
21
  dtype = torch.bfloat16
 
55
  )
56
  pipeline.set_adapters([CONTROL_LORAS["canny"]["adapter_name"]], adapter_weights=[1.0])
57
 
 
58
  canny_processor = CannyDetector()
59
 
60
+ current_dir = Path(__file__).parent
61
+
62
+ def cleanup_session_files(request: gr.Request):
63
+ """Clean up session-specific temporary files when user disconnects"""
64
+ try:
65
+ # Get the session ID from the request
66
+ session_id = request.session_hash
67
+
68
+ # Clean up the session directory
69
+ session_dir = os.path.join("/tmp/gradio", session_id)
70
+ if os.path.exists(session_dir):
71
+ shutil.rmtree(session_dir)
72
+ print(f"Cleaned up session directory: {session_dir}")
73
+
74
+ except Exception as e:
75
+ print(f"Error during session cleanup: {e}")
76
+
77
  @spaces.GPU()
78
  def read_video(video) -> torch.Tensor:
79
  """
 
155
  # Process video for canny edges
156
  processed_video = process_video_for_canny(video, width, height)
157
 
 
158
  output_filename = "control.mp4"
159
  video_uint8_frames = [(frame * 255).astype(np.uint8) for frame in processed_video]
160
  with imageio.get_writer(output_filename, fps=FPS, quality=8, macro_block_size=1) as writer:
 
305
  return None, seed
306
 
307
  # Create Gradio interface
308
+ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"), "sans-serif"]), delete_cache=(60, 3600)) as demo:
309
  gr.Markdown(
310
  """
311
  # Canny Control LTX Video Distilled
 
316
  """
317
  )
318
 
 
 
 
319
  with gr.Row():
320
  with gr.Column(scale=1):
321
 
 
469
  show_progress=True
470
  )
471
 
472
+ # Add the unload event handler for cleanup
473
+ demo.unload(cleanup_session_files)
474
+
475
  if __name__ == "__main__":
476
  demo.launch()