Spaces:
Running
on
Zero
Running
on
Zero
wanghaofan
commited on
Commit
•
bbfe670
1
Parent(s):
2d16a9f
Upload 3 files
Browse files- app.py +187 -122
- live_preview_helpers.py +166 -0
- loras.json +38 -0
app.py
CHANGED
@@ -1,142 +1,207 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
#import spaces #[uncomment to use ZeroGPU]
|
5 |
-
from diffusers import DiffusionPipeline
|
6 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
torch_dtype = torch.float32
|
15 |
|
16 |
-
|
17 |
-
pipe = pipe.to(device)
|
18 |
|
19 |
-
|
20 |
-
MAX_IMAGE_SIZE = 1024
|
21 |
|
22 |
-
|
23 |
-
def
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
generator = torch.Generator().manual_seed(seed)
|
29 |
-
|
30 |
-
image = pipe(
|
31 |
-
prompt = prompt,
|
32 |
-
negative_prompt = negative_prompt,
|
33 |
-
guidance_scale = guidance_scale,
|
34 |
-
num_inference_steps = num_inference_steps,
|
35 |
-
width = width,
|
36 |
-
height = height,
|
37 |
-
generator = generator
|
38 |
-
).images[0]
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
}
|
53 |
-
""
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
)
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
result = gr.Image(label="Result", show_label=False)
|
75 |
|
|
|
76 |
with gr.Accordion("Advanced Settings", open=False):
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
placeholder="Enter a negative prompt",
|
82 |
-
visible=False,
|
83 |
-
)
|
84 |
-
|
85 |
-
seed = gr.Slider(
|
86 |
-
label="Seed",
|
87 |
-
minimum=0,
|
88 |
-
maximum=MAX_SEED,
|
89 |
-
step=1,
|
90 |
-
value=0,
|
91 |
-
)
|
92 |
-
|
93 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
94 |
-
|
95 |
-
with gr.Row():
|
96 |
-
|
97 |
-
width = gr.Slider(
|
98 |
-
label="Width",
|
99 |
-
minimum=256,
|
100 |
-
maximum=MAX_IMAGE_SIZE,
|
101 |
-
step=32,
|
102 |
-
value=1024, #Replace with defaults that work for your model
|
103 |
-
)
|
104 |
-
|
105 |
-
height = gr.Slider(
|
106 |
-
label="Height",
|
107 |
-
minimum=256,
|
108 |
-
maximum=MAX_IMAGE_SIZE,
|
109 |
-
step=32,
|
110 |
-
value=1024, #Replace with defaults that work for your model
|
111 |
-
)
|
112 |
-
|
113 |
-
with gr.Row():
|
114 |
|
115 |
-
|
116 |
-
label="
|
117 |
-
|
118 |
-
maximum=10.0,
|
119 |
-
step=0.1,
|
120 |
-
value=0.0, #Replace with defaults that work for your model
|
121 |
-
)
|
122 |
|
123 |
-
|
124 |
-
label="
|
125 |
-
minimum=1,
|
126 |
-
maximum=
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
inputs = [prompt]
|
134 |
-
)
|
135 |
gr.on(
|
136 |
-
triggers=[
|
137 |
-
fn
|
138 |
-
inputs
|
139 |
-
outputs
|
140 |
)
|
141 |
|
142 |
-
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
import json
|
4 |
+
import logging
|
|
|
|
|
5 |
import torch
|
6 |
+
from PIL import Image
|
7 |
+
import spaces
|
8 |
+
from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
|
9 |
+
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
10 |
+
|
11 |
+
from huggingface_hub import HfFileSystem, ModelCard
|
12 |
+
import copy
|
13 |
+
import random
|
14 |
+
import time
|
15 |
+
|
16 |
+
# Load LoRAs from JSON file
|
17 |
+
with open('loras.json', 'r') as f:
|
18 |
+
loras = json.load(f)
|
19 |
|
20 |
+
# Initialize the base model
|
21 |
+
dtype = torch.bfloat16
|
22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
+
base_model = "black-forest-labs/FLUX.1-dev"
|
24 |
|
25 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
26 |
+
good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype).to(device)
|
27 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1).to(device)
|
|
|
28 |
|
29 |
+
MAX_SEED = 2**32-1
|
|
|
30 |
|
31 |
+
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
|
|
32 |
|
33 |
+
class calculateDuration:
|
34 |
+
def __init__(self, activity_name=""):
|
35 |
+
self.activity_name = activity_name
|
36 |
|
37 |
+
def __enter__(self):
|
38 |
+
self.start_time = time.time()
|
39 |
+
return self
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
42 |
+
self.end_time = time.time()
|
43 |
+
self.elapsed_time = self.end_time - self.start_time
|
44 |
+
if self.activity_name:
|
45 |
+
print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
|
46 |
+
else:
|
47 |
+
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
48 |
+
|
49 |
+
def update_selection(evt: gr.SelectData, width, height):
|
50 |
+
selected_lora = loras[evt.index]
|
51 |
+
new_placeholder = f"Type a prompt for {selected_lora['title']}"
|
52 |
+
lora_repo = selected_lora["repo"]
|
53 |
+
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
|
54 |
+
if "aspect" in selected_lora:
|
55 |
+
if selected_lora["aspect"] == "portrait":
|
56 |
+
width = 768
|
57 |
+
height = 1024
|
58 |
+
elif selected_lora["aspect"] == "landscape":
|
59 |
+
width = 1024
|
60 |
+
height = 768
|
61 |
+
else:
|
62 |
+
width = 1024
|
63 |
+
height = 1024
|
64 |
+
return (
|
65 |
+
gr.update(placeholder=new_placeholder),
|
66 |
+
updated_text,
|
67 |
+
evt.index,
|
68 |
+
width,
|
69 |
+
height,
|
70 |
+
)
|
71 |
+
|
72 |
+
@spaces.GPU(duration=70)
|
73 |
+
def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress):
|
74 |
+
pipe.to("cuda")
|
75 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
76 |
+
with calculateDuration("Generating image"):
|
77 |
+
# Generate image
|
78 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
79 |
+
prompt=prompt_mash,
|
80 |
+
num_inference_steps=steps,
|
81 |
+
guidance_scale=cfg_scale,
|
82 |
+
width=width,
|
83 |
+
height=height,
|
84 |
+
generator=generator,
|
85 |
+
joint_attention_kwargs={"scale": lora_scale},
|
86 |
+
output_type="pil",
|
87 |
+
good_vae=good_vae,
|
88 |
+
):
|
89 |
+
yield img
|
90 |
+
|
91 |
+
def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
92 |
+
if selected_index is None:
|
93 |
+
raise gr.Error("You must select a LoRA before proceeding.")
|
94 |
+
selected_lora = loras[selected_index]
|
95 |
+
lora_path = selected_lora["repo"]
|
96 |
+
trigger_word = selected_lora["trigger_word"]
|
97 |
+
if(trigger_word):
|
98 |
+
if "trigger_position" in selected_lora:
|
99 |
+
if selected_lora["trigger_position"] == "prepend":
|
100 |
+
prompt_mash = f"{trigger_word} {prompt}"
|
101 |
+
else:
|
102 |
+
prompt_mash = f"{prompt} {trigger_word}"
|
103 |
+
else:
|
104 |
+
prompt_mash = f"{trigger_word} {prompt}"
|
105 |
+
else:
|
106 |
+
prompt_mash = prompt
|
107 |
+
|
108 |
+
with calculateDuration("Unloading LoRA"):
|
109 |
+
pipe.unload_lora_weights()
|
110 |
+
|
111 |
+
# Load LoRA weights
|
112 |
+
with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
|
113 |
+
if "weights" in selected_lora:
|
114 |
+
pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
|
115 |
+
else:
|
116 |
+
pipe.load_lora_weights(lora_path)
|
117 |
+
|
118 |
+
# Set random seed for reproducibility
|
119 |
+
with calculateDuration("Randomizing seed"):
|
120 |
+
if randomize_seed:
|
121 |
+
seed = random.randint(0, MAX_SEED)
|
122 |
+
|
123 |
+
image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress)
|
124 |
|
125 |
+
# Consume the generator to get the final image
|
126 |
+
final_image = None
|
127 |
+
step_counter = 0
|
128 |
+
for image in image_generator:
|
129 |
+
step_counter+=1
|
130 |
+
final_image = image
|
131 |
+
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
132 |
+
yield image, seed, gr.update(value=progress_bar, visible=True)
|
133 |
|
134 |
+
yield final_image, seed, gr.update(value=progress_bar, visible=False)
|
135 |
+
|
136 |
+
run_lora.zerogpu = True
|
137 |
+
|
138 |
+
css = '''
|
139 |
+
#gen_btn{height: 100%}
|
140 |
+
#title{text-align: center}
|
141 |
+
#title h1{font-size: 3em; display:inline-flex; align-items:center}
|
142 |
+
#title img{width: 100px; margin-right: 0.5em}
|
143 |
+
#gallery .grid-wrap{height: 10vh}
|
144 |
+
#lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}
|
145 |
+
.card_internal{display: flex;height: 100px;margin-top: .5em}
|
146 |
+
.card_internal img{margin-right: 1em}
|
147 |
+
.styler{--form-gap-width: 0px !important}
|
148 |
+
#progress{height:30px}
|
149 |
+
#progress .generating{display:none}
|
150 |
+
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
151 |
+
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
152 |
+
'''
|
153 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
|
154 |
+
title = gr.HTML(
|
155 |
+
"""<h1><img src="https://huggingface.co/Shakker-Labs/FLUX.1-dev-LoRA-collections/resolve/main/logo.png" alt="LoRA"> FLUX LoRA Gallery from Shakker AI</h1>""",
|
156 |
+
elem_id="title",
|
157 |
+
)
|
158 |
+
selected_index = gr.State(None)
|
159 |
+
with gr.Row():
|
160 |
+
with gr.Column(scale=3):
|
161 |
+
prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
|
162 |
+
with gr.Column(scale=1, elem_id="gen_column"):
|
163 |
+
generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
164 |
+
with gr.Row():
|
165 |
+
with gr.Column():
|
166 |
+
selected_info = gr.Markdown("")
|
167 |
+
gallery = gr.Gallery(
|
168 |
+
[(item["image"], item["title"]) for item in loras],
|
169 |
+
label="LoRA Gallery",
|
170 |
+
allow_preview=False,
|
171 |
+
columns=3,
|
172 |
+
elem_id="gallery"
|
173 |
)
|
174 |
+
with gr.Column():
|
175 |
+
progress_bar = gr.Markdown(elem_id="progress",visible=False)
|
176 |
+
result = gr.Image(label="Generated Image")
|
|
|
177 |
|
178 |
+
with gr.Row():
|
179 |
with gr.Accordion("Advanced Settings", open=False):
|
180 |
+
with gr.Column():
|
181 |
+
with gr.Row():
|
182 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
183 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
+
with gr.Row():
|
186 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
187 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
|
|
|
|
|
|
|
|
188 |
|
189 |
+
with gr.Row():
|
190 |
+
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
191 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
192 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
|
193 |
+
|
194 |
+
gallery.select(
|
195 |
+
update_selection,
|
196 |
+
inputs=[width, height],
|
197 |
+
outputs=[prompt, selected_info, selected_index, width, height]
|
198 |
+
)
|
|
|
|
|
199 |
gr.on(
|
200 |
+
triggers=[generate_button.click, prompt.submit],
|
201 |
+
fn=run_lora,
|
202 |
+
inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
|
203 |
+
outputs=[result, seed, progress_bar]
|
204 |
)
|
205 |
|
206 |
+
app.queue()
|
207 |
+
app.launch()
|
live_preview_helpers.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
from diffusers import FluxPipeline, AutoencoderTiny, FlowMatchEulerDiscreteScheduler
|
4 |
+
from typing import Any, Dict, List, Optional, Union
|
5 |
+
|
6 |
+
# Helper functions
|
7 |
+
def calculate_shift(
|
8 |
+
image_seq_len,
|
9 |
+
base_seq_len: int = 256,
|
10 |
+
max_seq_len: int = 4096,
|
11 |
+
base_shift: float = 0.5,
|
12 |
+
max_shift: float = 1.16,
|
13 |
+
):
|
14 |
+
m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
|
15 |
+
b = base_shift - m * base_seq_len
|
16 |
+
mu = image_seq_len * m + b
|
17 |
+
return mu
|
18 |
+
|
19 |
+
def retrieve_timesteps(
|
20 |
+
scheduler,
|
21 |
+
num_inference_steps: Optional[int] = None,
|
22 |
+
device: Optional[Union[str, torch.device]] = None,
|
23 |
+
timesteps: Optional[List[int]] = None,
|
24 |
+
sigmas: Optional[List[float]] = None,
|
25 |
+
**kwargs,
|
26 |
+
):
|
27 |
+
if timesteps is not None and sigmas is not None:
|
28 |
+
raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
|
29 |
+
if timesteps is not None:
|
30 |
+
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
31 |
+
timesteps = scheduler.timesteps
|
32 |
+
num_inference_steps = len(timesteps)
|
33 |
+
elif sigmas is not None:
|
34 |
+
scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
|
35 |
+
timesteps = scheduler.timesteps
|
36 |
+
num_inference_steps = len(timesteps)
|
37 |
+
else:
|
38 |
+
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
39 |
+
timesteps = scheduler.timesteps
|
40 |
+
return timesteps, num_inference_steps
|
41 |
+
|
42 |
+
# FLUX pipeline function
|
43 |
+
@torch.inference_mode()
|
44 |
+
def flux_pipe_call_that_returns_an_iterable_of_images(
|
45 |
+
self,
|
46 |
+
prompt: Union[str, List[str]] = None,
|
47 |
+
prompt_2: Optional[Union[str, List[str]]] = None,
|
48 |
+
height: Optional[int] = None,
|
49 |
+
width: Optional[int] = None,
|
50 |
+
num_inference_steps: int = 28,
|
51 |
+
timesteps: List[int] = None,
|
52 |
+
guidance_scale: float = 3.5,
|
53 |
+
num_images_per_prompt: Optional[int] = 1,
|
54 |
+
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
55 |
+
latents: Optional[torch.FloatTensor] = None,
|
56 |
+
prompt_embeds: Optional[torch.FloatTensor] = None,
|
57 |
+
pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
58 |
+
output_type: Optional[str] = "pil",
|
59 |
+
return_dict: bool = True,
|
60 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
61 |
+
max_sequence_length: int = 512,
|
62 |
+
good_vae: Optional[Any] = None,
|
63 |
+
):
|
64 |
+
height = height or self.default_sample_size * self.vae_scale_factor
|
65 |
+
width = width or self.default_sample_size * self.vae_scale_factor
|
66 |
+
|
67 |
+
# 1. Check inputs
|
68 |
+
self.check_inputs(
|
69 |
+
prompt,
|
70 |
+
prompt_2,
|
71 |
+
height,
|
72 |
+
width,
|
73 |
+
prompt_embeds=prompt_embeds,
|
74 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
75 |
+
max_sequence_length=max_sequence_length,
|
76 |
+
)
|
77 |
+
|
78 |
+
self._guidance_scale = guidance_scale
|
79 |
+
self._joint_attention_kwargs = joint_attention_kwargs
|
80 |
+
self._interrupt = False
|
81 |
+
|
82 |
+
# 2. Define call parameters
|
83 |
+
batch_size = 1 if isinstance(prompt, str) else len(prompt)
|
84 |
+
device = self._execution_device
|
85 |
+
|
86 |
+
# 3. Encode prompt
|
87 |
+
lora_scale = joint_attention_kwargs.get("scale", None) if joint_attention_kwargs is not None else None
|
88 |
+
prompt_embeds, pooled_prompt_embeds, text_ids = self.encode_prompt(
|
89 |
+
prompt=prompt,
|
90 |
+
prompt_2=prompt_2,
|
91 |
+
prompt_embeds=prompt_embeds,
|
92 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
93 |
+
device=device,
|
94 |
+
num_images_per_prompt=num_images_per_prompt,
|
95 |
+
max_sequence_length=max_sequence_length,
|
96 |
+
lora_scale=lora_scale,
|
97 |
+
)
|
98 |
+
# 4. Prepare latent variables
|
99 |
+
num_channels_latents = self.transformer.config.in_channels // 4
|
100 |
+
latents, latent_image_ids = self.prepare_latents(
|
101 |
+
batch_size * num_images_per_prompt,
|
102 |
+
num_channels_latents,
|
103 |
+
height,
|
104 |
+
width,
|
105 |
+
prompt_embeds.dtype,
|
106 |
+
device,
|
107 |
+
generator,
|
108 |
+
latents,
|
109 |
+
)
|
110 |
+
# 5. Prepare timesteps
|
111 |
+
sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
|
112 |
+
image_seq_len = latents.shape[1]
|
113 |
+
mu = calculate_shift(
|
114 |
+
image_seq_len,
|
115 |
+
self.scheduler.config.base_image_seq_len,
|
116 |
+
self.scheduler.config.max_image_seq_len,
|
117 |
+
self.scheduler.config.base_shift,
|
118 |
+
self.scheduler.config.max_shift,
|
119 |
+
)
|
120 |
+
timesteps, num_inference_steps = retrieve_timesteps(
|
121 |
+
self.scheduler,
|
122 |
+
num_inference_steps,
|
123 |
+
device,
|
124 |
+
timesteps,
|
125 |
+
sigmas,
|
126 |
+
mu=mu,
|
127 |
+
)
|
128 |
+
self._num_timesteps = len(timesteps)
|
129 |
+
|
130 |
+
# Handle guidance
|
131 |
+
guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32).expand(latents.shape[0]) if self.transformer.config.guidance_embeds else None
|
132 |
+
|
133 |
+
# 6. Denoising loop
|
134 |
+
for i, t in enumerate(timesteps):
|
135 |
+
if self.interrupt:
|
136 |
+
continue
|
137 |
+
|
138 |
+
timestep = t.expand(latents.shape[0]).to(latents.dtype)
|
139 |
+
|
140 |
+
noise_pred = self.transformer(
|
141 |
+
hidden_states=latents,
|
142 |
+
timestep=timestep / 1000,
|
143 |
+
guidance=guidance,
|
144 |
+
pooled_projections=pooled_prompt_embeds,
|
145 |
+
encoder_hidden_states=prompt_embeds,
|
146 |
+
txt_ids=text_ids,
|
147 |
+
img_ids=latent_image_ids,
|
148 |
+
joint_attention_kwargs=self.joint_attention_kwargs,
|
149 |
+
return_dict=False,
|
150 |
+
)[0]
|
151 |
+
# Yield intermediate result
|
152 |
+
latents_for_image = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
153 |
+
latents_for_image = (latents_for_image / self.vae.config.scaling_factor) + self.vae.config.shift_factor
|
154 |
+
image = self.vae.decode(latents_for_image, return_dict=False)[0]
|
155 |
+
yield self.image_processor.postprocess(image, output_type=output_type)[0]
|
156 |
+
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
157 |
+
torch.cuda.empty_cache()
|
158 |
+
|
159 |
+
|
160 |
+
# Final image using good_vae
|
161 |
+
latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
162 |
+
latents = (latents / good_vae.config.scaling_factor) + good_vae.config.shift_factor
|
163 |
+
image = good_vae.decode(latents, return_dict=False)[0]
|
164 |
+
self.maybe_free_model_hooks()
|
165 |
+
torch.cuda.empty_cache()
|
166 |
+
yield self.image_processor.postprocess(image, output_type=output_type)[0]
|
loras.json
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"image": "https://huggingface.co/Shakker-Labs/FLUX.1-dev-LoRA-playful-metropolis/resolve/main/images/1d28cc19b815ce1ac7fc54dd54e94c4abea85b044f18fa7fe4dc5321.jpg",
|
4 |
+
"title": "Playful Metropolis",
|
5 |
+
"repo": "Shakker-Labs/FLUX.1-dev-LoRA-playful-metropolis",
|
6 |
+
"trigger_word": "",
|
7 |
+
"aspect": "portrait"
|
8 |
+
},
|
9 |
+
{
|
10 |
+
"image": "https://huggingface.co/Shakker-Labs/FLUX.1-dev-LoRA-live-3D/resolve/main/images/51a716fb6fe9ba5d54c260b70e7ff661d38acedc7fb725552fa77bcf.jpg",
|
11 |
+
"title": "Live 3D",
|
12 |
+
"repo": "Shakker-Labs/FLUX.1-dev-LoRA-live-3D",
|
13 |
+
"trigger_word": "",
|
14 |
+
"aspect": "portrait"
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"image": "https://huggingface.co/Shakker-Labs/FLUX.1-dev-LoRA-blended-realistic-illustration/resolve/main/images/example5.png",
|
18 |
+
"title": "Vector Journey",
|
19 |
+
"repo": "Shakker-Labs/FLUX.1-dev-LoRA-blended-realistic-illustration",
|
20 |
+
"trigger_word": "artistic style blends reality and illustration elements",
|
21 |
+
"aspect": "portrait"
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"image": "https://huggingface.co/Shakker-Labs/AWPortrait-FL/resolve/main/sample.png",
|
25 |
+
"title": "AWPortrait-FL",
|
26 |
+
"repo": "Shakker-Labs/AWPortrait-FL",
|
27 |
+
"trigger_word": "",
|
28 |
+
"aspect": "portrait"
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"image": "https://huggingface.co/Shakker-Labs/FLUX.1-dev-LoRA-collections/resolve/main/assets/1f1d61ad170064959db4f9347838a4624c681a7901bd50139e8ac0862ecbe29e.jpg",
|
32 |
+
"title": "Black Myth: Wukong",
|
33 |
+
"repo": "Shakker-Labs/Shakker-Labs/FLUX.1-dev-LoRA-collections",
|
34 |
+
"weights": "FLUX-dev-lora-Black_Myth_Wukong_hyperrealism_v1.safetensors",
|
35 |
+
"trigger_word": "aiyouxiketang",
|
36 |
+
"aspect": "portrait"
|
37 |
+
}
|
38 |
+
]
|