linoyts HF Staff commited on
Commit
9760de5
Β·
verified Β·
1 Parent(s): eaefc3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -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,42 @@ 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 +172,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 +322,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 +333,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 +486,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
+ # Check common gradio temp locations
69
+ gradio_temp_dirs = [
70
+ "/tmp/gradio",
71
+ os.path.join(tempfile.gettempdir(), "gradio"),
72
+ os.path.join(os.getcwd(), "gradio")
73
+ ]
74
+
75
+ files_cleaned = 0
76
+
77
+ # Clean session directories in gradio temp folders
78
+ for temp_dir in gradio_temp_dirs:
79
+ session_dir = os.path.join(temp_dir, session_id)
80
+ if os.path.exists(session_dir):
81
+ try:
82
+ shutil.rmtree(session_dir)
83
+ files_cleaned += 1
84
+ print(f"Cleaned up session directory: {session_dir}")
85
+ except (OSError, FileNotFoundError):
86
+ pass
87
+
88
+ if files_cleaned > 0:
89
+ print(f"Session cleanup completed: {files_cleaned} directories removed for session {session_id}")
90
+
91
+ except Exception as e:
92
+ print(f"Error during session cleanup: {e}")
93
+
94
  @spaces.GPU()
95
  def read_video(video) -> torch.Tensor:
96
  """
 
172
  # Process video for canny edges
173
  processed_video = process_video_for_canny(video, width, height)
174
 
 
175
  output_filename = "control.mp4"
176
  video_uint8_frames = [(frame * 255).astype(np.uint8) for frame in processed_video]
177
  with imageio.get_writer(output_filename, fps=FPS, quality=8, macro_block_size=1) as writer:
 
322
  return None, seed
323
 
324
  # Create Gradio interface
325
+ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Lexend Deca"), "sans-serif"]), delete_cache=(60, 3600)) as demo:
326
  gr.Markdown(
327
  """
328
  # Canny Control LTX Video Distilled
 
333
  """
334
  )
335
 
 
 
 
336
  with gr.Row():
337
  with gr.Column(scale=1):
338
 
 
486
  show_progress=True
487
  )
488
 
489
+ # Add the unload event handler for cleanup
490
+ demo.unload(cleanup_session_files)
491
+
492
  if __name__ == "__main__":
493
  demo.launch()