userIdc2024 commited on
Commit
dfc5ed5
·
verified ·
1 Parent(s): d643e6c

Update app_pages/image_generation.py

Browse files
Files changed (1) hide show
  1. 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}"); return None
 
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
- temp_dir = tempfile.mkdtemp()
58
- try:
59
- temp_path = os.path.join(temp_dir, zip_file.name if zip_file.name.lower().endswith(".zip") else f"input.{zip_file.name.split('.')[-1]}")
60
- with open(temp_path, "wb") as f: f.write(zip_file.read())
61
- images = process_zip_and_generate_images(temp_path, category, size, quality, user_prompt, sentiment, platform, num_images, demo_btn, gallery, blur)
62
- st.session_state[gallery_key] = images or []; images = st.session_state[gallery_key]
63
- if images:
64
- cols = st.columns(4)
65
- for idx, img_path in enumerate(images):
66
- with cols[idx % 4]: st.image(img_path, use_container_width=True)
67
- else:
68
- st.info("No images generated.")
69
- except Exception as e:
70
- st.error(f"Error: {e}"); logger.exception("Generation failed (embedded).")
71
- finally:
72
- shutil.rmtree(temp_dir, ignore_errors=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  else:
74
  st.warning("Please upload a file and fill all required fields.")
75
 
76
  if download_btn:
77
- zip_path = _zip_gallery_images(st.session_state.get(gallery_key, []))
78
- if zip_path:
79
- with open(zip_path, "rb") as f:
80
- st.download_button("Download ZIP", data=f.read(), file_name="generated_images.zip", mime="application/zip", key=f"{prefix}_zip_dl_btn", use_container_width=True)
81
- try: os.remove(zip_path)
82
- except Exception: pass
83
- else:
84
- st.warning("No images to zip and download.")
 
 
 
 
 
 
 
 
 
 
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.")