Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -3,41 +3,49 @@ from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
|
|
3 |
from diffusers.utils import load_image
|
4 |
import torch
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
elif torch.backends.mps.is_available():
|
9 |
-
device = "mps"
|
10 |
-
else:
|
11 |
-
device = "cpu"
|
12 |
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
pipes = {
|
15 |
-
|
16 |
-
|
17 |
}
|
18 |
|
19 |
if device == "cpu":
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
def run(prompt, image):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
demo = gr.Interface(
|
34 |
run,
|
35 |
inputs=[
|
36 |
-
|
37 |
-
|
38 |
],
|
39 |
-
outputs=gr.Image(width=512,height=512),
|
40 |
live=True
|
41 |
)
|
42 |
-
|
43 |
demo.launch()
|
|
|
3 |
from diffusers.utils import load_image
|
4 |
import torch
|
5 |
|
6 |
+
# Clear CUDA cache
|
7 |
+
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Set environment variable for memory fragmentation
|
10 |
+
import os
|
11 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:128'
|
12 |
+
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
|
15 |
pipes = {
|
16 |
+
"txt2img": AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16").to(device),
|
17 |
+
"img2img": AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16").to(device)
|
18 |
}
|
19 |
|
20 |
if device == "cpu":
|
21 |
+
pipes["txt2img"].enable_model_cpu_offload()
|
22 |
+
pipes["img2img"].enable_model_cpu_offload()
|
|
|
23 |
|
24 |
def run(prompt, image):
|
25 |
+
try:
|
26 |
+
print(f"prompt={prompt}, image={image}")
|
27 |
+
if image is None:
|
28 |
+
return pipes["txt2img"](prompt=prompt, num_inference_steps=1, guidance_scale=0.0).images[0]
|
29 |
+
else:
|
30 |
+
image = image.resize((512,512))
|
31 |
+
print(f"img2img image={image}")
|
32 |
+
return pipes["img2img"](prompt, image=image, num_inference_steps=2, strength=0.5, guidance_scale=0.0).images[0]
|
33 |
+
except RuntimeError as e:
|
34 |
+
if "CUDA out of memory" in str(e):
|
35 |
+
print("CUDA out of memory. Trying to clear cache.")
|
36 |
+
torch.cuda.empty_cache()
|
37 |
+
# Consider additional fallback strategies here
|
38 |
+
else:
|
39 |
+
raise e
|
40 |
|
41 |
demo = gr.Interface(
|
42 |
run,
|
43 |
inputs=[
|
44 |
+
gr.Textbox(label="Prompt"),
|
45 |
+
gr.Image(type="pil")
|
46 |
],
|
47 |
+
outputs=gr.Image(width=512, height=512),
|
48 |
live=True
|
49 |
)
|
50 |
+
|
51 |
demo.launch()
|