Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,7 +69,6 @@
|
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
-
|
| 73 |
# import gradio as gr
|
| 74 |
# import subprocess
|
| 75 |
# import os
|
|
@@ -77,14 +76,17 @@
|
|
| 77 |
# from PIL import Image
|
| 78 |
# import shutil
|
| 79 |
|
|
|
|
| 80 |
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 81 |
# GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
|
| 82 |
# OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
|
| 83 |
# MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
|
|
|
|
| 84 |
|
| 85 |
# os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 86 |
-
# os.makedirs(
|
| 87 |
|
|
|
|
| 88 |
# def generate_images():
|
| 89 |
# command = [
|
| 90 |
# "python",
|
|
@@ -99,62 +101,64 @@
|
|
| 99 |
# except subprocess.CalledProcessError as e:
|
| 100 |
# return f"Error generating images:\n{e.stderr}"
|
| 101 |
|
|
|
|
| 102 |
# def get_random_images():
|
| 103 |
# image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 104 |
# if len(image_files) < 10:
|
| 105 |
# generate_images()
|
| 106 |
# image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 107 |
-
#
|
| 108 |
-
#
|
|
|
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
#
|
| 112 |
-
#
|
| 113 |
-
#
|
| 114 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
|
|
|
| 116 |
# with gr.Blocks() as demo:
|
| 117 |
-
# gr.Markdown("
|
| 118 |
|
| 119 |
-
#
|
| 120 |
|
| 121 |
# image_components = []
|
| 122 |
-
# file_paths = []
|
| 123 |
# save_buttons = []
|
| 124 |
# outputs = []
|
|
|
|
| 125 |
|
| 126 |
# with gr.Row():
|
| 127 |
-
# for
|
| 128 |
-
# with gr.Column():
|
| 129 |
-
#
|
| 130 |
-
#
|
| 131 |
-
# with gr.Column():
|
| 132 |
-
# img = gr.Image()
|
| 133 |
-
# file_paths.append(gr.Textbox(visible=False)) # to pass file path
|
| 134 |
-
# btn = gr.Button("💾 Save")
|
| 135 |
-
# out = gr.Textbox(visible=False)
|
| 136 |
|
| 137 |
-
#
|
|
|
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
#
|
| 141 |
-
#
|
| 142 |
|
| 143 |
-
#
|
| 144 |
-
#
|
| 145 |
-
# updates = []
|
| 146 |
-
# for i, path in enumerate(files):
|
| 147 |
-
# updates.append(path) # for img.update(value=...)
|
| 148 |
-
# updates.append(path) # for file_path.update(value=...)
|
| 149 |
-
# # Fill remaining slots if less than 10
|
| 150 |
-
# for i in range(10 - len(files)):
|
| 151 |
-
# updates.append(None)
|
| 152 |
-
# updates.append("")
|
| 153 |
-
# return updates
|
| 154 |
|
| 155 |
-
#
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
|
| 160 |
|
|
@@ -163,17 +167,19 @@ import subprocess
|
|
| 163 |
import os
|
| 164 |
import random
|
| 165 |
from PIL import Image
|
| 166 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
# === Setup Paths ===
|
| 169 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 170 |
GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
|
| 171 |
OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
|
| 172 |
MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
|
| 173 |
-
SAVE_DIR = os.path.join(BASE_DIR, "saved_images")
|
| 174 |
|
| 175 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 176 |
-
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 177 |
|
| 178 |
# === Image Generation Function ===
|
| 179 |
def generate_images():
|
|
@@ -200,18 +206,39 @@ def get_random_images():
|
|
| 200 |
image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
|
| 201 |
return image_paths
|
| 202 |
|
| 203 |
-
# ===
|
| 204 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
try:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
except Exception as e:
|
| 211 |
-
return f"Error
|
| 212 |
|
| 213 |
# === Gradio Interface ===
|
| 214 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 216 |
|
| 217 |
generate_button = gr.Button("Generate New Designs")
|
|
@@ -234,12 +261,17 @@ with gr.Blocks() as demo:
|
|
| 234 |
output = gr.Textbox(visible=False)
|
| 235 |
outputs.append(output)
|
| 236 |
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
save_buttons.append(save_btn)
|
| 239 |
|
| 240 |
def generate_and_display_images():
|
| 241 |
image_paths = get_random_images()
|
| 242 |
-
return image_paths + image_paths # one set for images, one for
|
| 243 |
|
| 244 |
generate_button.click(
|
| 245 |
fn=generate_and_display_images,
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
|
|
|
|
| 72 |
# import gradio as gr
|
| 73 |
# import subprocess
|
| 74 |
# import os
|
|
|
|
| 76 |
# from PIL import Image
|
| 77 |
# import shutil
|
| 78 |
|
| 79 |
+
# # === Setup Paths ===
|
| 80 |
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 81 |
# GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
|
| 82 |
# OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
|
| 83 |
# MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
|
| 84 |
+
# SAVE_DIR = os.path.join(BASE_DIR, "saved_images")
|
| 85 |
|
| 86 |
# os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 87 |
+
# os.makedirs(SAVE_DIR, exist_ok=True)
|
| 88 |
|
| 89 |
+
# # === Image Generation Function ===
|
| 90 |
# def generate_images():
|
| 91 |
# command = [
|
| 92 |
# "python",
|
|
|
|
| 101 |
# except subprocess.CalledProcessError as e:
|
| 102 |
# return f"Error generating images:\n{e.stderr}"
|
| 103 |
|
| 104 |
+
# # === Select Random Images from Output Folder ===
|
| 105 |
# def get_random_images():
|
| 106 |
# image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 107 |
# if len(image_files) < 10:
|
| 108 |
# generate_images()
|
| 109 |
# image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
|
| 110 |
+
# random_images = random.sample(image_files, min(10, len(image_files)))
|
| 111 |
+
# image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
|
| 112 |
+
# return image_paths
|
| 113 |
|
| 114 |
+
# # === Save Image Function ===
|
| 115 |
+
# def save_image(img_path):
|
| 116 |
+
# try:
|
| 117 |
+
# filename = os.path.basename(img_path)
|
| 118 |
+
# dest_path = os.path.join(SAVE_DIR, filename)
|
| 119 |
+
# shutil.copy(img_path, dest_path)
|
| 120 |
+
# return f"Saved to: {dest_path}"
|
| 121 |
+
# except Exception as e:
|
| 122 |
+
# return f"Error saving image: {str(e)}"
|
| 123 |
|
| 124 |
+
# # === Gradio Interface ===
|
| 125 |
# with gr.Blocks() as demo:
|
| 126 |
+
# gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 127 |
|
| 128 |
+
# generate_button = gr.Button("Generate New Designs")
|
| 129 |
|
| 130 |
# image_components = []
|
|
|
|
| 131 |
# save_buttons = []
|
| 132 |
# outputs = []
|
| 133 |
+
# file_paths = []
|
| 134 |
|
| 135 |
# with gr.Row():
|
| 136 |
+
# for i in range(10): # 10 images
|
| 137 |
+
# with gr.Column(scale=1):
|
| 138 |
+
# img = gr.Image(width=200, height=200)
|
| 139 |
+
# image_components.append(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# file_path = gr.Textbox(visible=False)
|
| 142 |
+
# file_paths.append(file_path)
|
| 143 |
|
| 144 |
+
# save_btn = gr.Button("💾 Save")
|
| 145 |
+
# output = gr.Textbox(visible=False)
|
| 146 |
+
# outputs.append(output)
|
| 147 |
|
| 148 |
+
# save_btn.click(fn=save_image, inputs=file_path, outputs=output)
|
| 149 |
+
# save_buttons.append(save_btn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# def generate_and_display_images():
|
| 152 |
+
# image_paths = get_random_images()
|
| 153 |
+
# return image_paths + image_paths # one set for images, one for hidden file paths
|
| 154 |
|
| 155 |
+
# generate_button.click(
|
| 156 |
+
# fn=generate_and_display_images,
|
| 157 |
+
# outputs=image_components + file_paths
|
| 158 |
+
# )
|
| 159 |
+
|
| 160 |
+
# if __name__ == "__main__":
|
| 161 |
+
# demo.launch()
|
| 162 |
|
| 163 |
|
| 164 |
|
|
|
|
| 167 |
import os
|
| 168 |
import random
|
| 169 |
from PIL import Image
|
| 170 |
+
import requests
|
| 171 |
+
import io
|
| 172 |
+
import warnings
|
| 173 |
+
|
| 174 |
+
warnings.filterwarnings("ignore")
|
| 175 |
|
| 176 |
# === Setup Paths ===
|
| 177 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 178 |
GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
|
| 179 |
OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
|
| 180 |
MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
|
|
|
|
| 181 |
|
| 182 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
|
| 183 |
|
| 184 |
# === Image Generation Function ===
|
| 185 |
def generate_images():
|
|
|
|
| 206 |
image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
|
| 207 |
return image_paths
|
| 208 |
|
| 209 |
+
# === Upload to NestJS Backend ===
|
| 210 |
+
def send_to_backend(img_path, user_id):
|
| 211 |
+
if not user_id:
|
| 212 |
+
return "❌ user_id not found."
|
| 213 |
+
if not os.path.exists(img_path):
|
| 214 |
+
return "⚠️ Image path invalid."
|
| 215 |
+
|
| 216 |
try:
|
| 217 |
+
with open(img_path, "rb") as f:
|
| 218 |
+
file_bytes = f.read()
|
| 219 |
+
|
| 220 |
+
files = {'file': ('generated_image.png', file_bytes, 'image/png')}
|
| 221 |
+
|
| 222 |
+
url = f"https://5a4d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
|
| 223 |
+
response = requests.post(url, files=files)
|
| 224 |
+
|
| 225 |
+
if response.status_code == 201:
|
| 226 |
+
return "✅ Image uploaded and saved to database!"
|
| 227 |
+
else:
|
| 228 |
+
return f"❌ Upload failed: {response.status_code} - {response.text}"
|
| 229 |
+
|
| 230 |
except Exception as e:
|
| 231 |
+
return f"⚠️ Error: {str(e)}"
|
| 232 |
|
| 233 |
# === Gradio Interface ===
|
| 234 |
with gr.Blocks() as demo:
|
| 235 |
+
user_id_state = gr.State()
|
| 236 |
+
|
| 237 |
+
@demo.load(inputs=None, outputs=[user_id_state])
|
| 238 |
+
def on_load(request: gr.Request):
|
| 239 |
+
user_id = request.query_params.get('user_id', '')
|
| 240 |
+
return user_id
|
| 241 |
+
|
| 242 |
gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 243 |
|
| 244 |
generate_button = gr.Button("Generate New Designs")
|
|
|
|
| 261 |
output = gr.Textbox(visible=False)
|
| 262 |
outputs.append(output)
|
| 263 |
|
| 264 |
+
# Save button uses the backend-saving version
|
| 265 |
+
save_btn.click(
|
| 266 |
+
fn=send_to_backend,
|
| 267 |
+
inputs=[file_path, user_id_state],
|
| 268 |
+
outputs=output
|
| 269 |
+
)
|
| 270 |
save_buttons.append(save_btn)
|
| 271 |
|
| 272 |
def generate_and_display_images():
|
| 273 |
image_paths = get_random_images()
|
| 274 |
+
return image_paths + image_paths # one set for images, one for file paths
|
| 275 |
|
| 276 |
generate_button.click(
|
| 277 |
fn=generate_and_display_images,
|