n42 commited on
Commit
cec477c
·
1 Parent(s): ec25ef6

adding textual inversion adapters

Browse files
Files changed (3) hide show
  1. app.py +73 -16
  2. appConfig.json +9 -0
  3. config.py +20 -6
app.py CHANGED
@@ -161,6 +161,27 @@ def schedulers_change(scheduler, config):
161
 
162
  return scheduler_description, config, str(config), assemble_code(config)
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  def run_inference(config, config_history, progress=gr.Progress(track_tqdm=True)):
165
 
166
  # str_config = str_config.replace("'", '"').replace('None', 'null').replace('False', 'false')
@@ -173,13 +194,25 @@ def run_inference(config, config_history, progress=gr.Progress(track_tqdm=True))
173
  torch.backends.cuda.matmul.allow_tf32 = get_bool(config["allow_tensorfloat32"]) # Use TensorFloat-32 as of https://huggingface.co/docs/diffusers/main/en/optimization/fp16 faster, but slightly less accurate computations
174
 
175
  progress((2,3), desc="Initializing pipeline...")
176
-
 
177
  pipeline = DiffusionPipeline.from_pretrained(
178
  config["model"],
179
  use_safetensors = get_bool(config["use_safetensors"]),
180
  torch_dtype = get_data_type(config["data_type"]),
181
  variant = get_variant(config["variant"])).to(config["device"])
182
 
 
 
 
 
 
 
 
 
 
 
 
183
  if config['refiner'].lower() != 'none':
184
  refiner = DiffusionPipeline.from_pretrained(
185
  config['refiner'],
@@ -189,40 +222,48 @@ def run_inference(config, config_history, progress=gr.Progress(track_tqdm=True))
189
  use_safetensors=get_bool(config["use_safetensors"]),
190
  variant = get_variant(config["variant"])).to(config["device"])
191
 
192
- if str(config["safety_checker"]).lower() == 'false':
193
- pipeline.safety_checker = None
194
 
 
 
 
 
 
195
  pipeline.requires_safety_checker = get_bool(config["requires_safety_checker"])
196
 
 
197
  pipeline.scheduler = get_scheduler(config["scheduler"], pipeline.scheduler.config)
198
 
 
199
  if config["manual_seed"] < 0 or config["manual_seed"] is None or config["manual_seed"] == '':
200
  generator = None
201
  else:
202
  generator = torch.manual_seed(int(config["manual_seed"]))
203
 
 
 
 
 
 
204
  progress((3,3), desc="Creating the result...")
205
 
 
 
206
  image = pipeline(
207
- prompt = config["prompt"],
208
  negative_prompt = config["negative_prompt"],
209
  generator = generator,
210
  num_inference_steps = int(config["inference_steps"]),
211
  guidance_scale = float(config["guidance_scale"])).images
212
 
213
- if str(config["cpu_offload"]).lower() != 'false':
214
- pipeline.enable_model_cpu_offload()
215
-
216
  if config['refiner'].lower() != 'none':
217
  image = refiner(
218
- prompt = config["prompt"],
219
  num_inference_steps = int(config["inference_steps"]),
220
  image=image,
221
  ).images
222
 
223
- if str(config["cpu_offload"]).lower() != 'false':
224
- refiner.enable_model_cpu_offload()
225
-
226
  config_history.append(config.copy())
227
 
228
  return image[0], dict_list_to_markdown_table(config_history), config_history
@@ -236,6 +277,7 @@ models = appConfig.get("models", {})
236
  schedulers = appConfig.get("schedulers", {})
237
  devices = appConfig.get("devices", [])
238
  auto_encoders = appConfig.get("auto_encoders", [])
 
239
 
240
  # interface
241
  with gr.Blocks(analytics_enabled=False) as demo:
@@ -249,7 +291,7 @@ with gr.Blocks(analytics_enabled=False) as demo:
249
  </small>''')
250
  gr.Markdown("### Device specific settings")
251
  with gr.Row():
252
- in_devices = gr.Dropdown(label="Device:", value=config.value["device"], choices=devices, filterable=True, multiselect=False, allow_custom_value=True)
253
  in_data_type = gr.Radio(label="Data Type:", value=config.value["data_type"], choices=["bfloat16", "float16", "float32"], info="`bfloat16` is not supported on MPS devices right now; `float16` may also not be supported on all devices, Half-precision weights, will save GPU memory, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16")
254
  in_allow_tensorfloat32 = gr.Radio(label="Allow TensorFloat32:", value=config.value["allow_tensorfloat32"], choices=["True", "False"], info="is not supported on MPS devices right now; use TensorFloat-32 is faster, but results in slightly less accurate computations, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16 ")
255
  in_variant = gr.Radio(label="Variant:", value=config.value["variant"], choices=["fp16", None], info="Use half-precision weights will save GPU memory, not all models support that, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16 ")
@@ -285,10 +327,10 @@ with gr.Blocks(analytics_enabled=False) as demo:
285
  in_inference_steps = gr.Number(label="Inference steps", value=config.value["inference_steps"], info="Each step improves the final result but also results in higher computation")
286
  in_manual_seed = gr.Number(label="Manual seed", value=config.value["manual_seed"], info="Set this to -1 or leave it empty to randomly generate an image. A fixed value will result in a similar image for every run")
287
  in_guidance_scale = gr.Slider(minimum=0, maximum=100, step=0.1, label="Guidance Scale", value=config.value["guidance_scale"], info="A low guidance scale leads to a faster inference time, with the drawback that negative prompts don’t have any effect on the denoising process.")
 
 
288
  with gr.Row():
289
- gr.Markdown("### Auto Encoder")
290
- with gr.Row():
291
- gr.Markdown("**VAE** stands for Variational Autoencoders. An 'autoencoder' is an artificial neural network that is able to encode input data and decode to output data to bascially recreate the input. The VAE whereas adds a couple of additional layers of complexity to create new and unique output.")
292
  with gr.Row():
293
  with gr.Column():
294
  in_auto_encoders = gr.Dropdown(value="None", choices=list(auto_encoders.keys()), label="Auto encoder", info="leave empty to not add an auto encoder")
@@ -297,6 +339,18 @@ with gr.Blocks(analytics_enabled=False) as demo:
297
  in_enable_vae_slicing = gr.Radio(label="Enable VAE slicing:", value=config.value["enable_vae_slicing"], choices=["True", "False"], info="decoding the batches of latents one image at a time, which may reduce memory usage, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
298
  in_enable_vae_tiling= gr.Radio(label="Enable VAE tiling:", value=config.value["enable_vae_tiling"], choices=["True", "False"], info="splitting the image into overlapping tiles, decoding the tiles, and then blending the outputs together to compose the final image, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
299
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  gr.Markdown("### Output")
301
  with gr.Row():
302
  btn_start_pipeline = gr.Button(value="Run", variant="primary")
@@ -325,6 +379,8 @@ with gr.Blocks(analytics_enabled=False) as demo:
325
  in_auto_encoders.change(auto_encoders_change, inputs=[in_auto_encoders, config], outputs=[out_auto_encoder_description, config, out_config, out_code])
326
  in_enable_vae_slicing.change(enable_vae_slicing_change, inputs=[in_enable_vae_slicing, config], outputs=[config, out_config, out_code])
327
  in_enable_vae_tiling.change(enable_vae_tiling_change, inputs=[in_enable_vae_tiling, config], outputs=[config, out_config, out_code])
 
 
328
  in_prompt.change(prompt_change, inputs=[in_prompt, config], outputs=[config, out_config, out_code])
329
  in_trigger_token.change(trigger_token_change, inputs=[in_trigger_token, config], outputs=[config, out_config, out_code])
330
  in_negative_prompt.change(negative_prompt_change, inputs=[in_negative_prompt, config], outputs=[config, out_config, out_code])
@@ -354,7 +410,8 @@ with gr.Blocks(analytics_enabled=False) as demo:
354
  in_negative_prompt,
355
  in_inference_steps,
356
  in_manual_seed,
357
- in_guidance_scale
 
358
  ])
359
 
360
  demo.launch(show_error=True)
 
161
 
162
  return scheduler_description, config, str(config), assemble_code(config)
163
 
164
+ def adapters_textual_inversion_change(adapter_textual_inversion, config):
165
+
166
+ if str(adapter_textual_inversion) != 'None' and type(adapter_textual_inversion) != list:
167
+
168
+ adapter_textual_inversion_description = adapters['textual_inversion'][adapter_textual_inversion]['description']
169
+ in_adapters_textual_inversion_token = adapters['textual_inversion'][adapter_textual_inversion]['token']
170
+
171
+ else:
172
+ adapter_textual_inversion_description = ""
173
+ in_adapters_textual_inversion_token = ""
174
+
175
+ config = set_config(config, 'adapter_textual_inversion', adapter_textual_inversion)
176
+
177
+ return adapter_textual_inversion_description, in_adapters_textual_inversion_token, config, str(config), assemble_code(config)
178
+
179
+ def textual_inversion_token_change(adapter_textual_inversion_token, config):
180
+
181
+ config = set_config(config, 'adapter_textual_inversion_token', adapter_textual_inversion_token)
182
+
183
+ return config, str(config), assemble_code(config)
184
+
185
  def run_inference(config, config_history, progress=gr.Progress(track_tqdm=True)):
186
 
187
  # str_config = str_config.replace("'", '"').replace('None', 'null').replace('False', 'false')
 
194
  torch.backends.cuda.matmul.allow_tf32 = get_bool(config["allow_tensorfloat32"]) # Use TensorFloat-32 as of https://huggingface.co/docs/diffusers/main/en/optimization/fp16 faster, but slightly less accurate computations
195
 
196
  progress((2,3), desc="Initializing pipeline...")
197
+
198
+ # INIT PIPELINE
199
  pipeline = DiffusionPipeline.from_pretrained(
200
  config["model"],
201
  use_safetensors = get_bool(config["use_safetensors"]),
202
  torch_dtype = get_data_type(config["data_type"]),
203
  variant = get_variant(config["variant"])).to(config["device"])
204
 
205
+ if str(config["cpu_offload"]).lower() != 'false':
206
+ pipeline.enable_model_cpu_offload()
207
+
208
+ # AUTO ENCODER
209
+ if str(config["auto_encoder"]).lower() != 'none':
210
+ pipeline.vae = AutoencoderKL.from_pretrained(config["auto_encoder"], torch_dtype=get_data_type(config["data_type"])).to(config["device"])
211
+
212
+ if str(config["enable_vae_slicing"]).lower() != 'false': pipeline.enable_vae_slicing()
213
+ if str(config["enable_vae_tiling"]).lower() != 'false': pipeline.enable_vae_tiling()
214
+
215
+ # INIT REFINER
216
  if config['refiner'].lower() != 'none':
217
  refiner = DiffusionPipeline.from_pretrained(
218
  config['refiner'],
 
222
  use_safetensors=get_bool(config["use_safetensors"]),
223
  variant = get_variant(config["variant"])).to(config["device"])
224
 
225
+ if str(config["cpu_offload"]).lower() != 'false':
226
+ refiner.enable_model_cpu_offload()
227
 
228
+ if str(config["enable_vae_slicing"]).lower() != 'false': refiner.enable_vae_slicing()
229
+ if str(config["enable_vae_tiling"]).lower() != 'false': refiner.enable_vae_tiling()
230
+
231
+ # SAFETY CHECKER
232
+ if str(config["safety_checker"]).lower() == 'false': pipeline.safety_checker = None
233
  pipeline.requires_safety_checker = get_bool(config["requires_safety_checker"])
234
 
235
+ # SCHEDULER/SOLVER
236
  pipeline.scheduler = get_scheduler(config["scheduler"], pipeline.scheduler.config)
237
 
238
+ # MANUAL SEED/GENERATOR
239
  if config["manual_seed"] < 0 or config["manual_seed"] is None or config["manual_seed"] == '':
240
  generator = None
241
  else:
242
  generator = torch.manual_seed(int(config["manual_seed"]))
243
 
244
+ # ADAPTERS
245
+ # TEXTUAL INVERSION
246
+ if str(config["adapter_textual_inversion"]).lower() != 'none':
247
+ pipeline.load_textual_inversion(config["adapter_textual_inversion"], token=config["adapter_textual_inversion_token"])
248
+
249
  progress((3,3), desc="Creating the result...")
250
 
251
+ prompt = config["prompt"] + config["trigger_token"] + config["adapter_textual_inversion_token"]
252
+
253
  image = pipeline(
254
+ prompt = prompt,
255
  negative_prompt = config["negative_prompt"],
256
  generator = generator,
257
  num_inference_steps = int(config["inference_steps"]),
258
  guidance_scale = float(config["guidance_scale"])).images
259
 
 
 
 
260
  if config['refiner'].lower() != 'none':
261
  image = refiner(
262
+ prompt = prompt,
263
  num_inference_steps = int(config["inference_steps"]),
264
  image=image,
265
  ).images
266
 
 
 
 
267
  config_history.append(config.copy())
268
 
269
  return image[0], dict_list_to_markdown_table(config_history), config_history
 
277
  schedulers = appConfig.get("schedulers", {})
278
  devices = appConfig.get("devices", [])
279
  auto_encoders = appConfig.get("auto_encoders", [])
280
+ adapters = appConfig.get("adapters", [])
281
 
282
  # interface
283
  with gr.Blocks(analytics_enabled=False) as demo:
 
291
  </small>''')
292
  gr.Markdown("### Device specific settings")
293
  with gr.Row():
294
+ in_devices = gr.Dropdown(label="Device:", value=config.value["device"], choices=devices, filterable=True, multiselect=False, allow_custom_value=True, info="(you may add a custom device address at any time)")
295
  in_data_type = gr.Radio(label="Data Type:", value=config.value["data_type"], choices=["bfloat16", "float16", "float32"], info="`bfloat16` is not supported on MPS devices right now; `float16` may also not be supported on all devices, Half-precision weights, will save GPU memory, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16")
296
  in_allow_tensorfloat32 = gr.Radio(label="Allow TensorFloat32:", value=config.value["allow_tensorfloat32"], choices=["True", "False"], info="is not supported on MPS devices right now; use TensorFloat-32 is faster, but results in slightly less accurate computations, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16 ")
297
  in_variant = gr.Radio(label="Variant:", value=config.value["variant"], choices=["fp16", None], info="Use half-precision weights will save GPU memory, not all models support that, see https://huggingface.co/docs/diffusers/main/en/optimization/fp16 ")
 
327
  in_inference_steps = gr.Number(label="Inference steps", value=config.value["inference_steps"], info="Each step improves the final result but also results in higher computation")
328
  in_manual_seed = gr.Number(label="Manual seed", value=config.value["manual_seed"], info="Set this to -1 or leave it empty to randomly generate an image. A fixed value will result in a similar image for every run")
329
  in_guidance_scale = gr.Slider(minimum=0, maximum=100, step=0.1, label="Guidance Scale", value=config.value["guidance_scale"], info="A low guidance scale leads to a faster inference time, with the drawback that negative prompts don’t have any effect on the denoising process.")
330
+
331
+ gr.Markdown("### Auto Encoder")
332
  with gr.Row():
333
+ gr.Markdown("**VAE** stands for Variational Auto Encoders. An 'autoencoder' is an artificial neural network that is able to encode input data and decode to output data to bascially recreate the input. The VAE whereas adds a couple of additional layers of complexity to create new and unique output.")
 
 
334
  with gr.Row():
335
  with gr.Column():
336
  in_auto_encoders = gr.Dropdown(value="None", choices=list(auto_encoders.keys()), label="Auto encoder", info="leave empty to not add an auto encoder")
 
339
  in_enable_vae_slicing = gr.Radio(label="Enable VAE slicing:", value=config.value["enable_vae_slicing"], choices=["True", "False"], info="decoding the batches of latents one image at a time, which may reduce memory usage, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
340
  in_enable_vae_tiling= gr.Radio(label="Enable VAE tiling:", value=config.value["enable_vae_tiling"], choices=["True", "False"], info="splitting the image into overlapping tiles, decoding the tiles, and then blending the outputs together to compose the final image, see https://huggingface.co/docs/diffusers/main/en/optimization/memory")
341
 
342
+ gr.Markdown("### Adapters")
343
+ with gr.Row():
344
+ gr.Markdown('''Adapters allow you to apply finetuned weights to your base model. They come in many flavors depending on how they were trained. See see https://huggingface.co/docs/diffusers/using-diffusers/loading_adapters''')
345
+ with gr.Row():
346
+ gr.Markdown('#### Textual Inversion Adapters')
347
+ with gr.Row():
348
+ gr.Markdown('(a technique that enables a model like Stable Diffusion to learn a new concept from just a few sample images)')
349
+ with gr.Row():
350
+ in_adapters_textual_inversion = gr.Dropdown(value="None", choices=list(adapters['textual_inversion'].keys()), label="Adapter", info="leave empty to not use an adapter")
351
+ in_adapters_textual_inversion_token = gr.Textbox(value="None", label="Adapter token", info="required to activate the token, will be added to your prompt")
352
+ out_adapters_textual_inversion_description = gr.Textbox(value="", label="Description")
353
+
354
  gr.Markdown("### Output")
355
  with gr.Row():
356
  btn_start_pipeline = gr.Button(value="Run", variant="primary")
 
379
  in_auto_encoders.change(auto_encoders_change, inputs=[in_auto_encoders, config], outputs=[out_auto_encoder_description, config, out_config, out_code])
380
  in_enable_vae_slicing.change(enable_vae_slicing_change, inputs=[in_enable_vae_slicing, config], outputs=[config, out_config, out_code])
381
  in_enable_vae_tiling.change(enable_vae_tiling_change, inputs=[in_enable_vae_tiling, config], outputs=[config, out_config, out_code])
382
+ in_adapters_textual_inversion.change(adapters_textual_inversion_change, inputs=[in_adapters_textual_inversion, config], outputs=[out_adapters_textual_inversion_description, in_adapters_textual_inversion_token, config, out_config, out_code])
383
+ in_adapters_textual_inversion_token.change(textual_inversion_token_change, inputs=[in_adapters_textual_inversion_token, config], outputs=[config, out_config, out_code])
384
  in_prompt.change(prompt_change, inputs=[in_prompt, config], outputs=[config, out_config, out_code])
385
  in_trigger_token.change(trigger_token_change, inputs=[in_trigger_token, config], outputs=[config, out_config, out_code])
386
  in_negative_prompt.change(negative_prompt_change, inputs=[in_negative_prompt, config], outputs=[config, out_config, out_code])
 
410
  in_negative_prompt,
411
  in_inference_steps,
412
  in_manual_seed,
413
+ in_guidance_scale,
414
+ in_adapters_textual_inversion
415
  ])
416
 
417
  demo.launch(show_error=True)
appConfig.json CHANGED
@@ -78,6 +78,15 @@
78
  "madebyollin/sdxl-vae-fp16-fix": "stable diffusion models encoder with fp16 precision, see https://huggingface.co/madebyollin/sdxl-vae-fp16-fix",
79
  "stabilityai/sd-vae-ft-mse": "works best with CompVis/stable-diffusion-v1-4, see https://huggingface.co/stabilityai/sd-vae-ft-mse"
80
  },
 
 
 
 
 
 
 
 
 
81
  "schedulers": {
82
  "DDPMScheduler": "Denoising Diffusion Probabilistic Model",
83
  "DDIMScheduler": "Denoising Diffusion Incremental Sampling, efficient image generation, might require more tunin",
 
78
  "madebyollin/sdxl-vae-fp16-fix": "stable diffusion models encoder with fp16 precision, see https://huggingface.co/madebyollin/sdxl-vae-fp16-fix",
79
  "stabilityai/sd-vae-ft-mse": "works best with CompVis/stable-diffusion-v1-4, see https://huggingface.co/stabilityai/sd-vae-ft-mse"
80
  },
81
+ "adapters": {
82
+ "textual_inversion": {
83
+ "None": {"token": "", "description": ""},
84
+ "sd-concepts-library/gta5-artwork": {
85
+ "token": "<gta-artwork>",
86
+ "description": "see https://huggingface.co/sd-concepts-library/gta5-artwork"
87
+ }
88
+ }
89
+ },
90
  "schedulers": {
91
  "DDPMScheduler": "Denoising Diffusion Probabilistic Model",
92
  "DDIMScheduler": "Denoising Diffusion Incremental Sampling, efficient image generation, might require more tunin",
config.py CHANGED
@@ -52,6 +52,8 @@ def get_initial_config():
52
  "manual_seed": 42,
53
  "inference_steps": 10,
54
  "guidance_scale": 5,
 
 
55
  "prompt": 'A white rabbit',
56
  "trigger_token": '',
57
  "negative_prompt": 'lowres, cropped, worst quality, low quality',
@@ -98,7 +100,9 @@ def get_config_from_url(initial_config, request: Request):
98
  return_config['negative_prompt'],
99
  return_config['inference_steps'],
100
  return_config['manual_seed'],
101
- return_config['guidance_scale']
 
 
102
  ]
103
 
104
  def load_app_config():
@@ -137,6 +141,7 @@ def assemble_code(str_config):
137
  code.append('data_type = torch.bfloat16')
138
  else:
139
  code.append('data_type = torch.float16')
 
140
  code.append(f'torch.backends.cuda.matmul.allow_tf32 = {config["allow_tensorfloat32"]}')
141
 
142
  if str(config["variant"]) == 'None':
@@ -144,22 +149,25 @@ def assemble_code(str_config):
144
  else:
145
  code.append(f'variant = "{config["variant"]}"')
146
 
147
-
148
  code.append(f'''use_safetensors = {config["use_safetensors"]}''')
149
 
 
150
  code.append(f'''pipeline = DiffusionPipeline.from_pretrained(
151
  "{config['model']}",
152
  use_safetensors=use_safetensors,
153
  torch_dtype=data_type,
154
  variant=variant).to(device)''')
155
 
 
 
 
156
  if str(config["auto_encoder"]).lower() != 'none':
157
  code.append(f'pipeline.vae = AutoencoderKL.from_pretrained("{config["auto_encoder"]}", torch_dtype=data_type).to(device)')
158
 
159
- if str(config["cpu_offload"]).lower() != 'false': code.append("pipeline.enable_model_cpu_offload()")
160
  if str(config["enable_vae_slicing"]).lower() != 'false': code.append("pipeline.enable_vae_slicing()")
161
  if str(config["enable_vae_tiling"]).lower() != 'false': code.append("pipeline.enable_vae_tiling()")
162
 
 
163
  if config['refiner'].lower() != 'none':
164
  code.append(f'''refiner = DiffusionPipeline.from_pretrained(
165
  "{config['refiner']}",
@@ -174,13 +182,16 @@ def assemble_code(str_config):
174
  if str(config["enable_vae_slicing"]).lower() != 'false': code.append("refiner.enable_vae_slicing()")
175
  if str(config["enable_vae_tiling"]).lower() != 'false': code.append("refiner.enable_vae_tiling()")
176
 
 
177
  code.append(f'pipeline.requires_safety_checker = {config["requires_safety_checker"]}')
178
-
179
  if str(config["safety_checker"]).lower() == 'false':
180
  code.append(f'pipeline.safety_checker = None')
181
 
182
- code.append(f'pipeline.scheduler = {config["scheduler"]}.from_config(pipeline.scheduler.config)')
 
 
183
 
 
184
  if config['manual_seed'] < 0 or config['manual_seed'] is None or config['manual_seed'] == '':
185
  code.append(f'# manual_seed = {config["manual_seed"]}')
186
  code.append(f'generator = None')
@@ -188,7 +199,10 @@ def assemble_code(str_config):
188
  code.append(f'manual_seed = {config["manual_seed"]}')
189
  code.append(f'generator = torch.manual_seed(manual_seed)')
190
 
191
- code.append(f'prompt = "{config["prompt"]} {config["trigger_token"]}"')
 
 
 
192
  code.append(f'negative_prompt = "{config["negative_prompt"]}"')
193
  code.append(f'inference_steps = {config["inference_steps"]}')
194
  code.append(f'guidance_scale = {config["guidance_scale"]}')
 
52
  "manual_seed": 42,
53
  "inference_steps": 10,
54
  "guidance_scale": 5,
55
+ "adapter_textual_inversion": None,
56
+ "adapter_textual_inversion_token": None,
57
  "prompt": 'A white rabbit',
58
  "trigger_token": '',
59
  "negative_prompt": 'lowres, cropped, worst quality, low quality',
 
100
  return_config['negative_prompt'],
101
  return_config['inference_steps'],
102
  return_config['manual_seed'],
103
+ return_config['guidance_scale'],
104
+ return_config['adapter_textual_inversion'],
105
+ return_config['adapter_textual_inversion_token']
106
  ]
107
 
108
  def load_app_config():
 
141
  code.append('data_type = torch.bfloat16')
142
  else:
143
  code.append('data_type = torch.float16')
144
+
145
  code.append(f'torch.backends.cuda.matmul.allow_tf32 = {config["allow_tensorfloat32"]}')
146
 
147
  if str(config["variant"]) == 'None':
 
149
  else:
150
  code.append(f'variant = "{config["variant"]}"')
151
 
 
152
  code.append(f'''use_safetensors = {config["use_safetensors"]}''')
153
 
154
+ # INIT PIPELINE
155
  code.append(f'''pipeline = DiffusionPipeline.from_pretrained(
156
  "{config['model']}",
157
  use_safetensors=use_safetensors,
158
  torch_dtype=data_type,
159
  variant=variant).to(device)''')
160
 
161
+ if str(config["cpu_offload"]).lower() != 'false': code.append("pipeline.enable_model_cpu_offload()")
162
+
163
+ # AUTO ENCODER
164
  if str(config["auto_encoder"]).lower() != 'none':
165
  code.append(f'pipeline.vae = AutoencoderKL.from_pretrained("{config["auto_encoder"]}", torch_dtype=data_type).to(device)')
166
 
 
167
  if str(config["enable_vae_slicing"]).lower() != 'false': code.append("pipeline.enable_vae_slicing()")
168
  if str(config["enable_vae_tiling"]).lower() != 'false': code.append("pipeline.enable_vae_tiling()")
169
 
170
+ # INIT REFINER
171
  if config['refiner'].lower() != 'none':
172
  code.append(f'''refiner = DiffusionPipeline.from_pretrained(
173
  "{config['refiner']}",
 
182
  if str(config["enable_vae_slicing"]).lower() != 'false': code.append("refiner.enable_vae_slicing()")
183
  if str(config["enable_vae_tiling"]).lower() != 'false': code.append("refiner.enable_vae_tiling()")
184
 
185
+ # SAFETY CHECKER
186
  code.append(f'pipeline.requires_safety_checker = {config["requires_safety_checker"]}')
 
187
  if str(config["safety_checker"]).lower() == 'false':
188
  code.append(f'pipeline.safety_checker = None')
189
 
190
+ # SCHEDULER/SOLVER
191
+ if str(config["scheduler"]).lower() != 'none':
192
+ code.append(f'pipeline.scheduler = {config["scheduler"]}.from_config(pipeline.scheduler.config)')
193
 
194
+ # MANUAL SEED/GENERATOR
195
  if config['manual_seed'] < 0 or config['manual_seed'] is None or config['manual_seed'] == '':
196
  code.append(f'# manual_seed = {config["manual_seed"]}')
197
  code.append(f'generator = None')
 
199
  code.append(f'manual_seed = {config["manual_seed"]}')
200
  code.append(f'generator = torch.manual_seed(manual_seed)')
201
 
202
+ if str(config["adapter_textual_inversion"]).lower() != 'none':
203
+ code.append(f'pipeline.load_textual_inversion("{config["adapter_textual_inversion"]}", token="{config["adapter_textual_inversion_token"]}")')
204
+
205
+ code.append(f'prompt = "{config["prompt"]} {config["trigger_token"]} {config["adapter_textual_inversion_token"]}"')
206
  code.append(f'negative_prompt = "{config["negative_prompt"]}"')
207
  code.append(f'inference_steps = {config["inference_steps"]}')
208
  code.append(f'guidance_scale = {config["guidance_scale"]}')