P01yH3dr0n commited on
Commit
a779b59
β€’
1 Parent(s): bfe47f0

Update images_history.py

Browse files
Files changed (1) hide show
  1. images_history.py +19 -0
images_history.py CHANGED
@@ -3,9 +3,11 @@ import gradio as gr
3
  import toml
4
  from math import ceil
5
  from pnginfo import read_info_from_image
 
6
 
7
  client_config = toml.load("config.toml")['client']
8
  num_of_imgs_per_page = 60
 
9
 
10
  def get_img_list(folder, page):
11
  img_list = [os.path.join(client_config['save_path'], folder, f) for f in os.listdir(os.path.join(client_config['save_path'], folder))]
@@ -28,6 +30,21 @@ def get_img_info(evt: gr.SelectData):
28
  index = evt.index
29
  return index, info, items
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def img_history_ui(tab):
32
  os.makedirs(client_config['save_path'], exist_ok=True)
33
  all_dirs = os.listdir(client_config['save_path'])
@@ -51,6 +68,7 @@ def img_history_ui(tab):
51
  selected_index = gr.Number(value=-1, visible=False, precision=0)
52
  with gr.Row():
53
  send_btn = gr.Button('ε‚ζ•°ε‘ι€εˆ°ζ–‡η”Ÿε›Ύ')
 
54
 
55
  img_dir_path.change(lambda path: get_img_list(path, 1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items])
56
  img_dir_path.blur(update_img_dir, inputs=None, outputs=img_dir_path)
@@ -61,5 +79,6 @@ def img_history_ui(tab):
61
  end_page.click(lambda path: get_img_list(path, -1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items])
62
  history_gallery.select(get_img_info, inputs=None, outputs=[selected_index, info, items])
63
  history_gallery.change(lambda: -1, outputs=selected_index)
 
64
 
65
  return send_btn, items
 
3
  import toml
4
  from math import ceil
5
  from pnginfo import read_info_from_image
6
+ from huggingface_hub import HfApi
7
 
8
  client_config = toml.load("config.toml")['client']
9
  num_of_imgs_per_page = 60
10
+ api = HfApi()
11
 
12
  def get_img_list(folder, page):
13
  img_list = [os.path.join(client_config['save_path'], folder, f) for f in os.listdir(os.path.join(client_config['save_path'], folder))]
 
30
  index = evt.index
31
  return index, info, items
32
 
33
+ def del_img(index, folder, page):
34
+ if index < 0:
35
+ return gr.Gallery(selected_index=None)
36
+ img_list = [os.path.join(client_config['save_path'], folder, f) for f in os.listdir(os.path.join(client_config['save_path'], folder))]
37
+ img_list.sort(key=lambda f: os.path.getctime(f), reverse=True)
38
+ if page == 0:
39
+ page = 1
40
+ elif page*num_of_imgs_per_page > len(img_list) or page < 0:
41
+ page = ceil(len(img_list)/num_of_imgs_per_page)
42
+ res = img_list[(page - 1)*num_of_imgs_per_page: page*num_of_imgs_per_page]
43
+ os.remove(res[index])
44
+ api.delete_file(path_in_repo=res[index], repo_id="P01yH3dr0n/naimages", repo_type="dataset", token=os.environ.get("hf_token"))
45
+ res.pop(index)
46
+ return gr.Gallery(value=res, selected_index=None)
47
+
48
  def img_history_ui(tab):
49
  os.makedirs(client_config['save_path'], exist_ok=True)
50
  all_dirs = os.listdir(client_config['save_path'])
 
68
  selected_index = gr.Number(value=-1, visible=False, precision=0)
69
  with gr.Row():
70
  send_btn = gr.Button('ε‚ζ•°ε‘ι€εˆ°ζ–‡η”Ÿε›Ύ')
71
+ del_btn = gr.Button('Delete')
72
 
73
  img_dir_path.change(lambda path: get_img_list(path, 1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items])
74
  img_dir_path.blur(update_img_dir, inputs=None, outputs=img_dir_path)
 
79
  end_page.click(lambda path: get_img_list(path, -1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items])
80
  history_gallery.select(get_img_info, inputs=None, outputs=[selected_index, info, items])
81
  history_gallery.change(lambda: -1, outputs=selected_index)
82
+ del_btn.click(del_img, inputs=[selected_index, img_dir_path, page_index], outputs=history_gallery)
83
 
84
  return send_btn, items