Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,11 @@ 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 |
|
| 10 |
-
#
|
| 11 |
-
os.environ["HF_HUB_OFFLINE"] = "
|
| 12 |
|
| 13 |
# Set device to CPU
|
| 14 |
device = "cpu"
|
|
@@ -22,24 +24,42 @@ try:
|
|
| 22 |
except Exception as e:
|
| 23 |
raise RuntimeError(f"Failed to load InsightFace model: {e}. Ensure network access for initial download.")
|
| 24 |
|
| 25 |
-
# Define paths for preloaded weights
|
| 26 |
-
model_path = "./" # Start with root
|
| 27 |
ip_adapter_path = "./"
|
| 28 |
|
| 29 |
-
# Debug: List files to confirm preloading
|
| 30 |
print("Files in root directory:", os.listdir("."))
|
| 31 |
print("Files in ./unet/ directory:", os.listdir("./unet") if os.path.exists("./unet") else "No ./unet/ directory")
|
| 32 |
|
| 33 |
-
# Check if weights exist
|
| 34 |
kolors_weights = model_path + "diffusion_pytorch_model.safetensors"
|
| 35 |
if not os.path.exists(kolors_weights):
|
| 36 |
-
# Check if it's in ./unet/ as a fallback
|
| 37 |
kolors_weights_unet = "./unet/diffusion_pytorch_model.safetensors"
|
| 38 |
-
if os.path.exists(kolors_weights_unet):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
model_path = "./unet/"
|
| 40 |
kolors_weights = kolors_weights_unet
|
| 41 |
-
else:
|
| 42 |
-
raise FileNotFoundError(f"Kolors model weights not found at {kolors_weights} or {kolors_weights_unet}. Preloading failed. Please check .env configuration.")
|
| 43 |
if not os.path.exists(ip_adapter_path + "ip-adapter.bin"):
|
| 44 |
raise FileNotFoundError(f"IP-Adapter weights not found at {ip_adapter_path}")
|
| 45 |
|
|
|
|
| 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
|
| 13 |
+
os.environ["HF_HUB_OFFLINE"] = "0"
|
| 14 |
|
| 15 |
# Set device to CPU
|
| 16 |
device = "cpu"
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
raise RuntimeError(f"Failed to load InsightFace model: {e}. Ensure network access for initial download.")
|
| 26 |
|
| 27 |
+
# Define paths for preloaded or downloaded weights
|
| 28 |
+
model_path = "./" # Start with root
|
| 29 |
ip_adapter_path = "./"
|
| 30 |
|
| 31 |
+
# Debug: List files to confirm preloading or download
|
| 32 |
print("Files in root directory:", os.listdir("."))
|
| 33 |
print("Files in ./unet/ directory:", os.listdir("./unet") if os.path.exists("./unet") else "No ./unet/ directory")
|
| 34 |
|
| 35 |
+
# Check if weights exist or download them with retry logic
|
| 36 |
kolors_weights = model_path + "diffusion_pytorch_model.safetensors"
|
| 37 |
if not os.path.exists(kolors_weights):
|
|
|
|
| 38 |
kolors_weights_unet = "./unet/diffusion_pytorch_model.safetensors"
|
| 39 |
+
if not os.path.exists(kolors_weights_unet):
|
| 40 |
+
print("Preloading failed. Attempting runtime download with retry...")
|
| 41 |
+
os.makedirs("./unet", exist_ok=True)
|
| 42 |
+
max_retries = 3
|
| 43 |
+
for attempt in range(max_retries):
|
| 44 |
+
try:
|
| 45 |
+
print(f"Download attempt {attempt + 1} of {max_retries}")
|
| 46 |
+
urllib.request.urlretrieve(
|
| 47 |
+
"https://huggingface.co/Kwai-Kolors/Kolors-diffusers/resolve/main/unet/diffusion_pytorch_model.safetensors",
|
| 48 |
+
kolors_weights_unet
|
| 49 |
+
)
|
| 50 |
+
print("Kolors weights downloaded to", kolors_weights_unet)
|
| 51 |
+
model_path = "./unet/"
|
| 52 |
+
kolors_weights = kolors_weights_unet
|
| 53 |
+
break
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Download attempt {attempt + 1} failed: {e}")
|
| 56 |
+
if attempt < max_retries - 1:
|
| 57 |
+
time.sleep(5) # Wait 5 seconds before retrying
|
| 58 |
+
else:
|
| 59 |
+
raise FileNotFoundError(f"Failed to download Kolors weights after {max_retries} attempts: {e}. Check network access or contact support.")
|
| 60 |
+
else:
|
| 61 |
model_path = "./unet/"
|
| 62 |
kolors_weights = kolors_weights_unet
|
|
|
|
|
|
|
| 63 |
if not os.path.exists(ip_adapter_path + "ip-adapter.bin"):
|
| 64 |
raise FileNotFoundError(f"IP-Adapter weights not found at {ip_adapter_path}")
|
| 65 |
|