Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,65 +56,79 @@
|
|
| 56 |
# if __name__ == "__main__":
|
| 57 |
# demo.launch()
|
| 58 |
|
| 59 |
-
|
| 60 |
import gradio as gr
|
| 61 |
-
import
|
|
|
|
|
|
|
| 62 |
from PIL import Image
|
| 63 |
-
import numpy as np
|
| 64 |
-
import io
|
| 65 |
-
|
| 66 |
-
# Dummy function to simulate image generation
|
| 67 |
-
def generate_images():
|
| 68 |
-
# Replace with your actual image generation logic
|
| 69 |
-
img = Image.new("RGB", (256, 256), color="blue")
|
| 70 |
-
return [img] * 6
|
| 71 |
-
|
| 72 |
-
# Function to send image to backend
|
| 73 |
-
def save_image_to_backend(image):
|
| 74 |
-
if image is None:
|
| 75 |
-
return "No image selected."
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
-
gr.Markdown("#
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
save_button.click(
|
| 110 |
-
fn=save_image_to_backend,
|
| 111 |
-
inputs=selected_image,
|
| 112 |
-
outputs=save_status
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
save_status.change(fn=None, outputs=None, show_progress=False)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
gallery.value = generate_images()
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
| 56 |
# if __name__ == "__main__":
|
| 57 |
# demo.launch()
|
| 58 |
|
|
|
|
| 59 |
import gradio as gr
|
| 60 |
+
import subprocess
|
| 61 |
+
import os
|
| 62 |
+
import random
|
| 63 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# === Setup Paths ===
|
| 66 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 67 |
+
GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
|
| 68 |
+
OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
|
| 69 |
+
MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
|
| 70 |
|
| 71 |
+
# Ensure output directory exists
|
| 72 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 73 |
|
| 74 |
+
# === Image Generation Function ===
|
| 75 |
+
def generate_images():
|
| 76 |
+
command = [
|
| 77 |
+
"python",
|
| 78 |
+
GEN_SCRIPT,
|
| 79 |
+
f"--outdir={OUTPUT_DIR}",
|
| 80 |
+
"--trunc=1",
|
| 81 |
+
"--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
|
| 82 |
+
f"--network={MODEL_PATH}"
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
result = subprocess.run(command, check=True, capture_output=True, text=True)
|
| 87 |
+
print("stdout:\n", result.stdout)
|
| 88 |
+
print("stderr:\n", result.stderr)
|
| 89 |
+
except subprocess.CalledProcessError as e:
|
| 90 |
+
print("Image generation failed with error:")
|
| 91 |
+
print(e.stderr)
|
| 92 |
+
return f"Error generating images:\n{e.stderr}"
|
| 93 |
+
|
| 94 |
+
# === Select Random Images from Output Folder ===
|
| 95 |
+
def get_random_images():
|
| 96 |
+
image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 97 |
+
if len(image_files) < 10:
|
| 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):
|
| 105 |
+
image_path = os.path.join(OUTPUT_DIR, image_name)
|
| 106 |
+
return image_path
|
| 107 |
+
|
| 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()
|