aanchal77 commited on
Commit
5f976a3
Β·
verified Β·
1 Parent(s): 458b28e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -47
app.py CHANGED
@@ -16,70 +16,61 @@ pipe = StableDiffusionPipeline.from_pretrained(
16
  torch_dtype=dtype
17
  )
18
 
19
- # Debug LoRA files
20
  lora_loaded = False
21
  lora_path = "./lora"
22
 
23
- print("πŸ” Debugging LoRA files...")
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
- # Check adapter_config.json
33
- config_path = os.path.join(lora_path, "adapter_config.json")
34
- if os.path.exists(config_path):
35
- print("βœ… adapter_config.json exists")
36
- with open(config_path, 'r') as f:
37
- config_content = f.read()
38
- print(f"πŸ“„ Config content: {config_content[:200]}...")
39
- else:
40
- print("❌ adapter_config.json missing")
 
 
 
 
41
 
42
- # Check adapter_model.safetensors
43
- model_path = os.path.join(lora_path, "adapter_model.safetensors")
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
- # Try to read the safetensors file directly
50
  try:
51
- print("πŸ”§ Attempting to read safetensors file directly...")
52
  with safe_open(model_path, framework="pt") as f:
53
  keys = list(f.keys())
54
- print(f"βœ… File is readable! Found {len(keys)} tensors")
55
- print(f"πŸ“ First few keys: {keys[:5]}")
 
 
 
 
 
 
 
 
 
 
 
 
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"❌ Cannot read safetensors file: {e}")
66
- print("πŸ’‘ This means the file is corrupted or not a valid safetensors file")
67
-
68
  else:
69
- print("❌ adapter_model.safetensors missing")
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 - check logs for LoRA loading issues."
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