Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +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 |
|
| 10 |
# Force offline mode
|
| 11 |
os.environ["HF_HUB_OFFLINE"] = "1"
|
|
@@ -14,19 +15,26 @@ os.environ["HF_HUB_OFFLINE"] = "1"
|
|
| 14 |
device = "cpu"
|
| 15 |
dtype = torch.float32
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
face_app.prepare(ctx_id=0, det_size=(480, 480))
|
| 20 |
|
| 21 |
# Define paths for preloaded weights
|
| 22 |
-
model_path = "./" # Kolors base model
|
| 23 |
-
ip_adapter_path = "./
|
| 24 |
|
| 25 |
# Check if files exist
|
| 26 |
if not os.path.exists(model_path + "diffusion_pytorch_model.safetensors"):
|
| 27 |
raise FileNotFoundError(f"Kolors model weights not found at {model_path}")
|
| 28 |
-
if not os.path.exists(ip_adapter_path
|
| 29 |
-
raise FileNotFoundError(f"IP-Adapter weights
|
| 30 |
|
| 31 |
# Initialize model with empty weights
|
| 32 |
with init_empty_weights():
|
|
@@ -41,13 +49,12 @@ pipe = load_checkpoint_and_dispatch(pipe, model_path, device_map="cpu", offload_
|
|
| 41 |
pipe.load_ip_adapter("h94/IP-Adapter-FaceID-Plus-SDXL", subfolder=None, weight_name="ip-adapter.bin")
|
| 42 |
|
| 43 |
def generate_image(uploaded_image, prompt):
|
| 44 |
-
# Convert Gradio image to OpenCV format
|
| 45 |
img = cv2.cvtColor(np.array(uploaded_image), cv2.COLOR_RGB2BGR)
|
| 46 |
faces = face_app.get(img)
|
| 47 |
if not faces:
|
| 48 |
return "No face detected!", None
|
| 49 |
|
| 50 |
-
face_info = faces[-1]
|
| 51 |
face_emb = face_info["embedding"]
|
| 52 |
|
| 53 |
try:
|
|
@@ -63,17 +70,10 @@ def generate_image(uploaded_image, prompt):
|
|
| 63 |
except Exception as e:
|
| 64 |
return f"Generation failed: {e}", None
|
| 65 |
|
| 66 |
-
# Gradio interface
|
| 67 |
interface = gr.Interface(
|
| 68 |
fn=generate_image,
|
| 69 |
-
inputs=[
|
| 70 |
-
|
| 71 |
-
gr.Textbox(label="Enter Prompt", placeholder="e.g., A photorealistic astronaut in space")
|
| 72 |
-
],
|
| 73 |
-
outputs=[
|
| 74 |
-
gr.Textbox(label="Status"),
|
| 75 |
-
gr.Image(label="Generated Image")
|
| 76 |
-
],
|
| 77 |
title="Face Reference Image Generator (Kolors with IP-Adapter)",
|
| 78 |
description="Upload an image with a face, enter a prompt, and generate a new image preserving the reference face."
|
| 79 |
)
|
|
|
|
| 6 |
from insightface.app import FaceAnalysis
|
| 7 |
from accelerate import init_empty_weights, load_checkpoint_and_dispatch
|
| 8 |
import os
|
| 9 |
+
import zipfile
|
| 10 |
|
| 11 |
# Force offline mode
|
| 12 |
os.environ["HF_HUB_OFFLINE"] = "1"
|
|
|
|
| 15 |
device = "cpu"
|
| 16 |
dtype = torch.float32
|
| 17 |
|
| 18 |
+
# Extract InsightFace model if needed
|
| 19 |
+
insightface_model_dir = "/home/user/.insightface/models/buffalo_l"
|
| 20 |
+
if not os.path.exists(insightface_model_dir):
|
| 21 |
+
os.makedirs(insightface_model_dir, exist_ok=True)
|
| 22 |
+
with zipfile.ZipFile("./buffalo_l.zip", "r") as zip_ref:
|
| 23 |
+
zip_ref.extractall(insightface_model_dir)
|
| 24 |
+
|
| 25 |
+
# Load face encoder with preloaded model
|
| 26 |
+
face_app = FaceAnalysis(providers=["CPUExecutionProvider"], root="/home/user/.insightface/models")
|
| 27 |
face_app.prepare(ctx_id=0, det_size=(480, 480))
|
| 28 |
|
| 29 |
# Define paths for preloaded weights
|
| 30 |
+
model_path = "./" # Kolors base model weights
|
| 31 |
+
ip_adapter_path = "./"
|
| 32 |
|
| 33 |
# Check if files exist
|
| 34 |
if not os.path.exists(model_path + "diffusion_pytorch_model.safetensors"):
|
| 35 |
raise FileNotFoundError(f"Kolors model weights not found at {model_path}")
|
| 36 |
+
if not os.path.exists(ip_adapter_path + "ip-adapter.bin"):
|
| 37 |
+
raise FileNotFoundError(f"IP-Adapter weights not found at {ip_adapter_path}")
|
| 38 |
|
| 39 |
# Initialize model with empty weights
|
| 40 |
with init_empty_weights():
|
|
|
|
| 49 |
pipe.load_ip_adapter("h94/IP-Adapter-FaceID-Plus-SDXL", subfolder=None, weight_name="ip-adapter.bin")
|
| 50 |
|
| 51 |
def generate_image(uploaded_image, prompt):
|
|
|
|
| 52 |
img = cv2.cvtColor(np.array(uploaded_image), cv2.COLOR_RGB2BGR)
|
| 53 |
faces = face_app.get(img)
|
| 54 |
if not faces:
|
| 55 |
return "No face detected!", None
|
| 56 |
|
| 57 |
+
face_info = faces[-1]
|
| 58 |
face_emb = face_info["embedding"]
|
| 59 |
|
| 60 |
try:
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
return f"Generation failed: {e}", None
|
| 72 |
|
|
|
|
| 73 |
interface = gr.Interface(
|
| 74 |
fn=generate_image,
|
| 75 |
+
inputs=[gr.Image(type="pil", label="Upload Reference Image"), gr.Textbox(label="Enter Prompt", placeholder="e.g., A photorealistic astronaut in space")],
|
| 76 |
+
outputs=[gr.Textbox(label="Status"), gr.Image(label="Generated Image")],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
title="Face Reference Image Generator (Kolors with IP-Adapter)",
|
| 78 |
description="Upload an image with a face, enter a prompt, and generate a new image preserving the reference face."
|
| 79 |
)
|