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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import logging
|
4 |
+
import argparse
|
5 |
+
import torch
|
6 |
+
import os
|
7 |
+
from os import path
|
8 |
+
from PIL import Image
|
9 |
+
import numpy as np
|
10 |
+
import spaces
|
11 |
+
import copy
|
12 |
+
import random
|
13 |
+
import time
|
14 |
+
from typing import Any, Dict, List, Optional, Union
|
15 |
+
from huggingface_hub import hf_hub_download
|
16 |
+
from diffusers import DiffusionPipeline, AutoencoderTiny, AutoPipelineForImage2Image
|
17 |
+
import safetensors.torch
|
18 |
+
from safetensors.torch import load_file
|
19 |
+
from pipeline import FluxWithCFGPipeline
|
20 |
+
from transformers import CLIPModel, CLIPProcessor, CLIPConfig
|
21 |
+
import gc
|
22 |
+
import warnings
|
23 |
+
|
24 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
25 |
+
|
26 |
+
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
27 |
+
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
28 |
+
os.environ["HF_HUB_CACHE"] = cache_path
|
29 |
+
os.environ["HF_HOME"] = cache_path
|
30 |
+
|
31 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
32 |
+
|
33 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
34 |
+
|
35 |
+
torch._inductor.config.conv_1x1_as_mm = True
|
36 |
+
torch._inductor.config.coordinate_descent_tuning = True
|
37 |
+
torch._inductor.config.epilogue_fusion = False
|
38 |
+
torch._inductor.config.coordinate_descent_check_all_directions = True
|
39 |
+
|
40 |
+
dtype = torch.bfloat16
|
41 |
+
pipe = FluxWithCFGPipeline.from_pretrained("ostris/OpenFLUX.1", torch_dtype=dtype, text_encoder_3=None, tokenizer_3=None
|
42 |
+
).to("cuda")
|
43 |
+
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to("cuda")
|
44 |
+
|
45 |
+
pipe.to("cuda")
|
46 |
+
clipmodel = 'norm'
|
47 |
+
if clipmodel == "long":
|
48 |
+
model_id = "zer0int/LongCLIP-GmP-ViT-L-14"
|
49 |
+
config = CLIPConfig.from_pretrained(model_id)
|
50 |
+
maxtokens = 77
|
51 |
+
if clipmodel == "norm":
|
52 |
+
model_id = "zer0int/CLIP-GmP-ViT-L-14"
|
53 |
+
config = CLIPConfig.from_pretrained(model_id)
|
54 |
+
maxtokens = 77
|
55 |
+
clip_model = CLIPModel.from_pretrained(model_id, torch_dtype=torch.bfloat16, config=config, ignore_mismatched_sizes=True).to("cuda")
|
56 |
+
clip_processor = CLIPProcessor.from_pretrained(model_id, padding="max_length", max_length=maxtokens, ignore_mismatched_sizes=True, return_tensors="pt", truncation=True)
|
57 |
+
config.text_config.max_position_embeddings = 77
|
58 |
+
|
59 |
+
pipe.tokenizer = clip_processor.tokenizer
|
60 |
+
pipe.text_encoder = clip_model.text_model
|
61 |
+
pipe.tokenizer_max_length = maxtokens
|
62 |
+
pipe.text_encoder.dtype = torch.bfloat16
|
63 |
+
torch.cuda.empty_cache()
|
64 |
+
|
65 |
+
pipe.transformer.to(memory_format=torch.channels_last)
|
66 |
+
pipe.vae.to(memory_format=torch.channels_last)
|
67 |
+
|
68 |
+
pipe.transformer = torch.compile(pipe.transformer, mode="max-autotune", fullgraph=True)
|
69 |
+
pipe.vae.decode = torch.compile(pipe.vae.decode, mode="max-autotune", fullgraph=True)
|
70 |
+
|
71 |
+
# Load LoRAs from JSON file
|
72 |
+
with open('loras.json', 'r') as f:
|
73 |
+
loras = json.load(f)
|
74 |
+
|
75 |
+
MAX_SEED = 2**32-1
|
76 |
+
|
77 |
+
class calculateDuration:
|
78 |
+
def __init__(self, activity_name=""):
|
79 |
+
self.activity_name = activity_name
|
80 |
+
|
81 |
+
def __enter__(self):
|
82 |
+
self.start_time = time.time()
|
83 |
+
return self
|
84 |
+
|
85 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
86 |
+
self.end_time = time.time()
|
87 |
+
self.elapsed_time = self.end_time - self.start_time
|
88 |
+
if self.activity_name:
|
89 |
+
print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
|
90 |
+
else:
|
91 |
+
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
92 |
+
|
93 |
+
|
94 |
+
def update_selection(evt: gr.SelectData, width, height):
|
95 |
+
selected_lora = loras[evt.index]
|
96 |
+
new_placeholder = f"Type a prompt for {selected_lora['title']}"
|
97 |
+
lora_repo = selected_lora["repo"]
|
98 |
+
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
|
99 |
+
if "aspect" in selected_lora:
|
100 |
+
if selected_lora["aspect"] == "portrait":
|
101 |
+
width = 768
|
102 |
+
height = 1024
|
103 |
+
elif selected_lora["aspect"] == "landscape":
|
104 |
+
width = 1024
|
105 |
+
height = 768
|
106 |
+
return (
|
107 |
+
gr.update(placeholder=new_placeholder),
|
108 |
+
updated_text,
|
109 |
+
evt.index,
|
110 |
+
width,
|
111 |
+
height,
|
112 |
+
)
|
113 |
+
|
114 |
+
@spaces.GPU(duration=70)
|
115 |
+
def generate_image(prompt, negative_prompt, width, height, steps, seed, lora_scale=1.0, cfg_scale=3.5):
|
116 |
+
pipe.to("cuda")
|
117 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
118 |
+
|
119 |
+
with calculateDuration("Generating image"):
|
120 |
+
# Generate image
|
121 |
+
image = pipe(
|
122 |
+
prompt=f"{prompt}",
|
123 |
+
negative_prompt=negative_prompt,
|
124 |
+
num_inference_steps=steps,
|
125 |
+
guidance_scale=cfg_scale,
|
126 |
+
width=width,
|
127 |
+
height=height,
|
128 |
+
generator=generator,
|
129 |
+
joint_attention_kwargs={"scale": lora_scale},
|
130 |
+
).images[0]
|
131 |
+
return image
|
132 |
+
|
133 |
+
def run_lora(prompt, negative_prompt, lora_scale, cfg_scale, steps, selected_lora, seed, width, height):
|
134 |
+
if negative_prompt == "":
|
135 |
+
negative_prompt = None
|
136 |
+
if selected_index is None:
|
137 |
+
raise gr.Error("Select a LoRA adapter square before proceeding.")
|
138 |
+
|
139 |
+
lora_path = selected_lora["repo"]
|
140 |
+
|
141 |
+
# Load LoRA weights
|
142 |
+
with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
|
143 |
+
if "weights" in selected_lora:
|
144 |
+
pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"], adapter_name=selected_lora["repo"], lora_scale=[1.0], adapter_weights=lora_scale)
|
145 |
+
else:
|
146 |
+
pipe.load_lora_weights(lora_path, adapter_name=selected_lora["repo"], lora_scale=[1.0], adapter_weights=["scale"])
|
147 |
+
# Load LoRA weights
|
148 |
+
# with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
|
149 |
+
# if "weights" in selected_lora:
|
150 |
+
# pipe.load_lora_weights("ostris/OpenFLUX.1", weight_name="openflux1-v0.1.0-fast-lora.safetensors", adapter_name="soon", adapter_weights=[1.0], lora_scale=[1.0])
|
151 |
+
# pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"], adapter_name=selected_lora["repo"], lora_scale=[1.0])
|
152 |
+
# pipe.set_adapters(["fast", selected_lora["repo"]], adapter_weights=[1.0, 1.0])
|
153 |
+
# else:
|
154 |
+
# pipe.load_lora_weights("ostris/OpenFLUX.1", weight_name="openflux1-v0.1.0-fast-lora.safetensors", adapter_name="soon", adapter_weights=[1.0], lora_scale=[1.0])
|
155 |
+
# pipe.load_lora_weights(lora_path, adapter_name=selected_lora["repo"], lora_scale=[1.0])
|
156 |
+
# pipe.set_adapters(["fast", selected_lora["repo"]], adapter_weights=[1.0, 1.0])
|
157 |
+
|
158 |
+
image = generate_image(prompt, width, height, steps, negative_prompt, seed, lora_scale, cfg_scale)
|
159 |
+
pipe.to("cpu")
|
160 |
+
pipe.unload_lora_weights()
|
161 |
+
return image, seed
|
162 |
+
|
163 |
+
run_lora.zerogpu = True
|
164 |
+
#pipe.load_lora_weights("ostris/OpenFLUX.1", weight_name="openflux1-v0.1.0-fast-lora.safetensors", adapter_name="fast")
|
165 |
+
#pipe.set_adapters("fast")
|
166 |
+
#pipe.set_adapters(["fast", "toy"], adapter_weights=[0.5, 1.0])
|
167 |
+
#pipe.fuse_lora(adapter_names=["fast"], lora_scale=1.0)
|
168 |
+
|
169 |
+
css = '''
|
170 |
+
#gen_btn{height: 100%}
|
171 |
+
#title{text-align: center}
|
172 |
+
#title h1{font-size: 3em; display:inline-flex; align-items:center}
|
173 |
+
#title img{width: 100px; margin-right: 0.5em}
|
174 |
+
#gallery .grid-wrap{height: 10vh}
|
175 |
+
'''
|
176 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
|
177 |
+
title = gr.HTML(
|
178 |
+
"""<h1><img src="https://huggingface.co/AlekseyCalvin/HSTklimbimOPENfluxLora/resolve/main/acs62iv.png" alt="LoRA">OpenFlux LoRAsoon®</h1>""",
|
179 |
+
elem_id="title",
|
180 |
+
)
|
181 |
+
# Info blob stating what the app is running
|
182 |
+
info_blob = gr.HTML(
|
183 |
+
"""<div id="info_blob"> SOON®'s curated LoRa Gallery & Art Manufactory Space.|Runs on Ostris' OpenFLUX.1 model + fast-gen LoRA & Zer0int's fine-tuned CLIP-GmP-ViT-L-14*! (*'normal' 77 tokens)| Largely stocked w/our trained LoRAs: Historic Color, Silver Age Poets, Sots Art, more!|</div>"""
|
184 |
+
)
|
185 |
+
|
186 |
+
# Info blob stating what the app is running
|
187 |
+
info_blob = gr.HTML(
|
188 |
+
"""<div id="info_blob"> *Auto-planting of prompts with a choice LoRA trigger errors out in this space over flaws yet unclear. In its stead, we pose numbered LoRA-box rows & a matched token cheat-sheet: ungainly & free. So, prephrase your prompts w/: 1-2. HST style autochrome |3. RCA style Communist poster |4. SOTS art |5. HST Austin Osman Spare style |6. Vladimir Mayakovsky |7-8. Marina Tsvetaeva Tsvetaeva_02.CR2 |9. Anna Akhmatova |10. Osip Mandelshtam |11-12. Alexander Blok |13. Blok_02.CR2 |14. LEN Lenin |15. Leon Trotsky |16. Rosa Fluxemburg |17. HST Peterhof photo |18-19. HST |20. HST portrait |21. HST |22. HST 80s Perestroika-era Soviet photo |23-30. HST |31. How2Draw a__ |32. propaganda poster |33. TOK hybrid photo of__ with cartoon of__ |34. 2004 IMG_1099.CR2 photo |35. unexpected photo of |36. flmft |37. 80s yearbook photo |38. TOK portra |39. pficonics |40. retrofuturism |41. wh3r3sw4ld0 |42. amateur photo |43. crisp |44-45. IMG_1099.CR2 |46. FilmFotos |47. ff-collage |48. HST |49-50. AOS |51. cover </div>"""
|
189 |
+
)
|
190 |
+
selected_index = gr.State(None)
|
191 |
+
with gr.Row():
|
192 |
+
with gr.Column(scale=3):
|
193 |
+
prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Select LoRa/Style & type prompt! What do you want to see?")
|
194 |
+
with gr.Row():
|
195 |
+
with gr.Column(scale=3):
|
196 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", lines=1, placeholder="List unwanted conditions, open-fluxedly!")
|
197 |
+
with gr.Column(scale=1, elem_id="gen_column"):
|
198 |
+
generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
199 |
+
with gr.Row():
|
200 |
+
with gr.Column(scale=3):
|
201 |
+
selected_info = gr.Markdown("")
|
202 |
+
gallery = gr.Gallery(
|
203 |
+
[(item["image"], item["title"]) for item in loras],
|
204 |
+
label="LoRA Inventory",
|
205 |
+
allow_preview=False,
|
206 |
+
columns=3,
|
207 |
+
elem_id="gallery"
|
208 |
+
)
|
209 |
+
|
210 |
+
with gr.Column(scale=4):
|
211 |
+
result = gr.Image(label="Generated Image")
|
212 |
+
|
213 |
+
with gr.Row():
|
214 |
+
with gr.Accordion("Advanced Settings", open=True):
|
215 |
+
with gr.Column():
|
216 |
+
with gr.Row():
|
217 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3)
|
218 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=20)
|
219 |
+
|
220 |
+
with gr.Row():
|
221 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
222 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
223 |
+
|
224 |
+
with gr.Row():
|
225 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
226 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=0.2, maximum=2.0, step=0.01, value=1.0)
|
227 |
+
|
228 |
+
gallery.select(
|
229 |
+
update_selection,
|
230 |
+
inputs=[width, height],
|
231 |
+
outputs=[prompt, selected_info, selected_index, width, height]
|
232 |
+
)
|
233 |
+
|
234 |
+
gr.on(
|
235 |
+
triggers=[generate_button.click, prompt.submit],
|
236 |
+
fn=run_lora,
|
237 |
+
inputs=[prompt, seed, width, height, steps, negative_prompt, lora_scale, cfg_scale, selected_index],
|
238 |
+
outputs=[result, seed]
|
239 |
+
)
|
240 |
+
|
241 |
+
app.queue(default_concurrency_limit=None).launch(show_error=True)
|
242 |
+
app.launch()
|