sahadev10 commited on
Commit
7daeac9
·
verified ·
1 Parent(s): f6e3de4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -13
app.py CHANGED
@@ -56,6 +56,7 @@
56
  # if __name__ == "__main__":
57
  # demo.launch()
58
 
 
59
  import gradio as gr
60
  import subprocess
61
  import os
@@ -98,7 +99,8 @@ def get_random_images():
98
  generate_images()
99
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
100
  random_images = random.sample(image_files, min(10, len(image_files)))
101
- return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images], random_images
 
102
 
103
  # === Save Image Function ===
104
  def save_image(image_name):
@@ -108,27 +110,47 @@ def save_image(image_name):
108
  # === Gradio Interface ===
109
  def generate_and_display():
110
  images, image_names = get_random_images()
111
- return images, image_names
112
 
113
  with gr.Blocks() as demo:
114
  gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
115
  generate_button = gr.Button("Generate New Designs")
116
- output_gallery = gr.Gallery(label="Generated Designs", columns=5).style(height=300)
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
- # Output for image names
119
- image_names = gr.Variable()
120
 
121
- generate_button.click(fn=generate_and_display, outputs=[output_gallery, image_names])
 
 
 
122
 
123
- # Add a button to save the selected image
124
- save_button = gr.Button("Save Selected Image")
 
125
 
126
- def save_selected_image(selected_image_name):
127
- if selected_image_name:
128
- return save_image(selected_image_name)
 
 
 
129
  return None
130
-
131
- output_gallery.select(fn=save_selected_image, inputs=image_names, outputs=save_button)
132
 
133
  if __name__ == "__main__":
134
  demo.launch()
 
 
56
  # if __name__ == "__main__":
57
  # demo.launch()
58
 
59
+
60
  import gradio as gr
61
  import subprocess
62
  import os
 
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
+ images = [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images]
103
+ return images, random_images
104
 
105
  # === Save Image Function ===
106
  def save_image(image_name):
 
110
  # === Gradio Interface ===
111
  def generate_and_display():
112
  images, image_names = get_random_images()
113
+ return images, image_names, None # Clear selected image path on generate
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, height=300)
119
+ # Variable to hold image filenames aligned with gallery images
120
+ image_names = gr.State()
121
+
122
+ saved_image_path = gr.Textbox(label="Save Image Path", visible=False)
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
+