Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -161,25 +161,23 @@
|
|
| 161 |
# demo.launch()
|
| 162 |
|
| 163 |
|
| 164 |
-
|
| 165 |
import gradio as gr
|
| 166 |
import subprocess
|
| 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,21 +204,21 @@ def get_random_images():
|
|
| 206 |
image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
|
| 207 |
return image_paths
|
| 208 |
|
| 209 |
-
# ===
|
| 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 |
-
|
| 217 |
-
|
| 218 |
-
file_bytes = f.read()
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
|
|
|
| 224 |
|
| 225 |
if response.status_code == 201:
|
| 226 |
return "✅ Image uploaded and saved to database!"
|
|
@@ -232,46 +230,50 @@ def send_to_backend(img_path, user_id):
|
|
| 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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
image_components = []
|
|
|
|
| 247 |
save_buttons = []
|
| 248 |
outputs = []
|
| 249 |
-
file_paths = []
|
| 250 |
-
|
| 251 |
-
with gr.Row():
|
| 252 |
-
for i in range(10): # 10 images
|
| 253 |
-
with gr.Column(scale=1):
|
| 254 |
-
img = gr.Image(width=200, height=200)
|
| 255 |
-
image_components.append(img)
|
| 256 |
-
|
| 257 |
-
file_path = gr.Textbox(visible=False)
|
| 258 |
-
file_paths.append(file_path)
|
| 259 |
-
|
| 260 |
-
save_btn = gr.Button("💾 Save")
|
| 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 #
|
| 275 |
|
| 276 |
generate_button.click(
|
| 277 |
fn=generate_and_display_images,
|
|
|
|
| 161 |
# demo.launch()
|
| 162 |
|
| 163 |
|
|
|
|
| 164 |
import gradio as gr
|
| 165 |
import subprocess
|
| 166 |
import os
|
| 167 |
import random
|
| 168 |
from PIL import Image
|
| 169 |
+
import shutil
|
| 170 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# === Setup Paths ===
|
| 173 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 174 |
GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
|
| 175 |
OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
|
| 176 |
MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
|
| 177 |
+
SAVE_DIR = os.path.join(BASE_DIR, "saved_images")
|
| 178 |
|
| 179 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 180 |
+
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 181 |
|
| 182 |
# === Image Generation Function ===
|
| 183 |
def generate_images():
|
|
|
|
| 204 |
image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
|
| 205 |
return image_paths
|
| 206 |
|
| 207 |
+
# === Send Image to Backend ===
|
| 208 |
def send_to_backend(img_path, user_id):
|
| 209 |
if not user_id:
|
| 210 |
+
return "❌ user_id not found in URL."
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
if not img_path or not os.path.exists(img_path):
|
| 213 |
+
return "⚠️ No image selected or image not found."
|
|
|
|
| 214 |
|
| 215 |
+
try:
|
| 216 |
+
with open(img_path, 'rb') as f:
|
| 217 |
+
files = {'file': ('generated_image.png', f, 'image/png')}
|
| 218 |
|
| 219 |
+
# Your backend endpoint here
|
| 220 |
+
url = f"https://5a4d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
|
| 221 |
+
response = requests.post(url, files=files)
|
| 222 |
|
| 223 |
if response.status_code == 201:
|
| 224 |
return "✅ Image uploaded and saved to database!"
|
|
|
|
| 230 |
|
| 231 |
# === Gradio Interface ===
|
| 232 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
|
| 234 |
|
| 235 |
generate_button = gr.Button("Generate New Designs")
|
| 236 |
+
user_id_state = gr.State()
|
| 237 |
+
|
| 238 |
+
@demo.load(inputs=None, outputs=[user_id_state])
|
| 239 |
+
def get_user_id(request: gr.Request):
|
| 240 |
+
return request.query_params.get("user_id", "")
|
| 241 |
|
| 242 |
image_components = []
|
| 243 |
+
file_paths = []
|
| 244 |
save_buttons = []
|
| 245 |
outputs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
# Use 3 columns layout
|
| 248 |
+
for row_idx in range(4): # 4 rows (to cover 10 images)
|
| 249 |
+
with gr.Row():
|
| 250 |
+
for col_idx in range(3): # 3 columns
|
| 251 |
+
i = row_idx * 3 + col_idx
|
| 252 |
+
if i >= 10:
|
| 253 |
+
break
|
| 254 |
+
with gr.Column():
|
| 255 |
+
img = gr.Image(width=180, height=180, label=f"Design {i+1}")
|
| 256 |
+
image_components.append(img)
|
| 257 |
+
|
| 258 |
+
file_path = gr.Textbox(visible=False)
|
| 259 |
+
file_paths.append(file_path)
|
| 260 |
+
|
| 261 |
+
save_btn = gr.Button("💾 Save to DB")
|
| 262 |
+
save_buttons.append(save_btn)
|
| 263 |
+
|
| 264 |
+
output = gr.Textbox(label="Status", interactive=False)
|
| 265 |
+
outputs.append(output)
|
| 266 |
+
|
| 267 |
+
save_btn.click(
|
| 268 |
+
fn=send_to_backend,
|
| 269 |
+
inputs=[file_path, user_id_state],
|
| 270 |
+
outputs=output
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
# Generate button logic
|
| 274 |
def generate_and_display_images():
|
| 275 |
image_paths = get_random_images()
|
| 276 |
+
return image_paths + image_paths # One for display, one for hidden path tracking
|
| 277 |
|
| 278 |
generate_button.click(
|
| 279 |
fn=generate_and_display_images,
|