ford442 commited on
Commit
c0cf4b6
·
verified ·
1 Parent(s): 6fcc1a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -8,8 +8,8 @@ import random
8
  import torch
9
  from diffusers import StableDiffusion3Pipeline, AutoencoderKL, StableDiffusionXLImg2ImgPipeline, EulerAncestralDiscreteScheduler
10
  from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
11
- from threading import Thread
12
- from transformers import pipeline
13
  from transformers import T5Tokenizer, T5ForConditionalGeneration
14
  import re
15
  import paramiko
@@ -118,6 +118,7 @@ def infer(
118
  height,
119
  guidance_scale,
120
  num_inference_steps,
 
121
  progress=gr.Progress(track_tqdm=True),
122
  ):
123
  seed = random.randint(0, MAX_SEED)
@@ -154,6 +155,9 @@ def infer(
154
  enhanced_prompt = filter_text(enhanced_prompt,prompt)
155
  print('-- filtered prompt --')
156
  print(enhanced_prompt)
 
 
 
157
  print('-- generating image --')
158
  with torch.no_grad():
159
  sd_image = pipe(
@@ -169,6 +173,16 @@ def infer(
169
  image_path = f"sd35m_{seed}.png"
170
  sd_image.save(image_path,optimize=False,compress_level=0)
171
  upload_to_ftp(image_path)
 
 
 
 
 
 
 
 
 
 
172
  #refiner.scheduler.set_timesteps(num_inference_steps,device)
173
  refine = refiner(
174
  prompt=f"{prompt}, high quality masterpiece, complex details",
@@ -178,7 +192,7 @@ def infer(
178
  image=sd_image,
179
  generator=generator,
180
  ).images[0]
181
- refine_path = f"refine_{seed}.png"
182
  refine.save(refine_path,optimize=False,compress_level=0)
183
  upload_to_ftp(refine_path)
184
  return refine, seed, refine_path, enhanced_prompt
@@ -227,7 +241,7 @@ def repeat_infer(
227
  return result, seed, image_path, enhanced_prompt
228
 
229
 
230
- with gr.Blocks(css=css) as demo:
231
  with gr.Column(elem_id="col-container"):
232
  gr.Markdown(" # Text-to-Text-to-Image StableDiffusion 3.5 Medium (with refine)")
233
  expanded_prompt_output = gr.Textbox(label="Expanded Prompt", lines=5) # Add this line
@@ -244,6 +258,7 @@ with gr.Blocks(css=css) as demo:
244
  run_button = gr.Button("Run", scale=0, variant="primary")
245
  result = gr.Image(label="Result", show_label=False)
246
  with gr.Accordion("Advanced Settings", open=False):
 
247
  negative_prompt = gr.Text(
248
  label="Negative prompt",
249
  max_lines=1,
@@ -310,6 +325,7 @@ with gr.Blocks(css=css) as demo:
310
  height,
311
  guidance_scale,
312
  num_inference_steps,
 
313
  ],
314
  outputs=[result, seed, image_path_output, expanded_prompt_output],
315
  )
 
8
  import torch
9
  from diffusers import StableDiffusion3Pipeline, AutoencoderKL, StableDiffusionXLImg2ImgPipeline, EulerAncestralDiscreteScheduler
10
  from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
11
+ #from threading import Thread
12
+ #from transformers import pipeline
13
  from transformers import T5Tokenizer, T5ForConditionalGeneration
14
  import re
15
  import paramiko
 
118
  height,
119
  guidance_scale,
120
  num_inference_steps,
121
+ latent_file, # Add latents file input
122
  progress=gr.Progress(track_tqdm=True),
123
  ):
124
  seed = random.randint(0, MAX_SEED)
 
155
  enhanced_prompt = filter_text(enhanced_prompt,prompt)
156
  print('-- filtered prompt --')
157
  print(enhanced_prompt)
158
+ if latent_file: # Check if a latent file is provided
159
+ sd_image_a = torch.load(latent_file.name) # Load the latent
160
+ print("-- using latent file --")
161
  print('-- generating image --')
162
  with torch.no_grad():
163
  sd_image = pipe(
 
173
  image_path = f"sd35m_{seed}.png"
174
  sd_image.save(image_path,optimize=False,compress_level=0)
175
  upload_to_ftp(image_path)
176
+
177
+ # Convert the generated image to a tensor
178
+ generated_image_tensor = torch.tensor([np.array(sd_image).transpose(2, 0, 1)]).to('cuda') / 255.0
179
+ # Encode the generated image into latents
180
+ with torch.no_grad():
181
+ generated_latents = vae.encode(generated_image_tensor).latent_dist.sample().mul_(0.18215)
182
+ latent_path = f"sd35m_{seed}.pt"
183
+ # Save the latents to a .pt file
184
+ torch.save(generated_latents, latent_path)
185
+ upload_to_ftp(latent_path)
186
  #refiner.scheduler.set_timesteps(num_inference_steps,device)
187
  refine = refiner(
188
  prompt=f"{prompt}, high quality masterpiece, complex details",
 
192
  image=sd_image,
193
  generator=generator,
194
  ).images[0]
195
+ refine_path = f"sd35m_refine_{seed}.png"
196
  refine.save(refine_path,optimize=False,compress_level=0)
197
  upload_to_ftp(refine_path)
198
  return refine, seed, refine_path, enhanced_prompt
 
241
  return result, seed, image_path, enhanced_prompt
242
 
243
 
244
+ with gr.Blocks(theme=gr.themes.Origin()) as demo:
245
  with gr.Column(elem_id="col-container"):
246
  gr.Markdown(" # Text-to-Text-to-Image StableDiffusion 3.5 Medium (with refine)")
247
  expanded_prompt_output = gr.Textbox(label="Expanded Prompt", lines=5) # Add this line
 
258
  run_button = gr.Button("Run", scale=0, variant="primary")
259
  result = gr.Image(label="Result", show_label=False)
260
  with gr.Accordion("Advanced Settings", open=False):
261
+ latent_file = gr.File(label="Latents File (optional)") # Add latents file input
262
  negative_prompt = gr.Text(
263
  label="Negative prompt",
264
  max_lines=1,
 
325
  height,
326
  guidance_scale,
327
  num_inference_steps,
328
+ latent_file, # Add latent_file to the inputs
329
  ],
330
  outputs=[result, seed, image_path_output, expanded_prompt_output],
331
  )