Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,20 @@
|
|
| 1 |
-
# app.py โ InstantID ร Beautiful Realistic Asians v7
|
| 2 |
-
# 2025-06-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
import subprocess
|
| 18 |
-
import traceback
|
| 19 |
from pathlib import Path
|
| 20 |
from typing import Optional
|
| 21 |
|
|
@@ -25,10 +24,6 @@ import gradio as gr
|
|
| 25 |
import spaces
|
| 26 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 27 |
from PIL import Image
|
| 28 |
-
from huggingface_hub import hf_hub_download
|
| 29 |
-
import huggingface_hub as _hf_hub
|
| 30 |
-
if not hasattr(_hf_hub, "cached_download"): # v0.26+ ใซใฏๅญๅจใใชใ
|
| 31 |
-
_hf_hub.cached_download = hf_hub_download
|
| 32 |
|
| 33 |
from diffusers import (
|
| 34 |
StableDiffusionControlNetPipeline,
|
|
@@ -41,10 +36,9 @@ from insightface.app import FaceAnalysis
|
|
| 41 |
from basicsr.utils.download_util import load_file_from_url
|
| 42 |
from realesrgan import RealESRGANer
|
| 43 |
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
PERSIST_BASE = Path("/data")
|
| 49 |
CACHE_ROOT = (
|
| 50 |
PERSIST_BASE / "instantid_cache"
|
|
@@ -57,9 +51,30 @@ UPSCALE_DIR = CACHE_ROOT / "realesrgan"
|
|
| 57 |
for _p in (MODELS_DIR, LORA_DIR, UPSCALE_DIR):
|
| 58 |
_p.mkdir(parents=True, exist_ok=True)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
def download(url: str, dst: Path, attempts: int = 2):
|
| 62 |
-
"""ๅ็ดใชใใฉใคไปใใใฆใณใญใผใ๏ผcurl or basicsr fallback๏ผ"""
|
| 63 |
if dst.exists():
|
| 64 |
return dst
|
| 65 |
for i in range(1, attempts + 1):
|
|
@@ -68,104 +83,73 @@ def download(url: str, dst: Path, attempts: int = 2):
|
|
| 68 |
return dst
|
| 69 |
except subprocess.CalledProcessError:
|
| 70 |
print(f"[DL] Retry {i}/{attempts} failed: {url}")
|
| 71 |
-
# ๆๅพใซ basicsr ใฎใใฆใณใญใผใใงใใฉใผใซใใใฏ
|
| 72 |
load_file_from_url(url=url, model_dir=str(dst.parent), file_name=dst.name)
|
| 73 |
return dst
|
| 74 |
|
| 75 |
-
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
# ==============================================================
|
| 79 |
-
|
| 80 |
-
BRA_V7_URL = (
|
| 81 |
-
"https://huggingface.co/i0switch-assets/Beautiful_Realistic_Asians_v7/"
|
| 82 |
-
"resolve/main/beautiful_realistic_asians_v7_fp16.safetensors"
|
| 83 |
-
)
|
| 84 |
-
IP_ADAPTER_BIN_URL = (
|
| 85 |
-
"https://huggingface.co/h94/IP-Adapter/resolve/main/ip-adapter-plus-face_sd15.bin"
|
| 86 |
-
)
|
| 87 |
-
IP_ADAPTER_LORA_URL = (
|
| 88 |
-
"https://huggingface.co/h94/IP-Adapter-FaceID/resolve/main/"
|
| 89 |
-
"ip-adapter-faceid-plusv2_sd15_lora.safetensors"
|
| 90 |
-
)
|
| 91 |
-
REALESRGAN_URL = (
|
| 92 |
-
"https://huggingface.co/aimagelab/realesrgan/resolve/main/RealESRGAN_x4plus.pth"
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
# ==============================================================
|
| 96 |
-
# 2. ใฐใญใผใใซๅคๆฐ๏ผlazy-load ใใใ๏ผ
|
| 97 |
-
# ==============================================================
|
| 98 |
-
|
| 99 |
pipe: Optional[StableDiffusionControlNetPipeline] = None
|
| 100 |
face_analyser: Optional[FaceAnalysis] = None
|
| 101 |
upsampler: Optional[RealESRGANer] = None
|
| 102 |
|
| 103 |
-
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
def initialize_pipelines():
|
|
|
|
| 109 |
global pipe, face_analyser, upsampler
|
| 110 |
-
|
| 111 |
if pipe is not None:
|
| 112 |
-
return
|
| 113 |
|
| 114 |
print("[INIT] Downloading model assets โฆ")
|
| 115 |
|
| 116 |
-
#
|
| 117 |
bra_ckpt = download(BRA_V7_URL, MODELS_DIR / "bra_v7.safetensors")
|
| 118 |
ip_bin = download(IP_ADAPTER_BIN_URL, MODELS_DIR / "ip_adapter.bin")
|
| 119 |
ip_lora = download(IP_ADAPTER_LORA_URL, LORA_DIR / "ip_adapter_faceid.lora")
|
| 120 |
|
| 121 |
-
#
|
| 122 |
controlnet = ControlNetModel.from_pretrained(
|
| 123 |
"InstantID/ControlNet-Mediapipe-Face",
|
| 124 |
torch_dtype=torch.float16,
|
| 125 |
cache_dir=str(MODELS_DIR),
|
| 126 |
)
|
| 127 |
|
| 128 |
-
#
|
| 129 |
-
|
| 130 |
-
"
|
| 131 |
-
|
|
|
|
| 132 |
"stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16
|
| 133 |
),
|
| 134 |
-
|
| 135 |
-
"safety_checker": None,
|
| 136 |
-
}
|
| 137 |
-
pipe_base = "runwayml/stable-diffusion-v1-5"
|
| 138 |
-
pipe_kwargs = dict(
|
| 139 |
-
local_files_only=False,
|
| 140 |
cache_dir=str(MODELS_DIR),
|
| 141 |
-
|
| 142 |
-
)
|
| 143 |
-
pipe_tmp = StableDiffusionControlNetPipeline.from_pretrained(
|
| 144 |
-
pipe_base, **pipe_local_files_only, **pipe_kwargs
|
| 145 |
)
|
| 146 |
pipe_tmp.scheduler = DPMSolverMultistepScheduler.from_pretrained(
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
pipe_tmp.load_ip_adapter(
|
| 151 |
-
ip_bin,
|
| 152 |
-
subfolder=None,
|
| 153 |
-
weight_name=None,
|
| 154 |
)
|
|
|
|
|
|
|
| 155 |
ip_layers = AttnProcsLayers(pipe_tmp.unet.attn_processors)
|
| 156 |
-
ip_layers.load_lora_weights(
|
|
|
|
|
|
|
| 157 |
pipe_tmp.set_adapters(["ip_faceid"], adapter_weights=[0.6])
|
| 158 |
pipe_tmp.to("cuda")
|
| 159 |
|
| 160 |
pipe = pipe_tmp
|
| 161 |
|
| 162 |
-
#
|
| 163 |
face_analyser = FaceAnalysis(
|
| 164 |
name="buffalo_l", root=str(MODELS_DIR), providers=["CUDAExecutionProvider"]
|
| 165 |
)
|
| 166 |
face_analyser.prepare(ctx_id=0, det_size=(640, 640))
|
| 167 |
|
| 168 |
-
#
|
| 169 |
esrgan_ckpt = download(REALESRGAN_URL, UPSCALE_DIR / "realesrgan_x4plus.pth")
|
| 170 |
upsampler = RealESRGANer(
|
| 171 |
scale=4,
|
|
@@ -179,26 +163,22 @@ def initialize_pipelines():
|
|
| 179 |
|
| 180 |
print("[INIT] Pipelines ready.")
|
| 181 |
|
| 182 |
-
|
| 183 |
-
#
|
| 184 |
-
|
| 185 |
-
# ==============================================================
|
| 186 |
-
|
| 187 |
BASE_PROMPT = (
|
| 188 |
-
"(masterpiece:1.2), best quality, ultra-realistic,
|
| 189 |
"cinematic lighting, textured skin, "
|
| 190 |
)
|
| 191 |
NEG_PROMPT = (
|
| 192 |
"verybadimagenegative_v1.3, ng_deepnegative_v1_75t, "
|
| 193 |
"(worst quality:2), (low quality:2), lowres, blurry, bad anatomy, "
|
| 194 |
-
"bad hands, extra digits,
|
| 195 |
)
|
| 196 |
|
| 197 |
-
|
| 198 |
-
#
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
@spaces.GPU(duration=60)
|
| 203 |
def generate_core(
|
| 204 |
face_img: Image.Image,
|
|
@@ -214,17 +194,14 @@ def generate_core(
|
|
| 214 |
up_factor: int = 4,
|
| 215 |
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
| 216 |
):
|
| 217 |
-
"""
|
| 218 |
-
ใกใคใณ็ๆ้ขๆฐ
|
| 219 |
-
"""
|
| 220 |
try:
|
| 221 |
if pipe is None:
|
| 222 |
initialize_pipelines()
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
if len(
|
| 227 |
-
raise ValueError("
|
| 228 |
|
| 229 |
pipe.set_adapters(["ip_faceid"], adapter_weights=[ip_scale])
|
| 230 |
|
|
@@ -243,8 +220,7 @@ def generate_core(
|
|
| 243 |
).images[0]
|
| 244 |
|
| 245 |
if upscale and upsampler is not None:
|
| 246 |
-
scale = 4 if up_factor == 4 else 8
|
| 247 |
-
upsampler.scale = scale
|
| 248 |
result, _ = upsampler.enhance(np.array(result))
|
| 249 |
result = Image.fromarray(result)
|
| 250 |
|
|
@@ -254,17 +230,15 @@ def generate_core(
|
|
| 254 |
traceback.print_exc()
|
| 255 |
raise e
|
| 256 |
|
| 257 |
-
|
| 258 |
-
#
|
| 259 |
-
|
| 260 |
-
# ==============================================================
|
| 261 |
-
|
| 262 |
with gr.Blocks(title="InstantID ร BRA v7 (ZeroGPU)") as demo:
|
| 263 |
gr.Markdown("## InstantID ร Beautiful Realistic Asians v7")
|
| 264 |
with gr.Row():
|
| 265 |
face_img = gr.Image(type="pil", label="Face ID", sources=["upload"])
|
| 266 |
subject = gr.Textbox(
|
| 267 |
-
label="่ขซๅไฝ่ชฌๆ๏ผไพ:
|
| 268 |
)
|
| 269 |
add_prompt = gr.Textbox(label="่ฟฝๅ ใใญใณใใ", interactive=True)
|
| 270 |
add_neg = gr.Textbox(label="่ฟฝๅ ใใฌใใฃใ", interactive=True)
|
|
@@ -279,7 +253,6 @@ with gr.Blocks(title="InstantID ร BRA v7 (ZeroGPU)") as demo:
|
|
| 279 |
upscale = gr.Checkbox(label="Real-ESRGAN Upscale", value=False)
|
| 280 |
up_factor = gr.Radio([4, 8], value=4, label="Upscale Factor")
|
| 281 |
run_btn = gr.Button("Generate")
|
| 282 |
-
|
| 283 |
output_img = gr.Image(type="pil", label="Result")
|
| 284 |
|
| 285 |
run_btn.click(
|
|
@@ -301,13 +274,11 @@ with gr.Blocks(title="InstantID ร BRA v7 (ZeroGPU)") as demo:
|
|
| 301 |
show_progress=True,
|
| 302 |
)
|
| 303 |
|
| 304 |
-
|
| 305 |
-
#
|
| 306 |
-
|
| 307 |
-
|
| 308 |
app = FastAPI()
|
| 309 |
|
| 310 |
-
|
| 311 |
@app.post("/api/generate")
|
| 312 |
async def api_generate(
|
| 313 |
subject: str = Form(...),
|
|
@@ -342,10 +313,7 @@ async def api_generate(
|
|
| 342 |
traceback.print_exc()
|
| 343 |
raise HTTPException(status_code=500, detail=str(e))
|
| 344 |
|
| 345 |
-
|
| 346 |
-
#
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
# Spaces ใ่ชๅใง Uvicorn ใ่ตทๅใใใใใๆๅ่ตทๅใฏไธ่ฆใ
|
| 351 |
-
demo.queue(concurrency_count=2).launch(share=False)
|
|
|
|
| 1 |
+
# app.py โ InstantID ร Beautiful Realistic Asians v7๏ผZeroGPU ๅฏพๅฟใใซ็๏ผ
|
| 2 |
+
# 2025-06-22
|
| 3 |
+
|
| 4 |
+
##############################################################################
|
| 5 |
+
# 0. ๆง API โ ๆฐ API ไบๆใใใ๏ผๅฟ
ใ diffusers ใใๅใซๅฎ่ก๏ผ
|
| 6 |
+
##############################################################################
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
import huggingface_hub as _hf_hub
|
| 9 |
+
|
| 10 |
+
# diffusers-0.27 ใ import ใใ cached_download() ใ v0.28+ ใงใไฝฟใใใใใซๅฅๅๅฎ็พฉ
|
| 11 |
+
if not hasattr(_hf_hub, "cached_download"):
|
| 12 |
+
_hf_hub.cached_download = hf_hub_download
|
| 13 |
+
|
| 14 |
+
##############################################################################
|
| 15 |
+
# 1. ๆจๆบใฉใคใใฉใช & ๅค้จใฉใคใใฉใช
|
| 16 |
+
##############################################################################
|
| 17 |
+
import os, io, base64, subprocess, traceback
|
|
|
|
| 18 |
from pathlib import Path
|
| 19 |
from typing import Optional
|
| 20 |
|
|
|
|
| 24 |
import spaces
|
| 25 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 26 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
from diffusers import (
|
| 29 |
StableDiffusionControlNetPipeline,
|
|
|
|
| 36 |
from basicsr.utils.download_util import load_file_from_url
|
| 37 |
from realesrgan import RealESRGANer
|
| 38 |
|
| 39 |
+
##############################################################################
|
| 40 |
+
# 2. ใญใฃใใทใฅ๏ผๆฐธ็ถใใฃใฌใฏใใช
|
| 41 |
+
##############################################################################
|
|
|
|
| 42 |
PERSIST_BASE = Path("/data")
|
| 43 |
CACHE_ROOT = (
|
| 44 |
PERSIST_BASE / "instantid_cache"
|
|
|
|
| 51 |
for _p in (MODELS_DIR, LORA_DIR, UPSCALE_DIR):
|
| 52 |
_p.mkdir(parents=True, exist_ok=True)
|
| 53 |
|
| 54 |
+
##############################################################################
|
| 55 |
+
# 3. ใขใใซ URL ไธ่ฆง
|
| 56 |
+
##############################################################################
|
| 57 |
+
BRA_V7_URL = (
|
| 58 |
+
"https://huggingface.co/i0switch-assets/Beautiful_Realistic_Asians_v7/"
|
| 59 |
+
"resolve/main/beautiful_realistic_asians_v7_fp16.safetensors"
|
| 60 |
+
)
|
| 61 |
+
IP_ADAPTER_BIN_URL = (
|
| 62 |
+
"https://huggingface.co/h94/IP-Adapter/"
|
| 63 |
+
"resolve/main/ip-adapter-plus-face_sd15.bin"
|
| 64 |
+
)
|
| 65 |
+
IP_ADAPTER_LORA_URL = (
|
| 66 |
+
"https://huggingface.co/h94/IP-Adapter-FaceID/"
|
| 67 |
+
"resolve/main/ip-adapter-faceid-plusv2_sd15_lora.safetensors"
|
| 68 |
+
)
|
| 69 |
+
REALESRGAN_URL = (
|
| 70 |
+
"https://huggingface.co/aimagelab/realesrgan/"
|
| 71 |
+
"resolve/main/RealESRGAN_x4plus.pth"
|
| 72 |
+
)
|
| 73 |
|
| 74 |
+
##############################################################################
|
| 75 |
+
# 4. ใฆใผใใฃใชใใฃ๏ผๅ
็ขใใฆใณใญใผใ
|
| 76 |
+
##############################################################################
|
| 77 |
def download(url: str, dst: Path, attempts: int = 2):
|
|
|
|
| 78 |
if dst.exists():
|
| 79 |
return dst
|
| 80 |
for i in range(1, attempts + 1):
|
|
|
|
| 83 |
return dst
|
| 84 |
except subprocess.CalledProcessError:
|
| 85 |
print(f"[DL] Retry {i}/{attempts} failed: {url}")
|
|
|
|
| 86 |
load_file_from_url(url=url, model_dir=str(dst.parent), file_name=dst.name)
|
| 87 |
return dst
|
| 88 |
|
| 89 |
+
##############################################################################
|
| 90 |
+
# 5. ใฐใญใผใใซ๏ผlazy-load ใใใ๏ผ
|
| 91 |
+
##############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
pipe: Optional[StableDiffusionControlNetPipeline] = None
|
| 93 |
face_analyser: Optional[FaceAnalysis] = None
|
| 94 |
upsampler: Optional[RealESRGANer] = None
|
| 95 |
|
| 96 |
+
##############################################################################
|
| 97 |
+
# 6. ใใคใใฉใคใณๅๆๅ
|
| 98 |
+
##############################################################################
|
|
|
|
|
|
|
| 99 |
def initialize_pipelines():
|
| 100 |
+
"""ๆๅใฎๆจ่ซๆใซไธๅบฆใ ใๅผใณๅบใ"""
|
| 101 |
global pipe, face_analyser, upsampler
|
|
|
|
| 102 |
if pipe is not None:
|
| 103 |
+
return
|
| 104 |
|
| 105 |
print("[INIT] Downloading model assets โฆ")
|
| 106 |
|
| 107 |
+
# (1) ใใผในใขใใซ & IP-Adapter
|
| 108 |
bra_ckpt = download(BRA_V7_URL, MODELS_DIR / "bra_v7.safetensors")
|
| 109 |
ip_bin = download(IP_ADAPTER_BIN_URL, MODELS_DIR / "ip_adapter.bin")
|
| 110 |
ip_lora = download(IP_ADAPTER_LORA_URL, LORA_DIR / "ip_adapter_faceid.lora")
|
| 111 |
|
| 112 |
+
# (2) ControlNet (InstantID)
|
| 113 |
controlnet = ControlNetModel.from_pretrained(
|
| 114 |
"InstantID/ControlNet-Mediapipe-Face",
|
| 115 |
torch_dtype=torch.float16,
|
| 116 |
cache_dir=str(MODELS_DIR),
|
| 117 |
)
|
| 118 |
|
| 119 |
+
# (3) Diffusers Pipeline
|
| 120 |
+
pipe_tmp = StableDiffusionControlNetPipeline.from_pretrained(
|
| 121 |
+
"runwayml/stable-diffusion-v1-5",
|
| 122 |
+
controlnet=controlnet,
|
| 123 |
+
vae=AutoencoderKL.from_pretrained(
|
| 124 |
"stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16
|
| 125 |
),
|
| 126 |
+
torch_dtype=torch.float16,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
cache_dir=str(MODELS_DIR),
|
| 128 |
+
safety_checker=None,
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
pipe_tmp.scheduler = DPMSolverMultistepScheduler.from_pretrained(
|
| 131 |
+
"runwayml/stable-diffusion-v1-5",
|
| 132 |
+
subfolder="scheduler",
|
| 133 |
+
cache_dir=str(MODELS_DIR),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
)
|
| 135 |
+
# IP-Adapter๏ผLoRA ใ้ฉ็จ
|
| 136 |
+
pipe_tmp.load_ip_adapter(ip_bin)
|
| 137 |
ip_layers = AttnProcsLayers(pipe_tmp.unet.attn_processors)
|
| 138 |
+
ip_layers.load_lora_weights(
|
| 139 |
+
ip_lora, adapter_name="ip_faceid", safe_load=True
|
| 140 |
+
)
|
| 141 |
pipe_tmp.set_adapters(["ip_faceid"], adapter_weights=[0.6])
|
| 142 |
pipe_tmp.to("cuda")
|
| 143 |
|
| 144 |
pipe = pipe_tmp
|
| 145 |
|
| 146 |
+
# (4) InsightFace
|
| 147 |
face_analyser = FaceAnalysis(
|
| 148 |
name="buffalo_l", root=str(MODELS_DIR), providers=["CUDAExecutionProvider"]
|
| 149 |
)
|
| 150 |
face_analyser.prepare(ctx_id=0, det_size=(640, 640))
|
| 151 |
|
| 152 |
+
# (5) Real-ESRGAN
|
| 153 |
esrgan_ckpt = download(REALESRGAN_URL, UPSCALE_DIR / "realesrgan_x4plus.pth")
|
| 154 |
upsampler = RealESRGANer(
|
| 155 |
scale=4,
|
|
|
|
| 163 |
|
| 164 |
print("[INIT] Pipelines ready.")
|
| 165 |
|
| 166 |
+
##############################################################################
|
| 167 |
+
# 7. ใใญใณใใใใณใใฌ
|
| 168 |
+
##############################################################################
|
|
|
|
|
|
|
| 169 |
BASE_PROMPT = (
|
| 170 |
+
"(masterpiece:1.2), best quality, ultra-realistic, RAW photo, 8k, "
|
| 171 |
"cinematic lighting, textured skin, "
|
| 172 |
)
|
| 173 |
NEG_PROMPT = (
|
| 174 |
"verybadimagenegative_v1.3, ng_deepnegative_v1_75t, "
|
| 175 |
"(worst quality:2), (low quality:2), lowres, blurry, bad anatomy, "
|
| 176 |
+
"bad hands, extra digits, watermark, signature"
|
| 177 |
)
|
| 178 |
|
| 179 |
+
##############################################################################
|
| 180 |
+
# 8. ็ๆ้ขๆฐ๏ผGPU ใๆดใ๏ผ
|
| 181 |
+
##############################################################################
|
|
|
|
|
|
|
| 182 |
@spaces.GPU(duration=60)
|
| 183 |
def generate_core(
|
| 184 |
face_img: Image.Image,
|
|
|
|
| 194 |
up_factor: int = 4,
|
| 195 |
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
| 196 |
):
|
|
|
|
|
|
|
|
|
|
| 197 |
try:
|
| 198 |
if pipe is None:
|
| 199 |
initialize_pipelines()
|
| 200 |
|
| 201 |
+
np_face = np.array(face_img)
|
| 202 |
+
faces = face_analyser.get(np_face)
|
| 203 |
+
if len(faces) == 0:
|
| 204 |
+
raise ValueError("้กใๆคๅบใงใใพใใใงใใใๅฅใฎ็ปๅใงใ่ฉฆใใใ ใใใ")
|
| 205 |
|
| 206 |
pipe.set_adapters(["ip_faceid"], adapter_weights=[ip_scale])
|
| 207 |
|
|
|
|
| 220 |
).images[0]
|
| 221 |
|
| 222 |
if upscale and upsampler is not None:
|
| 223 |
+
upsampler.scale = 4 if up_factor == 4 else 8
|
|
|
|
| 224 |
result, _ = upsampler.enhance(np.array(result))
|
| 225 |
result = Image.fromarray(result)
|
| 226 |
|
|
|
|
| 230 |
traceback.print_exc()
|
| 231 |
raise e
|
| 232 |
|
| 233 |
+
##############################################################################
|
| 234 |
+
# 9. Gradio UI
|
| 235 |
+
##############################################################################
|
|
|
|
|
|
|
| 236 |
with gr.Blocks(title="InstantID ร BRA v7 (ZeroGPU)") as demo:
|
| 237 |
gr.Markdown("## InstantID ร Beautiful Realistic Asians v7")
|
| 238 |
with gr.Row():
|
| 239 |
face_img = gr.Image(type="pil", label="Face ID", sources=["upload"])
|
| 240 |
subject = gr.Textbox(
|
| 241 |
+
label="่ขซๅไฝ่ชฌๆ๏ผไพ: 30ไปฃๆฅๆฌไบบๅฅณๆงใ้ป้ซชใปใใญใณใฐ๏ผ", interactive=True
|
| 242 |
)
|
| 243 |
add_prompt = gr.Textbox(label="่ฟฝๅ ใใญใณใใ", interactive=True)
|
| 244 |
add_neg = gr.Textbox(label="่ฟฝๅ ใใฌใใฃใ", interactive=True)
|
|
|
|
| 253 |
upscale = gr.Checkbox(label="Real-ESRGAN Upscale", value=False)
|
| 254 |
up_factor = gr.Radio([4, 8], value=4, label="Upscale Factor")
|
| 255 |
run_btn = gr.Button("Generate")
|
|
|
|
| 256 |
output_img = gr.Image(type="pil", label="Result")
|
| 257 |
|
| 258 |
run_btn.click(
|
|
|
|
| 274 |
show_progress=True,
|
| 275 |
)
|
| 276 |
|
| 277 |
+
##############################################################################
|
| 278 |
+
# 10. FastAPI ใจใณใใใคใณใ
|
| 279 |
+
##############################################################################
|
|
|
|
| 280 |
app = FastAPI()
|
| 281 |
|
|
|
|
| 282 |
@app.post("/api/generate")
|
| 283 |
async def api_generate(
|
| 284 |
subject: str = Form(...),
|
|
|
|
| 313 |
traceback.print_exc()
|
| 314 |
raise HTTPException(status_code=500, detail=str(e))
|
| 315 |
|
| 316 |
+
##############################################################################
|
| 317 |
+
# 11. Launch๏ผGradio ใ่ชๅใง Uvicorn ใ็ซใกไธใใ๏ผ
|
| 318 |
+
##############################################################################
|
| 319 |
+
demo.queue(default_concurrency_limit=2).launch(share=False)
|
|
|
|
|
|
|
|
|