import os import gradio as gr import toml from math import ceil from pnginfo import read_info_from_image client_config = toml.load("config.toml")['client'] num_of_imgs_per_page = 60 def get_img_list(folder, page): img_list = [os.path.join(client_config['save_path'], folder, f) for f in os.listdir(os.path.join(client_config['save_path'], folder))] img_list.sort(key=lambda f: os.path.getctime(f), reverse=True) if page == 0: page = 1 elif page*num_of_imgs_per_page > len(img_list) or page < 0: page = ceil(len(img_list)/num_of_imgs_per_page) res = img_list[(page - 1)*num_of_imgs_per_page: page*num_of_imgs_per_page] return gr.Gallery(value=res, selected_index=None), page def update_img_dir(): all_dirs = os.listdir(client_config['save_path']) all_dirs.sort(reverse=True) img_dir_path = gr.update(choices=all_dirs) return img_dir_path def get_img_info(evt: gr.SelectData): info, items = read_info_from_image(evt.value['image']['path']) index = evt.index return index, info, items def img_history_ui(tab): os.makedirs(client_config['save_path'], exist_ok=True) all_dirs = os.listdir(client_config['save_path']) all_dirs.sort(reverse=True) with gr.Row(): img_dir_path = gr.Dropdown(choices=all_dirs, value=None, label='图片文件夹') tab.select(fn=update_img_dir, inputs=None, outputs=img_dir_path) with gr.Row(): with gr.Column(scale=2): with gr.Row(): first_page = gr.Button('第一页') prev_page = gr.Button('前一页') page_index = gr.Number(value=1, label="页码", precision=0) next_page = gr.Button('下一页') end_page = gr.Button('末尾页') with gr.Row(): history_gallery = gr.Gallery(show_label=False, columns=6) with gr.Column(scale=1): info = gr.HTML() items = gr.JSON(value=None, visible=False) selected_index = gr.Number(value=-1, visible=False, precision=0) with gr.Row(): send_btn = gr.Button('参数发送到文生图') img_dir_path.change(lambda path: get_img_list(path, 1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items]) img_dir_path.blur(update_img_dir, inputs=None, outputs=img_dir_path) first_page.click(lambda path: get_img_list(path, 1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items]) prev_page.click(lambda path, p: get_img_list(path, p - 1) + ('', None), inputs=[img_dir_path, page_index], outputs=[history_gallery, page_index, info, items]) page_index.submit(lambda path, p: get_img_list(path, p) + ('', None), inputs=[img_dir_path, page_index], outputs=[history_gallery, page_index, info, items]) next_page.click(lambda path, p: get_img_list(path, p + 1) + ('', None), inputs=[img_dir_path, page_index], outputs=[history_gallery, page_index, info, items]) end_page.click(lambda path: get_img_list(path, -1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items]) history_gallery.select(get_img_info, inputs=None, outputs=[selected_index, info, items]) return send_btn, items