Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -85,7 +85,8 @@ def load_models():
|
|
| 85 |
"""Load all models once and cache them globally."""
|
| 86 |
global vae, tokenizer, text_encoder, unet, scheduler, device
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
return
|
| 90 |
|
| 91 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -93,20 +94,30 @@ def load_models():
|
|
| 93 |
|
| 94 |
model_id = "CompVis/stable-diffusion-v1-4"
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
def generate_with_style(
|
|
|
|
| 85 |
"""Load all models once and cache them globally."""
|
| 86 |
global vae, tokenizer, text_encoder, unet, scheduler, device
|
| 87 |
|
| 88 |
+
# Check if already loaded
|
| 89 |
+
if vae is not None and scheduler is not None:
|
| 90 |
return
|
| 91 |
|
| 92 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 94 |
|
| 95 |
model_id = "CompVis/stable-diffusion-v1-4"
|
| 96 |
|
| 97 |
+
try:
|
| 98 |
+
print("Loading models... (this may take a few minutes on CPU)")
|
| 99 |
+
|
| 100 |
+
# Load with float16 on GPU, float32 on CPU
|
| 101 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 102 |
+
|
| 103 |
+
vae = AutoencoderKL.from_pretrained(model_id, subfolder="vae", torch_dtype=dtype).to(device)
|
| 104 |
+
tokenizer = CLIPTokenizer.from_pretrained(model_id, subfolder="tokenizer")
|
| 105 |
+
text_encoder = CLIPTextModel.from_pretrained(model_id, subfolder="text_encoder", torch_dtype=dtype).to(device)
|
| 106 |
+
unet = UNet2DConditionModel.from_pretrained(model_id, subfolder="unet", torch_dtype=dtype).to(device)
|
| 107 |
+
|
| 108 |
+
# Initialize scheduler
|
| 109 |
+
scheduler = LMSDiscreteScheduler(
|
| 110 |
+
beta_start=0.00085,
|
| 111 |
+
beta_end=0.012,
|
| 112 |
+
beta_schedule="scaled_linear",
|
| 113 |
+
num_train_timesteps=1000
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
print("Models loaded successfully!")
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error loading models: {e}")
|
| 120 |
+
raise RuntimeError(f"Failed to load models: {e}")
|
| 121 |
|
| 122 |
|
| 123 |
def generate_with_style(
|