Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,97 +5,51 @@ import gradio as gr
|
|
| 5 |
from diffusers import StableDiffusionPipeline, DDIMScheduler, IPAdapterFaceIDPipeline
|
| 6 |
from insightface.app import FaceAnalysis
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
-
import os
|
| 9 |
-
import logging
|
| 10 |
-
|
| 11 |
-
# Set up logging
|
| 12 |
-
logging.basicConfig(level=logging.INFO)
|
| 13 |
-
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
device = "cpu"
|
| 16 |
dtype = torch.float32
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
ip_adapter_dir = "./ip_adapter"
|
| 22 |
-
os.makedirs(ip_adapter_dir, exist_ok=True)
|
| 23 |
|
| 24 |
-
# Download IP-Adapter FaceID
|
| 25 |
-
logger.info("Downloading IP-Adapter FaceID weights...")
|
| 26 |
ip_adapter_path = hf_hub_download(
|
| 27 |
repo_id="h94/IP-Adapter",
|
| 28 |
filename="ip-adapter_sd15_faceid.bin",
|
| 29 |
-
subfolder="models"
|
| 30 |
-
local_dir=ip_adapter_dir,
|
| 31 |
-
cache_dir=cache_dir
|
| 32 |
)
|
| 33 |
-
logger.info(f"IP-Adapter FaceID weights downloaded to {ip_adapter_path}")
|
| 34 |
-
|
| 35 |
-
# Initialize InsightFace
|
| 36 |
-
logger.info("Initializing InsightFace...")
|
| 37 |
-
face_app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 38 |
-
face_app.prepare(ctx_id=0, det_size=(320, 320))
|
| 39 |
-
logger.info("InsightFace initialized successfully.")
|
| 40 |
|
| 41 |
-
# Load
|
| 42 |
-
|
| 43 |
-
pipe = IPAdapterFaceIDPipeline.from_pretrained(
|
| 44 |
-
"runwayml/stable-diffusion-v1-5",
|
| 45 |
-
torch_dtype=dtype,
|
| 46 |
-
cache_dir=cache_dir,
|
| 47 |
-
safety_checker=None
|
| 48 |
-
)
|
| 49 |
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
| 50 |
pipe.load_ip_adapter(ip_adapter_path)
|
| 51 |
pipe.to(device)
|
| 52 |
-
logger.info("Stable Diffusion with IP-Adapter FaceID loaded successfully.")
|
| 53 |
-
|
| 54 |
-
# Generation function
|
| 55 |
-
def generate_image(uploaded_image, prompt):
|
| 56 |
-
logger.info("Starting image generation...")
|
| 57 |
-
if uploaded_image is None:
|
| 58 |
-
return "Please upload an image!", None
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
faces = face_app.get(img)
|
| 63 |
-
if not faces:
|
| 64 |
-
logger.warning("No face detected in the uploaded image.")
|
| 65 |
-
return "No face detected!", None
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
face_emb = torch.from_numpy(face_info.normed_embedding).unsqueeze(0).to(device)
|
| 70 |
-
|
| 71 |
-
logger.info(f"Generating image with prompt: {prompt}")
|
| 72 |
-
image = pipe(
|
| 73 |
-
prompt=prompt,
|
| 74 |
-
face_embeds=face_emb,
|
| 75 |
-
num_inference_steps=30,
|
| 76 |
-
guidance_scale=7.5,
|
| 77 |
-
height=512,
|
| 78 |
-
width=512,
|
| 79 |
-
generator=torch.Generator(device="cpu").manual_seed(42)
|
| 80 |
-
).images[0]
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
fn=generate_image,
|
| 88 |
inputs=[
|
| 89 |
-
gr.Image(type="pil", label="Upload
|
| 90 |
-
gr.Textbox(label="Prompt"
|
| 91 |
],
|
| 92 |
outputs=[
|
| 93 |
-
gr.Textbox(label="Status"
|
| 94 |
gr.Image(label="Generated Image")
|
| 95 |
-
]
|
| 96 |
-
title="Face Reference Image Generator",
|
| 97 |
-
description="Upload an image with a face and generate a new image with the face preserved."
|
| 98 |
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
interface.launch()
|
|
|
|
| 5 |
from diffusers import StableDiffusionPipeline, DDIMScheduler, IPAdapterFaceIDPipeline
|
| 6 |
from insightface.app import FaceAnalysis
|
| 7 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
device = "cpu"
|
| 10 |
dtype = torch.float32
|
| 11 |
|
| 12 |
+
# Initialize Face Detection
|
| 13 |
+
face_app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 14 |
+
face_app.prepare(ctx_id=0, det_size=(320, 320))
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Download IP-Adapter FaceID Weights
|
|
|
|
| 17 |
ip_adapter_path = hf_hub_download(
|
| 18 |
repo_id="h94/IP-Adapter",
|
| 19 |
filename="ip-adapter_sd15_faceid.bin",
|
| 20 |
+
subfolder="models"
|
|
|
|
|
|
|
| 21 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Load Pipeline
|
| 24 |
+
pipe = IPAdapterFaceIDPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=dtype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
| 26 |
pipe.load_ip_adapter(ip_adapter_path)
|
| 27 |
pipe.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def generate(upload_image, prompt):
|
| 30 |
+
if upload_image is None:
|
| 31 |
+
return "No Image Uploaded", None
|
| 32 |
+
|
| 33 |
+
img = cv2.cvtColor(np.array(upload_image), cv2.COLOR_RGB2BGR)
|
| 34 |
faces = face_app.get(img)
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
if len(faces) == 0:
|
| 37 |
+
return "No Face Detected", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
face_emb = torch.tensor(faces[0].normed_embedding).unsqueeze(0).to(device)
|
| 40 |
+
result = pipe(prompt=prompt, face_embeds=face_emb, num_inference_steps=30).images[0]
|
| 41 |
+
return "Image Generated Successfully", result
|
| 42 |
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
generate,
|
|
|
|
| 45 |
inputs=[
|
| 46 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 47 |
+
gr.Textbox(label="Prompt")
|
| 48 |
],
|
| 49 |
outputs=[
|
| 50 |
+
gr.Textbox(label="Status"),
|
| 51 |
gr.Image(label="Generated Image")
|
| 52 |
+
]
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
+
demo.launch()
|
|
|