i0switch commited on
Commit
4fd7fe3
ยท
verified ยท
1 Parent(s): f1dc6b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -125
app.py CHANGED
@@ -1,21 +1,20 @@
1
- # app.py โ€” InstantID ร— Beautiful Realistic Asians v7 (ZeroGPU-ready, FastAPI + Gradio)
2
- # 2025-06-21 ็‰ˆ
3
- #
4
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
5
- # ไธปใช็‰นๅพด
6
- # โ€ข @spaces.GPU(duration=60) ใ‚’ๅ…ฌ้–‹ๅ generate_core() ใซไป˜ไธŽ
7
- # โ€ข ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏ lazy-load ใงๅˆๅ›žๆŽจ่ซ–ๆ™‚ใซ GPU ใธใƒญใƒผใƒ‰
8
- # โ€ข ใƒขใƒ‡ใƒซ่ณ‡็”ฃใฏ /data ใพใŸใฏ ~/.cache ใซๆฐธ็ถšๅŒ–
9
- # โ€ข Real-ESRGAN ใ‚ขใƒƒใƒ—ใ‚นใ‚ฑใƒผใƒซ (x4 / x8) ใ‚ชใƒ—ใ‚ทใƒงใƒณ
10
- # โ€ข Gradio UI + FastAPI REST ใ‚’ 1 ใƒ—ใƒญใ‚ปใ‚นใงๅ…ฑๅญ˜
11
- # โ€ข Uvicorn ๆ‰‹ๅ‹•่ตทๅ‹•ใฏไธ่ฆ๏ผˆSpaces ใŒ่‡ชๅ‰ใง็ซ‹ใฆใ‚‹๏ผ‰
12
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
13
-
14
- import os
15
- import io
16
- import base64
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
- # 0. ใ‚ญใƒฃใƒƒใ‚ทใƒฅใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใจใƒ€ใ‚ฆใƒณใƒญใƒผใƒ€
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
- # 1. ใƒขใƒ‡ใƒซ URL ๅฎš็พฉ
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
- # 3. ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณๅˆๆœŸๅŒ–
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
- # ---- 3-1. ๅŸบๆœฌใƒขใƒ‡ใƒซ & IP-Adapter ----
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
- # ---- 3-2. ControlNet (InstantID) ----
122
  controlnet = ControlNetModel.from_pretrained(
123
  "InstantID/ControlNet-Mediapipe-Face",
124
  torch_dtype=torch.float16,
125
  cache_dir=str(MODELS_DIR),
126
  )
127
 
128
- # ---- 3-3. Diffusers ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ ----
129
- pipe_local_files_only = {
130
- "controlnet": controlnet,
131
- "vae": AutoencoderKL.from_pretrained(
 
132
  "stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16
133
  ),
134
- "torch_dtype": torch.float16,
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
- load_safety_checker=False,
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
- pipe_base, subfolder="scheduler", cache_dir=str(MODELS_DIR)
148
- )
149
- # LoRA / IP-Adapter
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(ip_lora, adapter_name="ip_faceid", safe_load=True)
 
 
157
  pipe_tmp.set_adapters(["ip_faceid"], adapter_weights=[0.6])
158
  pipe_tmp.to("cuda")
159
 
160
  pipe = pipe_tmp
161
 
162
- # ---- 3-4. InsightFace ----
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
- # ---- 3-5. Real-ESRGAN ----
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
- # 4. ใƒ—ใƒญใƒณใƒ—ใƒˆ่จญๅฎš
185
- # ==============================================================
186
-
187
  BASE_PROMPT = (
188
- "(masterpiece:1.2), best quality, ultra-realistic, 8k, RAW photo, "
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, cropped, watermark, signature"
195
  )
196
 
197
- # ==============================================================
198
- # 5. ็”Ÿๆˆใ‚ณใ‚ข้–ขๆ•ฐ๏ผˆGPU ใ‚’ๆŽดใ‚€๏ผ‰
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
- face_np = np.array(face_img)
225
- face_info = face_analyser.get(face_np)
226
- if len(face_info) == 0:
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
- # 6. Gradio UI
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="่ขซๅ†™ไฝ“่ชฌๆ˜Ž๏ผˆไพ‹: '30ไปฃๆ—ฅๆœฌไบบๅฅณๆ€งใ€้ป’้ซชใ‚ปใƒŸใƒญใƒณใ‚ฐ'๏ผ‰", interactive=True
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
- # 7. FastAPI ใ‚จใƒณใƒ‰ใƒใ‚คใƒณใƒˆ
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
- # 8. Launch
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)