Spaces:
Running
Running
Update app_pages/image_generation.py
Browse files- app_pages/image_generation.py +59 -25
app_pages/image_generation.py
CHANGED
|
@@ -28,7 +28,8 @@ def _zip_gallery_images(gallery_items):
|
|
| 28 |
zipf.write(os.path.join(temp_dir, file_name), arcname=file_name)
|
| 29 |
return zip_path
|
| 30 |
except Exception as e:
|
| 31 |
-
logger.critical(f"Failed to create zip: {e}")
|
|
|
|
| 32 |
finally:
|
| 33 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 34 |
|
|
@@ -54,31 +55,64 @@ def render_bulk_image_generator(prefix: str = "ig_img"):
|
|
| 54 |
|
| 55 |
if demo_btn or gen_all_btn:
|
| 56 |
if zip_file and category and user_prompt:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
st.warning("Please upload a file and fill all required fields.")
|
| 75 |
|
| 76 |
if download_btn:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
zipf.write(os.path.join(temp_dir, file_name), arcname=file_name)
|
| 29 |
return zip_path
|
| 30 |
except Exception as e:
|
| 31 |
+
logger.critical(f"Failed to create zip: {e}")
|
| 32 |
+
return None
|
| 33 |
finally:
|
| 34 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 35 |
|
|
|
|
| 55 |
|
| 56 |
if demo_btn or gen_all_btn:
|
| 57 |
if zip_file and category and user_prompt:
|
| 58 |
+
with st.spinner(" Generating image variations..."):
|
| 59 |
+
temp_dir = tempfile.mkdtemp()
|
| 60 |
+
try:
|
| 61 |
+
temp_path = os.path.join(
|
| 62 |
+
temp_dir,
|
| 63 |
+
zip_file.name if zip_file.name.lower().endswith(".zip")
|
| 64 |
+
else f"input.{zip_file.name.split('.')[-1]}"
|
| 65 |
+
)
|
| 66 |
+
with open(temp_path, "wb") as f:
|
| 67 |
+
f.write(zip_file.read())
|
| 68 |
+
|
| 69 |
+
images = process_zip_and_generate_images(
|
| 70 |
+
temp_path,
|
| 71 |
+
category,
|
| 72 |
+
size,
|
| 73 |
+
quality,
|
| 74 |
+
user_prompt,
|
| 75 |
+
sentiment,
|
| 76 |
+
platform,
|
| 77 |
+
num_images,
|
| 78 |
+
demo_btn,
|
| 79 |
+
gallery,
|
| 80 |
+
blur,
|
| 81 |
+
)
|
| 82 |
+
st.session_state[gallery_key] = images or []
|
| 83 |
+
images = st.session_state[gallery_key]
|
| 84 |
+
|
| 85 |
+
if images:
|
| 86 |
+
cols = st.columns(4)
|
| 87 |
+
for idx, img_path in enumerate(images):
|
| 88 |
+
with cols[idx % 4]:
|
| 89 |
+
st.image(img_path, use_container_width=True)
|
| 90 |
+
st.success("Image generation completed!")
|
| 91 |
+
else:
|
| 92 |
+
st.info("No images generated.")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
st.error(f"Error: {e}")
|
| 95 |
+
logger.exception("Generation failed (embedded).")
|
| 96 |
+
finally:
|
| 97 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 98 |
else:
|
| 99 |
st.warning("Please upload a file and fill all required fields.")
|
| 100 |
|
| 101 |
if download_btn:
|
| 102 |
+
zip_path = _zip_gallery_images(st.session_state.get(gallery_key, []))
|
| 103 |
+
if zip_path:
|
| 104 |
+
with open(zip_path, "rb") as f:
|
| 105 |
+
st.download_button(
|
| 106 |
+
"Download ZIP",
|
| 107 |
+
data=f.read(),
|
| 108 |
+
file_name="generated_images.zip",
|
| 109 |
+
mime="application/zip",
|
| 110 |
+
key=f"{prefix}_zip_dl_btn",
|
| 111 |
+
use_container_width=True
|
| 112 |
+
)
|
| 113 |
+
try:
|
| 114 |
+
os.remove(zip_path)
|
| 115 |
+
except Exception:
|
| 116 |
+
pass
|
| 117 |
+
else:
|
| 118 |
+
st.warning("No images to zip and download.")
|