File size: 11,722 Bytes
45eb86f
 
 
 
 
 
 
 
 
 
 
 
 
92aaea0
45eb86f
 
92aaea0
 
 
45eb86f
fad8f4c
45eb86f
 
 
 
92aaea0
45eb86f
f1dc6b1
 
 
 
45eb86f
 
 
 
 
 
 
 
 
 
 
92aaea0
45eb86f
 
 
fad8f4c
92aaea0
45eb86f
 
 
 
 
 
 
 
 
 
fad8f4c
92aaea0
45eb86f
 
 
 
92aaea0
fad8f4c
45eb86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd4d606
45eb86f
 
 
 
 
 
 
 
 
 
 
 
 
 
cd4d606
45eb86f
cd4d606
45eb86f
 
 
 
 
 
 
 
 
 
cd4d606
45eb86f
 
cd4d606
45eb86f
 
 
 
 
 
 
 
 
 
fad8f4c
45eb86f
 
 
 
 
fad8f4c
45eb86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fad8f4c
45eb86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fad8f4c
942bdcb
45eb86f
 
 
 
92aaea0
 
45eb86f
 
 
 
 
 
 
 
fad8f4c
45eb86f
fad8f4c
92aaea0
45eb86f
 
 
 
 
 
 
 
 
 
 
 
 
 
fad8f4c
45eb86f
 
 
 
92aaea0
 
 
 
45eb86f
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# app.py โ€” InstantID ร— Beautiful Realistic Asians v7 (ZeroGPU-ready, FastAPI + Gradio)
# 2025-06-21 ็‰ˆ
#
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#  ไธปใช็‰นๅพด
#   โ€ข @spaces.GPU(duration=60) ใ‚’ๅ…ฌ้–‹ๅ generate_core() ใซไป˜ไธŽ
#   โ€ข ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏ lazy-load ใงๅˆๅ›žๆŽจ่ซ–ๆ™‚ใซ GPU ใธใƒญใƒผใƒ‰
#   โ€ข ใƒขใƒ‡ใƒซ่ณ‡็”ฃใฏ /data ใพใŸใฏ ~/.cache ใซๆฐธ็ถšๅŒ–
#   โ€ข Real-ESRGAN ใ‚ขใƒƒใƒ—ใ‚นใ‚ฑใƒผใƒซ (x4 / x8) ใ‚ชใƒ—ใ‚ทใƒงใƒณ
#   โ€ข Gradio UI + FastAPI REST ใ‚’ 1 ใƒ—ใƒญใ‚ปใ‚นใงๅ…ฑๅญ˜
#   โ€ข Uvicorn ๆ‰‹ๅ‹•่ตทๅ‹•ใฏไธ่ฆ๏ผˆSpaces ใŒ่‡ชๅ‰ใง็ซ‹ใฆใ‚‹๏ผ‰
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

import os
import io
import base64
import subprocess
import traceback
from pathlib import Path
from typing import Optional

import numpy as np
import torch
import gradio as gr
import spaces
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from PIL import Image
from huggingface_hub import hf_hub_download
import huggingface_hub as _hf_hub
if not hasattr(_hf_hub, "cached_download"):  # v0.26+ ใซใฏๅญ˜ๅœจใ—ใชใ„
    _hf_hub.cached_download = hf_hub_download

from diffusers import (
    StableDiffusionControlNetPipeline,
    ControlNetModel,
    DPMSolverMultistepScheduler,
    AutoencoderKL,
)
from diffusers.loaders import AttnProcsLayers
from insightface.app import FaceAnalysis
from basicsr.utils.download_util import load_file_from_url
from realesrgan import RealESRGANer

# ==============================================================
# 0. ใ‚ญใƒฃใƒƒใ‚ทใƒฅใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใจใƒ€ใ‚ฆใƒณใƒญใƒผใƒ€
# ==============================================================

PERSIST_BASE = Path("/data")
CACHE_ROOT = (
    PERSIST_BASE / "instantid_cache"
    if PERSIST_BASE.exists() and os.access(PERSIST_BASE, os.W_OK)
    else Path.home() / ".cache" / "instantid_cache"
)
MODELS_DIR = CACHE_ROOT / "models"
LORA_DIR = CACHE_ROOT / "lora"
UPSCALE_DIR = CACHE_ROOT / "realesrgan"
for _p in (MODELS_DIR, LORA_DIR, UPSCALE_DIR):
    _p.mkdir(parents=True, exist_ok=True)


def download(url: str, dst: Path, attempts: int = 2):
    """ๅ˜็ด”ใƒชใƒˆใƒฉใ‚คไป˜ใใƒ€ใ‚ฆใƒณใƒญใƒผใƒ€๏ผˆcurl or basicsr fallback๏ผ‰"""
    if dst.exists():
        return dst
    for i in range(1, attempts + 1):
        try:
            subprocess.check_call(["curl", "-L", "-o", str(dst), url])
            return dst
        except subprocess.CalledProcessError:
            print(f"[DL] Retry {i}/{attempts} failed: {url}")
    # ๆœ€ๅพŒใซ basicsr ใฎใƒ€ใ‚ฆใƒณใƒญใƒผใƒ€ใงใƒ•ใ‚ฉใƒผใƒซใƒใƒƒใ‚ฏ
    load_file_from_url(url=url, model_dir=str(dst.parent), file_name=dst.name)
    return dst


# ==============================================================
# 1. ใƒขใƒ‡ใƒซ URL ๅฎš็พฉ
# ==============================================================

BRA_V7_URL = (
    "https://huggingface.co/i0switch-assets/Beautiful_Realistic_Asians_v7/"
    "resolve/main/beautiful_realistic_asians_v7_fp16.safetensors"
)
IP_ADAPTER_BIN_URL = (
    "https://huggingface.co/h94/IP-Adapter/resolve/main/ip-adapter-plus-face_sd15.bin"
)
IP_ADAPTER_LORA_URL = (
    "https://huggingface.co/h94/IP-Adapter-FaceID/resolve/main/"
    "ip-adapter-faceid-plusv2_sd15_lora.safetensors"
)
REALESRGAN_URL = (
    "https://huggingface.co/aimagelab/realesrgan/resolve/main/RealESRGAN_x4plus.pth"
)

# ==============================================================
# 2. ใ‚ฐใƒญใƒผใƒใƒซๅค‰ๆ•ฐ๏ผˆlazy-load ใ•ใ‚Œใ‚‹๏ผ‰
# ==============================================================

pipe: Optional[StableDiffusionControlNetPipeline] = None
face_analyser: Optional[FaceAnalysis] = None
upsampler: Optional[RealESRGANer] = None

# ==============================================================
# 3. ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณๅˆๆœŸๅŒ–
# ==============================================================


def initialize_pipelines():
    global pipe, face_analyser, upsampler

    if pipe is not None:
        return  # ๆ—ขใซๅˆๆœŸๅŒ–ๆธˆใฟ

    print("[INIT] Downloading model assets โ€ฆ")

    # ---- 3-1. ๅŸบๆœฌใƒขใƒ‡ใƒซ & IP-Adapter ----
    bra_ckpt = download(BRA_V7_URL, MODELS_DIR / "bra_v7.safetensors")
    ip_bin = download(IP_ADAPTER_BIN_URL, MODELS_DIR / "ip_adapter.bin")
    ip_lora = download(IP_ADAPTER_LORA_URL, LORA_DIR / "ip_adapter_faceid.lora")

    # ---- 3-2. ControlNet (InstantID) ----
    controlnet = ControlNetModel.from_pretrained(
        "InstantID/ControlNet-Mediapipe-Face",
        torch_dtype=torch.float16,
        cache_dir=str(MODELS_DIR),
    )

    # ---- 3-3. Diffusers ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ ----
    pipe_local_files_only = {
        "controlnet": controlnet,
        "vae": AutoencoderKL.from_pretrained(
            "stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16
        ),
        "torch_dtype": torch.float16,
        "safety_checker": None,
    }
    pipe_base = "runwayml/stable-diffusion-v1-5"
    pipe_kwargs = dict(
        local_files_only=False,
        cache_dir=str(MODELS_DIR),
        load_safety_checker=False,
    )
    pipe_tmp = StableDiffusionControlNetPipeline.from_pretrained(
        pipe_base, **pipe_local_files_only, **pipe_kwargs
    )
    pipe_tmp.scheduler = DPMSolverMultistepScheduler.from_pretrained(
        pipe_base, subfolder="scheduler", cache_dir=str(MODELS_DIR)
    )
    # LoRA / IP-Adapter
    pipe_tmp.load_ip_adapter(
        ip_bin,
        subfolder=None,
        weight_name=None,
    )
    ip_layers = AttnProcsLayers(pipe_tmp.unet.attn_processors)
    ip_layers.load_lora_weights(ip_lora, adapter_name="ip_faceid", safe_load=True)
    pipe_tmp.set_adapters(["ip_faceid"], adapter_weights=[0.6])
    pipe_tmp.to("cuda")

    pipe = pipe_tmp

    # ---- 3-4. InsightFace ----
    face_analyser = FaceAnalysis(
        name="buffalo_l", root=str(MODELS_DIR), providers=["CUDAExecutionProvider"]
    )
    face_analyser.prepare(ctx_id=0, det_size=(640, 640))

    # ---- 3-5. Real-ESRGAN ----
    esrgan_ckpt = download(REALESRGAN_URL, UPSCALE_DIR / "realesrgan_x4plus.pth")
    upsampler = RealESRGANer(
        scale=4,
        model_path=str(esrgan_ckpt),
        half=True,
        tile=512,
        tile_pad=10,
        pre_pad=0,
        gpu_id=0,
    )

    print("[INIT] Pipelines ready.")


# ==============================================================
# 4. ใƒ—ใƒญใƒณใƒ—ใƒˆ่จญๅฎš
# ==============================================================

BASE_PROMPT = (
    "(masterpiece:1.2), best quality, ultra-realistic, 8k, RAW photo, "
    "cinematic lighting, textured skin, "
)
NEG_PROMPT = (
    "verybadimagenegative_v1.3, ng_deepnegative_v1_75t, "
    "(worst quality:2), (low quality:2), lowres, blurry, bad anatomy, "
    "bad hands, extra digits, cropped, watermark, signature"
)

# ==============================================================
# 5. ็”Ÿๆˆใ‚ณใ‚ข้–ขๆ•ฐ๏ผˆGPU ใ‚’ๆŽดใ‚€๏ผ‰
# ==============================================================


@spaces.GPU(duration=60)
def generate_core(
    face_img: Image.Image,
    subject: str,
    add_prompt: str = "",
    add_neg: str = "",
    cfg: float = 7.5,
    ip_scale: float = 0.6,
    steps: int = 30,
    w: int = 768,
    h: int = 768,
    upscale: bool = False,
    up_factor: int = 4,
    progress: gr.Progress = gr.Progress(track_tqdm=True),
):
    """
    ใƒกใ‚คใƒณ็”Ÿๆˆ้–ขๆ•ฐ
    """
    try:
        if pipe is None:
            initialize_pipelines()

        face_np = np.array(face_img)
        face_info = face_analyser.get(face_np)
        if len(face_info) == 0:
            raise ValueError("้ก”ใŒๆคœๅ‡บใงใใพใ›ใ‚“ใงใ—ใŸใ€‚")

        pipe.set_adapters(["ip_faceid"], adapter_weights=[ip_scale])

        prompt = BASE_PROMPT + subject + ", " + add_prompt
        negative = NEG_PROMPT + ", " + add_neg

        result = pipe(
            prompt=prompt,
            negative_prompt=negative,
            num_inference_steps=int(steps),
            guidance_scale=float(cfg),
            image=face_img,
            control_image=None,
            width=int(w),
            height=int(h),
        ).images[0]

        if upscale and upsampler is not None:
            scale = 4 if up_factor == 4 else 8
            upsampler.scale = scale
            result, _ = upsampler.enhance(np.array(result))
            result = Image.fromarray(result)

        return result

    except Exception as e:
        traceback.print_exc()
        raise e


# ==============================================================
# 6. Gradio UI
# ==============================================================

with gr.Blocks(title="InstantID ร— BRA v7 (ZeroGPU)") as demo:
    gr.Markdown("## InstantID ร— Beautiful Realistic Asians v7")
    with gr.Row():
        face_img = gr.Image(type="pil", label="Face ID", sources=["upload"])
        subject = gr.Textbox(
            label="่ขซๅ†™ไฝ“่ชฌๆ˜Ž๏ผˆไพ‹: '30ไปฃๆ—ฅๆœฌไบบๅฅณๆ€งใ€้ป’้ซชใ‚ปใƒŸใƒญใƒณใ‚ฐ'๏ผ‰", interactive=True
        )
    add_prompt = gr.Textbox(label="่ฟฝๅŠ ใƒ—ใƒญใƒณใƒ—ใƒˆ", interactive=True)
    add_neg = gr.Textbox(label="่ฟฝๅŠ ใƒใ‚ฌใƒ†ใ‚ฃใƒ–", interactive=True)
    with gr.Row():
        cfg = gr.Slider(1, 20, value=7.5, step=0.5, label="CFG Scale")
        ip_scale = gr.Slider(0.1, 1.0, value=0.6, step=0.05, label="IP-Adapter Weight")
    with gr.Row():
        steps = gr.Slider(10, 50, value=30, step=1, label="Steps")
        w = gr.Slider(512, 1024, value=768, step=64, label="Width")
        h = gr.Slider(512, 1024, value=768, step=64, label="Height")
    with gr.Row():
        upscale = gr.Checkbox(label="Real-ESRGAN Upscale", value=False)
        up_factor = gr.Radio([4, 8], value=4, label="Upscale Factor")
    run_btn = gr.Button("Generate")

    output_img = gr.Image(type="pil", label="Result")

    run_btn.click(
        fn=generate_core,
        inputs=[
            face_img,
            subject,
            add_prompt,
            add_neg,
            cfg,
            ip_scale,
            steps,
            w,
            h,
            upscale,
            up_factor,
        ],
        outputs=output_img,
        show_progress=True,
    )

# ==============================================================
# 7. FastAPI ใ‚จใƒณใƒ‰ใƒใ‚คใƒณใƒˆ
# ==============================================================

app = FastAPI()


@app.post("/api/generate")
async def api_generate(
    subject: str = Form(...),
    cfg: float = Form(7.5),
    steps: int = Form(30),
    ip_scale: float = Form(0.6),
    w: int = Form(768),
    h: int = Form(768),
    file: UploadFile = File(...),
):
    try:
        img_bytes = await file.read()
        pil = Image.open(io.BytesIO(img_bytes)).convert("RGB")
        res = generate_core(
            face_img=pil,
            subject=subject,
            add_prompt="",
            add_neg="",
            cfg=cfg,
            ip_scale=ip_scale,
            steps=steps,
            w=w,
            h=h,
            upscale=False,
            up_factor=4,
        )
        buf = io.BytesIO()
        res.save(buf, format="PNG")
        b64 = base64.b64encode(buf.getvalue()).decode()
        return {"image": f"data:image/png;base64,{b64}"}
    except Exception as e:
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))


# ==============================================================
# 8. Launch
# ==============================================================

# Spaces ใŒ่‡ชๅ‹•ใง Uvicorn ใ‚’่ตทๅ‹•ใ™ใ‚‹ใŸใ‚ใ€ๆ‰‹ๅ‹•่ตทๅ‹•ใฏไธ่ฆใ€‚
demo.queue(concurrency_count=2).launch(share=False)