|
| 1 | +from beam import endpoint, Image, Output, Volume |
| 2 | + |
| 3 | +VOLUME_PATH = "/instant-id" |
| 4 | + |
| 5 | +image = ( |
| 6 | + Image(python_version="python3.11") |
| 7 | + .add_commands( |
| 8 | + [ |
| 9 | + "apt-get update", |
| 10 | + "apt-get install -y libgl1-mesa-glx libglib2.0-0 build-essential g++ python3-dev wget unzip", |
| 11 | + ] |
| 12 | + ) |
| 13 | + .add_python_packages( |
| 14 | + [ |
| 15 | + "opencv-python==4.9.0.80", |
| 16 | + "transformers==4.37.0", |
| 17 | + "accelerate==0.26.1", |
| 18 | + "insightface==0.7.3", |
| 19 | + "diffusers==0.25.1", |
| 20 | + "onnxruntime==1.16.3", |
| 21 | + "omegaconf==2.3.0", |
| 22 | + "gradio==3.50.2", |
| 23 | + "peft==0.8.2", |
| 24 | + "controlnet-aux==0.0.7", |
| 25 | + "huggingface_hub==0.25.2", |
| 26 | + "gdown", |
| 27 | + ] |
| 28 | + ) |
| 29 | + .add_commands( |
| 30 | + [ |
| 31 | + "git clone https://github.com/zsxkib/InstantID.git /instantid", |
| 32 | + ] |
| 33 | + ) |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +@endpoint( |
| 38 | + name="instant-id", |
| 39 | + cpu=12, |
| 40 | + memory="32Gi", |
| 41 | + gpu="A10G", |
| 42 | + image=image, |
| 43 | + volumes=[Volume(name="instant-id", mount_path=VOLUME_PATH)], |
| 44 | +) |
| 45 | +def generate_image( |
| 46 | + image: str = "https://live-production.wcms.abc-cdn.net.au/a241657894f4d79f0c3ea0705f0f1f07?impolicy=wcms_crop_resize&cropH=1989&cropW=2992&xPos=8&yPos=8&width=862&height=575", |
| 47 | + prompt: str = "film noir style, ink sketch|vector, male man, highly detailed, sharp focus, ultra sharpness, monochrome, high contrast, dramatic shadows, 1940s style, mysterious, cinematic", |
| 48 | +): |
| 49 | + import sys |
| 50 | + |
| 51 | + sys.path.append("/instantid") |
| 52 | + from diffusers.utils import load_image |
| 53 | + from diffusers.models import ControlNetModel |
| 54 | + import cv2 |
| 55 | + import torch |
| 56 | + import numpy as np |
| 57 | + from insightface.app import FaceAnalysis |
| 58 | + import requests |
| 59 | + import uuid |
| 60 | + |
| 61 | + from pipeline_stable_diffusion_xl_instantid import ( |
| 62 | + StableDiffusionXLInstantIDPipeline, |
| 63 | + draw_kps, |
| 64 | + ) |
| 65 | + |
| 66 | + app = FaceAnalysis( |
| 67 | + name="antelopev2", |
| 68 | + root=VOLUME_PATH, |
| 69 | + providers=["CUDAExecutionProvider", "CPUExecutionProvider"], |
| 70 | + ) |
| 71 | + app.prepare(ctx_id=0, det_size=(640, 640)) |
| 72 | + |
| 73 | + face_adapter = f"{VOLUME_PATH}/checkpoints/ip-adapter.bin" |
| 74 | + controlnet_path = f"{VOLUME_PATH}/checkpoints/ControlNetModel/ControlNetModel" |
| 75 | + base_model = f"{VOLUME_PATH}/weights" |
| 76 | + |
| 77 | + controlnet = ControlNetModel.from_pretrained( |
| 78 | + controlnet_path, torch_dtype=torch.float16 |
| 79 | + ) |
| 80 | + |
| 81 | + pipe = StableDiffusionXLInstantIDPipeline.from_pretrained( |
| 82 | + base_model, controlnet=controlnet, torch_dtype=torch.float16 |
| 83 | + ) |
| 84 | + pipe.cuda() |
| 85 | + |
| 86 | + pipe.load_ip_adapter_instantid(face_adapter) |
| 87 | + |
| 88 | + response = requests.get(image) |
| 89 | + response.raise_for_status() |
| 90 | + |
| 91 | + img_path = "/tmp/" + str(uuid.uuid4()) + ".png" |
| 92 | + with open(img_path, "wb") as f: |
| 93 | + f.write(response.content) |
| 94 | + |
| 95 | + face_image = load_image(img_path) |
| 96 | + |
| 97 | + face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR)) |
| 98 | + face_info = sorted( |
| 99 | + face_info, |
| 100 | + key=lambda x: (x["bbox"][2] - x["bbox"][0]) * (x["bbox"][3] - x["bbox"][1]), |
| 101 | + )[-1] |
| 102 | + face_emb = face_info["embedding"] |
| 103 | + face_kps = draw_kps(face_image, face_info["kps"]) |
| 104 | + |
| 105 | + image = pipe( |
| 106 | + prompt, |
| 107 | + image_embeds=face_emb, |
| 108 | + image=face_kps, |
| 109 | + controlnet_conditioning_scale=0.8, |
| 110 | + ip_adapter_scale=0.8, |
| 111 | + ).images[0] |
| 112 | + |
| 113 | + output_path = "/tmp/" + str(uuid.uuid4()) + ".png" |
| 114 | + image.save(output_path) |
| 115 | + output = Output(path=output_path) |
| 116 | + output.save() |
| 117 | + output_url = output.public_url() |
| 118 | + |
| 119 | + return {"output_url": output_url} |
0 commit comments