annotate flux.py @ 0:0d0561746128 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
author bgruening
date Mon, 14 Oct 2024 16:50:38 +0000
parents
children 7933bed1ffab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
1 import sys
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
2
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
3 import torch
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
4 from diffusers import FluxPipeline
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
5
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
6 model = sys.argv[1]
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
7
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
8 prompt_type = sys.argv[2]
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
9 if prompt_type == "file":
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
10 with open(sys.argv[3], "r") as f:
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
11 prompt = f.read().strip()
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
12 elif prompt_type == "text":
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
13 prompt = sys.argv[3]
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
14
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
15 if model not in ["black-forest-labs/FLUX.1-dev", "black-forest-labs/FLUX.1-schnell"]:
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
16 print("Invalid model!")
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
17 sys.exit(1)
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
18
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
19
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
20 pipe = FluxPipeline.from_pretrained(model, torch_dtype=torch.bfloat16)
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
21 pipe.enable_sequential_cpu_offload()
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
22 pipe.vae.enable_slicing()
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
23 pipe.vae.enable_tiling()
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
24 pipe.to(torch.float16)
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
25
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
26 image = pipe(
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
27 prompt,
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
28 num_inference_steps=4,
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
29 generator=torch.Generator("cpu").manual_seed(42),
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
30 ).images[0]
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
31
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
32 image.save("output.png")