File size: 18,517 Bytes
9a7a252
 
 
6f708fa
 
54fdda4
9cb2a58
936607f
 
 
 
 
6f708fa
9cb2a58
6f708fa
9a7a252
 
2662f4d
 
 
 
31b8b5e
cdce176
2662f4d
ddd7baa
f351911
 
ddd7baa
f351911
ddd7baa
9cb2a58
 
f351911
 
c261e86
981a12e
2662f4d
 
 
 
 
 
 
 
 
 
 
 
9c98320
5f20534
9c98320
5f20534
2662f4d
 
 
 
 
 
31b8b5e
 
 
83d0be8
2662f4d
 
83d0be8
cdce176
 
2662f4d
cdce176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2662f4d
cdce176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2662f4d
cdce176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2662f4d
cdce176
 
 
2662f4d
cdce176
 
 
2662f4d
cdce176
2662f4d
 
 
 
f1a0bd5
f351911
 
596417c
c73f1c7
2662f4d
 
b63b0f3
d23e956
09f419b
2662f4d
104c1eb
2662f4d
104c1eb
2662f4d
104c1eb
2662f4d
 
 
c261e86
fff9530
2662f4d
9a7a252
2662f4d
104c1eb
 
 
c261e86
104c1eb
2662f4d
5f20534
9c98320
 
f351911
104c1eb
2662f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c98320
6a35169
 
 
2662f4d
 
 
 
 
 
 
 
 
9c98320
2662f4d
 
 
 
 
 
38ea7b1
 
2662f4d
5726d72
2662f4d
5726d72
2662f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c98320
6a35169
 
 
2662f4d
 
 
 
 
 
 
 
 
9c98320
2662f4d
 
 
 
 
 
38ea7b1
 
2662f4d
5726d72
2662f4d
5726d72
2662f4d
 
 
f351911
 
2662f4d
 
 
 
 
 
 
 
 
 
 
 
 
9c98320
2662f4d
 
 
 
 
 
 
 
 
 
 
 
9c98320
2662f4d
 
 
 
 
 
38ea7b1
 
2662f4d
5726d72
2662f4d
5726d72
2662f4d
 
 
 
 
 
 
 
 
 
c261e86
2662f4d
 
 
 
 
 
 
 
 
 
 
f351911
2662f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47667c4
2662f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f351911
 
2662f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import subprocess
subprocess.run(['sh', './spaces.sh'])

import os

os.putenv('PYTORCH_NVML_BASED_CUDA_CHECK','1')
os.putenv('TORCH_LINALG_PREFER_CUSOLVER','1')
alloc_conf_parts = [
    'expandable_segments:True',
    'pinned_use_background_threads:True'  # Specific to pinned memory.
]
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = ','.join(alloc_conf_parts)
os.environ["SAFETENSORS_FAST_GPU"] = "1"
os.putenv('HF_HUB_ENABLE_HF_TRANSFER','1')

import spaces

import gradio as gr
import numpy as np
import random



import torch
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")

from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, AutoencoderKL
from transformers import CLIPTextModelWithProjection, T5EncoderModel
from transformers import CLIPTokenizer, T5TokenizerFast

import re
import paramiko
import urllib
import time
from image_gen_aux import UpscaleWithModel
from huggingface_hub import hf_hub_download
import datetime
import cyper
from PIL import Image

#from accelerate import Accelerator

#accelerator = Accelerator(mixed_precision="bf16")

hftoken = os.getenv("HF_AUTH_TOKEN") 

code = r'''
import torch
import paramiko
import os
import socket
import threading # NEW IMPORT
import queue     # NEW IMPORT
FTP_HOST = 'noahcohn.com'
FTP_USER = 'ford442'
FTP_PASS = os.getenv("FTP_PASS")
FTP_DIR = 'img.noahcohn.com/stablediff/'
FTP_HOST_FALLBACK = '1ink.us'
FTP_DIR_FALLBACK = 'img.1ink.us/stablediff/'

# --- WORKER FUNCTION FOR THREADING ---
# This function contains the logic to connect to a single host.
# It will be executed by each of our threads.
def connect_worker(host, result_queue):
    """Tries to connect to a single host and puts the successful transport object into the queue."""
    transport = None
    try:
        transport = paramiko.Transport((host, 22))
        # We still use the 5-second timeout for the handshake
        transport.start_client(timeout=5)
        transport.auth_password(username=FTP_USER, password=FTP_PASS)
        
        # If we reach here, the connection was successful.
        # Put the result in the queue for the main thread to use.
        print(f"✅ Connection to {host} succeeded first.")
        result_queue.put(transport)
    except (paramiko.SSHException, socket.timeout, EOFError) as e:
        # This is an expected failure, just print a note.
        print(f"ℹ️ Connection to {host} failed or was too slow: {e}")
        if transport:
            transport.close()
    except Exception as e:
        # Handle any other unexpected errors.
        print(f"❌ Unexpected error connecting to {host}: {e}")
        if transport:
            transport.close()
            
def upload_to_ftp(filename):
    """
    Attempts to connect to two FTP hosts simultaneously and uses the first one that responds.
    It now uses a corresponding directory for the primary and fallback hosts.
    """
    hosts = [FTP_HOST]
    if FTP_HOST_FALLBACK:
        hosts.append(FTP_HOST_FALLBACK)
    
    result_queue = queue.Queue()
    threads = []
    
    print(f"--> Racing connections to {hosts} for uploading {filename}...")
    for host in hosts:
        thread = threading.Thread(target=connect_worker, args=(host, result_queue))
        thread.daemon = True
        thread.start()
        threads.append(thread)
    try:
        winning_transport = result_queue.get(timeout=7)
        
        # --- THIS IS THE NEW LOGIC ---
        # 1. Determine which host won the race.
        winning_host = winning_transport.getpeername()[0]
        
        # 2. Select the correct destination directory based on the winning host.
        #    If the fallback directory isn't specified, it safely defaults to the primary directory.
        if winning_host == FTP_HOST:
            destination_directory = FTP_DIR
        else:
            destination_directory = FTP_DIR_FALLBACK if FTP_DIR_FALLBACK else FTP_DIR
        print(f"--> Proceeding with upload to {winning_host} in directory {destination_directory}...")
        # 3. Construct the full destination path using the selected directory.
        sftp = paramiko.SFTPClient.from_transport(winning_transport)
        destination_path = os.path.join(destination_directory, os.path.basename(filename))
        sftp.put(filename, destination_path)
        
        print(f"✅ Successfully uploaded {filename}.")
        
        sftp.close()
        winning_transport.close()
    except queue.Empty:
        print("❌ Critical Error: Neither FTP host responded in time.")
    except Exception as e:
        print(f"❌ An unexpected error occurred during SFTP operation: {e}")
'''

pyx = cyper.inline(code, fast_indexing=True, directives=dict(boundscheck=False, wraparound=False, language_level=3))

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

#vae=AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16", use_safetensors=True, subfolder='vae',token=True)
vaeX=AutoencoderKL.from_pretrained("ford442/stable-diffusion-3.5-large-fp32", safety_checker=None, use_safetensors=True, subfolder='vae', low_cpu_mem_usage=False, torch_dtype=torch.float32, token=True)
pipe = StableDiffusion3Pipeline.from_pretrained(
    #"stabilityai  #  stable-diffusion-3.5-large",
    "ford442/stable-diffusion-3.5-large-bf16",
    trust_remote_code=True,
    #vae=None,
    #vae=AutoencoderKL.from_pretrained("ford442/stable-diffusion-3.5-large-fp32", use_safetensors=True, subfolder='vae',token=True),
     #scheduler = FlowMatchHeunDiscreteScheduler.from_pretrained('ford442/stable-diffusion-3.5-large-bf16', subfolder='scheduler',token=True),
    #text_encoder=None, #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder', token=True),
    # text_encoder=CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder', token=True),
    #text_encoder_2=None, #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_2',token=True),
  #  text_encoder_2=CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_2',token=True),
    #text_encoder_3=None, #T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True),
  #  text_encoder_3=T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True),
    #tokenizer=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer", token=True),
    #tokenizer_2=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer_2", token=True),
    transformer=None,
    #tokenizer_3=T5TokenizerFast.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=False, use_fast=True, subfolder="tokenizer_3", token=True),
    #torch_dtype=torch.bfloat16,
    use_safetensors=True,
)
#text_encoder=CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder', token=True).to(torch.device("cuda:0"), dtype=torch.bfloat16)
#text_encoder_2=CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_2',token=True).to(torch.device("cuda:0"), dtype=torch.bfloat16)
#text_encoder_3=T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True).to(torch.device("cuda:0"), dtype=torch.bfloat16)
ll_transformer=SD3Transformer2DModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='transformer',token=True).to(torch.device("cuda:0"), dtype=torch.bfloat16)
pipe.transformer=ll_transformer
pipe.load_lora_weights("ford442/sdxl-vae-bf16", weight_name="LoRA/UltraReal.safetensors")

#pipe.to(accelerator.device)
pipe.to(device=device, dtype=torch.bfloat16)

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

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

MAX_IMAGE_SIZE = 4096

@spaces.GPU(duration=70)
def infer_60(
    prompt,
    negative_prompt_1,
    negative_prompt_2,
    negative_prompt_3,
    width,
    height,
    guidance_scale,
    num_inference_steps,
    progress=gr.Progress(track_tqdm=True),
):
    seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device='cuda').manual_seed(seed)
    print('-- generating image --')
    sd_image = pipe(
            prompt=prompt,
            prompt_2=prompt,
            prompt_3=prompt,
            negative_prompt=negative_prompt_1,
            negative_prompt_2=negative_prompt_2,
            negative_prompt_3=negative_prompt_3,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
            generator=generator,
            max_sequence_length=512
    ).images[0]
    print('-- got image --')
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    sd35_path = f"sd35ll_{timestamp}.png"
    sd_image.save(sd35_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(sd35_path)
    with torch.no_grad():
        upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
        upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
    print('-- got upscaled image --')
    downscale = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
    upscale_path = f"sd35ll_upscale_{timestamp}.png"
    downscale.save(upscale_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(upscale_path)
    return sd_image, prompt

@spaces.GPU(duration=100)
def infer_90(
    prompt,
    negative_prompt_1,
    negative_prompt_2,
    negative_prompt_3,
    width,
    height,
    guidance_scale,
    num_inference_steps,
    progress=gr.Progress(track_tqdm=True),
):
    seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device='cuda').manual_seed(seed)
    print('-- generating image --')
    sd_image = pipe(
            prompt=prompt,
            prompt_2=prompt,
            prompt_3=prompt,
            negative_prompt=negative_prompt_1,
            negative_prompt_2=negative_prompt_2,
            negative_prompt_3=negative_prompt_3,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
            generator=generator,
            max_sequence_length=512
    ).images[0]
    print('-- got image --')
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    sd35_path = f"sd35ll_{timestamp}.png"
    sd_image.save(sd35_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(sd35_path)
    with torch.no_grad():
        upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
        upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
    print('-- got upscaled image --')
    downscale = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
    upscale_path = f"sd35ll_upscale_{timestamp}.png"
    downscale.save(upscale_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(upscale_path)
    return sd_image, prompt

@spaces.GPU(duration=120)
def infer_110(
    prompt,
    negative_prompt_1,
    negative_prompt_2,
    negative_prompt_3,
    width,
    height,
    guidance_scale,
    num_inference_steps,
    progress=gr.Progress(track_tqdm=True),
):
    seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device='cuda').manual_seed(seed)
    print('-- generating image --')
    sd_image = pipe(
            prompt=prompt,
            prompt_2=prompt,
            prompt_3=prompt,
            negative_prompt=negative_prompt_1,
            negative_prompt_2=negative_prompt_2,
            negative_prompt_3=negative_prompt_3,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
            generator=generator,
            max_sequence_length=512
    ).images[0]
    print('-- got image --')
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    sd35_path = f"sd35ll_{timestamp}.png"
    sd_image.save(sd35_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(sd35_path)
    with torch.no_grad():
        upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
        upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
    print('-- got upscaled image --')
    downscale = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
    upscale_path = f"sd35ll_upscale_{timestamp}.png"
    downscale.save(upscale_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(upscale_path)
    return sd_image, prompt
    
css = """
#col-container {margin: 0 auto;max-width: 640px;}
body{background-color: blue;}
"""

with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown(" # StableDiffusion 3.5 Large with UltraReal lora test")
        expanded_prompt_output = gr.Textbox(label="Prompt", lines=1)  # Add this line
        with gr.Row():
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt",
                container=False,
            )
            run_button_60 = gr.Button("Run 60", scale=0, variant="primary")
            run_button_90 = gr.Button("Run 90", scale=0, variant="primary")
            run_button_110 = gr.Button("Run 110", scale=0, variant="primary")
        result = gr.Image(label="Result", show_label=False)
        with gr.Accordion("Advanced Settings", open=True):
            negative_prompt_1 = gr.Text(
                label="Negative prompt 1",
                max_lines=1,
                placeholder="Enter a negative prompt",
                visible=True,
                value="bad anatomy, poorly drawn hands, distorted face, blurry, out of frame, low resolution, grainy, pixelated, disfigured, mutated, extra limbs, bad composition"
            )
            negative_prompt_2 = gr.Text(
                label="Negative prompt 2",
                max_lines=1,
                placeholder="Enter a second negative prompt",
                visible=True,
                value="unrealistic, cartoon, anime, sketch, painting, drawing, illustration, graphic, digital art, render, 3d, blurry, deformed, disfigured, poorly drawn, bad anatomy, mutated, extra limbs, ugly, out of frame, bad composition, low resolution, grainy, pixelated, noisy, oversaturated, undersaturated, (worst quality, low quality:1.3), (bad hands, missing fingers:1.2)"
            )
            negative_prompt_3 = gr.Text(
                label="Negative prompt 3",
                max_lines=1,
                placeholder="Enter a third negative prompt",
                visible=True,
                value="(worst quality, low quality:1.3), (bad anatomy, bad hands, missing fingers, extra digit, fewer digits:1.2), (blurry:1.1), cropped, watermark, text, signature, logo, jpeg artifacts, (ugly, deformed, disfigured:1.2), (poorly drawn:1.2), mutated, extra limbs, (bad proportions, gross proportions:1.2), (malformed limbs, missing arms, missing legs, extra arms, extra legs:1.2), (fused fingers, too many fingers, long neck:1.2), (unnatural body, unnatural pose:1.1), out of frame, (bad composition, poorly composed:1.1), (oversaturated, undersaturated:1.1), (grainy, pixelated:1.1), (low resolution, noisy:1.1), (unrealistic, distorted:1.1), (extra fingers, mutated hands, poorly drawn hands, bad hands:1.3), (missing fingers:1.3)"
            )
            num_iterations = gr.Number(
                value=1000, 
                label="Number of Iterations")
            with gr.Row():
                width = gr.Slider(
                    label="Width",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=32,
                    value=768,
                )
                height = gr.Slider(
                    label="Height",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=32,
                    value=768,
                )
                guidance_scale = gr.Slider(
                    label="Guidance scale",
                    minimum=0.0,
                    maximum=30.0,
                    step=0.1,
                    value=4.2,
                )
                num_inference_steps = gr.Slider(
                    label="Number of inference steps",
                    minimum=1,
                    maximum=500,
                    step=1,
                    value=100,
                )
        gr.on(
        triggers=[run_button_60.click, prompt.submit],
        fn=infer_60,
        inputs=[
            prompt,
            negative_prompt_1,
            negative_prompt_2,
            negative_prompt_3,
            width,
            height,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, expanded_prompt_output],
        )
        gr.on(
        triggers=[run_button_90.click, prompt.submit],
        fn=infer_90,
        inputs=[
            prompt,
            negative_prompt_1,
            negative_prompt_2,
            negative_prompt_3,
            width,
            height,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, expanded_prompt_output],
        )
        gr.on(
        triggers=[run_button_110.click, prompt.submit],
        fn=infer_110,
        inputs=[
            prompt,
            negative_prompt_1,
            negative_prompt_2,
            negative_prompt_3,
            width,
            height,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, expanded_prompt_output],
        )

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