Nef Caballero commited on
Commit
be23175
·
1 Parent(s): dfac101

starting over simple example

Browse files
Files changed (2) hide show
  1. app.py +56 -241
  2. requirements.txt +4 -3
app.py CHANGED
@@ -6,15 +6,9 @@ import torch
6
  import gradio as gr
7
  from huggingface_hub import hf_hub_download
8
  import spaces
9
- from comfy import model_management
10
 
11
- hf_hub_download(repo_id="black-forest-labs/FLUX.1-Redux-dev", filename="flux1-redux-dev.safetensors", local_dir="models/style_models")
12
- hf_hub_download(repo_id="black-forest-labs/FLUX.1-Depth-dev", filename="flux1-depth-dev.safetensors", local_dir="models/diffusion_models")
13
- hf_hub_download(repo_id="Comfy-Org/sigclip_vision_384", filename="sigclip_vision_patch14_384.safetensors", local_dir="models/clip_vision")
14
- hf_hub_download(repo_id="Kijai/DepthAnythingV2-safetensors", filename="depth_anything_v2_vitl_fp32.safetensors", local_dir="models/depthanything")
15
- hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev", filename="ae.safetensors", local_dir="models/vae/FLUX1")
16
- hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="clip_l.safetensors", local_dir="models/text_encoders")
17
- hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="t5xxl_fp16.safetensors", local_dir="models/text_encoders/t5")
18
 
19
  def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
20
  """Returns the value at the given index of a sequence or mapping.
@@ -125,266 +119,86 @@ def import_custom_nodes() -> None:
125
 
126
  from nodes import NODE_CLASS_MAPPINGS
127
 
128
- intconstant = NODE_CLASS_MAPPINGS["INTConstant"]()
129
- dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
130
-
131
- #To be added to `model_loaders` as it loads a model
132
- dualcliploader_357 = dualcliploader.load_clip(
133
- clip_name1="t5/t5xxl_fp16.safetensors",
134
- clip_name2="clip_l.safetensors",
135
- type="flux",
136
- )
137
- cr_clip_input_switch = NODE_CLASS_MAPPINGS["CR Clip Input Switch"]()
138
- cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
139
- loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
140
- imageresize = NODE_CLASS_MAPPINGS["ImageResize+"]()
141
- getimagesizeandcount = NODE_CLASS_MAPPINGS["GetImageSizeAndCount"]()
142
- vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
143
-
144
- #To be added to `model_loaders` as it loads a model
145
- vaeloader_359 = vaeloader.load_vae(vae_name="FLUX1/ae.safetensors")
146
-
147
- vaeencode = NODE_CLASS_MAPPINGS["VAEEncode"]()
148
- unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
149
-
150
- #To be added to `model_loaders` as it loads a model
151
- unetloader_358 = unetloader.load_unet(
152
- unet_name="flux1-depth-dev.safetensors", weight_dtype="default"
153
- )
154
- ksamplerselect = NODE_CLASS_MAPPINGS["KSamplerSelect"]()
155
- randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
156
- fluxguidance = NODE_CLASS_MAPPINGS["FluxGuidance"]()
157
- depthanything_v2 = NODE_CLASS_MAPPINGS["DepthAnything_V2"]()
158
- downloadandloaddepthanythingv2model = NODE_CLASS_MAPPINGS[
159
- "DownloadAndLoadDepthAnythingV2Model"
160
- ]()
161
-
162
- #To be added to `model_loaders` as it loads a model
163
- downloadandloaddepthanythingv2model_437 = (
164
- downloadandloaddepthanythingv2model.loadmodel(
165
- model="depth_anything_v2_vitl_fp32.safetensors"
166
- )
167
- )
168
- instructpixtopixconditioning = NODE_CLASS_MAPPINGS[
169
- "InstructPixToPixConditioning"
170
- ]()
171
- text_multiline_454 = text_multiline.text_multiline(text="FLUX_Redux")
172
- clipvisionloader = NODE_CLASS_MAPPINGS["CLIPVisionLoader"]()
173
-
174
- #To be added to `model_loaders` as it loads a model
175
- clipvisionloader_438 = clipvisionloader.load_clip(
176
- clip_name="sigclip_vision_patch14_384.safetensors"
177
- )
178
- clipvisionencode = NODE_CLASS_MAPPINGS["CLIPVisionEncode"]()
179
- stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
180
-
181
- #To be added to `model_loaders` as it loads a model
182
- stylemodelloader_441 = stylemodelloader.load_style_model(
183
- style_model_name="flux1-redux-dev.safetensors"
184
- )
185
- text_multiline = NODE_CLASS_MAPPINGS["Text Multiline"]()
186
- emptylatentimage = NODE_CLASS_MAPPINGS["EmptyLatentImage"]()
187
- cr_conditioning_input_switch = NODE_CLASS_MAPPINGS[
188
- "CR Conditioning Input Switch"
189
- ]()
190
- cr_model_input_switch = NODE_CLASS_MAPPINGS["CR Model Input Switch"]()
191
- stylemodelapplyadvanced = NODE_CLASS_MAPPINGS["StyleModelApplyAdvanced"]()
192
- basicguider = NODE_CLASS_MAPPINGS["BasicGuider"]()
193
- basicscheduler = NODE_CLASS_MAPPINGS["BasicScheduler"]()
194
- samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
195
- vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
196
- saveimage = NODE_CLASS_MAPPINGS["SaveImage"]()
197
- imagecrop = NODE_CLASS_MAPPINGS["ImageCrop+"]()
198
-
199
- #Add all the models that load a safetensors file
200
- model_loaders = [dualcliploader_357, vaeloader_359, unetloader_358, clipvisionloader_438, stylemodelloader_441, downloadandloaddepthanythingv2model_437]
201
-
202
- # Check which models are valid and how to best load them
203
- valid_models = [
204
- getattr(loader[0], 'patcher', loader[0])
205
- for loader in model_loaders
206
- if not isinstance(loader[0], dict) and not isinstance(getattr(loader[0], 'patcher', None), dict)
207
- ]
208
-
209
- #Finally loads the models
210
- model_management.load_models_gpu(valid_models)
211
-
212
- @spaces.GPU(duration=60)
213
- def generate_image(prompt, structure_image, style_image, depth_strength, style_strength):
214
  import_custom_nodes()
215
  with torch.inference_mode():
216
-
217
- intconstant_83 = intconstant.get_value(value=1024)
218
-
219
- intconstant_84 = intconstant.get_value(value=1024)
220
-
221
- cr_clip_input_switch_319 = cr_clip_input_switch.switch(
222
- Input=1,
223
- clip1=get_value_at_index(dualcliploader_357, 0),
224
- clip2=get_value_at_index(dualcliploader_357, 0),
225
- )
226
-
227
- cliptextencode_174 = cliptextencode.encode(
228
- text=prompt,
229
- clip=get_value_at_index(cr_clip_input_switch_319, 0),
230
- )
231
-
232
- cliptextencode_175 = cliptextencode.encode(
233
- text="purple", clip=get_value_at_index(cr_clip_input_switch_319, 0)
234
- )
235
-
236
- loadimage_429 = loadimage.load_image(image=structure_image)
237
-
238
- imageresize_72 = imageresize.execute(
239
- width=get_value_at_index(intconstant_83, 0),
240
- height=get_value_at_index(intconstant_84, 0),
241
- interpolation="bicubic",
242
- method="keep proportion",
243
- condition="always",
244
- multiple_of=16,
245
- image=get_value_at_index(loadimage_429, 0),
246
- )
247
-
248
- getimagesizeandcount_360 = getimagesizeandcount.getsize(
249
- image=get_value_at_index(imageresize_72, 0)
250
- )
251
-
252
- vaeencode_197 = vaeencode.encode(
253
- pixels=get_value_at_index(getimagesizeandcount_360, 0),
254
- vae=get_value_at_index(vaeloader_359, 0),
255
- )
256
-
257
- ksamplerselect_363 = ksamplerselect.get_sampler(sampler_name="euler")
258
-
259
- randomnoise_365 = randomnoise.get_noise(noise_seed=random.randint(1, 2**64))
260
-
261
-
262
- fluxguidance_430 = fluxguidance.append(
263
- guidance=15, conditioning=get_value_at_index(cliptextencode_174, 0)
264
- )
265
-
266
- depthanything_v2_436 = depthanything_v2.process(
267
- da_model=get_value_at_index(downloadandloaddepthanythingv2model_437, 0),
268
- images=get_value_at_index(getimagesizeandcount_360, 0),
269
  )
270
 
271
- instructpixtopixconditioning_431 = instructpixtopixconditioning.encode(
272
- positive=get_value_at_index(fluxguidance_430, 0),
273
- negative=get_value_at_index(cliptextencode_175, 0),
274
- vae=get_value_at_index(vaeloader_359, 0),
275
- pixels=get_value_at_index(depthanything_v2_436, 0),
276
  )
277
 
278
- loadimage_440 = loadimage.load_image(image=style_image)
279
-
280
- clipvisionencode_439 = clipvisionencode.encode(
281
- crop="center",
282
- clip_vision=get_value_at_index(clipvisionloader_438, 0),
283
- image=get_value_at_index(loadimage_440, 0),
284
  )
285
-
286
 
287
- emptylatentimage_10 = emptylatentimage.generate(
288
- width=get_value_at_index(imageresize_72, 1),
289
- height=get_value_at_index(imageresize_72, 2),
290
- batch_size=1,
291
  )
292
 
293
- cr_conditioning_input_switch_271 = cr_conditioning_input_switch.switch(
294
- Input=1,
295
- conditioning1=get_value_at_index(instructpixtopixconditioning_431, 0),
296
- conditioning2=get_value_at_index(instructpixtopixconditioning_431, 0),
297
- )
298
-
299
- cr_conditioning_input_switch_272 = cr_conditioning_input_switch.switch(
300
- Input=1,
301
- conditioning1=get_value_at_index(instructpixtopixconditioning_431, 1),
302
- conditioning2=get_value_at_index(instructpixtopixconditioning_431, 1),
303
- )
304
-
305
- cr_model_input_switch_320 = cr_model_input_switch.switch(
306
- Input=1,
307
- model1=get_value_at_index(unetloader_358, 0),
308
- model2=get_value_at_index(unetloader_358, 0),
309
- )
310
-
311
- stylemodelapplyadvanced_442 = stylemodelapplyadvanced.apply_stylemodel(
312
- strength=style_strength,
313
- conditioning=get_value_at_index(instructpixtopixconditioning_431, 0),
314
- style_model=get_value_at_index(stylemodelloader_441, 0),
315
- clip_vision_output=get_value_at_index(clipvisionencode_439, 0),
316
- )
317
-
318
- basicguider_366 = basicguider.get_guider(
319
- model=get_value_at_index(cr_model_input_switch_320, 0),
320
- conditioning=get_value_at_index(stylemodelapplyadvanced_442, 0),
321
- )
322
-
323
- basicscheduler_364 = basicscheduler.get_sigmas(
324
- scheduler="simple",
325
- steps=28,
326
- denoise=1,
327
- model=get_value_at_index(cr_model_input_switch_320, 0),
328
- )
329
-
330
- samplercustomadvanced_362 = samplercustomadvanced.sample(
331
- noise=get_value_at_index(randomnoise_365, 0),
332
- guider=get_value_at_index(basicguider_366, 0),
333
- sampler=get_value_at_index(ksamplerselect_363, 0),
334
- sigmas=get_value_at_index(basicscheduler_364, 0),
335
- latent_image=get_value_at_index(emptylatentimage_10, 0),
336
- )
337
 
338
- vaedecode_321 = vaedecode.decode(
339
- samples=get_value_at_index(samplercustomadvanced_362, 0),
340
- vae=get_value_at_index(vaeloader_359, 0),
341
- )
 
 
 
 
 
 
 
 
 
342
 
343
- saveimage_327 = saveimage.save_images(
344
- filename_prefix=get_value_at_index(text_multiline_454, 0),
345
- images=get_value_at_index(vaedecode_321, 0),
346
- )
347
 
 
 
 
348
 
349
- fluxguidance_382 = fluxguidance.append(
350
- guidance=depth_strength,
351
- conditioning=get_value_at_index(cr_conditioning_input_switch_272, 0),
352
- )
353
 
354
- imagecrop_447 = imagecrop.execute(
355
- width=2000,
356
- height=2000,
357
- position="top-center",
358
- x_offset=0,
359
- y_offset=0,
360
- image=get_value_at_index(loadimage_440, 0),
361
- )
362
 
363
- saved_path = f"output/{saveimage_327['ui']['images'][0]['filename']}"
364
- return saved_path
365
 
366
  if __name__ == "__main__":
367
- # Comment out the main() call
368
 
369
  # Start your Gradio app
370
  with gr.Blocks() as app:
371
  # Add a title
372
- gr.Markdown("# FLUX Style Shaping")
373
 
374
  with gr.Row():
375
  with gr.Column():
376
  # Add an input
377
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
378
  # Add a `Row` to include the groups side by side
379
- with gr.Row():
380
- # First group includes structure image and depth strength
381
- with gr.Group():
382
- structure_image = gr.Image(label="Structure Image", type="filepath")
383
- depth_strength = gr.Slider(minimum=0, maximum=50, value=15, label="Depth Strength")
384
- # Second group includes style image and style strength
385
- with gr.Group():
386
- style_image = gr.Image(label="Style Image", type="filepath")
387
- style_strength = gr.Slider(minimum=0, maximum=1, value=0.5, label="Style Strength")
388
 
389
  # The generate button
390
  generate_btn = gr.Button("Generate")
@@ -397,7 +211,8 @@ if __name__ == "__main__":
397
  # and the output an image
398
  generate_btn.click(
399
  fn=generate_image,
400
- inputs=[prompt_input, structure_image, style_image, depth_strength, style_strength],
 
401
  outputs=[output_image]
402
  )
403
  app.launch(share=True)
 
6
  import gradio as gr
7
  from huggingface_hub import hf_hub_download
8
  import spaces
 
9
 
10
+ hf_hub_download(repo_id="Comfy-Org/stable-diffusion-v1-5-archive", filename="v1-5-pruned-emaonly-fp16.safetensors", local_dir="models/checkpoints")
11
+
 
 
 
 
 
12
 
13
  def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
14
  """Returns the value at the given index of a sequence or mapping.
 
119
 
120
  from nodes import NODE_CLASS_MAPPINGS
121
 
122
+ @spaces.GPU(duration=60) #modify the duration for the average it takes for your worflow to run, in seconds
123
+ def generate_image(prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  import_custom_nodes()
125
  with torch.inference_mode():
126
+ checkpointloadersimple = NODE_CLASS_MAPPINGS["CheckpointLoaderSimple"]()
127
+ checkpointloadersimple_4 = checkpointloadersimple.load_checkpoint(
128
+ ckpt_name="v1-5-pruned.safetensors"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  )
130
 
131
+ emptylatentimage = NODE_CLASS_MAPPINGS["EmptyLatentImage"]()
132
+ emptylatentimage_5 = emptylatentimage.generate(
133
+ width=512, height=512, batch_size=1
 
 
134
  )
135
 
136
+ cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
137
+ cliptextencode_6 = cliptextencode.encode(
138
+ text=prompt, clip=get_value_at_index(checkpointloadersimple_4, 1)
 
 
 
139
  )
 
140
 
141
+ cliptextencode_7 = cliptextencode.encode(
142
+ text="(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (watermark, signature, text font, username, error, logo, words, letters, digits, autograph, trademark, name:1.2), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate, (airbrushed, cartoon, anime, semi-realistic, cgi, render, blender, digital art, manga, amateur:1.3), (3D ,3D Game, 3D Game Scene, 3D Character:1.1), (bad hands, bad anatomy, bad body, bad face, bad teeth, bad arms, bad legs, deformities:1.3)",
143
+ clip=get_value_at_index(checkpointloadersimple_4, 1),
 
144
  )
145
 
146
+ ksampler = NODE_CLASS_MAPPINGS["KSampler"]()
147
+ vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
148
+ saveimage = NODE_CLASS_MAPPINGS["SaveImage"]()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
+ for q in range(1):
151
+ ksampler_3 = ksampler.sample(
152
+ seed=random.randint(1, 2**64),
153
+ steps=35,
154
+ cfg=7,
155
+ sampler_name="dpmpp_2m",
156
+ scheduler="karras",
157
+ denoise=1,
158
+ model=get_value_at_index(checkpointloadersimple_4, 0),
159
+ positive=get_value_at_index(cliptextencode_6, 0),
160
+ negative=get_value_at_index(cliptextencode_7, 0),
161
+ latent_image=get_value_at_index(emptylatentimage_5, 0),
162
+ )
163
 
164
+ vaedecode_8 = vaedecode.decode(
165
+ samples=get_value_at_index(ksampler_3, 0),
166
+ vae=get_value_at_index(checkpointloadersimple_4, 2),
167
+ )
168
 
169
+ saveimage_9 = saveimage.save_images(
170
+ filename_prefix="ComfyUI", images=get_value_at_index(vaedecode_8, 0)
171
+ )
172
 
173
+ saved_path = f"output/{saveimage_9['ui']['images'][0]['filename']}"
174
+ return saved_path
 
 
175
 
 
 
 
 
 
 
 
 
176
 
177
+ # if __name__ == "__main__":
178
+ # main()
179
 
180
  if __name__ == "__main__":
181
+ # Comment out the main() call in the exported Python code
182
 
183
  # Start your Gradio app
184
  with gr.Blocks() as app:
185
  # Add a title
186
+ gr.Markdown("# Simple Example")
187
 
188
  with gr.Row():
189
  with gr.Column():
190
  # Add an input
191
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
192
  # Add a `Row` to include the groups side by side
193
+ # with gr.Row():
194
+ # # First group includes structure image and depth strength
195
+ # with gr.Group():
196
+ # # structure_image = gr.Image(label="Structure Image", type="filepath")
197
+ # # depth_strength = gr.Slider(minimum=0, maximum=50, value=15, label="Depth Strength")
198
+ # # Second group includes style image and style strength
199
+ # # with gr.Group():
200
+ # # style_image = gr.Image(label="Style Image", type="filepath")
201
+ # # style_strength = gr.Slider(minimum=0, maximum=1, value=0.5, label="Style Strength")
202
 
203
  # The generate button
204
  generate_btn = gr.Button("Generate")
 
211
  # and the output an image
212
  generate_btn.click(
213
  fn=generate_image,
214
+ # inputs=[prompt_input, structure_image, style_image, depth_strength, style_strength],
215
+ inputs=[prompt_input],
216
  outputs=[output_image]
217
  )
218
  app.launch(share=True)
requirements.txt CHANGED
@@ -13,12 +13,13 @@ Pillow
13
  scipy
14
  tqdm
15
  psutil
 
 
16
 
17
  #non essential dependencies:
18
  kornia>=0.7.1
19
  spandrel
20
  soundfile
21
 
22
- # needed for huggingface spaces, from custom nodes folders
23
- accelerate
24
- huggingface_hub
 
13
  scipy
14
  tqdm
15
  psutil
16
+ gradio>=4.0.0
17
+ huggingface_hub
18
 
19
  #non essential dependencies:
20
  kornia>=0.7.1
21
  spandrel
22
  soundfile
23
 
24
+ # needed for huggingface spaces
25
+ accelerate