DonImages commited on
Commit
bd9bf48
Β·
verified Β·
1 Parent(s): 6583c62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -5,30 +5,41 @@ import spaces
5
  import random
6
  from diffusers import StableDiffusion3Pipeline
7
  from diffusers.loaders import SD3LoraLoaderMixin
 
8
 
9
  # Device selection
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
12
 
13
- # Load the Hugging Face token securely
14
  token = os.getenv("HF_TOKEN")
15
 
16
  # Model ID for SD 3.5 Large
17
  model_repo_id = "stabilityai/stable-diffusion-3.5-large"
18
 
19
- # Load Stable Diffusion pipeline once at the start
 
 
 
 
 
 
 
 
 
 
20
  pipeline = StableDiffusion3Pipeline.from_pretrained(
21
  model_repo_id,
22
  torch_dtype=torch_dtype,
23
  use_safetensors=True, # Use safetensors format if supported
24
  ).to(device)
25
 
26
- # Load the LoRA trained weights once at the start
27
- lora_path = "lora_trained_model.safetensors" # Use the correct file name
28
- if os.path.exists(lora_path):
29
  try:
30
- SD3LoraLoaderMixin.load_lora_into_model(pipeline, lora_path) # Correct method
31
- print("βœ… LoRA weights loaded successfully!")
 
32
  except Exception as e:
33
  print(f"❌ Error loading LoRA: {e}")
34
  else:
@@ -37,7 +48,7 @@ else:
37
  # Verify if LoRA is applied
38
  for name, param in pipeline.text_encoder.named_parameters():
39
  if "lora" in name.lower():
40
- print(f"LoRA applied to: {name}, requires_grad={param.requires_grad}")
41
 
42
  # Ensure GPU allocation in Hugging Face Spaces
43
  @spaces.GPU(duration=65)
 
5
  import random
6
  from diffusers import StableDiffusion3Pipeline
7
  from diffusers.loaders import SD3LoraLoaderMixin
8
+ from safetensors.torch import load_file, save_file
9
 
10
  # Device selection
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
13
 
14
+ # Load Hugging Face token securely
15
  token = os.getenv("HF_TOKEN")
16
 
17
  # Model ID for SD 3.5 Large
18
  model_repo_id = "stabilityai/stable-diffusion-3.5-large"
19
 
20
+ # Convert .pt to .safetensors if needed
21
+ lora_pt_path = "lora_trained_model.pt"
22
+ lora_safetensors_path = "lora_trained_model.safetensors"
23
+
24
+ if os.path.exists(lora_pt_path) and not os.path.exists(lora_safetensors_path):
25
+ print("πŸ”„ Converting LoRA .pt to .safetensors...")
26
+ lora_weights = torch.load(lora_pt_path, map_location="cpu")
27
+ save_file(lora_weights, lora_safetensors_path)
28
+ print(f"βœ… LoRA saved as {lora_safetensors_path}")
29
+
30
+ # Load Stable Diffusion pipeline
31
  pipeline = StableDiffusion3Pipeline.from_pretrained(
32
  model_repo_id,
33
  torch_dtype=torch_dtype,
34
  use_safetensors=True, # Use safetensors format if supported
35
  ).to(device)
36
 
37
+ # Load and fuse LoRA trained weights
38
+ if os.path.exists(lora_safetensors_path):
 
39
  try:
40
+ pipeline.load_lora_weights(".", weight_name="lora_trained_model.safetensors") # Corrected loading method
41
+ pipeline.fuse_lora() # Merges LoRA into the base model
42
+ print("βœ… LoRA weights loaded and fused successfully!")
43
  except Exception as e:
44
  print(f"❌ Error loading LoRA: {e}")
45
  else:
 
48
  # Verify if LoRA is applied
49
  for name, param in pipeline.text_encoder.named_parameters():
50
  if "lora" in name.lower():
51
+ print(f"βœ… LoRA applied to: {name}, requires_grad={param.requires_grad}")
52
 
53
  # Ensure GPU allocation in Hugging Face Spaces
54
  @spaces.GPU(duration=65)