AlekseyCalvin commited on
Commit
fa5a76b
·
verified ·
1 Parent(s): dc052ea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +221 -0
app.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import json
4
+ import logging
5
+ import torch
6
+ from PIL import Image
7
+ from os import path
8
+ from torchvision import transforms
9
+ from dataclasses import dataclass
10
+ import math
11
+ from typing import Callable
12
+ import spaces
13
+ from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
14
+ from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
15
+ from transformers import CLIPModel, CLIPProcessor, CLIPTextModel, CLIPTokenizer, CLIPConfig, T5EncoderModel, T5Tokenizer
16
+ from diffusers.models.transformers import FluxTransformer2DModel
17
+ import copy
18
+ import random
19
+ import time
20
+ import safetensors.torch
21
+ from tqdm import tqdm
22
+ from safetensors.torch import load_file
23
+ from huggingface_hub import HfFileSystem, ModelCard
24
+ from huggingface_hub import login, hf_hub_download
25
+ hf_token = os.environ.get("HF_TOKEN")
26
+ login(token=hf_token)
27
+ from sdnq import SDNQConfig # import sdnq to register it into diffusers and transformers
28
+
29
+ cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
30
+ os.environ["TRANSFORMERS_CACHE"] = cache_path
31
+ os.environ["HF_HUB_CACHE"] = cache_path
32
+ os.environ["HF_HOME"] = cache_path
33
+
34
+ #torch.set_float32_matmul_precision("medium")
35
+
36
+ # Load LoRAs from JSON file
37
+ with open('loras.json', 'r') as f:
38
+ loras = json.load(f)
39
+
40
+ # Initialize the base model
41
+ #dtype = torch.bfloat16
42
+ #base_model = "AlekseyCalvin/Artsy_Lite_Flux_v1_by_jurdn_Diffusers"
43
+ #pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype).to("cuda")
44
+ ##pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=torch.float16).to("cuda")
45
+
46
+ pipe = diffusers.ZImagePipeline.from_pretrained("Disty0/Z-Image-Turbo-SDNQ-uint4-svd-r32", torch_dtype=torch.bfloat16)
47
+ torch.cuda.empty_cache()
48
+
49
+ device = "cuda" if torch.cuda.is_available() else "cpu"
50
+
51
+ pipe.enable_model_cpu_offload()
52
+
53
+
54
+ #model_id = ("zer0int/LongCLIP-GmP-ViT-L-14")
55
+ #config = CLIPConfig.from_pretrained(model_id)
56
+ #config.text_config.max_position_embeddings = 248
57
+ #clip_model = CLIPModel.from_pretrained(model_id, torch_dtype=torch.bfloat16, config=config, ignore_mismatched_sizes=True)
58
+ #clip_processor = CLIPProcessor.from_pretrained(model_id, padding="max_length", max_length=248)
59
+ #pipe.tokenizer = clip_processor.tokenizer
60
+ #pipe.text_encoder = clip_model.text_model
61
+ #pipe.tokenizer_max_length = 248
62
+ #pipe.text_encoder.dtype = torch.bfloat16
63
+ #pipe.text_encoder_2 = t5.text_model
64
+
65
+ MAX_SEED = 2**32-1
66
+
67
+ class calculateDuration:
68
+ def __init__(self, activity_name=""):
69
+ self.activity_name = activity_name
70
+
71
+ def __enter__(self):
72
+ self.start_time = time.time()
73
+ return self
74
+
75
+ def __exit__(self, exc_type, exc_value, traceback):
76
+ self.end_time = time.time()
77
+ self.elapsed_time = self.end_time - self.start_time
78
+ if self.activity_name:
79
+ print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
80
+ else:
81
+ print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
82
+
83
+
84
+ def update_selection(evt: gr.SelectData, width, height):
85
+ selected_lora = loras[evt.index]
86
+ new_placeholder = f"Prompt with activator word(s): '{selected_lora['trigger_word']}'! "
87
+ lora_repo = selected_lora["repo"]
88
+ lora_trigger = selected_lora['trigger_word']
89
+ updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}). Prompt using: '{lora_trigger}'!"
90
+ if "aspect" in selected_lora:
91
+ if selected_lora["aspect"] == "portrait":
92
+ width = 768
93
+ height = 1024
94
+ elif selected_lora["aspect"] == "landscape":
95
+ width = 1024
96
+ height = 768
97
+ return (
98
+ gr.update(placeholder=new_placeholder),
99
+ updated_text,
100
+ evt.index,
101
+ width,
102
+ height,
103
+ )
104
+
105
+ @spaces.GPU(duration=50)
106
+ def generate_image(prompt, trigger_word, steps, seed, cfg_scale, width, height, lora_scale, progress):
107
+ pipe.to("cuda")
108
+ generator = torch.Generator(device="cuda").manual_seed(seed)
109
+
110
+ with calculateDuration("Generating image"):
111
+ # Generate image
112
+ image = pipe(
113
+ prompt=f"{prompt} {trigger_word}",
114
+ num_inference_steps=steps,
115
+ guidance_scale=cfg_scale,
116
+ width=width,
117
+ height=height,
118
+ generator=generator,
119
+ joint_attention_kwargs={"scale": lora_scale},
120
+ ).images[0]
121
+ return image
122
+
123
+ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
124
+ if selected_index is None:
125
+ raise gr.Error("You must select a LoRA before proceeding.")
126
+
127
+ selected_lora = loras[selected_index]
128
+ lora_path = selected_lora["repo"]
129
+ trigger_word = selected_lora['trigger_word']
130
+
131
+ # Load LoRA weights
132
+ with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
133
+ if "weights" in selected_lora:
134
+ pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
135
+ else:
136
+ pipe.load_lora_weights(lora_path)
137
+
138
+ # Set random seed for reproducibility
139
+ with calculateDuration("Randomizing seed"):
140
+ if randomize_seed:
141
+ seed = random.randint(0, MAX_SEED)
142
+
143
+ image = generate_image(prompt, trigger_word, steps, seed, cfg_scale, width, height, lora_scale, progress)
144
+ pipe.to("cpu")
145
+ pipe.unload_lora_weights()
146
+ return image, seed
147
+
148
+ run_lora.zerogpu = True
149
+
150
+ css = '''
151
+ #gen_btn{height: 100%}
152
+ #title{text-align: center}
153
+ #title h1{font-size: 3em; display:inline-flex; align-items:center}
154
+ #title img{width: 100px; margin-right: 0.5em}
155
+ #gallery .grid-wrap{height: 10vh}
156
+ '''
157
+ with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
158
+ title = gr.HTML(
159
+ """<h1><img src="https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer/resolve/main/flux_lora.png" alt="LoRA"> SOONfactory </h1>""",
160
+ elem_id="title",
161
+ )
162
+ # Info blob stating what the app is running
163
+ info_blob = gr.HTML(
164
+ """<div id="info_blob"> Hosted Gallery of Custom-Trained Text2Image Generative Low-Rank Adaptors (LoRAs) for FLUX models. Running On: ArtsyLite FLUX base variant. First 4 gallery rows are adapters fine-tuned for the use of RCA (Revolutionary Communists of America at [https://CommunistUSA.org/]), & other activists/artists. Below them are adapters trained on works of Soviet Avant-Garde, Dada, Surrealism, & other radical styles + some original conceptions/fusions. Under those are identity models of notable revolutionaries & poets. Click squares to switch adapters & see links to their pages, many of them offering more info/resources. </div>"""
165
+ )
166
+
167
+ # Info blob stating what the app is running
168
+ info_blob = gr.HTML(
169
+ """<div id="info_blob"> To reinforce/focus a selected adapter style, add its pre-encoded “trigger" word/phrase to your prompt. Corresponding activator info &/or prompt template appears once an adapter square is clicked. Copy/Paste these into prompt box as a starting point. </div>"""
170
+ )
171
+ selected_index = gr.State(None)
172
+ with gr.Row():
173
+ with gr.Column(scale=3):
174
+ prompt = gr.Textbox(label="Prompt", lines=2, placeholder="Select LoRa/Style & type prompt!")
175
+ with gr.Column(scale=1, elem_id="gen_column"):
176
+ generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
177
+ with gr.Row():
178
+ with gr.Column(scale=3):
179
+ selected_info = gr.Markdown("")
180
+ gallery = gr.Gallery(
181
+ [(item["image"], item["title"]) for item in loras],
182
+ label="LoRA Inventory",
183
+ allow_preview=False,
184
+ columns=3,
185
+ elem_id="gallery"
186
+ )
187
+
188
+ with gr.Column(scale=4):
189
+ result = gr.Image(label="Generated Image")
190
+
191
+ with gr.Row():
192
+ with gr.Accordion("Advanced Settings", open=True):
193
+ with gr.Column():
194
+ with gr.Row():
195
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=0, maximum=20, step=.1, value=1.0)
196
+ steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=9)
197
+
198
+ with gr.Row():
199
+ width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
200
+ height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
201
+
202
+ with gr.Row():
203
+ randomize_seed = gr.Checkbox(True, label="Randomize seed")
204
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
205
+ lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=2.5, step=0.01, value=1.0)
206
+
207
+ gallery.select(
208
+ update_selection,
209
+ inputs=[width, height],
210
+ outputs=[prompt, selected_info, selected_index, width, height]
211
+ )
212
+
213
+ gr.on(
214
+ triggers=[generate_button.click, prompt.submit],
215
+ fn=run_lora,
216
+ inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
217
+ outputs=[result, seed]
218
+ )
219
+
220
+ app.queue(default_concurrency_limit=2).launch(show_error=True)
221
+ app.launch()