ginipick commited on
Commit
2f6382c
Β·
verified Β·
1 Parent(s): 5159f05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -33
app.py CHANGED
@@ -1,48 +1,49 @@
1
  """
2
  Gradio UI that calls the remote animation server.
3
- Set ANIM_API_URL env-var when deploying:
4
  ANIM_API_URL=http://211.233.58.201:7788/
5
  """
6
 
7
- import os, time, logging
8
  from datetime import datetime
9
 
10
  import gradio as gr
11
  import httpx
12
  from gradio_client import Client, handle_file
13
 
 
14
  logging.basicConfig(
15
  level=logging.INFO,
16
  format="%(asctime)s [%(levelname)s] %(message)s"
17
  )
18
  log = logging.getLogger(__name__)
19
 
20
- # ------------------------------------------------------------------ #
21
- # κΈ°λ³Έ 접속 μ£Όμ†Œ – λ‚΄λΆ€(Loopback) 여뢀에 따라 μžλ™ νŒλ‹¨ + ν™˜κ²½λ³€μˆ˜ μž¬μ •μ˜
22
- # ------------------------------------------------------------------ #
23
  _DEFAULT_URL = "http://127.0.0.1:7788/"
24
  REMOTE_URL = "http://211.233.58.201:7788/"
25
- API_URL = os.getenv("ANIM_API_URL", REMOTE_URL)
26
 
27
- if API_URL == REMOTE_URL and os.getenv("HF_SPACE") == "true":
28
- # HuggingFace 슀페이슀 μ»¨ν…Œμ΄λ„ˆ μ•ˆ β†’ μ™ΈλΆ€ μ„œλ²„ 접속
29
- pass
30
- elif "127.0.0.1" in API_URL and os.getenv("HF_SPACE") == "true":
31
- raise RuntimeError("HF Space λ‚΄λΆ€μ—μ„œλŠ” 곡인 IP λ˜λŠ” λ„λ©”μΈμœΌλ‘œ ANIM_API_URL을 μ§€μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€.")
32
 
33
- # ------------------------------------------------------------------ #
34
  def make_client() -> Client:
35
- """컀λ„₯μ…˜ νƒ€μž„μ•„μ›ƒμ„ 30s둜 늘렀 μž¬μ‹œλ„ κ°€λŠ₯μ„± 확보"""
36
- timeout = httpx.Timeout(connect=30.0, read=120.0)
 
 
 
37
  return Client(API_URL, timeout=timeout, retries=3)
38
 
39
  def test_api_connection():
40
  now = datetime.now().strftime("%H:%M:%S")
41
  try:
42
  client = make_client()
43
- # readiness-probe
44
- ready = client.get('/healthz')['ready']
45
- msg = f"[{now}] μ„œλ²„ μ—°κ²° 성곡 βœ… (ready={ready})"
46
  log.info(msg)
47
  return True, msg
48
  except Exception as e:
@@ -50,18 +51,17 @@ def test_api_connection():
50
  log.error(msg)
51
  return False, msg
52
 
53
- # ------------------------------------------------------------------ #
54
  def generate_animation(image, audio, guidance_scale, steps, progress=gr.Progress()):
55
  start = datetime.now().strftime("%H:%M:%S")
56
- logs = [f"[{start}] μš”μ²­ μ‹œμž‘"]
57
  try:
58
  if image is None or audio is None:
59
  raise ValueError("이미지와 μ˜€λ””μ˜€λ₯Ό λͺ¨λ‘ μ—…λ‘œλ“œν•˜μ„Έμš”.")
60
 
61
  progress(0.05, desc="파일 μ€€λΉ„")
62
  client = make_client()
63
- progress(0.15, desc="μ„œλ²„ 호좜 쀑… (μ΅œλŒ€ 2-3λΆ„ μ†Œμš”)")
64
 
 
65
  result = client.predict(
66
  image_path=handle_file(image),
67
  audio_path=handle_file(audio),
@@ -77,41 +77,47 @@ def generate_animation(image, audio, guidance_scale, steps, progress=gr.Progress
77
  return anim, comp, "\n".join(logs)
78
  else:
79
  raise RuntimeError(f"μ˜ˆμƒμΉ˜ λͺ»ν•œ λ°˜ν™˜ ν˜•μ‹: {type(result)}")
 
80
  except Exception as e:
81
  logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] 였λ₯˜: {e}")
82
  return None, None, "\n".join(logs)
83
 
84
- # ------------------------------------------------------------------ #
85
  with gr.Blocks(title="Animation Generator Client") as demo:
86
  gr.Markdown("# 🎬 Animation Generator – Client UI")
87
 
88
- # health check
89
  status_box = gr.Textbox(label="API μƒνƒœ", interactive=False)
90
  test_btn = gr.Button("μ„œλ²„ μ—°κ²° ν…ŒμŠ€νŠΈ")
91
  test_btn.click(test_api_connection, outputs=[status_box, status_box])
92
 
93
  gr.Markdown("---")
 
94
  with gr.Row():
95
  with gr.Column():
96
- img_in = gr.Image(type="filepath", label="Portrait")
97
- aud_in = gr.Audio(type="filepath", label="Audio")
98
  scale = gr.Slider(1, 10, value=3.0, step=0.1, label="Guidance Scale")
99
  steps = gr.Slider(5, 30, value=10, step=1, label="Inference Steps")
100
- gen_btn = gr.Button("πŸš€ Generate") # λ²„νŠΌμ„ λ³€μˆ˜λ‘œ
101
  with gr.Column():
102
  anim_out = gr.Video(label="Animation Result")
103
- comp_out = gr.Video(label="Side-by-side")
104
- with gr.Accordion("둜그", open=False):
105
- log_out = gr.Textbox(label="μ‹€ν–‰ 둜그", lines=12, max_lines=20, interactive=False)
106
 
107
- # ⭐ click μ—°κ²° ­– outputs에 **μ»΄ν¬λ„ŒνŠΈ λ³€μˆ˜** μ‚¬μš©
 
 
 
108
  gen_btn.click(
109
  generate_animation,
110
  inputs=[img_in, aud_in, scale, steps],
111
- outputs=[anim_out, comp_out, log_out] # ← λ¬Έμžμ—΄ X
112
  )
113
 
 
114
  if __name__ == "__main__":
115
- demo.queue(max_size=4).launch(server_name="0.0.0.0",
116
- server_port=7860,
117
- show_api=False)
 
 
 
1
  """
2
  Gradio UI that calls the remote animation server.
3
+ ν™˜κ²½λ³€μˆ˜ ANIM_API_URL 둜 μ„œλ²„ μ£Όμ†Œλ₯Ό μ„€μ •ν•˜μ„Έμš”
4
  ANIM_API_URL=http://211.233.58.201:7788/
5
  """
6
 
7
+ import os, logging
8
  from datetime import datetime
9
 
10
  import gradio as gr
11
  import httpx
12
  from gradio_client import Client, handle_file
13
 
14
+ # ────────────────────────── λ‘œκΉ… ──────────────────────────── #
15
  logging.basicConfig(
16
  level=logging.INFO,
17
  format="%(asctime)s [%(levelname)s] %(message)s"
18
  )
19
  log = logging.getLogger(__name__)
20
 
21
+ # ───────────────────── μ„œλ²„ URL κ²°μ • 둜직 ───────────────────── #
 
 
22
  _DEFAULT_URL = "http://127.0.0.1:7788/"
23
  REMOTE_URL = "http://211.233.58.201:7788/"
24
+ API_URL = os.getenv("ANIM_API_URL", REMOTE_URL)
25
 
26
+ # Hugging Face Space μ»¨ν…Œμ΄λ„ˆ μ•ˆμ—μ„œ 127.0.0.1 μ‚¬μš©ν•˜λ©΄ μ™ΈλΆ€ 접속 λΆˆκ°€
27
+ if "127.0.0.1" in API_URL and os.getenv("HF_SPACE") == "true":
28
+ raise RuntimeError(
29
+ "HF Space λ‚΄λΆ€μ—μ„œλŠ” ANIM_API_URL 을 곡인 IPλ‚˜ λ„λ©”μΈμœΌλ‘œ μ„€μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€."
30
+ )
31
 
32
+ # ──────────────────────── 헬퍼 ν•¨μˆ˜ ───────────────────────── #
33
  def make_client() -> Client:
34
+ """
35
+ httpx.Timeout 은 0.27+ μ—μ„œ 4개 λ˜λŠ” default λͺ¨λ‘ μ§€μ •ν•΄μ•Ό 함
36
+ connect 30s β€’ read/write 120s β€’ pool 30s
37
+ """
38
+ timeout = httpx.Timeout(connect=30.0, read=120.0, write=120.0, pool=30.0)
39
  return Client(API_URL, timeout=timeout, retries=3)
40
 
41
  def test_api_connection():
42
  now = datetime.now().strftime("%H:%M:%S")
43
  try:
44
  client = make_client()
45
+ ready = client.get("/healthz")["ready"]
46
+ msg = f"[{now}] μ„œλ²„ μ—°κ²° 성곡 βœ… (ready={ready})"
 
47
  log.info(msg)
48
  return True, msg
49
  except Exception as e:
 
51
  log.error(msg)
52
  return False, msg
53
 
 
54
  def generate_animation(image, audio, guidance_scale, steps, progress=gr.Progress()):
55
  start = datetime.now().strftime("%H:%M:%S")
56
+ logs = [f"[{start}] μš”μ²­ μ‹œμž‘"]
57
  try:
58
  if image is None or audio is None:
59
  raise ValueError("이미지와 μ˜€λ””μ˜€λ₯Ό λͺ¨λ‘ μ—…λ‘œλ“œν•˜μ„Έμš”.")
60
 
61
  progress(0.05, desc="파일 μ€€λΉ„")
62
  client = make_client()
 
63
 
64
+ progress(0.15, desc="μ„œλ²„ 호좜 쀑… (μ΅œλŒ€ 2~3λΆ„)")
65
  result = client.predict(
66
  image_path=handle_file(image),
67
  audio_path=handle_file(audio),
 
77
  return anim, comp, "\n".join(logs)
78
  else:
79
  raise RuntimeError(f"μ˜ˆμƒμΉ˜ λͺ»ν•œ λ°˜ν™˜ ν˜•μ‹: {type(result)}")
80
+
81
  except Exception as e:
82
  logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] 였λ₯˜: {e}")
83
  return None, None, "\n".join(logs)
84
 
85
+ # ────────────────────────── UI μ •μ˜ ───────────────────────── #
86
  with gr.Blocks(title="Animation Generator Client") as demo:
87
  gr.Markdown("# 🎬 Animation Generator – Client UI")
88
 
89
+ # μ„œλ²„ μƒνƒœ 체크
90
  status_box = gr.Textbox(label="API μƒνƒœ", interactive=False)
91
  test_btn = gr.Button("μ„œλ²„ μ—°κ²° ν…ŒμŠ€νŠΈ")
92
  test_btn.click(test_api_connection, outputs=[status_box, status_box])
93
 
94
  gr.Markdown("---")
95
+
96
  with gr.Row():
97
  with gr.Column():
98
+ img_in = gr.Image(type="filepath", label="Portrait Image")
99
+ aud_in = gr.Audio(type="filepath", label="Driving Audio")
100
  scale = gr.Slider(1, 10, value=3.0, step=0.1, label="Guidance Scale")
101
  steps = gr.Slider(5, 30, value=10, step=1, label="Inference Steps")
102
+ gen_btn = gr.Button("πŸš€ Generate")
103
  with gr.Column():
104
  anim_out = gr.Video(label="Animation Result")
105
+ comp_out = gr.Video(label="Side-by-Side")
 
 
106
 
107
+ with gr.Accordion("μ‹€ν–‰ 둜그", open=False):
108
+ log_out = gr.Textbox(label="Logs", lines=12, max_lines=20, interactive=False)
109
+
110
+ # λ²„νŠΌ 이벀트 μ—°κ²°
111
  gen_btn.click(
112
  generate_animation,
113
  inputs=[img_in, aud_in, scale, steps],
114
+ outputs=[anim_out, comp_out, log_out]
115
  )
116
 
117
+ # ────────────────────────── μ‹€ν–‰ ─────────────────────────── #
118
  if __name__ == "__main__":
119
+ demo.queue(max_size=4).launch(
120
+ server_name="0.0.0.0",
121
+ server_port=7860,
122
+ show_api=False
123
+ )