Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,8 @@ 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 |
|
| 11 |
-
# Force offline mode for Hugging Face Hub
|
| 12 |
os.environ["HF_HUB_OFFLINE"] = "1"
|
| 13 |
|
| 14 |
# Set device to CPU
|
|
@@ -20,20 +19,9 @@ insightface_model_dir = "/home/user/.insightface/models/buffalo_l"
|
|
| 20 |
os.makedirs(insightface_model_dir, exist_ok=True)
|
| 21 |
buffalo_l_zip = "./buffalo_l.zip"
|
| 22 |
|
| 23 |
-
#
|
| 24 |
if not os.path.exists(buffalo_l_zip):
|
| 25 |
-
|
| 26 |
-
print("Downloading buffalo_l.zip for InsightFace...")
|
| 27 |
-
urllib.request.urlretrieve(
|
| 28 |
-
"https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip",
|
| 29 |
-
buffalo_l_zip
|
| 30 |
-
)
|
| 31 |
-
print("Download completed.")
|
| 32 |
-
except Exception as e:
|
| 33 |
-
print(f"Failed to download buffalo_l.zip: {e}")
|
| 34 |
-
raise RuntimeError("Cannot download buffalo_l.zip. Please ensure network access.")
|
| 35 |
-
|
| 36 |
-
# Extract buffalo_l.zip
|
| 37 |
if os.path.exists(buffalo_l_zip):
|
| 38 |
import zipfile
|
| 39 |
with zipfile.ZipFile(buffalo_l_zip, "r") as zip_ref:
|
|
@@ -48,23 +36,10 @@ face_app.prepare(ctx_id=0, det_size=(480, 480))
|
|
| 48 |
model_path = "./unet/" # Adjusted for preloaded Kolors weights
|
| 49 |
ip_adapter_path = "./"
|
| 50 |
|
| 51 |
-
# Check if
|
| 52 |
kolors_weights = model_path + "diffusion_pytorch_model.safetensors"
|
| 53 |
-
if not os.path.exists(model_path):
|
| 54 |
-
os.makedirs(model_path, exist_ok=True)
|
| 55 |
if not os.path.exists(kolors_weights):
|
| 56 |
-
|
| 57 |
-
print("Downloading Kolors model weights...")
|
| 58 |
-
urllib.request.urlretrieve(
|
| 59 |
-
"https://huggingface.co/Kwai-Kolors/Kolors-diffusers/resolve/main/unet/diffusion_pytorch_model.safetensors",
|
| 60 |
-
kolors_weights
|
| 61 |
-
)
|
| 62 |
-
print("Kolors weights downloaded.")
|
| 63 |
-
except Exception as e:
|
| 64 |
-
print(f"Failed to download Kolors weights: {e}")
|
| 65 |
-
raise RuntimeError("Cannot download Kolors weights. Please ensure network access.")
|
| 66 |
-
|
| 67 |
-
# Check if IP-Adapter weights exist
|
| 68 |
if not os.path.exists(ip_adapter_path + "ip-adapter.bin"):
|
| 69 |
raise FileNotFoundError(f"IP-Adapter weights not found at {ip_adapter_path}")
|
| 70 |
|
|
@@ -97,7 +72,7 @@ def generate_image(uploaded_image, prompt):
|
|
| 97 |
guidance_scale=7.5,
|
| 98 |
height=512,
|
| 99 |
width=512,
|
| 100 |
-
).images[0]
|
| 101 |
return "Image generated successfully!", image
|
| 102 |
except Exception as e:
|
| 103 |
return f"Generation failed: {e}", None
|
|
|
|
| 6 |
from insightface.app import FaceAnalysis
|
| 7 |
from accelerate import init_empty_weights, load_checkpoint_and_dispatch
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
+
# Force offline mode for Hugging Face Hub
|
| 11 |
os.environ["HF_HUB_OFFLINE"] = "1"
|
| 12 |
|
| 13 |
# Set device to CPU
|
|
|
|
| 19 |
os.makedirs(insightface_model_dir, exist_ok=True)
|
| 20 |
buffalo_l_zip = "./buffalo_l.zip"
|
| 21 |
|
| 22 |
+
# Check and extract buffalo_l.zip (runtime download removed due to network issues)
|
| 23 |
if not os.path.exists(buffalo_l_zip):
|
| 24 |
+
raise FileNotFoundError(f"buffalo_l.zip not found at {buffalo_l_zip}. Preloading failed. Please check .env configuration.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if os.path.exists(buffalo_l_zip):
|
| 26 |
import zipfile
|
| 27 |
with zipfile.ZipFile(buffalo_l_zip, "r") as zip_ref:
|
|
|
|
| 36 |
model_path = "./unet/" # Adjusted for preloaded Kolors weights
|
| 37 |
ip_adapter_path = "./"
|
| 38 |
|
| 39 |
+
# Check if weights exist
|
| 40 |
kolors_weights = model_path + "diffusion_pytorch_model.safetensors"
|
|
|
|
|
|
|
| 41 |
if not os.path.exists(kolors_weights):
|
| 42 |
+
raise FileNotFoundError(f"Kolors model weights not found at {kolors_weights}. 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 |
|
|
|
|
| 72 |
guidance_scale=7.5,
|
| 73 |
height=512,
|
| 74 |
width=512,
|
| 75 |
+
).images[0]
|
| 76 |
return "Image generated successfully!", image
|
| 77 |
except Exception as e:
|
| 78 |
return f"Generation failed: {e}", None
|