Zak Kessler commited on
Commit
eb85ef9
·
1 Parent(s): 2937b1f

Lora loader

Browse files
Files changed (1) hide show
  1. app.py +73 -32
app.py CHANGED
@@ -11,68 +11,72 @@ import spaces
11
  from comfy import model_management
12
 
13
  CHROMA_VERSION = "chroma-unlocked-v48-detail-calibrated.safetensors"
 
14
 
15
  # Download required models
16
  t5_path = hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="t5xxl_fp8_e4m3fn.safetensors", local_dir="models/text_encoders/")
17
  vae_path = hf_hub_download(repo_id="lodestones/Chroma", filename="ae.safetensors", local_dir="models/vae")
18
  unet_path = hf_hub_download(repo_id="lodestones/Chroma", filename=CHROMA_VERSION, local_dir="models/unet")
19
 
20
- # Example prompts with their parameters
 
 
 
21
  EXAMPLES = [
22
  [
23
  "A high-fashion close-up portrait of a blonde woman in clear sunglasses. The image uses a bold teal and red color split for dramatic lighting. The background is a simple teal-green. The photo is sharp and well-composed, and is designed for viewing with anaglyph 3D glasses for optimal effect. It looks professionally done.",
24
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
25
- 1024, 1024, 36, 3.0, 229
26
  ],
27
  [
28
  "A young man smiles broadly while wearing a blue captain's hat and blue mirrored aviator sunglasses. The man is the sharp focus of the image, set against a nicely blurred waterfront background featuring boats and buildings. The shot is cheerful, playful, and amateur looking.",
29
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
30
- 1024, 1024, 40, 2.1, 424
31
  ],
32
  [
33
  "A Shiba Inu smiles broadly while wearing a blue captain's hat and blue mirrored aviator sunglasses. The dog is the sharp focus of the image, set against a nicely blurred waterfront background featuring boats and buildings. The shot is cheerful, playful, and amateur looking.",
34
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
35
- 1024, 1024, 28, 2.9, 413
36
  ],
37
  [
38
  "A painting shows a bearded man in a beige shirt gripping a rope on a sailboat, battling choppy seas under a dramatic sunset. Another figure appears in the distant background, and the style is impressionistic with clear brushstrokes and high skill. The overall effect is one of adventure and freedom.",
39
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
40
- 1024, 1024, 33, 4.6, 281
41
  ],
42
  [
43
  "A painting shows an anthro male wolf sitting on a rock in a forest, anthro, male, solo, canine, canid, werewolf, muscular anthro, black body, black fur, reaching at viewer with his paws, detailed background, outdoors, outside. The scene is detailed, with a shallow depth of field focusing on the anthro wolf. The overall effect is high fantasy and cinematic.",
44
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
45
- 1024, 1024, 26, 3.8, 326
46
  ],
47
  [
48
  "A cute cartoon illustration of A massive black dragon with cosmic galaxy-patterned wings sits atop a castle overlooking a medieval port town at sunset. The style is bright and pastel-toned with soft lines. The artwork is clean and fairly well-executed.",
49
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
50
- 1024, 1024, 41, 5.7, 244
51
  ],
52
  [
53
  "A soft longhaired male anthro dog, with a white chest patch and bright yellow-green eyes, sits on a light grey kitchen island in a sun-drenched NYC apartment. The city view is blurred, showcasing a shallow depth of field. The cat has a dark collar with a small bell. The photo's aesthetic is clean, modern, and slightly dramatic due to the lighting.",
54
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
55
- 1024, 1024, 26, 4.7, 242
56
  ],
57
  [
58
  "A cheerful collage showcases 4 individual leopard geckos, each in a separate square. The geckos vary in color and pattern, but all are shown close-up and looking directly at the camera. Backgrounds differ, illustrating various terrarium elements and simple surfaces. High-quality shots, bright lighting, amateur photo quality. The overall aesthetic is heartwarming and simple.",
59
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
60
- 1024, 1024, 26, 4.7, 249
61
  ],
62
  [
63
  "A cheerful collage showcases 4 cute anthro wolf nick wilde plushie, each in a separate square. Backgrounds differ, illustrating various bed and simple surfaces. High-quality shots, bright lighting, amateur photo quality. The overall aesthetic is heartwarming and simple.",
64
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
65
- 1024, 1024, 26, 5.4, 263
66
  ],
67
  [
68
  "this picture depics a telegram sticker with thick white outlines of a male anthro fox",
69
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
70
- 1024, 1024, 28, 3.9, 399
71
  ],
72
  [
73
  "Extreme close-up of a single tiger eye, direct frontal view. Detailed iris and pupil. Sharp focus on eye texture and color. Natural lighting to capture authentic eye shine and depth. The word \"Chroma\" is painted over it in big, white brush strokes with visible texture.",
74
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
75
- 1024, 1024, 26, 4.0, 9
76
  ]
77
  ]
78
 
@@ -154,7 +158,10 @@ samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
154
  vaedecode = VAEDecode()
155
  saveimage = SaveImage()
156
 
157
- # Load models
 
 
 
158
  cliploader_78 = cliploader.load_clip(
159
  clip_name="t5xxl_fp8_e4m3fn.safetensors", type="chroma", device="default"
160
  )
@@ -180,32 +187,56 @@ valid_models = [
180
  model_management.load_models_gpu(valid_models)
181
 
182
  @spaces.GPU
183
- def generate_image(prompt, negative_prompt, width, height, steps, cfg, seed):
184
  with torch.inference_mode():
185
  # Set random seed if provided
186
  if seed == -1:
187
  seed = random.randint(1, 2**64)
188
  random.seed(seed)
189
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  randomnoise_68 = randomnoise.get_noise(noise_seed=seed)
191
  emptysd3latentimage_69 = emptysd3latentimage.generate(
192
  width=width, height=height, batch_size=1
193
  )
194
  ksamplerselect_72 = ksamplerselect.get_sampler(sampler_name="euler")
195
 
 
196
  cliptextencode_74 = cliptextencode.encode(
197
  text=prompt,
198
- clip=get_value_at_index(t5tokenizeroptions_82, 0),
199
  )
200
 
201
  cliptextencode_75 = cliptextencode.encode(
202
  text=negative_prompt,
203
- clip=get_value_at_index(t5tokenizeroptions_82, 0),
204
  )
205
 
 
206
  cfgguider_73 = cfgguider.get_guider(
207
  cfg=cfg,
208
- model=get_value_at_index(unetloader_76, 0),
209
  positive=get_value_at_index(cliptextencode_74, 0),
210
  negative=get_value_at_index(cliptextencode_75, 0),
211
  )
@@ -214,7 +245,7 @@ def generate_image(prompt, negative_prompt, width, height, steps, cfg, seed):
214
  scheduler="beta",
215
  steps=steps,
216
  denoise=1,
217
- model=get_value_at_index(unetloader_76, 0),
218
  )
219
 
220
  samplercustomadvanced_67 = samplercustomadvanced.sample(
@@ -232,10 +263,10 @@ def generate_image(prompt, negative_prompt, width, height, steps, cfg, seed):
232
 
233
  # Save image using SaveImage node with simple string prefix
234
  saved = saveimage.save_images(
235
- filename_prefix="Chroma_Generated",
236
  images=get_value_at_index(vaedecode_79, 0),
237
  )
238
-
239
  # Return the path to the saved image
240
  saved_path = f"output/{saved['ui']['images'][0]['filename']}"
241
  return saved_path
@@ -243,17 +274,18 @@ def generate_image(prompt, negative_prompt, width, height, steps, cfg, seed):
243
  # Create Gradio interface
244
  with gr.Blocks() as app:
245
  gr.Markdown(f"""
246
- # Chroma
247
 
248
  Model: [Chroma](https://huggingface.co/lodestones/Chroma) by [lodestones](https://huggingface.co/lodestones)
249
 
250
  Chroma Version: {CHROMA_VERSION}
 
251
 
252
  Run any ComfyUI Workflow on Spaces: [ComfyUI Workflows](https://huggingface.co/blog/run-comfyui-workflows-on-spaces)
253
 
254
  Space Author: [GitHub](https://github.com/gokayfem) | [X.com](https://x.com/gokayfem)
255
  """)
256
-
257
  with gr.Row():
258
  with gr.Column():
259
  prompt = gr.Textbox(
@@ -267,7 +299,7 @@ Space Author: [GitHub](https://github.com/gokayfem) | [X.com](https://x.com/goka
267
  value="low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
268
  lines=2
269
  )
270
-
271
  with gr.Row():
272
  width = gr.Slider(
273
  minimum=512,
@@ -283,7 +315,7 @@ Space Author: [GitHub](https://github.com/gokayfem) | [X.com](https://x.com/goka
283
  step=64,
284
  label="Height"
285
  )
286
-
287
  with gr.Row():
288
  steps = gr.Slider(
289
  minimum=1,
@@ -299,30 +331,39 @@ Space Author: [GitHub](https://github.com/gokayfem) | [X.com](https://x.com/goka
299
  step=0.5,
300
  label="CFG Scale"
301
  )
 
 
302
  seed = gr.Number(
303
  value=-1,
304
  label="Seed (-1 for random)"
305
  )
306
-
 
 
 
 
 
 
 
307
  generate_btn = gr.Button("Generate")
308
-
309
  with gr.Column():
310
  output_image = gr.Image(label="Generated Image")
311
-
312
  generate_btn.click(
313
  fn=generate_image,
314
- inputs=[prompt, negative_prompt, width, height, steps, cfg, seed],
315
  outputs=[output_image]
316
  )
317
-
318
  # Add examples section
319
  gr.Examples(
320
  examples=EXAMPLES,
321
- inputs=[prompt, negative_prompt, width, height, steps, cfg, seed],
322
  outputs=[output_image],
323
  fn=generate_image,
324
  cache_examples=True,
325
- label="Example Prompts - Click to try!"
326
  )
327
 
328
  if __name__ == "__main__":
 
11
  from comfy import model_management
12
 
13
  CHROMA_VERSION = "chroma-unlocked-v48-detail-calibrated.safetensors"
14
+ LORA_NAME = "adapter_model.safetensors"
15
 
16
  # Download required models
17
  t5_path = hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="t5xxl_fp8_e4m3fn.safetensors", local_dir="models/text_encoders/")
18
  vae_path = hf_hub_download(repo_id="lodestones/Chroma", filename="ae.safetensors", local_dir="models/vae")
19
  unet_path = hf_hub_download(repo_id="lodestones/Chroma", filename=CHROMA_VERSION, local_dir="models/unet")
20
 
21
+ # Download LoRA file - you'll need to replace this with the correct repo_id and ensure the file exists
22
+ lora_path = hf_hub_download(repo_id="zaksynack/chroma-kwis-v1", filename=LORA_NAME, local_dir="models/loras")
23
+
24
+ # Example prompts with their parameters (updated to include LoRA strength)
25
  EXAMPLES = [
26
  [
27
  "A high-fashion close-up portrait of a blonde woman in clear sunglasses. The image uses a bold teal and red color split for dramatic lighting. The background is a simple teal-green. The photo is sharp and well-composed, and is designed for viewing with anaglyph 3D glasses for optimal effect. It looks professionally done.",
28
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
29
+ 1024, 1024, 36, 3.0, 229, 0.8
30
  ],
31
  [
32
  "A young man smiles broadly while wearing a blue captain's hat and blue mirrored aviator sunglasses. The man is the sharp focus of the image, set against a nicely blurred waterfront background featuring boats and buildings. The shot is cheerful, playful, and amateur looking.",
33
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
34
+ 1024, 1024, 40, 2.1, 424, 0.7
35
  ],
36
  [
37
  "A Shiba Inu smiles broadly while wearing a blue captain's hat and blue mirrored aviator sunglasses. The dog is the sharp focus of the image, set against a nicely blurred waterfront background featuring boats and buildings. The shot is cheerful, playful, and amateur looking.",
38
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
39
+ 1024, 1024, 28, 2.9, 413, 0.9
40
  ],
41
  [
42
  "A painting shows a bearded man in a beige shirt gripping a rope on a sailboat, battling choppy seas under a dramatic sunset. Another figure appears in the distant background, and the style is impressionistic with clear brushstrokes and high skill. The overall effect is one of adventure and freedom.",
43
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
44
+ 1024, 1024, 33, 4.6, 281, 0.6
45
  ],
46
  [
47
  "A painting shows an anthro male wolf sitting on a rock in a forest, anthro, male, solo, canine, canid, werewolf, muscular anthro, black body, black fur, reaching at viewer with his paws, detailed background, outdoors, outside. The scene is detailed, with a shallow depth of field focusing on the anthro wolf. The overall effect is high fantasy and cinematic.",
48
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
49
+ 1024, 1024, 26, 3.8, 326, 1.0
50
  ],
51
  [
52
  "A cute cartoon illustration of A massive black dragon with cosmic galaxy-patterned wings sits atop a castle overlooking a medieval port town at sunset. The style is bright and pastel-toned with soft lines. The artwork is clean and fairly well-executed.",
53
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
54
+ 1024, 1024, 41, 5.7, 244, 0.5
55
  ],
56
  [
57
  "A soft longhaired male anthro dog, with a white chest patch and bright yellow-green eyes, sits on a light grey kitchen island in a sun-drenched NYC apartment. The city view is blurred, showcasing a shallow depth of field. The cat has a dark collar with a small bell. The photo's aesthetic is clean, modern, and slightly dramatic due to the lighting.",
58
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
59
+ 1024, 1024, 26, 4.7, 242, 0.8
60
  ],
61
  [
62
  "A cheerful collage showcases 4 individual leopard geckos, each in a separate square. The geckos vary in color and pattern, but all are shown close-up and looking directly at the camera. Backgrounds differ, illustrating various terrarium elements and simple surfaces. High-quality shots, bright lighting, amateur photo quality. The overall aesthetic is heartwarming and simple.",
63
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
64
+ 1024, 1024, 26, 4.7, 249, 0.7
65
  ],
66
  [
67
  "A cheerful collage showcases 4 cute anthro wolf nick wilde plushie, each in a separate square. Backgrounds differ, illustrating various bed and simple surfaces. High-quality shots, bright lighting, amateur photo quality. The overall aesthetic is heartwarming and simple.",
68
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
69
+ 1024, 1024, 26, 5.4, 263, 0.9
70
  ],
71
  [
72
  "this picture depics a telegram sticker with thick white outlines of a male anthro fox",
73
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
74
+ 1024, 1024, 28, 3.9, 399, 0.8
75
  ],
76
  [
77
  "Extreme close-up of a single tiger eye, direct frontal view. Detailed iris and pupil. Sharp focus on eye texture and color. Natural lighting to capture authentic eye shine and depth. The word \"Chroma\" is painted over it in big, white brush strokes with visible texture.",
78
  "low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
79
+ 1024, 1024, 26, 4.0, 9, 0.6
80
  ]
81
  ]
82
 
 
158
  vaedecode = VAEDecode()
159
  saveimage = SaveImage()
160
 
161
+ # Initialize LoRA loader
162
+ loraloader = NODE_CLASS_MAPPINGS["LoraLoader"]()
163
+
164
+ # Load base models
165
  cliploader_78 = cliploader.load_clip(
166
  clip_name="t5xxl_fp8_e4m3fn.safetensors", type="chroma", device="default"
167
  )
 
187
  model_management.load_models_gpu(valid_models)
188
 
189
  @spaces.GPU
190
+ def generate_image(prompt, negative_prompt, width, height, steps, cfg, seed, lora_strength):
191
  with torch.inference_mode():
192
  # Set random seed if provided
193
  if seed == -1:
194
  seed = random.randint(1, 2**64)
195
  random.seed(seed)
196
+
197
+ # Load LoRA if strength > 0
198
+ if lora_strength > 0:
199
+ try:
200
+ loraloader_result = loraloader.load_lora(
201
+ lora_name=LORA_NAME,
202
+ strength_model=lora_strength,
203
+ strength_clip=lora_strength,
204
+ model=get_value_at_index(unetloader_76, 0),
205
+ clip=get_value_at_index(t5tokenizeroptions_82, 0)
206
+ )
207
+ model_with_lora = get_value_at_index(loraloader_result, 0)
208
+ clip_with_lora = get_value_at_index(loraloader_result, 1)
209
+ print(f"LoRA {LORA_NAME} loaded with strength {lora_strength}")
210
+ except Exception as e:
211
+ print(f"Failed to load LoRA: {e}")
212
+ # Fall back to original model if LoRA loading fails
213
+ model_with_lora = get_value_at_index(unetloader_76, 0)
214
+ clip_with_lora = get_value_at_index(t5tokenizeroptions_82, 0)
215
+ else:
216
+ model_with_lora = get_value_at_index(unetloader_76, 0)
217
+ clip_with_lora = get_value_at_index(t5tokenizeroptions_82, 0)
218
+
219
  randomnoise_68 = randomnoise.get_noise(noise_seed=seed)
220
  emptysd3latentimage_69 = emptysd3latentimage.generate(
221
  width=width, height=height, batch_size=1
222
  )
223
  ksamplerselect_72 = ksamplerselect.get_sampler(sampler_name="euler")
224
 
225
+ # Use the clip with LoRA for text encoding
226
  cliptextencode_74 = cliptextencode.encode(
227
  text=prompt,
228
+ clip=clip_with_lora,
229
  )
230
 
231
  cliptextencode_75 = cliptextencode.encode(
232
  text=negative_prompt,
233
+ clip=clip_with_lora,
234
  )
235
 
236
+ # Use the model with LoRA for CFG guider
237
  cfgguider_73 = cfgguider.get_guider(
238
  cfg=cfg,
239
+ model=model_with_lora,
240
  positive=get_value_at_index(cliptextencode_74, 0),
241
  negative=get_value_at_index(cliptextencode_75, 0),
242
  )
 
245
  scheduler="beta",
246
  steps=steps,
247
  denoise=1,
248
+ model=model_with_lora,
249
  )
250
 
251
  samplercustomadvanced_67 = samplercustomadvanced.sample(
 
263
 
264
  # Save image using SaveImage node with simple string prefix
265
  saved = saveimage.save_images(
266
+ filename_prefix="Chroma_LoRA_Generated",
267
  images=get_value_at_index(vaedecode_79, 0),
268
  )
269
+
270
  # Return the path to the saved image
271
  saved_path = f"output/{saved['ui']['images'][0]['filename']}"
272
  return saved_path
 
274
  # Create Gradio interface
275
  with gr.Blocks() as app:
276
  gr.Markdown(f"""
277
+ # Chroma with LoRA Support
278
 
279
  Model: [Chroma](https://huggingface.co/lodestones/Chroma) by [lodestones](https://huggingface.co/lodestones)
280
 
281
  Chroma Version: {CHROMA_VERSION}
282
+ LoRA: {LORA_NAME}
283
 
284
  Run any ComfyUI Workflow on Spaces: [ComfyUI Workflows](https://huggingface.co/blog/run-comfyui-workflows-on-spaces)
285
 
286
  Space Author: [GitHub](https://github.com/gokayfem) | [X.com](https://x.com/gokayfem)
287
  """)
288
+
289
  with gr.Row():
290
  with gr.Column():
291
  prompt = gr.Textbox(
 
299
  value="low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
300
  lines=2
301
  )
302
+
303
  with gr.Row():
304
  width = gr.Slider(
305
  minimum=512,
 
315
  step=64,
316
  label="Height"
317
  )
318
+
319
  with gr.Row():
320
  steps = gr.Slider(
321
  minimum=1,
 
331
  step=0.5,
332
  label="CFG Scale"
333
  )
334
+
335
+ with gr.Row():
336
  seed = gr.Number(
337
  value=-1,
338
  label="Seed (-1 for random)"
339
  )
340
+ lora_strength = gr.Slider(
341
+ minimum=0,
342
+ maximum=2.0,
343
+ value=0.8,
344
+ step=0.1,
345
+ label="LoRA Strength (0 = disabled)"
346
+ )
347
+
348
  generate_btn = gr.Button("Generate")
349
+
350
  with gr.Column():
351
  output_image = gr.Image(label="Generated Image")
352
+
353
  generate_btn.click(
354
  fn=generate_image,
355
+ inputs=[prompt, negative_prompt, width, height, steps, cfg, seed, lora_strength],
356
  outputs=[output_image]
357
  )
358
+
359
  # Add examples section
360
  gr.Examples(
361
  examples=EXAMPLES,
362
+ inputs=[prompt, negative_prompt, width, height, steps, cfg, seed, lora_strength],
363
  outputs=[output_image],
364
  fn=generate_image,
365
  cache_examples=True,
366
+ label="Example Prompts with LoRA - Click to try!"
367
  )
368
 
369
  if __name__ == "__main__":