alexnasa commited on
Commit
d8a55e1
·
verified ·
1 Parent(s): 8bd5eb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -39,6 +39,9 @@ import numpy as np
39
  from huggingface_hub import snapshot_download, hf_hub_download
40
  import torch
41
 
 
 
 
42
  # # FLUX.1-dev
43
  # snapshot_download(
44
  # repo_id="black-forest-labs/FLUX.1-dev",
@@ -259,7 +262,7 @@ def generate_image(
259
  src_inputs = []
260
  use_words = []
261
  cur_run_time = time.strftime("%m%d-%H%M%S")
262
- tmp_dir_root = f"tmp/gradio_demo/{run_name}"
263
  temp_dir = f"{tmp_dir_root}/{session_id}/{cur_run_time}_{generate_random_string(4)}"
264
  os.makedirs(temp_dir, exist_ok=True)
265
  print(f"Temporary directory created: {temp_dir}")
@@ -452,6 +455,23 @@ def start_session(request: gr.Request):
452
  """
453
  return request.session_hash
454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
455
  css = """
456
  #col-container {
457
  margin: 0 auto;
@@ -595,6 +615,7 @@ if __name__ == "__main__":
595
  det_btns[i].click(det_seg_img, inputs=[images[i], captions[i]], outputs=[images[i]])
596
  vlm_btns[i].click(vlm_img_caption, inputs=[images[i]], outputs=[captions[i]])
597
  images[i].upload(vlm_img_caption, inputs=[images[i]], outputs=[captions[i]])
598
-
 
599
  demo.queue()
600
  demo.launch()
 
39
  from huggingface_hub import snapshot_download, hf_hub_download
40
  import torch
41
 
42
+
43
+ os.environ["XVERSE_PREPROCESSED_DATA"] = f"{os.getcwd()}/proprocess_data"
44
+
45
  # # FLUX.1-dev
46
  # snapshot_download(
47
  # repo_id="black-forest-labs/FLUX.1-dev",
 
262
  src_inputs = []
263
  use_words = []
264
  cur_run_time = time.strftime("%m%d-%H%M%S")
265
+ tmp_dir_root = f"{os.environ["XVERSE_PREPROCESSED_DATA"]}"
266
  temp_dir = f"{tmp_dir_root}/{session_id}/{cur_run_time}_{generate_random_string(4)}"
267
  os.makedirs(temp_dir, exist_ok=True)
268
  print(f"Temporary directory created: {temp_dir}")
 
455
  """
456
  return request.session_hash
457
 
458
+ # Cleanup on unload
459
+ def cleanup(request: gr.Request):
460
+ """
461
+ Clean up session-specific directories and temporary files when the user session ends.
462
+
463
+ This function is triggered when the Gradio demo is unloaded (e.g., when the user
464
+ closes the browser tab or navigates away). It removes all temporary files and
465
+ directories created during the user's session to free up storage space.
466
+
467
+ Args:
468
+ request (gr.Request): Gradio request object containing session information
469
+ """
470
+ sid = request.session_hash
471
+ if sid:
472
+ d1 = os.path.join(os.environ["XVERSE_PREPROCESSED_DATA"], sid)
473
+ shutil.rmtree(d1, ignore_errors=True)
474
+
475
  css = """
476
  #col-container {
477
  margin: 0 auto;
 
615
  det_btns[i].click(det_seg_img, inputs=[images[i], captions[i]], outputs=[images[i]])
616
  vlm_btns[i].click(vlm_img_caption, inputs=[images[i]], outputs=[captions[i]])
617
  images[i].upload(vlm_img_caption, inputs=[images[i]], outputs=[captions[i]])
618
+
619
+ demo.unload(cleanup)
620
  demo.queue()
621
  demo.launch()