Update app.py
Browse files
app.py
CHANGED
|
@@ -16,70 +16,61 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
| 16 |
torch_dtype=dtype
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
lora_loaded = False
|
| 21 |
lora_path = "./lora"
|
| 22 |
|
| 23 |
-
print("π
|
| 24 |
|
| 25 |
if os.path.exists(lora_path):
|
| 26 |
-
print(f"β
LoRA folder exists: {lora_path}")
|
| 27 |
-
|
| 28 |
-
# List all files in lora folder
|
| 29 |
files = os.listdir(lora_path)
|
| 30 |
print(f"π Files in lora folder: {files}")
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
if os.path.exists(model_path):
|
| 45 |
file_size = os.path.getsize(model_path)
|
| 46 |
-
print(f"β
adapter_model.safetensors exists")
|
| 47 |
print(f"π File size: {file_size:,} bytes ({file_size/1024/1024:.2f} MB)")
|
| 48 |
|
| 49 |
-
#
|
| 50 |
try:
|
| 51 |
-
print("π§
|
| 52 |
with safe_open(model_path, framework="pt") as f:
|
| 53 |
keys = list(f.keys())
|
| 54 |
-
print(f"β
File is
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
# Check tensor shapes
|
| 58 |
-
for key in keys[:3]:
|
| 59 |
-
tensor = f.get_tensor(key)
|
| 60 |
-
print(f" {key}: shape {tensor.shape}, dtype {tensor.dtype}")
|
| 61 |
-
|
| 62 |
-
print("β
Safetensors file is valid!")
|
| 63 |
-
|
| 64 |
except Exception as e:
|
| 65 |
-
print(f"β
|
| 66 |
-
print("π‘
|
| 67 |
-
|
| 68 |
else:
|
| 69 |
-
print("β
|
| 70 |
|
| 71 |
-
# Now try to load with diffusers
|
| 72 |
-
if os.path.exists(config_path) and os.path.exists(model_path):
|
| 73 |
-
try:
|
| 74 |
-
print("π― Attempting to load LoRA weights with diffusers...")
|
| 75 |
-
pipe.load_lora_weights(lora_path)
|
| 76 |
-
lora_loaded = True
|
| 77 |
-
print("β
LoRA weights loaded successfully!")
|
| 78 |
-
except Exception as e:
|
| 79 |
-
print(f"β Failed to load LoRA weights: {e}")
|
| 80 |
-
print("π Running with base model only")
|
| 81 |
-
lora_loaded = False
|
| 82 |
-
|
| 83 |
else:
|
| 84 |
print("β LoRA folder not found")
|
| 85 |
|
|
@@ -109,11 +100,11 @@ def generate(prompt, quality):
|
|
| 109 |
# Build Gradio UI
|
| 110 |
title = "Fine-tuned Stable Diffusion"
|
| 111 |
if lora_loaded:
|
| 112 |
-
title += " with LoRA"
|
| 113 |
description = "β
LoRA weights loaded! Your custom trained model is active."
|
| 114 |
else:
|
| 115 |
title += " (Base Model)"
|
| 116 |
-
description = "β οΈ Running with base model only
|
| 117 |
|
| 118 |
description += "\nChoose 'Fast' for quicker generation or 'High Quality' for better details."
|
| 119 |
|
|
|
|
| 16 |
torch_dtype=dtype
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Try to load LoRA weights with multiple file name options
|
| 20 |
lora_loaded = False
|
| 21 |
lora_path = "./lora"
|
| 22 |
|
| 23 |
+
print("π Looking for LoRA files...")
|
| 24 |
|
| 25 |
if os.path.exists(lora_path):
|
|
|
|
|
|
|
|
|
|
| 26 |
files = os.listdir(lora_path)
|
| 27 |
print(f"π Files in lora folder: {files}")
|
| 28 |
|
| 29 |
+
# List of possible LoRA filenames to try
|
| 30 |
+
possible_names = [
|
| 31 |
+
"adapter_model.safetensors",
|
| 32 |
+
"pytorch_lora_weights.safetensors",
|
| 33 |
+
"lora_weights.safetensors"
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
lora_file_found = None
|
| 37 |
+
for filename in possible_names:
|
| 38 |
+
if filename in files:
|
| 39 |
+
lora_file_found = filename
|
| 40 |
+
print(f"β
Found LoRA file: {filename}")
|
| 41 |
+
break
|
| 42 |
|
| 43 |
+
if lora_file_found:
|
| 44 |
+
model_path = os.path.join(lora_path, lora_file_found)
|
|
|
|
| 45 |
file_size = os.path.getsize(model_path)
|
|
|
|
| 46 |
print(f"π File size: {file_size:,} bytes ({file_size/1024/1024:.2f} MB)")
|
| 47 |
|
| 48 |
+
# Test if the file is readable
|
| 49 |
try:
|
| 50 |
+
print("π§ Testing file integrity...")
|
| 51 |
with safe_open(model_path, framework="pt") as f:
|
| 52 |
keys = list(f.keys())
|
| 53 |
+
print(f"β
File is valid! Found {len(keys)} tensors")
|
| 54 |
+
|
| 55 |
+
# Now try to load it with diffusers
|
| 56 |
+
print("π― Loading with diffusers...")
|
| 57 |
+
|
| 58 |
+
if lora_file_found == "pytorch_lora_weights.safetensors":
|
| 59 |
+
# For pytorch_lora_weights.safetensors, try loading it directly
|
| 60 |
+
pipe.load_lora_weights(lora_path, weight_name=lora_file_found)
|
| 61 |
+
else:
|
| 62 |
+
# For standard naming, load normally
|
| 63 |
+
pipe.load_lora_weights(lora_path)
|
| 64 |
+
|
| 65 |
+
lora_loaded = True
|
| 66 |
+
print("β
LoRA weights loaded successfully!")
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
+
print(f"β Error with {lora_file_found}: {e}")
|
| 70 |
+
print("π‘ File appears to be corrupted or incompatible")
|
|
|
|
| 71 |
else:
|
| 72 |
+
print("β No LoRA files found with expected names")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
else:
|
| 75 |
print("β LoRA folder not found")
|
| 76 |
|
|
|
|
| 100 |
# Build Gradio UI
|
| 101 |
title = "Fine-tuned Stable Diffusion"
|
| 102 |
if lora_loaded:
|
| 103 |
+
title += " with LoRA β¨"
|
| 104 |
description = "β
LoRA weights loaded! Your custom trained model is active."
|
| 105 |
else:
|
| 106 |
title += " (Base Model)"
|
| 107 |
+
description = "β οΈ Running with base model only. Check logs for details."
|
| 108 |
|
| 109 |
description += "\nChoose 'Fast' for quicker generation or 'High Quality' for better details."
|
| 110 |
|