Update app.py
#5
by
ford442
- opened
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import subprocess
|
| 3 |
subprocess.run(['sh', './spaces.sh'])
|
| 4 |
import spaces
|
|
@@ -35,6 +34,10 @@ import random
|
|
| 35 |
import datetime
|
| 36 |
import threading
|
| 37 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
from google.oauth2 import service_account
|
| 40 |
from google.cloud import storage
|
|
@@ -42,10 +45,15 @@ from google.cloud import storage
|
|
| 42 |
import torch
|
| 43 |
|
| 44 |
@spaces.GPU(required=True)
|
| 45 |
-
def
|
| 46 |
subprocess.run(['sh', './flashattn.sh'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
|
|
|
| 49 |
|
| 50 |
torch.backends.cuda.matmul.allow_tf32 = False
|
| 51 |
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
|
@@ -58,7 +66,6 @@ torch.backends.cuda.preferred_linalg_library="cusolver"
|
|
| 58 |
torch.set_float32_matmul_precision("highest")
|
| 59 |
|
| 60 |
from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, AutoencoderKL
|
| 61 |
-
from PIL import Image
|
| 62 |
from image_gen_aux import UpscaleWithModel
|
| 63 |
|
| 64 |
|
|
@@ -73,23 +80,22 @@ if GCS_SA_KEY and GCS_BUCKET_NAME:
|
|
| 73 |
gcs_client = storage.Client(credentials=credentials)
|
| 74 |
print("✅ GCS Client initialized successfully.")
|
| 75 |
except Exception as e:
|
| 76 |
-
print(f"❌ Failed to initialize GCS client:
|
| 77 |
|
| 78 |
-
def upload_to_gcs(
|
| 79 |
if not gcs_client:
|
| 80 |
print("⚠️ GCS client not initialized. Skipping upload.")
|
| 81 |
return
|
| 82 |
try:
|
| 83 |
-
print(f"--> Starting GCS upload for {
|
| 84 |
bucket = gcs_client.bucket(GCS_BUCKET_NAME)
|
| 85 |
-
blob = bucket.blob(f"stablediff/{
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
blob.upload_from_string(img_byte_arr, content_type='image/png')
|
| 90 |
-
print(f"✅ Successfully uploaded {filename} to GCS.")
|
| 91 |
except Exception as e:
|
| 92 |
-
print(f"❌ An error occurred during GCS upload: {
|
|
|
|
| 93 |
|
| 94 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 95 |
|
|
@@ -136,7 +142,7 @@ class FlashAttentionProcessor(AttnProcessor2_0):
|
|
| 136 |
out = out_reshaped.permute(0, 2, 1, 3).reshape(b, t, c)
|
| 137 |
out = attn.to_out(out)
|
| 138 |
return out
|
| 139 |
-
|
| 140 |
@spaces.GPU(duration=120)
|
| 141 |
def compile_transformer():
|
| 142 |
with spaces.aoti_capture(pipe.transformer) as call:
|
|
@@ -147,7 +153,7 @@ def compile_transformer():
|
|
| 147 |
kwargs=call.kwargs,
|
| 148 |
)
|
| 149 |
return spaces.aoti_compile(exported)
|
| 150 |
-
|
| 151 |
def load_model():
|
| 152 |
pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 153 |
"ford442/stable-diffusion-3.5-large-bf16",
|
|
@@ -162,6 +168,15 @@ def load_model():
|
|
| 162 |
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(device)
|
| 163 |
return pipe, upscaler_2
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
pipe, upscaler_2 = load_model()
|
| 166 |
|
| 167 |
fa_processor = FlashAttentionProcessor()
|
|
@@ -176,123 +191,94 @@ spaces.aoti_apply(compiled_transformer, pipe.transformer)
|
|
| 176 |
MAX_SEED = np.iinfo(np.int32).max
|
| 177 |
MAX_IMAGE_SIZE = 4096
|
| 178 |
|
| 179 |
-
|
| 180 |
-
def
|
| 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 |
-
print('-- generating image --')
|
| 232 |
-
torch.cuda.empty_cache()
|
| 233 |
-
torch.cuda.reset_peak_memory_stats()
|
| 234 |
-
sd_image = pipe(
|
| 235 |
-
prompt=prompt, prompt_2=prompt, prompt_3=prompt,
|
| 236 |
-
negative_prompt=neg_prompt_1, negative_prompt_2=neg_prompt_2, negative_prompt_3=neg_prompt_3,
|
| 237 |
-
guidance_scale=guidance, num_inference_steps=steps,
|
| 238 |
-
width=width, height=height, generator=generator,
|
| 239 |
-
max_sequence_length=384
|
| 240 |
-
).images[0]
|
| 241 |
-
print('-- got image --')
|
| 242 |
-
torch.cuda.empty_cache()
|
| 243 |
-
torch.cuda.reset_peak_memory_stats()
|
| 244 |
-
with torch.no_grad():
|
| 245 |
-
upscale = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 246 |
-
upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
|
| 247 |
-
print('-- got upscaled image --')
|
| 248 |
-
downscaled_upscale = upscale2.resize((upscale2.width // 16, upscale2.height // 16), Image.LANCZOS)
|
| 249 |
-
return sd_image, downscaled_upscale, prompt
|
| 250 |
-
|
| 251 |
-
def run_inference_and_upload_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, save_consent, progress=gr.Progress(track_tqdm=True)):
|
| 252 |
-
sd_image, upscaled_image, expanded_prompt = generate_images_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress)
|
| 253 |
-
if save_consent:
|
| 254 |
-
print("✅ User consented to save. Preparing uploads...")
|
| 255 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 256 |
-
sd_filename = f"sd35ll_{timestamp}.png"
|
| 257 |
-
upscale_filename = f"sd35ll_upscale_{timestamp}.png"
|
| 258 |
-
sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
|
| 259 |
-
upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
|
| 260 |
-
sd_thread.start()
|
| 261 |
-
upscale_thread.start()
|
| 262 |
-
else:
|
| 263 |
-
print("ℹ️ User did not consent to save. Skipping upload.")
|
| 264 |
-
return sd_image, expanded_prompt
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
if save_consent:
|
| 269 |
print("✅ User consented to save. Preparing uploads...")
|
| 270 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 271 |
-
sd_filename = f"sd35ll_{
|
| 272 |
-
upscale_filename = f"sd35ll_upscale_{
|
| 273 |
-
|
| 274 |
-
|
|
|
|
|
|
|
| 275 |
sd_thread.start()
|
| 276 |
upscale_thread.start()
|
| 277 |
else:
|
| 278 |
print("ℹ️ User did not consent to save. Skipping upload.")
|
| 279 |
-
|
|
|
|
|
|
|
| 280 |
|
| 281 |
-
def run_inference_and_upload_110(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, save_consent, progress=gr.Progress(track_tqdm=True)):
|
| 282 |
-
sd_image, upscaled_image, expanded_prompt = generate_images_110(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress)
|
| 283 |
-
if save_consent:
|
| 284 |
-
print("✅ User consented to save. Preparing uploads...")
|
| 285 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 286 |
-
sd_filename = f"sd35ll_{timestamp}.png"
|
| 287 |
-
upscale_filename = f"sd35ll_upscale_{timestamp}.png"
|
| 288 |
-
sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
|
| 289 |
-
upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
|
| 290 |
-
sd_thread.start()
|
| 291 |
-
upscale_thread.start()
|
| 292 |
-
else:
|
| 293 |
-
print("ℹ️ User did not consent to save. Skipping upload.")
|
| 294 |
-
return sd_image, expanded_prompt
|
| 295 |
-
|
| 296 |
css = """
|
| 297 |
#col-container {margin: 0 auto;max-width: 640px;}
|
| 298 |
body{background-color: blue;}
|
|
@@ -309,11 +295,12 @@ with gr.Blocks(theme=gr.themes.Origin(), css=css) as demo:
|
|
| 309 |
)
|
| 310 |
run_button_30 = gr.Button("Run30", scale=0, variant="primary")
|
| 311 |
run_button_60 = gr.Button("Run60", scale=0, variant="primary")
|
| 312 |
-
run_button_110 = gr.Button("
|
| 313 |
-
result
|
|
|
|
| 314 |
save_consent_checkbox = gr.Checkbox(
|
| 315 |
-
label="✅ Anonymously upload result to a public gallery",
|
| 316 |
-
value=True,
|
| 317 |
info="Check this box to help us by contributing your image."
|
| 318 |
)
|
| 319 |
with gr.Accordion("Advanced Settings", open=True):
|
|
@@ -327,50 +314,30 @@ with gr.Blocks(theme=gr.themes.Origin(), css=css) as demo:
|
|
| 327 |
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=30.0, step=0.1, value=4.2)
|
| 328 |
num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=150, step=1, value=60)
|
| 329 |
|
|
|
|
| 330 |
run_button_30.click(
|
| 331 |
-
fn=
|
| 332 |
inputs=[
|
| 333 |
-
prompt,
|
| 334 |
-
|
| 335 |
-
negative_prompt_2,
|
| 336 |
-
negative_prompt_3,
|
| 337 |
-
width,
|
| 338 |
-
height,
|
| 339 |
-
guidance_scale,
|
| 340 |
-
num_inference_steps,
|
| 341 |
-
save_consent_checkbox # Pass the checkbox value
|
| 342 |
],
|
| 343 |
outputs=[result, expanded_prompt_output],
|
| 344 |
)
|
| 345 |
|
| 346 |
run_button_60.click(
|
| 347 |
-
fn=
|
| 348 |
inputs=[
|
| 349 |
-
prompt,
|
| 350 |
-
|
| 351 |
-
negative_prompt_2,
|
| 352 |
-
negative_prompt_3,
|
| 353 |
-
width,
|
| 354 |
-
height,
|
| 355 |
-
guidance_scale,
|
| 356 |
-
num_inference_steps,
|
| 357 |
-
save_consent_checkbox # Pass the checkbox value
|
| 358 |
],
|
| 359 |
outputs=[result, expanded_prompt_output],
|
| 360 |
)
|
| 361 |
|
| 362 |
run_button_110.click(
|
| 363 |
-
fn=
|
| 364 |
inputs=[
|
| 365 |
-
prompt,
|
| 366 |
-
|
| 367 |
-
negative_prompt_2,
|
| 368 |
-
negative_prompt_3,
|
| 369 |
-
width,
|
| 370 |
-
height,
|
| 371 |
-
guidance_scale,
|
| 372 |
-
num_inference_steps,
|
| 373 |
-
save_consent_checkbox # Pass the checkbox value
|
| 374 |
],
|
| 375 |
outputs=[result, expanded_prompt_output],
|
| 376 |
)
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
subprocess.run(['sh', './spaces.sh'])
|
| 3 |
import spaces
|
|
|
|
| 34 |
import datetime
|
| 35 |
import threading
|
| 36 |
import io
|
| 37 |
+
from PIL import Image
|
| 38 |
+
|
| 39 |
+
# For Ultra HDR
|
| 40 |
+
import pillow_ultrahdr
|
| 41 |
|
| 42 |
from google.oauth2 import service_account
|
| 43 |
from google.cloud import storage
|
|
|
|
| 45 |
import torch
|
| 46 |
|
| 47 |
@spaces.GPU(required=True)
|
| 48 |
+
def install_dependencies():
|
| 49 |
subprocess.run(['sh', './flashattn.sh'])
|
| 50 |
+
# Install the UltraHDR library
|
| 51 |
+
print("Installing pillow-ultrahdr...")
|
| 52 |
+
subprocess.run(['pip', 'install', 'pillow-ultrahdr'])
|
| 53 |
+
print("✅ pillow-ultrahdr installed.")
|
| 54 |
|
| 55 |
+
# Install all dependencies
|
| 56 |
+
# install_dependencies()
|
| 57 |
|
| 58 |
torch.backends.cuda.matmul.allow_tf32 = False
|
| 59 |
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
|
|
|
| 66 |
torch.set_float32_matmul_precision("highest")
|
| 67 |
|
| 68 |
from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, AutoencoderKL
|
|
|
|
| 69 |
from image_gen_aux import UpscaleWithModel
|
| 70 |
|
| 71 |
|
|
|
|
| 80 |
gcs_client = storage.Client(credentials=credentials)
|
| 81 |
print("✅ GCS Client initialized successfully.")
|
| 82 |
except Exception as e:
|
| 83 |
+
print(f"❌ Failed to initialize GCS client: ")
|
| 84 |
|
| 85 |
+
def upload_to_gcs(image_bytes, filename):
|
| 86 |
if not gcs_client:
|
| 87 |
print("⚠️ GCS client not initialized. Skipping upload.")
|
| 88 |
return
|
| 89 |
try:
|
| 90 |
+
print(f"--> Starting GCS upload for {}...")
|
| 91 |
bucket = gcs_client.bucket(GCS_BUCKET_NAME)
|
| 92 |
+
blob = bucket.blob(f"stablediff/{}")
|
| 93 |
+
# The image_bytes is already a bytes object, so we can upload it directly
|
| 94 |
+
blob.upload_from_string(image_bytes, content_type='image/jpeg')
|
| 95 |
+
print(f"✅ Successfully uploaded {} to GCS.")
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
+
print(f"❌ An error occurred during GCS upload for {}: {}")
|
| 98 |
+
|
| 99 |
|
| 100 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 101 |
|
|
|
|
| 142 |
out = out_reshaped.permute(0, 2, 1, 3).reshape(b, t, c)
|
| 143 |
out = attn.to_out(out)
|
| 144 |
return out
|
| 145 |
+
|
| 146 |
@spaces.GPU(duration=120)
|
| 147 |
def compile_transformer():
|
| 148 |
with spaces.aoti_capture(pipe.transformer) as call:
|
|
|
|
| 153 |
kwargs=call.kwargs,
|
| 154 |
)
|
| 155 |
return spaces.aoti_compile(exported)
|
| 156 |
+
|
| 157 |
def load_model():
|
| 158 |
pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 159 |
"ford442/stable-diffusion-3.5-large-bf16",
|
|
|
|
| 168 |
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(device)
|
| 169 |
return pipe, upscaler_2
|
| 170 |
|
| 171 |
+
def srgb_to_linear(img_tensor):
|
| 172 |
+
"""Converts a batched sRGB tensor [0, 1] to a linear tensor."""
|
| 173 |
+
# Using the standard sRGB to linear conversion formula
|
| 174 |
+
return torch.where(
|
| 175 |
+
img_tensor <= 0.04045,
|
| 176 |
+
img_tensor / 12.92,
|
| 177 |
+
((img_tensor + 0.055) / 1.055).pow(2.4)
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
pipe, upscaler_2 = load_model()
|
| 181 |
|
| 182 |
fa_processor = FlashAttentionProcessor()
|
|
|
|
| 191 |
MAX_SEED = np.iinfo(np.int32).max
|
| 192 |
MAX_IMAGE_SIZE = 4096
|
| 193 |
|
| 194 |
+
# Consolidated generation function
|
| 195 |
+
def generate_images(duration, prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
|
| 196 |
+
@spaces.GPU(duration=duration)
|
| 197 |
+
def _generate():
|
| 198 |
+
seed = random.randint(0, MAX_SEED)
|
| 199 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 200 |
+
print('-- generating image --')
|
| 201 |
+
torch.cuda.empty_cache()
|
| 202 |
+
torch.cuda.reset_peak_memory_stats()
|
| 203 |
+
|
| 204 |
+
# Generate tensor output in sRGB space
|
| 205 |
+
sd_image_tensor_srgb = pipe(
|
| 206 |
+
prompt=prompt, prompt_2=prompt, prompt_3=prompt,
|
| 207 |
+
negative_prompt=neg_prompt_1, negative_prompt_2=neg_prompt_2, negative_prompt_3=neg_prompt_3,
|
| 208 |
+
guidance_scale=guidance, num_inference_steps=steps,
|
| 209 |
+
width=width, height=height, generator=generator,
|
| 210 |
+
max_sequence_length=384,
|
| 211 |
+
output_type="pt" # Request tensor output
|
| 212 |
+
).images
|
| 213 |
+
|
| 214 |
+
# Convert the sRGB tensor [0,1] to a PIL Image for display and upscaling
|
| 215 |
+
sd_image_pil_srgb = Image.fromarray((sd_image_tensor_srgb.squeeze(0).permute(1, 2, 0).cpu().numpy() * 255).astype(np.uint8))
|
| 216 |
+
print('-- got image --')
|
| 217 |
+
|
| 218 |
+
# --- Upscaling ---
|
| 219 |
+
torch.cuda.empty_cache()
|
| 220 |
+
torch.cuda.reset_peak_memory_stats()
|
| 221 |
+
with torch.no_grad():
|
| 222 |
+
upscale = upscaler_2(sd_image_pil_srgb, tiling=True, tile_width=256, tile_height=256)
|
| 223 |
+
upscale2 = upscaler_2(upscale, tiling=True, tile_width=256, tile_height=256)
|
| 224 |
+
print('-- got upscaled image --')
|
| 225 |
+
|
| 226 |
+
# --- HDR Conversion and Saving ---
|
| 227 |
+
# Convert the original sRGB tensor to linear space
|
| 228 |
+
sd_image_tensor_linear = srgb_to_linear(sd_image_tensor_srgb)
|
| 229 |
+
# Convert the linear tensor to a PIL Image (this will be HDR data)
|
| 230 |
+
sd_image_pil_linear = Image.fromarray((sd_image_tensor_linear.squeeze(0).permute(1, 2, 0).clamp(0, 1).cpu().numpy() * 255).astype(np.uint8))
|
| 231 |
+
|
| 232 |
+
# Save to a bytes buffer as JPEG Ultra HDR
|
| 233 |
+
buffer = io.BytesIO()
|
| 234 |
+
pillow_ultrahdr.save_ultrahdr(
|
| 235 |
+
sdr=sd_image_pil_srgb, # The standard dynamic range image
|
| 236 |
+
hdr=sd_image_pil_linear, # The linear (high dynamic range) image
|
| 237 |
+
outfile=buffer,
|
| 238 |
+
quality=90 # Standard JPEG quality setting
|
| 239 |
+
)
|
| 240 |
+
hdr_image_bytes = buffer.getvalue()
|
| 241 |
+
|
| 242 |
+
# For the upscaled image, we will do the same
|
| 243 |
+
# First convert upscaled PIL image to tensor, normalize to [0,1]
|
| 244 |
+
upscaled_tensor_srgb = torch.from_numpy(np.array(upscale2)).float().to(device) / 255.0
|
| 245 |
+
upscaled_tensor_srgb = upscaled_tensor_srgb.permute(2, 0, 1).unsqueeze(0) # HWC to BCHW
|
| 246 |
+
upscaled_tensor_linear = srgb_to_linear(upscaled_tensor_srgb)
|
| 247 |
+
upscaled_pil_linear = Image.fromarray((upscaled_tensor_linear.squeeze(0).permute(1, 2, 0).clamp(0, 1).cpu().numpy() * 255).astype(np.uint8))
|
| 248 |
+
|
| 249 |
+
upscaled_buffer = io.BytesIO()
|
| 250 |
+
pillow_ultrahdr.save_ultrahdr(sdr=upscale2, hdr=upscaled_pil_linear, outfile=upscaled_buffer, quality=95)
|
| 251 |
+
upscaled_hdr_image_bytes = upscaled_buffer.getvalue()
|
| 252 |
|
| 253 |
+
# Return the sRGB PIL image for display, and the HDR bytes for upload
|
| 254 |
+
return sd_image_pil_srgb, hdr_image_bytes, upscaled_hdr_image_bytes, prompt
|
| 255 |
+
|
| 256 |
+
return _generate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
+
# Consolidated upload function
|
| 259 |
+
def run_inference_and_upload(duration, prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, save_consent, progress=gr.Progress(track_tqdm=True)):
|
| 260 |
+
# Generate images and get both PIL (for display) and bytes (for upload)
|
| 261 |
+
sd_image_pil, sd_hdr_bytes, upscaled_hdr_bytes, expanded_prompt = generate_images(
|
| 262 |
+
duration, prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
if save_consent:
|
| 266 |
print("✅ User consented to save. Preparing uploads...")
|
| 267 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 268 |
+
sd_filename = f"sd35ll_{}.jpg"
|
| 269 |
+
upscale_filename = f"sd35ll_upscale_{}.jpg"
|
| 270 |
+
|
| 271 |
+
# Upload using threading
|
| 272 |
+
sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_hdr_bytes, sd_filename))
|
| 273 |
+
upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_hdr_bytes, upscale_filename))
|
| 274 |
sd_thread.start()
|
| 275 |
upscale_thread.start()
|
| 276 |
else:
|
| 277 |
print("ℹ️ User did not consent to save. Skipping upload.")
|
| 278 |
+
|
| 279 |
+
# Return the standard sRGB PIL image to the Gradio interface for display
|
| 280 |
+
return sd_image_pil, expanded_prompt
|
| 281 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
css = """
|
| 283 |
#col-container {margin: 0 auto;max-width: 640px;}
|
| 284 |
body{background-color: blue;}
|
|
|
|
| 295 |
)
|
| 296 |
run_button_30 = gr.Button("Run30", scale=0, variant="primary")
|
| 297 |
run_button_60 = gr.Button("Run60", scale=0, variant="primary")
|
| 298 |
+
run_button_110 = gr.Button("Run110", scale=0, variant="primary")
|
| 299 |
+
# The result will display the standard PIL image, the HDR is saved/uploaded
|
| 300 |
+
result = gr.Image(label="Result (SDR Preview)", show_label=False, type="pil")
|
| 301 |
save_consent_checkbox = gr.Checkbox(
|
| 302 |
+
label="✅ Anonymously upload result to a public gallery (as JPEG Ultra HDR)",
|
| 303 |
+
value=True,
|
| 304 |
info="Check this box to help us by contributing your image."
|
| 305 |
)
|
| 306 |
with gr.Accordion("Advanced Settings", open=True):
|
|
|
|
| 314 |
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=30.0, step=0.1, value=4.2)
|
| 315 |
num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=150, step=1, value=60)
|
| 316 |
|
| 317 |
+
# Clicks now call the same function with a different duration parameter
|
| 318 |
run_button_30.click(
|
| 319 |
+
fn=lambda *args: run_inference_and_upload(45, *args),
|
| 320 |
inputs=[
|
| 321 |
+
prompt, negative_prompt_1, negative_prompt_2, negative_prompt_3,
|
| 322 |
+
width, height, guidance_scale, num_inference_steps, save_consent_checkbox
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
],
|
| 324 |
outputs=[result, expanded_prompt_output],
|
| 325 |
)
|
| 326 |
|
| 327 |
run_button_60.click(
|
| 328 |
+
fn=lambda *args: run_inference_and_upload(70, *args),
|
| 329 |
inputs=[
|
| 330 |
+
prompt, negative_prompt_1, negative_prompt_2, negative_prompt_3,
|
| 331 |
+
width, height, guidance_scale, num_inference_steps, save_consent_checkbox
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
],
|
| 333 |
outputs=[result, expanded_prompt_output],
|
| 334 |
)
|
| 335 |
|
| 336 |
run_button_110.click(
|
| 337 |
+
fn=lambda *args: run_inference_and_upload(120, *args),
|
| 338 |
inputs=[
|
| 339 |
+
prompt, negative_prompt_1, negative_prompt_2, negative_prompt_3,
|
| 340 |
+
width, height, guidance_scale, num_inference_steps, save_consent_checkbox
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
],
|
| 342 |
outputs=[result, expanded_prompt_output],
|
| 343 |
)
|