Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,11 @@ import io
|
|
| 8 |
import time
|
| 9 |
|
| 10 |
# ===== CONFIG =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 13 |
|
|
@@ -20,9 +25,21 @@ pipe = DiffusionPipeline.from_pretrained(
|
|
| 20 |
)
|
| 21 |
pipe.to(device)
|
| 22 |
|
| 23 |
-
# Enable
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
MAX_SEED = np.iinfo(np.int32).max
|
| 28 |
IMAGE_SIZE = 1024 # Same as original code
|
|
@@ -74,27 +91,34 @@ def generate(
|
|
| 74 |
|
| 75 |
generator = torch.manual_seed(seed)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# ===== EXAMPLES =====
|
| 100 |
examples = [
|
|
|
|
| 8 |
import time
|
| 9 |
|
| 10 |
# ===== CONFIG =====
|
| 11 |
+
# Print debug info
|
| 12 |
+
print(f"PyTorch version: {torch.__version__}")
|
| 13 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 14 |
+
print(f"CUDA device count: {torch.cuda.device_count()}")
|
| 15 |
+
|
| 16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 18 |
|
|
|
|
| 25 |
)
|
| 26 |
pipe.to(device)
|
| 27 |
|
| 28 |
+
# Enable optimizations only if GPU is available
|
| 29 |
+
if device == "cuda":
|
| 30 |
+
try:
|
| 31 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 32 |
+
print("Enabled xformers memory efficient attention")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Could not enable xformers: {str(e)}")
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
pipe.unet.to(memory_format=torch.channels_last)
|
| 38 |
+
print("Enabled channels last memory format")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Could not enable channels last: {str(e)}")
|
| 41 |
+
else:
|
| 42 |
+
print("Running on CPU - skipping GPU optimizations")
|
| 43 |
|
| 44 |
MAX_SEED = np.iinfo(np.int32).max
|
| 45 |
IMAGE_SIZE = 1024 # Same as original code
|
|
|
|
| 91 |
|
| 92 |
generator = torch.manual_seed(seed)
|
| 93 |
|
| 94 |
+
try:
|
| 95 |
+
# Ultra-fast generation with minimal steps
|
| 96 |
+
result = pipe(
|
| 97 |
+
prompt=prompt,
|
| 98 |
+
negative_prompt=negative_prompt,
|
| 99 |
+
width=IMAGE_SIZE,
|
| 100 |
+
height=IMAGE_SIZE,
|
| 101 |
+
guidance_scale=guidance_scale,
|
| 102 |
+
num_inference_steps=max(1, num_inference_steps), # Minimum 1 step
|
| 103 |
+
generator=generator,
|
| 104 |
+
).images[0]
|
| 105 |
+
|
| 106 |
+
# Optimized watermark and JPG conversion
|
| 107 |
+
watermarked = add_watermark(result)
|
| 108 |
+
buffer = io.BytesIO()
|
| 109 |
+
watermarked.save(buffer, format="JPEG", quality=85, optimize=True)
|
| 110 |
+
buffer.seek(0)
|
| 111 |
+
|
| 112 |
+
gen_time = time.time() - start_time
|
| 113 |
+
status = f"✔️ Generated in {gen_time:.2f}s | Seed: {seed}"
|
| 114 |
+
|
| 115 |
+
return Image.open(buffer), status
|
| 116 |
|
| 117 |
+
except torch.cuda.OutOfMemoryError:
|
| 118 |
+
return None, "⚠️ GPU out of memory - try a simpler prompt"
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Generation error: {str(e)}")
|
| 121 |
+
return None, f"⚠️ Error: {str(e)[:200]}"
|
| 122 |
|
| 123 |
# ===== EXAMPLES =====
|
| 124 |
examples = [
|