annotate flux.py @ 3:21ee409e6cde draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit f8d8a9399068a8f11582d528e1eb54d36125fb76
author bgruening
date Fri, 22 Nov 2024 13:41:32 +0000
parents 7933bed1ffab
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
1 import os
0
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
2 import sys
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
3
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
4 import torch
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
5 from diffusers import FluxPipeline
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
6
1
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
7 model_path = sys.argv[1]
0
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
8
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
9 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
10 if prompt_type == "file":
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
11 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
12 prompt = f.read().strip()
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
13 elif prompt_type == "text":
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
14 prompt = sys.argv[3]
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
15
1
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
16 if "dev" in model_path:
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
17 num_inference_steps = 20
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
18 elif "schnell" in model_path:
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
19 num_inference_steps = 4
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
20 else:
0
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
21 print("Invalid model!")
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
22 sys.exit(1)
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
23
1
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
24 snapshots = []
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
25 for d in os.listdir(os.path.join(model_path, "snapshots")):
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
26 snapshots.append(os.path.join(model_path, "snapshots", d))
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
27 latest_snapshot_path = max(snapshots, key=os.path.getmtime)
0
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
28
1
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
29 pipe = FluxPipeline.from_pretrained(latest_snapshot_path, torch_dtype=torch.bfloat16)
0
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
30 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
31 pipe.vae.enable_slicing()
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
32 pipe.vae.enable_tiling()
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
33 pipe.to(torch.float16)
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
34
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
35 image = pipe(
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
36 prompt,
1
7933bed1ffab planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents: 0
diff changeset
37 num_inference_steps=num_inference_steps,
0
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
38 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
39 ).images[0]
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
40
0d0561746128 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff changeset
41 image.save("output.png")