Spaces:
Runtime error
Runtime error
Update flux1_img2img.py
Browse files- flux1_img2img.py +49 -56
flux1_img2img.py
CHANGED
@@ -1,58 +1,51 @@
|
|
1 |
-
import os
|
2 |
import torch
|
3 |
-
from diffusers import StableDiffusionImg2ImgPipeline
|
4 |
-
from PIL import Image
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
pipe.
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
prompt
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
output
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
)
|
54 |
-
|
55 |
-
# Save the output image
|
56 |
-
output_image = output.images[0]
|
57 |
-
output_image.save("output.png")
|
58 |
-
print("Output image saved as output.png")
|
|
|
|
|
1 |
import torch
|
2 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
|
|
3 |
|
4 |
+
from PIL import Image
|
5 |
+
import sys
|
6 |
+
import spaces
|
7 |
+
|
8 |
+
# Defaulting to Stable Diffusion v1.5 here. Adjust model_id as you like.
|
9 |
+
@spaces.GPU
|
10 |
+
def process_image(
|
11 |
+
image,
|
12 |
+
mask_image,
|
13 |
+
prompt="a person",
|
14 |
+
model_id="runwayml/stable-diffusion-v1-5",
|
15 |
+
strength=0.75,
|
16 |
+
seed=0,
|
17 |
+
num_inference_steps=4
|
18 |
+
):
|
19 |
+
print("start process image process_image")
|
20 |
+
if image is None:
|
21 |
+
print("empty input image returned")
|
22 |
+
return None
|
23 |
+
|
24 |
+
# Load pipeline
|
25 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
26 |
+
pipe.to("cuda")
|
27 |
+
|
28 |
+
# Create generator for reproducible results
|
29 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
30 |
+
|
31 |
+
# The mask is not currently used in this snippet (TODO in your code)
|
32 |
+
# If you want to use the mask, you'd switch to an inpainting pipeline or handle the mask in code.
|
33 |
+
|
34 |
+
print(prompt)
|
35 |
+
output = pipe(
|
36 |
+
prompt=prompt,
|
37 |
+
image=image,
|
38 |
+
generator=generator,
|
39 |
+
strength=strength,
|
40 |
+
guidance_scale=0,
|
41 |
+
num_inference_steps=num_inference_steps
|
42 |
+
)
|
43 |
+
|
44 |
+
return output.images[0]
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
# args: input-image input-mask output
|
48 |
+
image = Image.open(sys.argv[1])
|
49 |
+
mask = Image.open(sys.argv[2])
|
50 |
+
output = process_image(image, mask)
|
51 |
+
output.save(sys.argv[3])
|
|
|
|
|
|
|
|
|
|