Spaces:
Running
on
Zero
Running
on
Zero
File size: 14,343 Bytes
ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 1348bec ff9a9d1 adb849f ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a 7bfef4d ff9a9d1 7bfef4d ff9a9d1 0b270d8 ff9a9d1 67f9f1a ff9a9d1 7bfef4d ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a 7bfef4d 67f9f1a 7bfef4d 67f9f1a 7bfef4d 67f9f1a 7bfef4d 67f9f1a 7bfef4d ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a ff9a9d1 7bfef4d 67f9f1a 7bfef4d 67f9f1a ff9a9d1 67f9f1a ff9a9d1 67f9f1a 7bfef4d 67f9f1a ff9a9d1 7bfef4d ff9a9d1 |
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 |
import subprocess
subprocess.run(['sh', './spaces.sh'])
import os
# Environment variable setup
os.putenv('PYTORCH_NVML_BASED_CUDA_CHECK','1')
os.putenv('TORCH_LINALG_PREFER_CUSOLVER','1')
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True,pinned_use_background_threads:True'
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 datetime
import threading
import io
# --- New GCS Imports ---
from google.oauth2 import service_account
from google.cloud import storage
import torch
# Torch performance settings
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 PIL import Image
from image_gen_aux import UpscaleWithModel
# --- GCS Configuration ---
# Make sure to set these secrets in your Hugging Face Space settings
GCS_BUCKET_NAME = os.getenv("GCS_BUCKET_NAME")
GCS_SA_KEY = os.getenv("GCS_SA_KEY") # The full JSON key content as a string
# Initialize GCS client if credentials are available
gcs_client = None
if GCS_SA_KEY and GCS_BUCKET_NAME:
try:
credentials_info = eval(GCS_SA_KEY) # Using eval is safe here if you trust the secret source
credentials = service_account.Credentials.from_service_account_info(credentials_info)
gcs_client = storage.Client(credentials=credentials)
print("✅ GCS Client initialized successfully.")
except Exception as e:
print(f"❌ Failed to initialize GCS client: {e}")
def upload_to_gcs(image_object, filename):
if not gcs_client:
print("⚠️ GCS client not initialized. Skipping upload.")
return
try:
print(f"--> Starting GCS upload for {filename}...")
bucket = gcs_client.bucket(GCS_BUCKET_NAME)
blob = bucket.blob(f"stablediff/{filename}")
img_byte_arr = io.BytesIO()
image_object.save(img_byte_arr, format='PNG', optimize=False, compress_level=0)
img_byte_arr = img_byte_arr.getvalue()
blob.upload_from_string(img_byte_arr, content_type='image/png')
print(f"✅ Successfully uploaded {filename} to GCS.")
except Exception as e:
print(f"❌ An error occurred during GCS upload: {e}")
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
pipe = StableDiffusion3Pipeline.from_pretrained(
"ford442/stable-diffusion-3.5-large-bf16",
trust_remote_code=True,
transformer=None, # Load transformer separately
use_safetensors=True
)
ll_transformer=SD3Transformer2DModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='transformer').to(device, dtype=torch.bfloat16)
pipe.transformer=ll_transformer
pipe.load_lora_weights("ford442/sdxl-vae-bf16", weight_name="LoRA/UltraReal.safetensors")
pipe.to(device=device, dtype=torch.bfloat16)
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 4096
@spaces.GPU(duration=45)
def generate_images_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(seed)
print('-- generating image --')
sd_image = pipe(
prompt=prompt, prompt_2=prompt, prompt_3=prompt,
negative_prompt=neg_prompt_1, negative_prompt_2=neg_prompt_2, negative_prompt_3=neg_prompt_3,
guidance_scale=guidance, num_inference_steps=steps,
width=width, height=height, generator=generator,
max_sequence_length=512
).images[0]
print('-- got image --')
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 --')
downscaled_upscale = upscale2.resize((upscale2.width // 4, upscale2.height // 4), Image.LANCZOS)
return sd_image, downscaled_upscale, prompt
@spaces.GPU(duration=70)
def generate_images_60(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(seed)
print('-- generating image --')
sd_image = pipe(
prompt=prompt, prompt_2=prompt, prompt_3=prompt,
negative_prompt=neg_prompt_1, negative_prompt_2=neg_prompt_2, negative_prompt_3=neg_prompt_3,
guidance_scale=guidance, num_inference_steps=steps,
width=width, height=height, generator=generator,
max_sequence_length=512
).images[0]
print('-- got image --')
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 --')
downscaled_upscale = upscale2.resize((upscale2.width // 4, upscale2.height // 4), Image.LANCZOS)
return sd_image, downscaled_upscale, prompt
@spaces.GPU(duration=110)
def generate_images_100(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress=gr.Progress(track_tqdm=True)):
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(seed)
print('-- generating image --')
sd_image = pipe(
prompt=prompt, prompt_2=prompt, prompt_3=prompt,
negative_prompt=neg_prompt_1, negative_prompt_2=neg_prompt_2, negative_prompt_3=neg_prompt_3,
guidance_scale=guidance, num_inference_steps=steps,
width=width, height=height, generator=generator,
max_sequence_length=512
).images[0]
print('-- got image --')
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 --')
downscaled_upscale = upscale2.resize((upscale2.width // 4, upscale2.height // 4), Image.LANCZOS)
return sd_image, downscaled_upscale, prompt
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)):
sd_image, upscaled_image, expanded_prompt = generate_images_30(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress)
if save_consent:
print("✅ User consented to save. Preparing uploads...")
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
sd_filename = f"sd35ll_{timestamp}.png"
upscale_filename = f"sd35ll_upscale_{timestamp}.png"
sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
sd_thread.start()
upscale_thread.start()
else:
print("ℹ️ User did not consent to save. Skipping upload.")
return sd_image, expanded_prompt
def run_inference_and_upload_60(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, save_consent, progress=gr.Progress(track_tqdm=True)):
sd_image, upscaled_image, expanded_prompt = generate_images_60(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress)
if save_consent:
print("✅ User consented to save. Preparing uploads...")
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
sd_filename = f"sd35ll_{timestamp}.png"
upscale_filename = f"sd35ll_upscale_{timestamp}.png"
sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
sd_thread.start()
upscale_thread.start()
else:
print("ℹ️ User did not consent to save. Skipping upload.")
return sd_image, expanded_prompt
def run_inference_and_upload_100(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, save_consent, progress=gr.Progress(track_tqdm=True)):
sd_image, upscaled_image, expanded_prompt = generate_images_100(prompt, neg_prompt_1, neg_prompt_2, neg_prompt_3, width, height, guidance, steps, progress)
if save_consent:
print("✅ User consented to save. Preparing uploads...")
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
sd_filename = f"sd35ll_{timestamp}.png"
upscale_filename = f"sd35ll_upscale_{timestamp}.png"
sd_thread = threading.Thread(target=upload_to_gcs, args=(sd_image, sd_filename))
upscale_thread = threading.Thread(target=upload_to_gcs, args=(upscaled_image, upscale_filename))
sd_thread.start()
upscale_thread.start()
else:
print("ℹ️ User did not consent to save. Skipping upload.")
return sd_image, expanded_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)
with gr.Row():
prompt = gr.Text(
label="Prompt", show_label=False, max_lines=1,
placeholder="Enter your prompt", container=False,
)
run_button_30 = gr.Button("Run30", scale=0, variant="primary")
run_button_60 = gr.Button("Run60", scale=0, variant="primary")
run_button_100 = gr.Button("Run100", scale=0, variant="primary")
result = gr.Image(label="Result", show_label=False, type="pil")
save_consent_checkbox = gr.Checkbox(
label="✅ Anonymously upload result to a public gallery",
value=True, # Default to not uploading
info="Check this box to help us by contributing your image."
)
with gr.Accordion("Advanced Settings", open=True):
negative_prompt_1 = gr.Text(label="Negative prompt 1", max_lines=1, placeholder="Enter a negative prompt", 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", 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", 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)")
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)
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="Inference steps", minimum=1, maximum=150, step=1, value=60)
run_button_30.click(
fn=run_inference_and_upload_30,
inputs=[
prompt,
negative_prompt_1,
negative_prompt_2,
negative_prompt_3,
width,
height,
guidance_scale,
num_inference_steps,
save_consent_checkbox # Pass the checkbox value
],
outputs=[result, expanded_prompt_output],
)
run_button_60.click(
fn=run_inference_and_upload_60,
inputs=[
prompt,
negative_prompt_1,
negative_prompt_2,
negative_prompt_3,
width,
height,
guidance_scale,
num_inference_steps,
save_consent_checkbox # Pass the checkbox value
],
outputs=[result, expanded_prompt_output],
)
run_button_100.click(
fn=run_inference_and_upload_100,
inputs=[
prompt,
negative_prompt_1,
negative_prompt_2,
negative_prompt_3,
width,
height,
guidance_scale,
num_inference_steps,
save_consent_checkbox # Pass the checkbox value
],
outputs=[result, expanded_prompt_output],
)
if __name__ == "__main__":
demo.launch() |