Spaces:
Running
Running
Update app_pages/image_generation.py
Browse files- app_pages/image_generation.py +67 -14
app_pages/image_generation.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
-
import zipfile, requests, logging, tempfile, shutil, os
|
| 3 |
import streamlit as st
|
| 4 |
from generator_function.image_processor import process_zip_and_generate_images
|
| 5 |
from generator_function.multimodel_image_processor import process_zip_and_generate_images_multimodel
|
|
@@ -12,24 +12,53 @@ logger = logging.getLogger(__name__)
|
|
| 12 |
def _zip_gallery_images(gallery_items):
|
| 13 |
if not gallery_items:
|
| 14 |
return None
|
| 15 |
-
image_urls = [item[0] if isinstance(item, tuple) else item for item in gallery_items]
|
| 16 |
temp_dir = tempfile.mkdtemp()
|
| 17 |
try:
|
| 18 |
zip_path = tempfile.NamedTemporaryFile(delete=False, suffix=".zip").name
|
| 19 |
-
for i,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
file_path = os.path.join(temp_dir, f"image_{i}.{ext}")
|
| 24 |
-
if url.startswith(("http://", "https://")):
|
| 25 |
-
resp = requests.get(url, timeout=10)
|
| 26 |
-
resp.raise_for_status()
|
| 27 |
with open(file_path, "wb") as f:
|
| 28 |
-
f.write(
|
| 29 |
-
elif
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
logger.error(f"Error processing image {url}: {e}")
|
| 33 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 34 |
for file_name in os.listdir(temp_dir):
|
| 35 |
zipf.write(os.path.join(temp_dir, file_name), arcname=file_name)
|
|
@@ -42,6 +71,7 @@ def _zip_gallery_images(gallery_items):
|
|
| 42 |
|
| 43 |
|
| 44 |
def render_bulk_image_generator(uid: str, prefix: str = "ig_img"):
|
|
|
|
| 45 |
zip_file = st.file_uploader(
|
| 46 |
"Upload Zip or Single File",
|
| 47 |
type=["zip", "png", "jpg", "jpeg"],
|
|
@@ -136,6 +166,9 @@ def render_bulk_image_generator(uid: str, prefix: str = "ig_img"):
|
|
| 136 |
gallery = st.session_state.setdefault(gallery_key, [])
|
| 137 |
|
| 138 |
|
|
|
|
|
|
|
|
|
|
| 139 |
if demo_btn or gen_all_btn:
|
| 140 |
if zip_file and category and user_prompt:
|
| 141 |
# Clear previous images when starting new generation
|
|
@@ -168,6 +201,7 @@ def render_bulk_image_generator(uid: str, prefix: str = "ig_img"):
|
|
| 168 |
None, # Pass None instead of gallery to avoid appending
|
| 169 |
blur,
|
| 170 |
uid,
|
|
|
|
| 171 |
)
|
| 172 |
else:
|
| 173 |
images = process_zip_and_generate_images_multimodel(
|
|
@@ -185,6 +219,7 @@ def render_bulk_image_generator(uid: str, prefix: str = "ig_img"):
|
|
| 185 |
uid,
|
| 186 |
selected_model_key,
|
| 187 |
dynamic_params,
|
|
|
|
| 188 |
)
|
| 189 |
st.session_state[gallery_key] = images or []
|
| 190 |
images = st.session_state[gallery_key]
|
|
@@ -193,7 +228,25 @@ def render_bulk_image_generator(uid: str, prefix: str = "ig_img"):
|
|
| 193 |
cols = st.columns(4)
|
| 194 |
for idx, img_path in enumerate(images):
|
| 195 |
with cols[idx % 4]:
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
st.success("Image generation completed!")
|
| 198 |
else:
|
| 199 |
st.info("No images generated.")
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
+
import zipfile, requests, logging, tempfile, shutil, os, base64
|
| 3 |
import streamlit as st
|
| 4 |
from generator_function.image_processor import process_zip_and_generate_images
|
| 5 |
from generator_function.multimodel_image_processor import process_zip_and_generate_images_multimodel
|
|
|
|
| 12 |
def _zip_gallery_images(gallery_items):
|
| 13 |
if not gallery_items:
|
| 14 |
return None
|
|
|
|
| 15 |
temp_dir = tempfile.mkdtemp()
|
| 16 |
try:
|
| 17 |
zip_path = tempfile.NamedTemporaryFile(delete=False, suffix=".zip").name
|
| 18 |
+
for i, item in enumerate(gallery_items):
|
| 19 |
+
filename = None
|
| 20 |
+
url = None
|
| 21 |
+
data_bytes = None
|
| 22 |
+
|
| 23 |
+
if isinstance(item, tuple):
|
| 24 |
+
if len(item) == 2 and isinstance(item[1], (bytes, bytearray)):
|
| 25 |
+
filename = item[0]
|
| 26 |
+
data_bytes = item[1]
|
| 27 |
+
else:
|
| 28 |
+
url = item[0]
|
| 29 |
+
elif isinstance(item, str):
|
| 30 |
+
if item.startswith("data:image"):
|
| 31 |
+
try:
|
| 32 |
+
header, encoded = item.split(",", 1)
|
| 33 |
+
mime = header.split(";")[0].split("/")[-1] or "png"
|
| 34 |
+
data_bytes = base64.b64decode(encoded)
|
| 35 |
+
filename = f"image_{i}.{mime}"
|
| 36 |
+
except Exception:
|
| 37 |
+
logger.error("Failed to decode private image data for zip bundle.")
|
| 38 |
+
continue
|
| 39 |
+
else:
|
| 40 |
+
url = item
|
| 41 |
+
else:
|
| 42 |
+
continue
|
| 43 |
+
|
| 44 |
try:
|
| 45 |
+
if data_bytes is not None:
|
| 46 |
+
file_path = os.path.join(temp_dir, filename or f"image_{i}.png")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with open(file_path, "wb") as f:
|
| 48 |
+
f.write(data_bytes)
|
| 49 |
+
elif url:
|
| 50 |
+
ext = url.split('?')[0].split('.')[-1]
|
| 51 |
+
ext = ext if ext and len(ext) <= 5 else "png"
|
| 52 |
+
file_path = os.path.join(temp_dir, f"image_{i}.{ext}")
|
| 53 |
+
if url.startswith(("http://", "https://")):
|
| 54 |
+
resp = requests.get(url, timeout=10)
|
| 55 |
+
resp.raise_for_status()
|
| 56 |
+
with open(file_path, "wb") as f:
|
| 57 |
+
f.write(resp.content)
|
| 58 |
+
elif os.path.exists(url):
|
| 59 |
+
shutil.copy(url, file_path)
|
| 60 |
except Exception as e:
|
| 61 |
+
logger.error(f"Error processing image {url or filename}: {e}")
|
| 62 |
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 63 |
for file_name in os.listdir(temp_dir):
|
| 64 |
zipf.write(os.path.join(temp_dir, file_name), arcname=file_name)
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
def render_bulk_image_generator(uid: str, prefix: str = "ig_img"):
|
| 74 |
+
private_mode = st.session_state.get("private_mode_enabled", False)
|
| 75 |
zip_file = st.file_uploader(
|
| 76 |
"Upload Zip or Single File",
|
| 77 |
type=["zip", "png", "jpg", "jpeg"],
|
|
|
|
| 166 |
gallery = st.session_state.setdefault(gallery_key, [])
|
| 167 |
|
| 168 |
|
| 169 |
+
if private_mode:
|
| 170 |
+
st.info("Private mode is active. Generated variations stay only in this session and are not uploaded to storage or the AI Library.")
|
| 171 |
+
|
| 172 |
if demo_btn or gen_all_btn:
|
| 173 |
if zip_file and category and user_prompt:
|
| 174 |
# Clear previous images when starting new generation
|
|
|
|
| 201 |
None, # Pass None instead of gallery to avoid appending
|
| 202 |
blur,
|
| 203 |
uid,
|
| 204 |
+
private_mode=private_mode,
|
| 205 |
)
|
| 206 |
else:
|
| 207 |
images = process_zip_and_generate_images_multimodel(
|
|
|
|
| 219 |
uid,
|
| 220 |
selected_model_key,
|
| 221 |
dynamic_params,
|
| 222 |
+
private_mode=private_mode,
|
| 223 |
)
|
| 224 |
st.session_state[gallery_key] = images or []
|
| 225 |
images = st.session_state[gallery_key]
|
|
|
|
| 228 |
cols = st.columns(4)
|
| 229 |
for idx, img_path in enumerate(images):
|
| 230 |
with cols[idx % 4]:
|
| 231 |
+
if isinstance(img_path, str) and img_path.startswith("data:image"):
|
| 232 |
+
try:
|
| 233 |
+
_, encoded = img_path.split(",", 1)
|
| 234 |
+
img_bytes = base64.b64decode(encoded)
|
| 235 |
+
except Exception:
|
| 236 |
+
img_bytes = None
|
| 237 |
+
if img_bytes:
|
| 238 |
+
st.image(img_bytes, width='stretch')
|
| 239 |
+
st.download_button(
|
| 240 |
+
f"Download image {idx + 1}",
|
| 241 |
+
img_bytes,
|
| 242 |
+
file_name=f"private_image_{idx + 1}.png",
|
| 243 |
+
mime="image/png",
|
| 244 |
+
key=f"{prefix}_private_dl_{idx}"
|
| 245 |
+
)
|
| 246 |
+
else:
|
| 247 |
+
st.warning("Unable to display private image data.")
|
| 248 |
+
else:
|
| 249 |
+
st.image(img_path,width='stretch')
|
| 250 |
st.success("Image generation completed!")
|
| 251 |
else:
|
| 252 |
st.info("No images generated.")
|