File size: 9,231 Bytes
e6e2e14
 
1f23262
f8406a5
a86abeb
 
c2ccaee
 
1f23262
c2ccaee
 
 
 
 
 
 
 
 
 
a86abeb
c2ccaee
e6e2e14
 
 
 
 
 
e011c4f
503fc45
e6e2e14
 
 
 
 
038dec8
 
e6e2e14
 
 
 
 
 
 
 
 
 
1ede5f6
503fc45
3bd0d59
ac01906
271f98b
1ede5f6
 
503fc45
271f98b
 
 
 
 
e6e2e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580ff26
e6e2e14
 
 
 
 
 
 
 
 
 
 
 
 
 
503fc45
 
 
e011c4f
 
e6e2e14
 
a27d48a
e6e2e14
 
 
 
 
 
 
 
 
 
5e8015d
e6e2e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503fc45
 
 
e011c4f
 
e6e2e14
 
a27d48a
e6e2e14
 
 
 
 
 
 
 
 
 
5e8015d
e6e2e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503fc45
 
 
e011c4f
 
e6e2e14
 
a27d48a
e6e2e14
 
 
 
 
 
 
 
 
 
5e8015d
e6e2e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503fc45
e6e2e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503fc45
 
 
 
 
 
 
e6e2e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spaces
import os
import subprocess
#subprocess.run(['sh', './jax.sh'])

'''
subprocess.run(['sh', './conda.sh'])

import sys
conda_prefix = os.path.expanduser("~/miniconda3") 
conda_bin = os.path.join(conda_prefix, "bin")

# Add Conda's bin directory to your PATH
os.environ["PATH"] = conda_bin + os.pathsep + os.environ["PATH"]

# Activate the base environment (adjust if needed)
os.system(f'{conda_bin}/conda init  --all') 
os.system(f'{conda_bin}/conda activate base') 
os.system(f'{conda_bin}/conda install nvidia/label/cudnn-9.3.0::cudnn')
'''

import gradio as gr
import numpy as np
import paramiko
from image_gen_aux import UpscaleWithModel
import cyper
from PIL import Image
os.environ['JAX_PLATFORMS'] = 'cpu'
import random
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"]="1.00"
import keras
import keras_hub
import torch

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

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

upscaler_2 = None # UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(device)
text_to_image = None

@spaces.GPU(duration=60)
def load_model():
    global upscaler_2
    upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(device)
    global text_to_image
    text_to_image = keras_hub.models.StableDiffusion3TextToImage.from_preset(
        "stable_diffusion_3_medium", width=768, height=768, dtype="bfloat16"
    )
    return text_to_image

code = r'''
import paramiko
import os
FTP_HOST = '1ink.us'
FTP_USER = 'ford442'
FTP_PASS = os.getenv("FTP_PASS")
FTP_DIR = '1ink.us/stable_diff/'
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}")
'''

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

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

MAX_IMAGE_SIZE = 4096

@spaces.GPU(duration=40)
def infer_30(
    prompt,
    negative_prompt,
    guidance_scale,
    num_inference_steps,
    progress=gr.Progress(track_tqdm=True),
):
    global text_to_image
    if text_to_image is None:
        text_to_image = load_model()
    os.environ['JAX_PLATFORMS'] = 'gpu'
    os.environ['KERAS_BACKEND'] = 'jax'
    seed = random.randint(0, MAX_SEED)
    sd_image = text_to_image.generate(
        prompt,
        num_steps=num_inference_steps,
        guidance_scale=guidance_scale,
        seed=seed
    )
    print('-- got image --')
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    sd35_path = f"sd3keras_{timestamp}.png"
    sd_image.save(sd35_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(sd35_path)
    with torch.no_grad():
        upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
    print('-- got upscaled image --')
    downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
    upscale_path = f"sd3keras_upscale_{timestamp}.png"
    downscale2.save(upscale_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(upscale_path)
    return sd_image, prompt

@spaces.GPU(duration=70)
def infer_60(
    prompt,
    negative_prompt,
    guidance_scale,
    num_inference_steps,
    progress=gr.Progress(track_tqdm=True),
):
    global text_to_image
    if text_to_image is None:
        text_to_image = load_model()
    os.environ['JAX_PLATFORMS'] = 'gpu'
    os.environ['KERAS_BACKEND'] = 'jax'
    seed = random.randint(0, MAX_SEED)
    sd_image = text_to_image.generate(
        prompt,
        num_steps=num_inference_steps,
        guidance_scale=guidance_scale,
        seed=seed
    )
    print('-- got image --')
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    sd35_path = f"sd3keras_{timestamp}.png"
    sd_image.save(sd35_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(sd35_path)
    with torch.no_grad():
        upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
    print('-- got upscaled image --')
    downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
    upscale_path = f"sd3keras_upscale_{timestamp}.png"
    downscale2.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,
    guidance_scale,
    num_inference_steps,
    progress=gr.Progress(track_tqdm=True),
):
    global text_to_image
    if text_to_image is None:
        text_to_image = load_model()
    os.environ['JAX_PLATFORMS'] = 'gpu'
    os.environ['KERAS_BACKEND'] = 'jax'
    seed = random.randint(0, MAX_SEED)
    sd_image = text_to_image.generate(
        prompt,
        num_steps=num_inference_steps,
        guidance_scale=guidance_scale,
        seed=seed
    )
    print('-- got image --')
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    sd35_path = f"sd3keras_{timestamp}.png"
    sd_image.save(sd35_path,optimize=False,compress_level=0)
    pyx.upload_to_ftp(sd35_path)
    with torch.no_grad():
        upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
    print('-- got upscaled image --')
    downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
    upscale_path = f"sd3keras_upscale_{timestamp}.png"
    downscale2.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 Medium from Keras-hub")
        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,
            )
            load_button = gr.Button("Load model", scale=0, variant="primary")
            run_button_30 = gr.Button("Run 30", scale=0, variant="primary")
            run_button_60 = gr.Button("Run 60", scale=0, variant="primary")
            run_button_90 = gr.Button("Run 90", scale=0, variant="primary")
        result = gr.Image(label="Result", show_label=False)
        with gr.Accordion("Advanced Settings", open=True):
            negative_prompt = gr.Text(
                label="Negative prompt",
                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"
            )
            with gr.Row():
                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=50,
                )
                
        gr.on(
        triggers=[load_button.click],
        fn=load_model,
        inputs=[],
        outputs=[],
        )
        
        gr.on(
        triggers=[run_button_30.click, prompt.submit],
        fn=infer_30,
        inputs=[
            prompt,
            negative_prompt,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, expanded_prompt_output],
        )
        
        gr.on(
        triggers=[run_button_60.click, prompt.submit],
        fn=infer_60,
        inputs=[
            prompt,
            negative_prompt,
            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,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, expanded_prompt_output],
        )

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