fantaxy commited on
Commit
abb87aa
·
verified ·
1 Parent(s): 6a51f96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -8
app.py CHANGED
@@ -2,10 +2,15 @@ import spaces
2
  import time
3
  import os
4
 
 
 
 
 
5
  import gradio as gr
6
  import torch
7
  from einops import rearrange
8
  from PIL import Image
 
9
 
10
  from flux.cli import SamplingOptions
11
  from flux.sampling import denoise, get_noise, get_schedule, prepare, unpack
@@ -160,14 +165,34 @@ def generate_image(
160
  torch.cuda.empty_cache()
161
 
162
  t1 = time.perf_counter()
163
-
164
  print(f"Done in {t1 - t0:.1f}s.")
 
165
  # bring into PIL format
166
  x = x.clamp(-1, 1)
167
  x = rearrange(x[0], "c h w -> h w c")
168
-
169
- img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy())
170
- return img, str(opts.seed), flux_generator.pulid_model.debug_img_list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
 
173
  def create_demo(args, model_name: str, device: str = "cuda" if torch.cuda.is_available() else "cpu", offload: bool = False):
@@ -189,7 +214,7 @@ def create_demo(args, model_name: str, device: str = "cuda" if torch.cuda.is_ava
189
  with gr.Row():
190
  with gr.Column():
191
  prompt = gr.Textbox(label="Prompt", value="portrait, color, cinematic")
192
- id_image = gr.Image(label="ID Image")
193
  id_weight = gr.Slider(0.0, 3.0, 1, step=0.05, label="id weight")
194
 
195
  width = gr.Slider(256, 1536, 896, step=16, label="Width")
@@ -214,9 +239,14 @@ def create_demo(args, model_name: str, device: str = "cuda" if torch.cuda.is_ava
214
  generate_btn = gr.Button("Generate")
215
 
216
  with gr.Column():
217
- output_image = gr.Image(label="Generated Image")
218
  seed_output = gr.Textbox(label="Used Seed")
219
- intermediate_output = gr.Gallery(label="Output", elem_id="gallery", visible=args.dev)
 
 
 
 
 
220
 
221
  with gr.Row(), gr.Column():
222
  gr.Markdown("## Examples")
@@ -296,4 +326,6 @@ if __name__ == "__main__":
296
  huggingface_hub.login(hf_token)
297
 
298
  demo = create_demo(args, args.name, args.device, args.offload)
299
- demo.launch()
 
 
 
2
  import time
3
  import os
4
 
5
+ # ONNX Runtime에서 CUDA provider 우선 사용 (insightface → ORT 세션에 반영)
6
+ os.environ.setdefault("INSIGHTFACE_ONNX_PROVIDERS", "CUDAExecutionProvider,CPUExecutionProvider")
7
+ os.environ.setdefault("ORT_LOG_severity_level", "3") # ORT 로그 최소화
8
+
9
  import gradio as gr
10
  import torch
11
  from einops import rearrange
12
  from PIL import Image
13
+ import numpy as np
14
 
15
  from flux.cli import SamplingOptions
16
  from flux.sampling import denoise, get_noise, get_schedule, prepare, unpack
 
165
  torch.cuda.empty_cache()
166
 
167
  t1 = time.perf_counter()
 
168
  print(f"Done in {t1 - t0:.1f}s.")
169
+
170
  # bring into PIL format
171
  x = x.clamp(-1, 1)
172
  x = rearrange(x[0], "c h w -> h w c")
173
+ img = Image.fromarray((127.5 * (x + 1.0)).cpu().byte().numpy()).convert("RGB")
174
+
175
+ # Gallery용 디버그 이미지들을 PIL 리스트로 강제 변환
176
+ debug = []
177
+ for it in (flux_generator.pulid_model.debug_img_list or []):
178
+ try:
179
+ if isinstance(it, Image.Image):
180
+ pil = it.convert("RGB")
181
+ else:
182
+ if hasattr(it, "detach"):
183
+ arr = it.detach().cpu().numpy()
184
+ else:
185
+ arr = np.array(it)
186
+ if arr.ndim == 3 and arr.shape[0] in (1, 3): # C,H,W → H,W,C
187
+ arr = np.transpose(arr, (1, 2, 0))
188
+ if arr.dtype != np.uint8:
189
+ arr = np.clip(arr, 0, 255).astype(np.uint8)
190
+ pil = Image.fromarray(arr).convert("RGB")
191
+ debug.append(pil)
192
+ except Exception:
193
+ continue
194
+
195
+ return img, str(opts.seed), debug
196
 
197
 
198
  def create_demo(args, model_name: str, device: str = "cuda" if torch.cuda.is_available() else "cpu", offload: bool = False):
 
214
  with gr.Row():
215
  with gr.Column():
216
  prompt = gr.Textbox(label="Prompt", value="portrait, color, cinematic")
217
+ id_image = gr.Image(label="ID Image", type="numpy")
218
  id_weight = gr.Slider(0.0, 3.0, 1, step=0.05, label="id weight")
219
 
220
  width = gr.Slider(256, 1536, 896, step=16, label="Width")
 
239
  generate_btn = gr.Button("Generate")
240
 
241
  with gr.Column():
242
+ output_image = gr.Image(label="Generated Image", type="pil", format="png")
243
  seed_output = gr.Textbox(label="Used Seed")
244
+ intermediate_output = gr.Gallery(
245
+ label="Output",
246
+ elem_id="gallery",
247
+ visible=args.dev,
248
+ allow_preview=True,
249
+ )
250
 
251
  with gr.Row(), gr.Column():
252
  gr.Markdown("## Examples")
 
326
  huggingface_hub.login(hf_token)
327
 
328
  demo = create_demo(args, args.name, args.device, args.offload)
329
+ # SSR 비활성화: 업로드/탭 이동 중 ClientDisconnect 노이즈 감소 및 렌더 안정화
330
+ demo.launch(ssr_mode=False)
331
+