Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,45 +4,58 @@ import os
|
|
| 4 |
import random
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
# Paths
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Ensure
|
| 12 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
def generate_images():
|
| 16 |
-
command =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
subprocess.run(command,
|
|
|
|
|
|
|
| 19 |
except subprocess.CalledProcessError as e:
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def get_random_images():
|
| 24 |
image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
|
|
|
| 25 |
if len(image_files) < 5:
|
| 26 |
generate_images()
|
| 27 |
image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 28 |
-
|
| 29 |
-
if len(image_files) == 0:
|
| 30 |
-
return ["No images found. Please try again."]
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# Gradio
|
| 37 |
def generate_and_display():
|
| 38 |
generate_images()
|
| 39 |
return get_random_images()
|
| 40 |
|
| 41 |
-
# UI
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 44 |
generate_button = gr.Button("Generate New Designs")
|
| 45 |
-
output_gallery = gr.Gallery(label="Generated Designs", columns=5
|
| 46 |
generate_button.click(fn=generate_and_display, outputs=output_gallery)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import random
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# === Setup Paths ===
|
| 8 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
+
GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
|
| 10 |
+
OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
|
| 11 |
+
MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
|
| 12 |
|
| 13 |
+
# Ensure output directory exists
|
| 14 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 15 |
|
| 16 |
+
# === Image Generation Function ===
|
| 17 |
def generate_images():
|
| 18 |
+
command = [
|
| 19 |
+
"python",
|
| 20 |
+
GEN_SCRIPT,
|
| 21 |
+
f"--outdir={OUTPUT_DIR}",
|
| 22 |
+
"--trunc=1",
|
| 23 |
+
"--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
|
| 24 |
+
f"--network={MODEL_PATH}"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
try:
|
| 28 |
+
result = subprocess.run(command, check=True, capture_output=True, text=True)
|
| 29 |
+
print("stdout:\n", result.stdout)
|
| 30 |
+
print("stderr:\n", result.stderr)
|
| 31 |
except subprocess.CalledProcessError as e:
|
| 32 |
+
print("Image generation failed with error:")
|
| 33 |
+
print(e.stderr)
|
| 34 |
+
return f"Error generating images:\n{e.stderr}"
|
| 35 |
|
| 36 |
+
# === Select Random Images from Output Folder ===
|
| 37 |
def get_random_images():
|
| 38 |
image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 39 |
+
|
| 40 |
if len(image_files) < 5:
|
| 41 |
generate_images()
|
| 42 |
image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
if not image_files:
|
| 45 |
+
return [Image.new("RGB", (256, 256), color="gray")] * 5 # fallback
|
| 46 |
|
| 47 |
+
random_images = random.sample(image_files, min(5, len(image_files)))
|
| 48 |
+
return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images]
|
| 49 |
|
| 50 |
+
# === Gradio Interface ===
|
| 51 |
def generate_and_display():
|
| 52 |
generate_images()
|
| 53 |
return get_random_images()
|
| 54 |
|
|
|
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 57 |
generate_button = gr.Button("Generate New Designs")
|
| 58 |
+
output_gallery = gr.Gallery(label="Generated Designs", columns=5)
|
| 59 |
generate_button.click(fn=generate_and_display, outputs=output_gallery)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|