Mercurial > repos > bgruening > black_forest_labs_flux
annotate flux.py @ 4:7939ae8c5fd5 draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit d9ac789235499840b8965cbe5efe1d26406f5d9a
author | bgruening |
---|---|
date | Mon, 02 Dec 2024 20:26:07 +0000 |
parents | 7933bed1ffab |
children |
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] |
4
7939ae8c5fd5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit d9ac789235499840b8965cbe5efe1d26406f5d9a
bgruening
parents:
1
diff
changeset
|
15 print(f"Creating image from prompt: {prompt}") |
0
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
16 |
1
7933bed1ffab
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents:
0
diff
changeset
|
17 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
|
18 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
|
19 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
|
20 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
|
21 else: |
0
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
22 print("Invalid model!") |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
23 sys.exit(1) |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
24 |
1
7933bed1ffab
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents:
0
diff
changeset
|
25 snapshots = [] |
7933bed1ffab
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents:
0
diff
changeset
|
26 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
|
27 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
|
28 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
|
29 |
1
7933bed1ffab
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents:
0
diff
changeset
|
30 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
|
31 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
|
32 pipe.vae.enable_slicing() |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
33 pipe.vae.enable_tiling() |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
34 pipe.to(torch.float16) |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
35 |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
36 image = pipe( |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
37 prompt, |
1
7933bed1ffab
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 0959e15618c76e05c78e5811218c9c4bc343db27
bgruening
parents:
0
diff
changeset
|
38 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
|
39 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
|
40 ).images[0] |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
41 |
0d0561746128
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/flux commit 71b3dacee16dc999cb4fa113858d6ace1781c71c
bgruening
parents:
diff
changeset
|
42 image.save("output.png") |