Gemini899 commited on
Commit
f88cfbb
·
verified ·
1 Parent(s): 3c7b92a

Update flux1_img2img.py

Browse files
Files changed (1) hide show
  1. flux1_img2img.py +36 -36
flux1_img2img.py CHANGED
@@ -1,36 +1,36 @@
1
- import torch
2
- from diffusers import FluxImg2ImgPipeline
3
-
4
- from PIL import Image
5
- import sys
6
- import spaces
7
-
8
- # I only test with FLUX.1-schnell
9
-
10
- @spaces.GPU
11
- def process_image(image,mask_image,prompt="a person",model_id="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0,num_inference_steps=4):
12
- print("start process image process_image")
13
- if image == None:
14
- print("empty input image returned")
15
- return None
16
-
17
- pipe = FluxImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
18
- pipe.to("cuda")
19
-
20
- generators = []
21
- generator = torch.Generator("cuda").manual_seed(seed)
22
- generators.append(generator)
23
- # more parameter see https://huggingface.co/docs/diffusers/api/pipelines/flux#diffusers.FluxInpaintPipeline
24
- print(prompt)
25
- output = pipe(prompt=prompt, image=image,generator=generator,strength=strength
26
- ,guidance_scale=0,num_inference_steps=num_inference_steps,max_sequence_length=256)
27
-
28
- # TODO support mask
29
- return output.images[0]
30
-
31
- if __name__ == "__main__":
32
- #args input-image input-mask output
33
- image = Image.open(sys.argv[1])
34
- mask = Image.open(sys.argv[2])
35
- output = process_image(image,mask)
36
- output.save(sys.argv[3])
 
1
+ import torch
2
+ from diffusers import FluxImg2ImgPipeline
3
+ from PIL import Image
4
+ import sys
5
+ import spaces
6
+
7
+ # I only test with FLUX.1-schnell
8
+
9
+ @spaces.GPU
10
+ def process_image(image, mask_image, prompt="a person", model_id="black-forest-labs/FLUX.1-schnell", strength=0.75, seed=0, num_inference_steps=4):
11
+ print("start process image process_image")
12
+ if image is None:
13
+ print("empty input image returned")
14
+ return None
15
+
16
+ # Ensure image is in RGB mode (helps with WebP and other formats)
17
+ if image.mode != "RGB":
18
+ image = image.convert("RGB")
19
+
20
+ pipe = FluxImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
21
+ pipe.to("cuda")
22
+
23
+ generator = torch.Generator("cuda").manual_seed(seed)
24
+ print(prompt)
25
+ output = pipe(prompt=prompt, image=image, generator=generator, strength=strength,
26
+ guidance_scale=0, num_inference_steps=num_inference_steps, max_sequence_length=256)
27
+
28
+ # TODO: support mask
29
+ return output.images[0]
30
+
31
+ if __name__ == "__main__":
32
+ # args: input-image input-mask output
33
+ image = Image.open(sys.argv[1])
34
+ mask = Image.open(sys.argv[2])
35
+ output = process_image(image, mask)
36
+ output.save(sys.argv[3])