Upload 6 files
Browse files- app.py +21 -1
- image_generator.py +28 -6
app.py
CHANGED
|
@@ -552,7 +552,27 @@ def main():
|
|
| 552 |
image_cols = st.columns(min(len(images), 3))
|
| 553 |
for i, img_path in enumerate(images):
|
| 554 |
with image_cols[i % len(image_cols)]:
|
| 555 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
|
| 557 |
# Force garbage collection after image generation
|
| 558 |
if memory_optimization or apply_vram_optimization:
|
|
|
|
| 552 |
image_cols = st.columns(min(len(images), 3))
|
| 553 |
for i, img_path in enumerate(images):
|
| 554 |
with image_cols[i % len(image_cols)]:
|
| 555 |
+
try:
|
| 556 |
+
# Verify image exists and is valid
|
| 557 |
+
if os.path.exists(img_path):
|
| 558 |
+
# Try to open and verify the image
|
| 559 |
+
from PIL import Image
|
| 560 |
+
try:
|
| 561 |
+
img = Image.open(img_path)
|
| 562 |
+
# Convert to RGB if needed
|
| 563 |
+
if img.mode != "RGB":
|
| 564 |
+
img = img.convert("RGB")
|
| 565 |
+
# Save as JPEG to ensure compatibility
|
| 566 |
+
safe_path = f"temp/safe_image_{int(time.time() * 1000)}_{i}.jpg"
|
| 567 |
+
img.save(safe_path, format="JPEG", quality=95)
|
| 568 |
+
# Display the safe image
|
| 569 |
+
st.image(safe_path, caption=f"Image {i+1}", use_container_width=True)
|
| 570 |
+
except Exception as e:
|
| 571 |
+
st.error(f"Error loading image {i+1}: {str(e)}")
|
| 572 |
+
else:
|
| 573 |
+
st.warning(f"Image {i+1} not found")
|
| 574 |
+
except Exception as e:
|
| 575 |
+
st.error(f"Error displaying image {i+1}: {str(e)}")
|
| 576 |
|
| 577 |
# Force garbage collection after image generation
|
| 578 |
if memory_optimization or apply_vram_optimization:
|
image_generator.py
CHANGED
|
@@ -188,9 +188,20 @@ class ImageGenerator:
|
|
| 188 |
height=min(self.target_size[1], 512) # Limit size for Hugging Face
|
| 189 |
).images[0]
|
| 190 |
|
| 191 |
-
# Save the image to a temporary file
|
| 192 |
-
output_path = f"temp/image_{int(time.time() * 1000)}.
|
| 193 |
-
image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
# Force garbage collection after inference
|
| 196 |
gc.collect()
|
|
@@ -248,9 +259,20 @@ class ImageGenerator:
|
|
| 248 |
height=512
|
| 249 |
).images[0]
|
| 250 |
|
| 251 |
-
# Save the image
|
| 252 |
-
output_path = f"temp/retry_image_{int(time.time() * 1000)}.
|
| 253 |
-
image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
return output_path
|
| 256 |
except Exception as e:
|
|
|
|
| 188 |
height=min(self.target_size[1], 512) # Limit size for Hugging Face
|
| 189 |
).images[0]
|
| 190 |
|
| 191 |
+
# Save the image to a temporary file with explicit format
|
| 192 |
+
output_path = f"temp/image_{int(time.time() * 1000)}.jpg"
|
| 193 |
+
image = image.convert("RGB") # Ensure image is in RGB mode
|
| 194 |
+
image.save(output_path, format="JPEG", quality=95) # Use JPEG format explicitly
|
| 195 |
+
|
| 196 |
+
# Verify the image was saved correctly
|
| 197 |
+
try:
|
| 198 |
+
from PIL import Image
|
| 199 |
+
test_load = Image.open(output_path)
|
| 200 |
+
test_load.verify() # Verify image is valid
|
| 201 |
+
test_load.close()
|
| 202 |
+
except Exception as e:
|
| 203 |
+
st.error(f"Image verification failed: {str(e)}. Using fallback.")
|
| 204 |
+
return self.create_fallback_image(prompt)
|
| 205 |
|
| 206 |
# Force garbage collection after inference
|
| 207 |
gc.collect()
|
|
|
|
| 259 |
height=512
|
| 260 |
).images[0]
|
| 261 |
|
| 262 |
+
# Save the image with explicit format
|
| 263 |
+
output_path = f"temp/retry_image_{int(time.time() * 1000)}.jpg"
|
| 264 |
+
image = image.convert("RGB") # Ensure image is in RGB mode
|
| 265 |
+
image.save(output_path, format="JPEG", quality=95) # Use JPEG format explicitly
|
| 266 |
+
|
| 267 |
+
# Verify the image was saved correctly
|
| 268 |
+
try:
|
| 269 |
+
from PIL import Image
|
| 270 |
+
test_load = Image.open(output_path)
|
| 271 |
+
test_load.verify() # Verify image is valid
|
| 272 |
+
test_load.close()
|
| 273 |
+
except Exception as e:
|
| 274 |
+
st.error(f"Image verification failed: {str(e)}. Using fallback.")
|
| 275 |
+
return self.create_fallback_image(prompt)
|
| 276 |
|
| 277 |
return output_path
|
| 278 |
except Exception as e:
|