AlekseyCalvin commited on
Commit
3978f3e
·
verified ·
1 Parent(s): fa0c18e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +284 -0
app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Search models, datasets, users...
4
+
5
+ Spaces:
6
+
7
+ AlekseyCalvin
8
+ /
9
+ LibreFLUX_LoRAs_Gallery
10
+
11
+ like
12
+ 6
13
+
14
+ App
15
+ Files
16
+ Community
17
+ Settings
18
+ LibreFLUX_LoRAs_Gallery
19
+ /
20
+ app.py
21
+
22
+ AlekseyCalvin's picture
23
+ AlekseyCalvin
24
+ Update app.py
25
+ c000701
26
+ verified
27
+ raw
28
+
29
+ Copy download link
30
+ history
31
+ blame
32
+ edit
33
+ delete
34
+
35
+ 10.5 kB
36
+ import os
37
+ import gradio as gr
38
+ import json
39
+ import logging
40
+ import torch
41
+ from PIL import Image
42
+ from os import path
43
+ import spaces
44
+ from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
45
+ from diffusers.models.transformers import FluxTransformer2DModel
46
+ from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
47
+ from transformers import CLIPModel, CLIPProcessor, CLIPTextModel, CLIPTokenizer, CLIPConfig, T5EncoderModel, T5Tokenizer
48
+ import copy
49
+ import random
50
+ import time
51
+ from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
52
+ from huggingface_hub import HfFileSystem, ModelCard
53
+ from huggingface_hub import login, hf_hub_download
54
+ import safetensors.torch
55
+ from safetensors.torch import load_file
56
+ hf_token = os.environ.get("HF_TOKEN")
57
+ login(token=hf_token)
58
+
59
+ cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
60
+ os.environ["TRANSFORMERS_CACHE"] = cache_path
61
+ os.environ["HF_HUB_CACHE"] = cache_path
62
+ os.environ["HF_HOME"] = cache_path
63
+
64
+ torch.set_float32_matmul_precision("medium")
65
+
66
+ #torch._inductor.config.conv_1x1_as_mm = True
67
+ #torch._inductor.config.coordinate_descent_tuning = True
68
+ #torch._inductor.config.epilogue_fusion = False
69
+ #torch._inductor.config.coordinate_descent_check_all_directions = False
70
+
71
+ # Load LoRAs from JSON file
72
+ with open('loras.json', 'r') as f:
73
+ loras = json.load(f)
74
+
75
+ # Initialize the base model
76
+ dtype = torch.bfloat16
77
+ device = "cuda" if torch.cuda.is_available() else "cpu"
78
+
79
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
80
+ good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
81
+
82
+ pipe = DiffusionPipeline.from_pretrained(
83
+ "AlekseyCalvin/SilverAgePoets_FluxS_TestAlpha_Diffusers",
84
+ custom_pipeline="jimmycarter/LibreFLUX",
85
+ use_safetensors=True,
86
+ torch_dtype=torch.bfloat16,
87
+ trust_remote_code=True,
88
+ ).to(device)
89
+
90
+ clipmodel = 'norm'
91
+ if clipmodel == "long":
92
+ model_id = "zer0int/LongCLIP-GmP-ViT-L-14"
93
+ config = CLIPConfig.from_pretrained(model_id)
94
+ maxtokens = 77
95
+ if clipmodel == "norm":
96
+ model_id = "zer0int/CLIP-GmP-ViT-L-14"
97
+ config = CLIPConfig.from_pretrained(model_id)
98
+ maxtokens = 77
99
+ clip_model = CLIPModel.from_pretrained(model_id, torch_dtype=torch.bfloat16, config=config, ignore_mismatched_sizes=True).to("cuda")
100
+ clip_processor = CLIPProcessor.from_pretrained(model_id, padding="max_length", max_length=maxtokens, ignore_mismatched_sizes=True, return_tensors="pt", truncation=True)
101
+
102
+ pipe.tokenizer = clip_processor.tokenizer
103
+ pipe.text_encoder = clip_model.text_model
104
+ pipe.tokenizer_max_length = maxtokens
105
+ pipe.text_encoder.dtype = torch.bfloat16
106
+ pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to("cuda")
107
+
108
+
109
+ #pipe.transformer.to(memory_format=torch.channels_last)
110
+ #pipe.vae.to(memory_format=torch.channels_last)
111
+
112
+ #pipe.transformer = torch.compile(pipe.transformer, mode="max-autotune", fullgraph=False)
113
+ #pipe.vae.decode = torch.compile(pipe.vae.decode, mode="max-autotune", fullgraph=False)
114
+
115
+ MAX_SEED = 2**32-1
116
+
117
+ class calculateDuration:
118
+ def __init__(self, activity_name=""):
119
+ self.activity_name = activity_name
120
+
121
+ def __enter__(self):
122
+ self.start_time = time.time()
123
+ return self
124
+
125
+ def __exit__(self, exc_type, exc_value, traceback):
126
+ self.end_time = time.time()
127
+ self.elapsed_time = self.end_time - self.start_time
128
+ if self.activity_name:
129
+ print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
130
+ else:
131
+ print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
132
+
133
+
134
+ def update_selection(evt: gr.SelectData, width, height):
135
+ selected_lora = loras[evt.index]
136
+ new_placeholder = f"Type a prompt for {selected_lora['title']}"
137
+ lora_repo = selected_lora["repo"]
138
+ updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
139
+ if "aspect" in selected_lora:
140
+ if selected_lora["aspect"] == "portrait":
141
+ width = 768
142
+ height = 1024
143
+ elif selected_lora["aspect"] == "landscape":
144
+ width = 1024
145
+ height = 768
146
+ return (
147
+ gr.update(placeholder=new_placeholder),
148
+ updated_text,
149
+ evt.index,
150
+ width,
151
+ height,
152
+ )
153
+
154
+ @spaces.GPU(duration=70)
155
+ def generate_image(prompt, trigger_word, steps, seed, cfg_scale, width, height, negative_prompt, lora_scale, progress, no_cfg_until_timestep):
156
+ pipe.to("cuda")
157
+ generator = torch.Generator(device="cuda").manual_seed(seed)
158
+
159
+ with calculateDuration("Generating image"):
160
+ # Generate image
161
+ image = pipe(
162
+ prompt=f"{prompt} {trigger_word}",
163
+ num_inference_steps=steps,
164
+ guidance_scale=cfg_scale,
165
+ width=width,
166
+ height=height,
167
+ generator=generator,
168
+ negative_prompt=negative_prompt,
169
+ joint_attention_kwargs={"scale": lora_scale},
170
+ no_cfg_until_timestep=2,
171
+ ).images[0]
172
+ return image
173
+
174
+ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, negative_prompt, lora_scale, no_cfg_until_timestep=2, progress=gr.Progress(track_tqdm=True)):
175
+ if selected_index is None:
176
+ raise gr.Error("You must select a LoRA before proceeding.")
177
+
178
+ selected_lora = loras[selected_index]
179
+ lora_path = selected_lora["repo"]
180
+ trigger_word = selected_lora["trigger_word"]
181
+ if(trigger_word):
182
+ if "trigger_position" in selected_lora:
183
+ if selected_lora["trigger_position"] == "prepend":
184
+ prompt_mash = f"{trigger_word} {prompt}"
185
+ else:
186
+ prompt_mash = f"{prompt} {trigger_word}"
187
+ else:
188
+ prompt_mash = f"{trigger_word} {prompt}"
189
+ else:
190
+ prompt_mash = prompt
191
+
192
+ # Load LoRA weights
193
+ with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
194
+ if "weights" in selected_lora:
195
+ pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
196
+ else:
197
+ pipe.load_lora_weights(lora_path)
198
+
199
+ # Set random seed for reproducibility
200
+ with calculateDuration("Randomizing seed"):
201
+ if randomize_seed:
202
+ seed = random.randint(0, MAX_SEED)
203
+
204
+ image = generate_image(prompt, trigger_word, steps, seed, cfg_scale, width, height, negative_prompt, lora_scale, progress, no_cfg_until_timestep)
205
+ pipe.to("cpu")
206
+ pipe.unload_lora_weights()
207
+ return image, seed
208
+
209
+ run_lora.zerogpu = True
210
+
211
+ css = '''
212
+ #gen_btn{height: 100%}
213
+ #title{text-align: center}
214
+ #title h1{font-size: 3em; display:inline-flex; align-items:center}
215
+ #title img{width: 100px; margin-right: 0.5em}
216
+ #gallery .grid-wrap{height: 10vh}
217
+ '''
218
+ with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
219
+ title = gr.HTML(
220
+ """<h1><img src="https://huggingface.co/AlekseyCalvin/HSTklimbimOPENfluxLora/resolve/main/acs62iv.png" alt="LoRA"> LibreFLUX SOONfactory </h1>""",
221
+ elem_id="title",
222
+ )
223
+ # Info blob stating what the app is running
224
+ info_blob = gr.HTML(
225
+ """<div id="info_blob"> SOON®'s curated LoRa Gallery & Art Manufactory Space | Over LibreFLUX (jimmycarter/LibreFLUX): a slowish, rawer, & freest Flux de-distilled from Schnell + Zer0int's fine-tuned CLIP(*'norm' 77 size for now)| Largely stocked w/our trained LoRAs: Historic Color, Silver Age Poets, Sots Art, more!|</div>"""
226
+ )
227
+
228
+ # Info blob stating what the app is running
229
+ info_blob = gr.HTML(
230
+ """<div id="info_blob"> Pre-phrase Prompts w/: 1-2. HST style |3. RCA poster |4.SOTS art |5.HST Austin Osman Spare |6. Mayakovsky |7-8. Tsvetaeva |9. Akhmatova |10. Mandelshtam |11-13. Blok |14. LEN Lenin |15. Trotsky |16. Rosa Fluxenburg |17-30. HST |31. how2draw |32. propaganda poster |33. TOK photo cartoon hybrid |34. photo |35.unexpected photo |36. flmft |37. Yearbook |38. TOK portra |39. pficonics |40. retrofuturism |41. wh3r3sw4ld0 |42. amateur photo |43. crisp photo |44-45. ADU |46. Film Photo |47. ff-collage |48. HST|49-50. AOS Austin Osman Spare art |51. cover </div>"""
231
+ )
232
+ selected_index = gr.State(None)
233
+ with gr.Row():
234
+ with gr.Column(scale=2):
235
+ prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Select LoRa/Style & type prompt!")
236
+ with gr.Column(scale=2):
237
+ negative_prompt = gr.Textbox(label="Negative Prompt", lines=1, placeholder="What to exclude!")
238
+ with gr.Column(scale=1, elem_id="gen_column"):
239
+ generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
240
+ with gr.Row():
241
+ with gr.Column(scale=3):
242
+ selected_info = gr.Markdown("")
243
+ gallery = gr.Gallery(
244
+ [(item["image"], item["title"]) for item in loras],
245
+ label="LoRA Inventory",
246
+ allow_preview=False,
247
+ columns=3,
248
+ elem_id="gallery"
249
+ )
250
+
251
+ with gr.Column(scale=4):
252
+ result = gr.Image(label="Generated Image")
253
+
254
+ with gr.Row():
255
+ with gr.Accordion("Advanced Settings", open=True):
256
+ with gr.Column():
257
+ with gr.Row():
258
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=0, maximum=20, step=0.5, value=2.5)
259
+ steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=20)
260
+
261
+ with gr.Row():
262
+ width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=768)
263
+ height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=768)
264
+
265
+ with gr.Row():
266
+ randomize_seed = gr.Checkbox(True, label="Randomize seed")
267
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
268
+ lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=2.0, step=0.01, value=0.9)
269
+
270
+ gallery.select(
271
+ update_selection,
272
+ inputs=[width, height],
273
+ outputs=[prompt, selected_info, selected_index, width, height]
274
+ )
275
+
276
+ gr.on(
277
+ triggers=[generate_button.click, prompt.submit],
278
+ fn=run_lora,
279
+ inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, negative_prompt, lora_scale],
280
+ outputs=[result, seed]
281
+ )
282
+
283
+ app.queue(default_concurrency_limit=2).launch(show_error=True)
284
+ app.launch()