File size: 16,375 Bytes
2fd5bd1
 
 
 
 
 
ab983f2
2fd5bd1
c0cf4b6
 
2fd5bd1
 
 
 
 
 
7be8c80
554fad9
 
 
 
2fd5bd1
 
 
 
 
 
 
 
 
 
 
350b93d
2fd5bd1
9aae468
2fd5bd1
 
8b40745
 
554fad9
 
 
2fd5bd1
 
 
 
 
 
 
 
 
 
 
 
 
c687aa5
2fd5bd1
 
7be8c80
2fd5bd1
 
80c3a22
e3da6fc
2fd5bd1
554fad9
 
 
 
 
 
 
0930495
71141f1
c35010f
2fd5bd1
 
 
 
 
6fcc1a7
10268d7
2fd5bd1
9353b81
2fd5bd1
6136e66
2fd5bd1
6136e66
2fd5bd1
 
 
6fcc1a7
2fd5bd1
6fcc1a7
80c3a22
 
2fd5bd1
 
 
 
 
554fad9
 
 
 
 
2c472d5
 
 
0314275
2fd5bd1
7bd01b2
 
bc534c7
a8057ad
aa94aa2
0cad593
a8057ad
 
aa94aa2
a8057ad
 
0314275
a8057ad
 
 
457b416
 
 
2fd5bd1
 
 
 
f4a1397
2fd5bd1
 
 
 
 
 
 
 
 
7f2f8a9
c0cf4b6
2fd5bd1
 
 
79ebb83
ffdb810
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0cf4b6
e21b5a0
 
 
 
 
 
 
 
 
7be8c80
 
c7abc95
36815aa
554fad9
 
 
 
 
 
 
 
9383295
 
 
c7abc95
6f8cde1
36815aa
 
35eabb3
e3da6fc
35eabb3
c7abc95
 
 
 
 
7291b44
0956914
7291b44
ad1ae32
0956914
c7abc95
ffdb810
 
 
 
9383295
ffdb810
 
 
 
 
 
c7abc95
378493f
c0cf4b6
36815aa
750c1cd
 
c0cf4b6
750c1cd
 
074432c
9353b81
e3da6fc
9353b81
 
 
 
 
e3da6fc
9383295
9353b81
 
2752d4c
2fd5bd1
 
 
 
 
 
 
 
 
 
36815aa
2fd5bd1
36815aa
0c0a153
e7a29cf
2fd5bd1
 
9353b81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7a29cf
2fd5bd1
 
 
 
 
 
 
 
 
 
 
 
7f2f8a9
 
 
 
 
 
 
 
2e254d8
 
2fd5bd1
 
7be8c80
2fd5bd1
 
 
 
 
 
9353b81
 
 
2fd5bd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2679e2
2fd5bd1
aa3e2b2
2fd5bd1
 
 
 
 
 
f4a1397
2fd5bd1
 
 
 
 
 
 
 
 
 
 
 
 
 
7f2f8a9
c0cf4b6
2fd5bd1
f238e99
2fd5bd1
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import spaces
import gradio as gr
import numpy as np
#import tensorrt as trt
import random
import torch
from diffusers import StableDiffusion3Pipeline, AutoencoderKL, StableDiffusionXLImg2ImgPipeline, EulerAncestralDiscreteScheduler 
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
#from threading import Thread
#from transformers import pipeline
from transformers import T5Tokenizer, T5ForConditionalGeneration
import re
import paramiko
import urllib
import time
import os
from image_gen_aux import UpscaleWithModel
from huggingface_hub import hf_hub_download
from models.transformer_sd3 import SD3Transformer2DModel
from pipeline_stable_diffusion_3_ipa import StableDiffusion3Pipeline
from PIL import Image

FTP_HOST = "1ink.us"
FTP_USER = "ford442"
FTP_PASS = "GoogleBez12!"
FTP_DIR = "1ink.us/stable_diff/"  # Remote directory on FTP server

torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
torch.backends.cudnn.allow_tf32 = False
torch.backends.cudnn.deterministic = False
#torch.backends.cudnn.benchmark = False
torch.backends.cuda.preferred_blas_library="cublas"
#torch.backends.cuda.preferred_linalg_library="cusolver"
torch.set_float32_matmul_precision("highest")

hftoken = os.getenv("HF_AUTH_TOKEN") 

image_encoder_path = "google/siglip-so400m-patch14-384"
ipadapter_path = hf_hub_download(repo_id="InstantX/SD3.5-Large-IP-Adapter", filename="ip-adapter.bin")

def upload_to_ftp(filename):
    try:
        transport = paramiko.Transport((FTP_HOST, 22))
        destination_path=FTP_DIR+filename
        transport.connect(username = FTP_USER, password = FTP_PASS)
        sftp = paramiko.SFTPClient.from_transport(transport)
        sftp.put(filename, destination_path)
        sftp.close()
        transport.close()
        print(f"Uploaded {filename} to FTP server")
    except Exception as e:
        print(f"FTP upload error: {e}")

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch_dtype = torch.bfloat16


checkpoint = "microsoft/Phi-3.5-mini-instruct"
#vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16")
#vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16")

transformer = SD3Transformer2DModel.from_pretrained(
    model_path, 
    subfolder="transformer", 
    torch_dtype=torch.bfloat16
)

pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16", transformer=transformer).to(device=torch.device("cuda:0"), dtype=torch.bfloat16)
#pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16").to(torch.device("cuda:0"))
#pipe = StableDiffusion3Pipeline.from_pretrained("ford442/RealVis_Medium_1.0b_bf16", torch_dtype=torch.bfloat16)
#pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium", token=hftoken, torch_dtype=torch.float32, device_map='balanced')

# pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")

#pipe.scheduler.config.requires_aesthetics_score = False
#pipe.enable_model_cpu_offload()
#pipe.to(device)
#pipe.to(device=device, dtype=torch.bfloat16)
#pipe = torch.compile(pipe)
# pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear")

refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("ford442/stable-diffusion-xl-refiner-1.0-bf16", vae=AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16"), use_safetensors=True, requires_aesthetics_score=True).to(device=torch.device("cuda:0"), dtype=torch.bfloat16)
#refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float32, requires_aesthetics_score=True, device_map='balanced')
refiner.scheduler=EulerAncestralDiscreteScheduler.from_config(refiner.scheduler.config, beta_schedule="scaled_linear")
#refiner.enable_model_cpu_offload()

#refiner.scheduler.config.requires_aesthetics_score=False
#refiner.to(device)
#refiner = torch.compile(refiner)
#refiner.scheduler = EulerAncestralDiscreteScheduler.from_config(refiner.scheduler.config, beta_schedule="scaled_linear")
#refiner.scheduler = EulerAncestralDiscreteScheduler.from_config(refiner.scheduler.config)

tokenizer = AutoTokenizer.from_pretrained(checkpoint, add_prefix_space=False, device_map='balanced')
tokenizer.tokenizer_legacy=False
model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map='balanced')
#model = torch.compile(model)

pipe.init_ipadapter(
    ip_adapter_path=ipadapter_path, 
    image_encoder_path=image_encoder_path, 
    nb_token=64, 
)

upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device("cuda:0"))

def filter_text(text,phraseC):
  """Filters out the text up to and including 'Rewritten Prompt:'."""
  phrase = "Rewritten Prompt:"
  phraseB = "rewritten text:"
  pattern = f"(.*?){re.escape(phrase)}(.*)"
  patternB = f"(.*?){re.escape(phraseB)}(.*)"
  #  matchB = re.search(patternB, text)
  matchB = re.search(patternB, text, flags=re.DOTALL)
  if matchB:
        filtered_text = matchB.group(2)
        match = re.search(pattern, filtered_text, flags=re.DOTALL)
        if match:
          filtered_text = match.group(2)
          filtered_text = re.sub(phraseC, "", filtered_text, flags=re.DOTALL)  # Replaces the matched pattern with an empty string
          return filtered_text
        else:
          return filtered_text
  else:
        # Handle the case where no match is found
        return text

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 4096

@spaces.GPU(duration=80)
def infer(
    prompt,
    negative_prompt,
    seed,
    randomize_seed,
    width,
    height,
    guidance_scale,
    num_inference_steps,
    expanded,
    latent_file,  # Add latents file input
    progress=gr.Progress(track_tqdm=True),
):
    seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device='cuda').manual_seed(seed)
    if expanded:
        system_prompt_rewrite = (
        "You are an AI assistant that rewrites image prompts to be more descriptive and detailed."
        )
        user_prompt_rewrite = (
        "Rewrite this prompt to be more descriptive and detailed and only return the rewritten text: "
        )
        user_prompt_rewrite_2 = (
        "Rephrase this scene to have more elaborate details: "
        )
        input_text = f"{system_prompt_rewrite} {user_prompt_rewrite} {prompt}"
        input_text_2 = f"{system_prompt_rewrite} {user_prompt_rewrite_2} {prompt}"
        print("-- got prompt --")
        # Encode the input text and include the attention mask
        encoded_inputs = tokenizer(input_text, return_tensors="pt", return_attention_mask=True)
        encoded_inputs_2 = tokenizer(input_text_2, return_tensors="pt", return_attention_mask=True)
        # Ensure all values are on the correct device
        input_ids = encoded_inputs["input_ids"].to(device)
        input_ids_2 = encoded_inputs_2["input_ids"].to(device)
        attention_mask = encoded_inputs["attention_mask"].to(device)
        attention_mask_2 = encoded_inputs_2["attention_mask"].to(device)
        print("-- tokenize prompt --")
          # Google T5
        #input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
        outputs = model.generate(
            input_ids=input_ids,
            attention_mask=attention_mask,
            max_new_tokens=512,
            temperature=0.2,
            top_p=0.9,
            do_sample=True,
        )
        outputs_2 = model.generate(
            input_ids=input_ids_2,
            attention_mask=attention_mask_2,
            max_new_tokens=65,
            temperature=0.2,
            top_p=0.9,
            do_sample=True,
        )
        # Use the encoded tensor 'text_inputs' here
        enhanced_prompt = tokenizer.decode(outputs[0], skip_special_tokens=True)
        enhanced_prompt_2 = tokenizer.decode(outputs_2[0], skip_special_tokens=True)
        print('-- generated prompt --')
        enhanced_prompt = filter_text(enhanced_prompt,prompt)
        enhanced_prompt_2 = filter_text(enhanced_prompt_2,prompt)
        print('-- filtered prompt --')
        print(enhanced_prompt)
        print('-- filtered prompt 2 --')
        print(enhanced_prompt_2)
    else:
        enhanced_prompt = prompt
        enhanced_prompt_2 = prompt
    if latent_file:  # Check if a latent file is provided
      #  initial_latents = pipe.prepare_latents(
      #      batch_size=1,
      #      num_channels_latents=pipe.transformer.in_channels,
      #      height=pipe.transformer.config.sample_size[0],
       #     width=pipe.transformer.config.sample_size[1],
      #      dtype=pipe.transformer.dtype,
      #      device=pipe.device,
      #      generator=generator,
      #  )
        sd_image_a = Image.open(latent_file.name)
        print("-- using image file --")
        print('-- generating image --')
        #with torch.no_grad():
        result = pipe(
            clip_image=image,
            prompt=prompt,
            ipadapter_scale=scale,
            width=width,
            height=height,
            generator=torch.Generator().manual_seed(seed)
        ).images[0]
        rv_path = f"sd35_{seed}.png"
        sd_image[0].save(rv_path,optimize=False,compress_level=0)
        upload_to_ftp(rv_path)
    else:
        print('-- generating image --')
        #with torch.no_grad():
        sd_image = pipe(
            prompt=prompt,  # This conversion is fine
            prompt_2=enhanced_prompt_2,
            prompt_3=enhanced_prompt,
            negative_prompt=negative_prompt,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
         #   latents=None,
          #  output='latent',
            generator=generator,
            max_sequence_length=512
        ).images[0]
        print('-- got image --')
        sd35_image_image = pipe.vae.decode(sd_image / 0.18215).sample
        sd35_image = sd35_image.cpu().permute(0, 2, 3, 1).float().detach().numpy()
        sd35_image = (sd35_image * 255).round().astype("uint8")
        image_pil = Image.fromarray(sd35_image[0])
        sd35_path = f"sd35_{seed}.png"
        image_pil.save(sd35_path,optimize=False,compress_level=0)
        upload_to_ftp(sd35_path)

    #sd35_path = f"sd35_{seed}.png"
    #sd_image.save(sd35_path,optimize=False,compress_level=0)
    #upload_to_ftp(sd35_path)
        # Convert the generated image to a tensor
    #generated_image_tensor = torch.tensor([np.array(sd_image).transpose(2, 0, 1)]).to('cuda') / 255.0
    # Encode the generated image into latents
    #with torch.no_grad():
    #    generated_latents = pipe.vae.encode(generated_image_tensor.to(torch.bfloat16)).latent_dist.sample().mul_(0.18215)
    #latent_path = f"sd35m_{seed}.pt"
    # Save the latents to a .pt file
    #torch.save(generated_latents, latent_path)
    #upload_to_ftp(latent_path)
    #refiner.scheduler.set_timesteps(num_inference_steps,device)
    refine = refiner(
            prompt=f"{enhanced_prompt_2}, high quality masterpiece, complex details",
            negative_prompt = negative_prompt,
            guidance_scale=7.5,
            num_inference_steps=num_inference_steps,
            image=sd_image,
            generator=generator,
    ).images[0]
    refine_path = f"sd35_refine_{seed}.png"
    refine.save(refine_path,optimize=False,compress_level=0)
    upload_to_ftp(refine_path)
    return refine, seed, enhanced_prompt

examples = [
    "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
    "An astronaut riding a green horse",
    "A delicious ceviche cheesecake slice",
]

css = """
#col-container {
    margin: 0 auto;
    max-width: 640px;
}
body{
  background-color: blue;
}
"""

def repeat_infer(
    prompt,
    negative_prompt,
    seed,
    randomize_seed,
    width,
    height,
    guidance_scale,
    num_inference_steps,
    num_iterations,  # New input for number of iterations
):
    i = 0
    while i < num_iterations:
        time.sleep(700)  # Wait for 10 minutes (600 seconds)
        result, seed, image_path, enhanced_prompt = infer(
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            width,
            height,
            guidance_scale,
            num_inference_steps,
        )
        
        # Optionally, you can add logic here to process the results of each iteration
        # For example, you could display the image, save it with a different name, etc.
        i += 1
    return result, seed, image_path, enhanced_prompt

with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown(" # Text-to-Text-to-Image StableDiffusion 3.5 Medium (with refine)")
        expanded_prompt_output = gr.Textbox(label="Expanded Prompt", lines=5)  # Add this line
        with gr.Row():
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt",
                value="A captivating Christmas scene.",
                container=False,
            )
            options = [True, False]
            expanded = gr.Radio(
                show_label=True,
                container=True,
                interactive=True,
                choices=options,
                value=True,
                label="Use expanded prompt: ",
            )
            run_button = gr.Button("Run", scale=0, variant="primary")
        result = gr.Image(label="Result", show_label=False)
        with gr.Accordion("Advanced Settings", open=False):
            latent_file = gr.File(label="Image File (optional)")  # Add latents file input
            negative_prompt = gr.Text(
                label="Negative prompt",
                max_lines=1,
                placeholder="Enter a negative prompt",
                visible=False,
            )
            num_iterations = gr.Number(
                value=1000, 
                label="Number of Iterations")
            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=0,
            )
            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
            with gr.Row():
                width = gr.Slider(
                    label="Width",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=32,
                    value=768,  # Replace with defaults that work for your model
                )
                height = gr.Slider(
                    label="Height",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=32,
                    value=768,  # Replace with defaults that work for your model
                )
                guidance_scale = gr.Slider(
                    label="Guidance scale",
                    minimum=0.0,
                    maximum=30.0,
                    step=0.1,
                    value=4.2,  # Replace with defaults that work for your model
                )
                num_inference_steps = gr.Slider(
                    label="Number of inference steps",
                    minimum=1,
                    maximum=500,
                    step=1,
                    value=150,  # Replace with defaults that work for your model
                )
            gr.Examples(examples=examples, inputs=[prompt])
        gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            width,
            height,
            guidance_scale,
            num_inference_steps,
            expanded,
            latent_file,  # Add latent_file to the inputs
        ],
        outputs=[result, seed, expanded_prompt_output],
        )

if __name__ == "__main__":
    demo.launch()