Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
import spaces
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
|
|
|
|
|
|
|
| 11 |
|
| 12 |
if torch.cuda.is_available():
|
| 13 |
torch_dtype = torch.bfloat16
|
| 14 |
else:
|
| 15 |
torch_dtype = torch.float32
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
MAX_IMAGE_SIZE = 1024
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
+
import json
|
|
|
|
|
|
|
| 5 |
import torch
|
| 6 |
+
import spaces
|
| 7 |
+
|
| 8 |
+
from diffusers import AutoencoderKL, SD3Transformer2DModel, StableDiffusion3Pipeline
|
| 9 |
+
from diffusers.loaders.single_file_utils import convert_sd3_transformer_checkpoint_to_diffusers
|
| 10 |
+
from huggingface_hub import hf_hub_download
|
| 11 |
+
from transformers import (
|
| 12 |
+
CLIPTextModelWithProjection,
|
| 13 |
+
CLIPTokenizer,
|
| 14 |
+
T5EncoderModel,
|
| 15 |
+
T5Tokenizer
|
| 16 |
+
)
|
| 17 |
+
from accelerate import (
|
| 18 |
+
init_empty_weights,
|
| 19 |
+
set_module_tensor_to_device,
|
| 20 |
+
infer_auto_device_map,
|
| 21 |
+
load_checkpoint_and_dispatch
|
| 22 |
+
)
|
| 23 |
+
from safetensors import safe_open
|
| 24 |
|
| 25 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
|
| 27 |
+
finetune_repo_id = "DoctorDiffusion/Absynth-2.0"
|
| 28 |
+
finetune_filename = "Absynth_SD3.5L_2.0.safetensors"
|
| 29 |
|
| 30 |
if torch.cuda.is_available():
|
| 31 |
torch_dtype = torch.bfloat16
|
| 32 |
else:
|
| 33 |
torch_dtype = torch.float32
|
| 34 |
|
| 35 |
+
# Initialize models from base SD3.5
|
| 36 |
+
vae = AutoencoderKL.from_pretrained(model_repo_id, subfolder="vae")
|
| 37 |
+
text_encoder = CLIPTextModelWithProjection.from_pretrained(model_repo_id, subfolder="text_encoder")
|
| 38 |
+
text_encoder_2 = CLIPTextModelWithProjection.from_pretrained(model_repo_id, subfolder="text_encoder_2")
|
| 39 |
+
text_encoder_3 = T5EncoderModel.from_pretrained(mdoel_repo_id, subfolder="text_encoder_3")
|
| 40 |
+
tokenizer = CLIPTokenizer.from_pretrained(model_repo_id, subfolder="tokenizer")
|
| 41 |
+
tokenizer_2 = CLIPTokenizer.from_pretrained(model_repo_id, subfolder="tokenizer_2")
|
| 42 |
+
tokenizer_3 = T5Tokenizer.from_pretrained(model_repo_id, subfolder="tokenizer_3")
|
| 43 |
+
|
| 44 |
+
# Initialize transformer
|
| 45 |
+
config_file = hf_hub_download(repo_id=model_repo_id, filename="transformer/config.json")
|
| 46 |
+
with open(config_file, "r") as fp:
|
| 47 |
+
config = json.loads(fp)
|
| 48 |
+
with no_init_weights():
|
| 49 |
+
transformer = SD3Transformer2DModel.from_config(config)
|
| 50 |
+
|
| 51 |
+
# Get transformer state dict and load
|
| 52 |
+
model_file = hf_hub_download(repo_id=finetune_repo_id, filename=finetune_filename)
|
| 53 |
+
state_dict = {}
|
| 54 |
+
with safe_open(model_file, framework="pt") as f:
|
| 55 |
+
for key in f.keys():
|
| 56 |
+
state_dict[key] = f.get_tensor(key)
|
| 57 |
+
|
| 58 |
+
state_dict = convert_sd3_transformer_checkpoint_to_diffusers(state_dict)
|
| 59 |
+
transformer.load_state_dict(state_dict)
|
| 60 |
+
|
| 61 |
+
# Create pipeline from our models
|
| 62 |
+
pipe = StableDiffusion3Pipeline(
|
| 63 |
+
vae=vae,
|
| 64 |
+
text_encoder=text_encoder,
|
| 65 |
+
text_encoder_2=text_encoder_2,
|
| 66 |
+
text_encoder_3=text_encoder_3,
|
| 67 |
+
tokenizer=tokenizer,
|
| 68 |
+
tokenizer_2=tokenizer_2,
|
| 69 |
+
tokenizer_3=tokenizer_3,
|
| 70 |
+
transformer=transformer
|
| 71 |
+
)
|
| 72 |
+
pipe = pipe.to(device, dtype=torch_dtype)
|
| 73 |
+
|
| 74 |
+
# The rest of the code is from the official SD3.5 space
|
| 75 |
|
| 76 |
MAX_SEED = np.iinfo(np.int32).max
|
| 77 |
MAX_IMAGE_SIZE = 1024
|