Commit ·
1d4eb4e
1
Parent(s): d58278e
Enhance Gallery UI: add clear, download zip, improve layout
Browse files- ui/layout.py +45 -4
ui/layout.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from core.settings import *
|
| 4 |
|
|
@@ -35,6 +38,36 @@ def build_ui(event_handler_function):
|
|
| 35 |
image_paths.append(os.path.join(output_dir, fname))
|
| 36 |
return image_paths
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# All UI components, including tabs, must be created inside the same Gradio Blocks context.
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("# ImageGen")
|
|
@@ -69,12 +102,20 @@ def build_ui(event_handler_function):
|
|
| 69 |
|
| 70 |
# New Gallery tab to display generated images
|
| 71 |
with gr.TabItem("Gallery", id=5):
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
ui_components["gallery"] = gallery
|
| 76 |
-
#
|
|
|
|
|
|
|
| 77 |
refresh_btn.click(fn=load_gallery_images, outputs=gallery)
|
|
|
|
|
|
|
| 78 |
|
| 79 |
ui_components["tabs"] = tabs
|
| 80 |
ui_components["image_gen_tabs"] = tabs
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import zipfile
|
| 4 |
+
import tempfile
|
| 5 |
import gradio as gr
|
| 6 |
from core.settings import *
|
| 7 |
|
|
|
|
| 38 |
image_paths.append(os.path.join(output_dir, fname))
|
| 39 |
return image_paths
|
| 40 |
|
| 41 |
+
# Clear all images from the gallery folder and return an empty list for the UI.
|
| 42 |
+
def clear_gallery_images():
|
| 43 |
+
"""Remove every file in ``OUTPUT_DIR`` and return an empty list for the gallery.
|
| 44 |
+
This provides a quick way for users to reset the gallery view.
|
| 45 |
+
"""
|
| 46 |
+
output_dir = os.path.abspath(OUTPUT_DIR)
|
| 47 |
+
if os.path.isdir(output_dir):
|
| 48 |
+
for f in os.listdir(output_dir):
|
| 49 |
+
try:
|
| 50 |
+
os.remove(os.path.join(output_dir, f))
|
| 51 |
+
except Exception:
|
| 52 |
+
pass
|
| 53 |
+
return []
|
| 54 |
+
|
| 55 |
+
# Create a zip archive of all images in the gallery for downloading.
|
| 56 |
+
def download_gallery_zip():
|
| 57 |
+
"""Package all files in ``OUTPUT_DIR`` into a temporary zip file.
|
| 58 |
+
Returns the file path which Gradio will serve as a downloadable file.
|
| 59 |
+
"""
|
| 60 |
+
output_dir = os.path.abspath(OUTPUT_DIR)
|
| 61 |
+
tmp_fd, tmp_path = tempfile.mkstemp(suffix=".zip")
|
| 62 |
+
os.close(tmp_fd)
|
| 63 |
+
with zipfile.ZipFile(tmp_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 64 |
+
if os.path.isdir(output_dir):
|
| 65 |
+
for fname in sorted(os.listdir(output_dir)):
|
| 66 |
+
file_path = os.path.join(output_dir, fname)
|
| 67 |
+
if os.path.isfile(file_path):
|
| 68 |
+
zf.write(file_path, arcname=fname)
|
| 69 |
+
return tmp_path
|
| 70 |
+
|
| 71 |
# All UI components, including tabs, must be created inside the same Gradio Blocks context.
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
gr.Markdown("# ImageGen")
|
|
|
|
| 102 |
|
| 103 |
# New Gallery tab to display generated images
|
| 104 |
with gr.TabItem("Gallery", id=5):
|
| 105 |
+
# Row of control buttons for the gallery.
|
| 106 |
+
with gr.Row():
|
| 107 |
+
refresh_btn = gr.Button("Refresh Gallery", variant="secondary")
|
| 108 |
+
clear_btn = gr.Button("Clear Gallery", variant="secondary")
|
| 109 |
+
download_btn = gr.Button("Download All", variant="secondary")
|
| 110 |
+
# Main gallery component.
|
| 111 |
+
gallery = gr.Gallery(label="Generated Images", show_label=False, columns=4, height="auto", type="filepath")
|
| 112 |
ui_components["gallery"] = gallery
|
| 113 |
+
# Hidden file component for zip download.
|
| 114 |
+
download_file = gr.File(label="Download", visible=False)
|
| 115 |
+
# Bind buttons.
|
| 116 |
refresh_btn.click(fn=load_gallery_images, outputs=gallery)
|
| 117 |
+
clear_btn.click(fn=clear_gallery_images, outputs=gallery)
|
| 118 |
+
download_btn.click(fn=download_gallery_zip, outputs=download_file)
|
| 119 |
|
| 120 |
ui_components["tabs"] = tabs
|
| 121 |
ui_components["image_gen_tabs"] = tabs
|