fantos commited on
Commit
02fd843
Β·
verified Β·
1 Parent(s): e770015

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -32
app.py CHANGED
@@ -66,7 +66,7 @@ def initialize_model():
66
 
67
  # 이미 λ‘œλ“œλœ 경우 λ‹€μ‹œ λ‘œλ“œν•˜μ§€ μ•ŠμŒ
68
  if pipe is not None:
69
- return
70
 
71
  try:
72
  if not path.exists(cache_path):
@@ -161,9 +161,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
161
  </div>
162
  """)
163
 
164
- # μƒνƒœ ν‘œμ‹œ λ³€μˆ˜
165
- error_message = gr.HTML(visible=False, elem_classes=["error-message"])
166
- loading_status = gr.HTML(visible=False, elem_classes=["loading-indicator"])
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  with gr.Row():
169
  with gr.Column(scale=3):
@@ -257,32 +268,24 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
257
 
258
  @spaces.GPU
259
  def process_image(height, width, steps, scales, prompt, seed):
 
 
260
  # λͺ¨λΈ μ΄ˆκΈ°ν™” μƒνƒœ 확인
261
  if pipe is None:
262
- loading_status.update("λͺ¨λΈμ„ λ‘œλ”© μ€‘μž…λ‹ˆλ‹€... 처음 μ‹€ν–‰ μ‹œ μ‹œκ°„μ΄ μ†Œμš”λ  수 μžˆμŠ΅λ‹ˆλ‹€.", visible=True)
263
 
264
  model_loaded = initialize_model()
265
  if not model_loaded:
266
- error_message.update("λͺ¨λΈ λ‘œλ”© 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. νŽ˜μ΄μ§€λ₯Ό μƒˆλ‘œκ³ μΉ¨ν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.", visible=True)
267
- loading_status.update(visible=False)
268
- return None
269
-
270
- loading_status.update(visible=False)
271
 
272
  # μž…λ ₯κ°’ 검증
273
  if not prompt or prompt.strip() == "":
274
- error_message.update("이미지 μ„€λͺ…을 μž…λ ₯ν•΄μ£Όμ„Έμš”.", visible=True)
275
- return None
276
 
277
  # ν”„λ‘¬ν”„νŠΈ 필터링
278
  is_safe, filtered_prompt = filter_prompt(prompt)
279
  if not is_safe:
280
- error_message.update("λΆ€μ μ ˆν•œ λ‚΄μš©μ΄ ν¬ν•¨λœ ν”„λ‘¬ν”„νŠΈμž…λ‹ˆλ‹€.", visible=True)
281
- return None
282
-
283
- # μ—λŸ¬ λ©”μ‹œμ§€ μ΄ˆκΈ°ν™”
284
- error_message.update(visible=False)
285
- loading_status.update("이미지λ₯Ό 생성 μ€‘μž…λ‹ˆλ‹€...", visible=True)
286
 
287
  try:
288
  # λ©”λͺ¨λ¦¬ 확보λ₯Ό μœ„ν•œ κ°€λΉ„μ§€ μ½œλ ‰μ…˜
@@ -295,6 +298,9 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
295
  else:
296
  seed = int(seed) # νƒ€μž… λ³€ν™˜ μ•ˆμ „ν•˜κ²Œ 처리
297
 
 
 
 
298
  # 이미지 생성
299
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
300
  generator = torch.Generator(device="cuda").manual_seed(seed)
@@ -317,42 +323,46 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
317
  max_sequence_length=256
318
  ).images[0]
319
 
320
- loading_status.update(visible=False)
321
- return generated_image
322
 
323
  except Exception as e:
324
  error_msg = f"이미지 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
325
  print(error_msg)
326
  traceback.print_exc()
327
- error_message.update(error_msg, visible=True)
328
- loading_status.update(visible=False)
329
 
330
  # 였λ₯˜ ν›„ λ©”λͺ¨λ¦¬ 정리
331
  gc.collect()
332
  torch.cuda.empty_cache()
333
 
334
- return None
335
 
336
  def update_seed():
337
  return get_random_seed()
338
-
339
- # λ²„νŠΌ 클릭 이벀트 - λͺ¨λ“  UI μš”μ†Œ μ΄ˆκΈ°ν™” μΆ”κ°€
340
- def on_generate_click(height, width, steps, scales, prompt, seed):
341
- error_message.update(visible=False)
 
 
 
 
 
 
342
  return process_image(height, width, steps, scales, prompt, seed)
343
 
 
344
  generate_btn.click(
345
- on_generate_click,
346
  inputs=[height, width, steps, scales, prompt, seed],
347
- outputs=[output]
348
  )
349
 
350
  randomize_seed.click(
351
- update_seed,
352
  outputs=[seed]
353
  )
354
 
355
  if __name__ == "__main__":
356
  # μ•± μ‹œμž‘ μ‹œ λͺ¨λΈ 미리 λ‘œλ“œν•˜μ§€ μ•ŠμŒ (첫 μš”μ²­ μ‹œ μ§€μ—° λ‘œλ”©)
357
- demo.queue(max_size=10).launch()
358
-
 
66
 
67
  # 이미 λ‘œλ“œλœ 경우 λ‹€μ‹œ λ‘œλ“œν•˜μ§€ μ•ŠμŒ
68
  if pipe is not None:
69
+ return True
70
 
71
  try:
72
  if not path.exists(cache_path):
 
161
  </div>
162
  """)
163
 
164
+ # μƒνƒœ ν‘œμ‹œ λ³€μˆ˜ (HTML λŒ€μ‹  Textbox μ‚¬μš©)
165
+ error_message = gr.Textbox(
166
+ value="",
167
+ label="Error",
168
+ visible=False,
169
+ elem_classes=["error-message"]
170
+ )
171
+
172
+ loading_status = gr.Textbox(
173
+ value="",
174
+ label="Status",
175
+ visible=False,
176
+ elem_classes=["loading-indicator"]
177
+ )
178
 
179
  with gr.Row():
180
  with gr.Column(scale=3):
 
268
 
269
  @spaces.GPU
270
  def process_image(height, width, steps, scales, prompt, seed):
271
+ global pipe
272
+
273
  # λͺ¨λΈ μ΄ˆκΈ°ν™” μƒνƒœ 확인
274
  if pipe is None:
275
+ return None, "λͺ¨λΈμ„ λ‘œλ”© μ€‘μž…λ‹ˆλ‹€... 처음 μ‹€ν–‰ μ‹œ μ‹œκ°„μ΄ μ†Œμš”λ  수 μžˆμŠ΅λ‹ˆλ‹€.", True, "", False
276
 
277
  model_loaded = initialize_model()
278
  if not model_loaded:
279
+ return None, "", False, "λͺ¨λΈ λ‘œλ”© 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. νŽ˜μ΄μ§€λ₯Ό μƒˆλ‘œκ³ μΉ¨ν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.", True
 
 
 
 
280
 
281
  # μž…λ ₯κ°’ 검증
282
  if not prompt or prompt.strip() == "":
283
+ return None, "", False, "이미지 μ„€λͺ…을 μž…λ ₯ν•΄μ£Όμ„Έμš”.", True
 
284
 
285
  # ν”„λ‘¬ν”„νŠΈ 필터링
286
  is_safe, filtered_prompt = filter_prompt(prompt)
287
  if not is_safe:
288
+ return None, "", False, "λΆ€μ μ ˆν•œ λ‚΄μš©μ΄ ν¬ν•¨λœ ν”„λ‘¬ν”„νŠΈμž…λ‹ˆλ‹€.", True
 
 
 
 
 
289
 
290
  try:
291
  # λ©”λͺ¨λ¦¬ 확보λ₯Ό μœ„ν•œ κ°€λΉ„μ§€ μ½œλ ‰μ…˜
 
298
  else:
299
  seed = int(seed) # νƒ€μž… λ³€ν™˜ μ•ˆμ „ν•˜κ²Œ 처리
300
 
301
+ # 이미지 생성 μƒνƒœ λ©”μ‹œμ§€
302
+ loading_message = "이미지λ₯Ό 생성 μ€‘μž…λ‹ˆλ‹€..."
303
+
304
  # 이미지 생성
305
  with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
306
  generator = torch.Generator(device="cuda").manual_seed(seed)
 
323
  max_sequence_length=256
324
  ).images[0]
325
 
326
+ # 성곡 μ‹œ 이미지 λ°˜ν™˜, μƒνƒœ λ©”μ‹œμ§€ μˆ¨κΉ€
327
+ return generated_image, "", False, "", False
328
 
329
  except Exception as e:
330
  error_msg = f"이미지 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
331
  print(error_msg)
332
  traceback.print_exc()
 
 
333
 
334
  # 였λ₯˜ ν›„ λ©”λͺ¨λ¦¬ 정리
335
  gc.collect()
336
  torch.cuda.empty_cache()
337
 
338
+ return None, "", False, error_msg, True
339
 
340
  def update_seed():
341
  return get_random_seed()
342
+
343
+ # 이미지 생성 μ€€λΉ„ ν•¨μˆ˜
344
+ def prepare_generation(height, width, steps, scales, prompt, seed):
345
+ # λͺ¨λΈμ΄ 아직 λ‘œλ“œλ˜μ§€ μ•Šμ•˜λ‹€λ©΄ λ‘œλ“œ
346
+ if pipe is None:
347
+ is_loaded = initialize_model()
348
+ if not is_loaded:
349
+ return None, "λͺ¨λΈ λ‘œλ”©μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€. νŽ˜μ΄μ§€λ₯Ό μƒˆλ‘œκ³ μΉ¨ν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.", True, "", False
350
+
351
+ # 생성 ν”„λ‘œμ„ΈμŠ€ μ‹œμž‘
352
  return process_image(height, width, steps, scales, prompt, seed)
353
 
354
+ # λ²„νŠΌ 클릭 이벀트 μ—°κ²°
355
  generate_btn.click(
356
+ fn=prepare_generation,
357
  inputs=[height, width, steps, scales, prompt, seed],
358
+ outputs=[output, loading_status, loading_status, error_message, error_message]
359
  )
360
 
361
  randomize_seed.click(
362
+ fn=update_seed,
363
  outputs=[seed]
364
  )
365
 
366
  if __name__ == "__main__":
367
  # μ•± μ‹œμž‘ μ‹œ λͺ¨λΈ 미리 λ‘œλ“œν•˜μ§€ μ•ŠμŒ (첫 μš”μ²­ μ‹œ μ§€μ—° λ‘œλ”©)
368
+ demo.queue(max_size=10).launch()