Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,11 +57,14 @@
|
|
| 57 |
# demo.launch()
|
| 58 |
|
| 59 |
|
|
|
|
|
|
|
| 60 |
import gradio as gr
|
| 61 |
import subprocess
|
| 62 |
import os
|
| 63 |
import random
|
| 64 |
from PIL import Image
|
|
|
|
| 65 |
|
| 66 |
# === Setup Paths ===
|
| 67 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
@@ -99,58 +102,33 @@ def get_random_images():
|
|
| 99 |
generate_images()
|
| 100 |
image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 101 |
random_images = random.sample(image_files, min(10, len(image_files)))
|
| 102 |
-
|
| 103 |
-
return images, random_images
|
| 104 |
|
| 105 |
# === Save Image Function ===
|
| 106 |
-
def save_image(
|
| 107 |
-
image_path = os.path.join(OUTPUT_DIR,
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# === Gradio Interface ===
|
| 111 |
def generate_and_display():
|
| 112 |
-
images,
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 117 |
generate_button = gr.Button("Generate New Designs")
|
| 118 |
-
output_gallery = gr.Gallery(label="Generated Designs", columns=5
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
generate_button.click(fn=generate_and_display, inputs=None, outputs=[output_gallery, image_names, saved_image_path])
|
| 125 |
-
|
| 126 |
-
# When user selects image from gallery, output selected image filename and show the save button
|
| 127 |
-
def on_select(idx, names):
|
| 128 |
-
if names and idx is not None and 0 <= idx < len(names):
|
| 129 |
-
return names[idx]
|
| 130 |
-
return None
|
| 131 |
-
|
| 132 |
-
selected_image = gr.Textbox(label="Selected Image", visible=False)
|
| 133 |
-
output_gallery.select(fn=on_select, inputs=[gr.Gallery.index, image_names], outputs=selected_image)
|
| 134 |
-
|
| 135 |
-
# Save Button, outputs a downloadable href link to the image path
|
| 136 |
-
with gr.Row(visible=False) as save_row:
|
| 137 |
-
save_button = gr.Button("Save Selected Image")
|
| 138 |
-
download_link = gr.File(label="Download Saved Image")
|
| 139 |
-
|
| 140 |
-
def show_save_button(selected_img_path):
|
| 141 |
-
# Show save button only if an image is selected
|
| 142 |
-
return gr.Row.update(visible=bool(selected_img_path))
|
| 143 |
-
|
| 144 |
-
selected_image.change(fn=show_save_button, inputs=selected_image, outputs=save_row)
|
| 145 |
-
|
| 146 |
-
def save_selected_image(selected_img_path):
|
| 147 |
-
if selected_img_path and os.path.exists(selected_img_path):
|
| 148 |
-
# Return path to file for download
|
| 149 |
-
return selected_img_path
|
| 150 |
-
return None
|
| 151 |
-
|
| 152 |
-
save_button.click(fn=save_selected_image, inputs=selected_image, outputs=download_link)
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
demo.launch()
|
| 156 |
-
|
|
|
|
| 57 |
# demo.launch()
|
| 58 |
|
| 59 |
|
| 60 |
+
|
| 61 |
+
|
| 62 |
import gradio as gr
|
| 63 |
import subprocess
|
| 64 |
import os
|
| 65 |
import random
|
| 66 |
from PIL import Image
|
| 67 |
+
import shutil
|
| 68 |
|
| 69 |
# === Setup Paths ===
|
| 70 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
| 102 |
generate_images()
|
| 103 |
image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 104 |
random_images = random.sample(image_files, min(10, len(image_files)))
|
| 105 |
+
return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images], image_files
|
|
|
|
| 106 |
|
| 107 |
# === Save Image Function ===
|
| 108 |
+
def save_image(image_filename):
|
| 109 |
+
image_path = os.path.join(OUTPUT_DIR, image_filename)
|
| 110 |
+
save_path = os.path.join(BASE_DIR, "saved_images", image_filename)
|
| 111 |
+
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
| 112 |
+
shutil.copy(image_path, save_path)
|
| 113 |
+
return f"Image saved as {save_path}"
|
| 114 |
|
| 115 |
# === Gradio Interface ===
|
| 116 |
def generate_and_display():
|
| 117 |
+
images, filenames = get_random_images()
|
| 118 |
+
buttons = [gr.Button("Save", elem_id=filename) for filename in filenames]
|
| 119 |
+
return images, buttons
|
| 120 |
+
|
| 121 |
+
def save_button_clicked(image_filename):
|
| 122 |
+
return save_image(image_filename)
|
| 123 |
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 126 |
generate_button = gr.Button("Generate New Designs")
|
| 127 |
+
output_gallery = gr.Gallery(label="Generated Designs", columns=5)
|
| 128 |
+
generate_button.click(fn=generate_and_display, outputs=output_gallery)
|
| 129 |
+
|
| 130 |
+
# Add save buttons to each image dynamically
|
| 131 |
+
output_gallery.select(fn=save_button_clicked, inputs=output_gallery, outputs=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
demo.launch()
|
|
|