AkashKumarave commited on
Commit
278fdd9
·
verified ·
1 Parent(s): 64d0b1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -6,7 +6,7 @@ from diffusers import StableDiffusionXLPipeline
6
  from insightface.app import FaceAnalysis
7
  from accelerate import init_empty_weights, load_checkpoint_and_dispatch
8
  import os
9
- import urllib.request
10
  import time
11
 
12
  # Allow network access for runtime download
@@ -42,34 +42,51 @@ if not os.path.exists(kolors_weights):
42
  print("Preloading failed. Attempting runtime download with retry...")
43
  os.makedirs("./unet", exist_ok=True)
44
  max_retries = 3
45
- correct_url = "https://huggingface.co/Kwai-Kolors/Kolors/raw/main/unet/diffusion_pytorch_model.fp16.safetensors"
46
  for attempt in range(max_retries):
47
  try:
48
  print(f"Download attempt {attempt + 1} of {max_retries}")
49
- urllib.request.urlretrieve(correct_url, kolors_weights_unet)
 
 
 
 
 
50
  print("Kolors base weights downloaded to", kolors_weights_unet)
51
  model_path = "./unet/"
52
  kolors_weights = kolors_weights_unet
53
  break
54
- except urllib.error.HTTPError as e:
55
- print(f"Download attempt {attempt + 1} failed: HTTP Error {e.code} - {e.reason}")
56
- if attempt < max_retries - 1:
57
- time.sleep(5)
58
- else:
59
- raise FileNotFoundError(f"Failed to download Kolors base weights after {max_retries} attempts: HTTP Error {e.code} - {e.reason}. Verify the URL or contact support.")
60
  except Exception as e:
61
  print(f"Download attempt {attempt + 1} failed: {e}")
62
  if attempt < max_retries - 1:
63
  time.sleep(5)
64
  else:
65
- raise FileNotFoundError(f"Failed to download Kolors base weights after {max_retries} attempts: {e}. Check network access or contact support.")
66
  else:
67
  model_path = "./unet/"
68
  kolors_weights = kolors_weights_unet
69
 
70
- # Check if IP-Adapter weights exist (preloaded)
71
- if not os.path.exists(ip_adapter_path + "ipa-faceid-plus.bin"):
72
- raise FileNotFoundError(f"IP-Adapter weights not found at {ip_adapter_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  # Initialize model with empty weights
75
  with init_empty_weights():
 
6
  from insightface.app import FaceAnalysis
7
  from accelerate import init_empty_weights, load_checkpoint_and_dispatch
8
  import os
9
+ from huggingface_hub import hf_hub_download
10
  import time
11
 
12
  # Allow network access for runtime download
 
42
  print("Preloading failed. Attempting runtime download with retry...")
43
  os.makedirs("./unet", exist_ok=True)
44
  max_retries = 3
 
45
  for attempt in range(max_retries):
46
  try:
47
  print(f"Download attempt {attempt + 1} of {max_retries}")
48
+ hf_hub_download(
49
+ repo_id="Kwai-Kolors/Kolors",
50
+ filename="unet/diffusion_pytorch_model.fp16.safetensors",
51
+ local_dir="./unet",
52
+ local_files_only=False
53
+ )
54
  print("Kolors base weights downloaded to", kolors_weights_unet)
55
  model_path = "./unet/"
56
  kolors_weights = kolors_weights_unet
57
  break
 
 
 
 
 
 
58
  except Exception as e:
59
  print(f"Download attempt {attempt + 1} failed: {e}")
60
  if attempt < max_retries - 1:
61
  time.sleep(5)
62
  else:
63
+ raise FileNotFoundError(f"Failed to download Kolors base weights after {max_retries} attempts: {e}. Verify the repo or contact support.")
64
  else:
65
  model_path = "./unet/"
66
  kolors_weights = kolors_weights_unet
67
 
68
+ # Check if IP-Adapter weights exist or download them
69
+ ip_adapter_weights = ip_adapter_path + "ipa-faceid-plus.bin"
70
+ if not os.path.exists(ip_adapter_weights):
71
+ print("IP-Adapter preloading failed. Attempting runtime download with retry...")
72
+ max_retries = 3
73
+ for attempt in range(max_retries):
74
+ try:
75
+ print(f"IP-Adapter download attempt {attempt + 1} of {max_retries}")
76
+ hf_hub_download(
77
+ repo_id="Kwai-Kolors/Kolors-IP-Adapter-FaceID-Plus",
78
+ filename="ipa-faceid-plus.bin",
79
+ local_dir="./",
80
+ local_files_only=False
81
+ )
82
+ print("IP-Adapter weights downloaded to", ip_adapter_weights)
83
+ break
84
+ except Exception as e:
85
+ print(f"IP-Adapter download attempt {attempt + 1} failed: {e}")
86
+ if attempt < max_retries - 1:
87
+ time.sleep(5)
88
+ else:
89
+ raise FileNotFoundError(f"Failed to download IP-Adapter weights after {max_retries} attempts: {e}. Verify the repo or contact support.")
90
 
91
  # Initialize model with empty weights
92
  with init_empty_weights():