Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,21 +4,35 @@ import logging
|
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
import spaces
|
| 7 |
-
from diffusers import DiffusionPipeline
|
| 8 |
import copy
|
| 9 |
import random
|
| 10 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Load LoRAs from JSON file
|
| 13 |
with open('loras.json', 'r') as f:
|
| 14 |
loras = json.load(f)
|
| 15 |
|
| 16 |
# Initialize the base model
|
|
|
|
|
|
|
| 17 |
base_model = "John6666/real-flux-10b-schnell-fp8-flux"
|
| 18 |
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
MAX_SEED = 2**32-1
|
| 21 |
|
|
|
|
|
|
|
| 22 |
class calculateDuration:
|
| 23 |
def __init__(self, activity_name=""):
|
| 24 |
self.activity_name = activity_name
|
|
@@ -49,30 +63,33 @@ def update_selection(evt: gr.SelectData, width, height):
|
|
| 49 |
width = 1024
|
| 50 |
height = 768
|
| 51 |
return (
|
| 52 |
-
gr.update(placeholder=new_placeholder),
|
|
|
|
| 53 |
updated_text,
|
| 54 |
evt.index,
|
| 55 |
width,
|
| 56 |
height,
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
@spaces.GPU(duration=70)
|
| 60 |
def generate_image(prompt, trigger_word, steps, seed, cfg_scale, width, height, lora_scale, progress):
|
| 61 |
pipe.to("cuda")
|
| 62 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 63 |
-
|
| 64 |
with calculateDuration("Generating image"):
|
| 65 |
# Generate image
|
| 66 |
-
|
| 67 |
-
prompt=
|
| 68 |
num_inference_steps=steps,
|
| 69 |
guidance_scale=cfg_scale,
|
| 70 |
width=width,
|
| 71 |
height=height,
|
| 72 |
generator=generator,
|
| 73 |
joint_attention_kwargs={"scale": lora_scale},
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
| 78 |
if selected_index is None:
|
|
@@ -81,6 +98,19 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid
|
|
| 81 |
selected_lora = loras[selected_index]
|
| 82 |
lora_path = selected_lora["repo"]
|
| 83 |
trigger_word = selected_lora["trigger_word"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Load LoRA weights
|
| 86 |
with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
|
|
@@ -94,10 +124,18 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid
|
|
| 94 |
if randomize_seed:
|
| 95 |
seed = random.randint(0, MAX_SEED)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
run_lora.zerogpu = True
|
| 103 |
|
|
@@ -156,20 +194,21 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
|
|
| 156 |
with gr.Row():
|
| 157 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 158 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 159 |
-
|
|
|
|
| 160 |
|
| 161 |
gallery.select(
|
| 162 |
update_selection,
|
| 163 |
-
inputs=[width, height],
|
| 164 |
-
outputs=[prompt, selected_info, selected_index, width, height]
|
| 165 |
)
|
| 166 |
|
| 167 |
gr.on(
|
| 168 |
triggers=[generate_button.click, prompt.submit],
|
| 169 |
fn=run_lora,
|
| 170 |
inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
|
| 171 |
-
outputs=[result, seed]
|
| 172 |
)
|
| 173 |
|
| 174 |
app.queue(default_concurrency_limit=2).launch(show_error=True)
|
| 175 |
-
app.launch()
|
|
|
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
import spaces
|
| 7 |
+
from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
|
| 8 |
import copy
|
| 9 |
import random
|
| 10 |
import time
|
| 11 |
+
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
| 12 |
+
from huggingface_hub import HfFileSystem, ModelCard
|
| 13 |
+
|
| 14 |
+
from huggingface_hub import login
|
| 15 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 16 |
+
login(token=hf_token)
|
| 17 |
|
| 18 |
# Load LoRAs from JSON file
|
| 19 |
with open('loras.json', 'r') as f:
|
| 20 |
loras = json.load(f)
|
| 21 |
|
| 22 |
# Initialize the base model
|
| 23 |
+
dtype = torch.bfloat16
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
base_model = "John6666/real-flux-10b-schnell-fp8-flux"
|
| 26 |
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
| 27 |
|
| 28 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
| 29 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
| 30 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1).to(device)
|
| 31 |
+
|
| 32 |
MAX_SEED = 2**32-1
|
| 33 |
|
| 34 |
+
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
| 35 |
+
|
| 36 |
class calculateDuration:
|
| 37 |
def __init__(self, activity_name=""):
|
| 38 |
self.activity_name = activity_name
|
|
|
|
| 63 |
width = 1024
|
| 64 |
height = 768
|
| 65 |
return (
|
| 66 |
+
#gr.update(placeholder=new_placeholder),
|
| 67 |
+
prompt,
|
| 68 |
updated_text,
|
| 69 |
evt.index,
|
| 70 |
width,
|
| 71 |
height,
|
| 72 |
+
lora_scale,
|
| 73 |
)
|
| 74 |
|
| 75 |
@spaces.GPU(duration=70)
|
| 76 |
def generate_image(prompt, trigger_word, steps, seed, cfg_scale, width, height, lora_scale, progress):
|
| 77 |
pipe.to("cuda")
|
| 78 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
|
|
|
| 79 |
with calculateDuration("Generating image"):
|
| 80 |
# Generate image
|
| 81 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 82 |
+
prompt=prompt_mash,
|
| 83 |
num_inference_steps=steps,
|
| 84 |
guidance_scale=cfg_scale,
|
| 85 |
width=width,
|
| 86 |
height=height,
|
| 87 |
generator=generator,
|
| 88 |
joint_attention_kwargs={"scale": lora_scale},
|
| 89 |
+
output_type="pil",
|
| 90 |
+
good_vae=good_vae,
|
| 91 |
+
):
|
| 92 |
+
yield img
|
| 93 |
|
| 94 |
def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
| 95 |
if selected_index is None:
|
|
|
|
| 98 |
selected_lora = loras[selected_index]
|
| 99 |
lora_path = selected_lora["repo"]
|
| 100 |
trigger_word = selected_lora["trigger_word"]
|
| 101 |
+
if(trigger_word):
|
| 102 |
+
if "trigger_position" in selected_lora:
|
| 103 |
+
if selected_lora["trigger_position"] == "prepend":
|
| 104 |
+
prompt_mash = f"{trigger_word} {prompt}"
|
| 105 |
+
else:
|
| 106 |
+
prompt_mash = f"{prompt} {trigger_word}"
|
| 107 |
+
else:
|
| 108 |
+
prompt_mash = f"{trigger_word} {prompt}"
|
| 109 |
+
else:
|
| 110 |
+
prompt_mash = prompt
|
| 111 |
+
|
| 112 |
+
with calculateDuration("Unloading LoRA"):
|
| 113 |
+
pipe.unload_lora_weights()
|
| 114 |
|
| 115 |
# Load LoRA weights
|
| 116 |
with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
|
|
|
|
| 124 |
if randomize_seed:
|
| 125 |
seed = random.randint(0, MAX_SEED)
|
| 126 |
|
| 127 |
+
image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress)
|
| 128 |
+
# Consume the generator to get the final image
|
| 129 |
+
final_image = None
|
| 130 |
+
step_counter = 0
|
| 131 |
+
for image in image_generator:
|
| 132 |
+
step_counter+=1
|
| 133 |
+
final_image = image
|
| 134 |
+
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
| 135 |
+
yield image, seed, gr.update(value=progress_bar, visible=True)
|
| 136 |
+
|
| 137 |
+
yield final_image, seed, gr.update(value=progress_bar, visible=False)
|
| 138 |
+
|
| 139 |
|
| 140 |
run_lora.zerogpu = True
|
| 141 |
|
|
|
|
| 194 |
with gr.Row():
|
| 195 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 196 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 197 |
+
default_scale = gr.Checkbox(True, label="Use default LoRA scale")
|
| 198 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3.0, step=0.01, value=0.95)
|
| 199 |
|
| 200 |
gallery.select(
|
| 201 |
update_selection,
|
| 202 |
+
inputs=[width, height, default_scale, lora_scale],
|
| 203 |
+
outputs=[prompt, selected_info, selected_index, width, height, lora_scale]
|
| 204 |
)
|
| 205 |
|
| 206 |
gr.on(
|
| 207 |
triggers=[generate_button.click, prompt.submit],
|
| 208 |
fn=run_lora,
|
| 209 |
inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
|
| 210 |
+
outputs=[result, seed, progress_bar]
|
| 211 |
)
|
| 212 |
|
| 213 |
app.queue(default_concurrency_limit=2).launch(show_error=True)
|
| 214 |
+
app.launch()
|