Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,250 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
-
import logging
|
| 4 |
-
import traceback
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
import json
|
| 7 |
|
| 8 |
-
# λ‘κΉ
μ€μ
|
| 9 |
logging.basicConfig(
|
| 10 |
-
level=logging.
|
| 11 |
-
format=
|
| 12 |
)
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def test_api_connection():
|
| 19 |
-
|
| 20 |
try:
|
| 21 |
-
client =
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
|
|
|
| 28 |
def generate_animation(image, audio, guidance_scale, steps, progress=gr.Progress()):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
logs = []
|
| 32 |
-
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
error_msg = "μ΄λ―Έμ§κ° μ 곡λμ§ μμμ΅λλ€."
|
| 42 |
-
logger.error(error_msg)
|
| 43 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] ERROR: {error_msg}")
|
| 44 |
-
return None, None, "\n".join(logs)
|
| 45 |
-
|
| 46 |
-
if audio is None:
|
| 47 |
-
error_msg = "μ€λμ€κ° μ 곡λμ§ μμμ΅λλ€."
|
| 48 |
-
logger.error(error_msg)
|
| 49 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] ERROR: {error_msg}")
|
| 50 |
-
return None, None, "\n".join(logs)
|
| 51 |
-
|
| 52 |
-
# Progress μ
λ°μ΄νΈ
|
| 53 |
-
progress(0.1, desc="API ν΄λΌμ΄μΈνΈ μ΄κΈ°ν μ€...")
|
| 54 |
-
|
| 55 |
-
# API ν΄λΌμ΄μΈνΈ μμ±
|
| 56 |
-
client = Client(API_URL)
|
| 57 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] API ν΄λΌμ΄μΈνΈ μμ± μλ£")
|
| 58 |
-
|
| 59 |
-
# Progress μ
λ°μ΄νΈ
|
| 60 |
-
progress(0.3, desc="νμΌ νΈλ€ μμ± μ€...")
|
| 61 |
-
|
| 62 |
-
# νμΌ νΈλ€ μμ±
|
| 63 |
-
image_handle = handle_file(image)
|
| 64 |
-
audio_handle = handle_file(audio)
|
| 65 |
-
|
| 66 |
-
log_msg = f"νμΌ νΈλ€ μμ± μλ£:\n- Image handle: {type(image_handle)}\n- Audio handle: {type(audio_handle)}"
|
| 67 |
-
logger.debug(log_msg)
|
| 68 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] {log_msg}")
|
| 69 |
-
|
| 70 |
-
# Progress μ
λ°μ΄νΈ
|
| 71 |
-
progress(0.5, desc="API νΈμΆ μ€... (μ΄ κ³Όμ μ μκ°μ΄ 걸릴 μ μμ΅λλ€)")
|
| 72 |
-
|
| 73 |
-
# API νΈμΆ
|
| 74 |
-
logger.info("API νΈμΆ μμ")
|
| 75 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] API νΈμΆ μμ...")
|
| 76 |
-
|
| 77 |
result = client.predict(
|
| 78 |
-
image_path=
|
| 79 |
-
audio_path=
|
| 80 |
guidance_scale=guidance_scale,
|
| 81 |
steps=steps,
|
| 82 |
api_name="/generate_animation"
|
| 83 |
)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
progress(0.9, desc="κ²°κ³Ό μ²λ¦¬ μ€...")
|
| 87 |
-
|
| 88 |
-
# κ²°κ³Ό λ‘κΉ
|
| 89 |
-
log_msg = f"API νΈμΆ μ±κ³΅!\nκ²°κ³Ό νμ
: {type(result)}\nκ²°κ³Ό κΈΈμ΄: {len(result) if isinstance(result, (list, tuple)) else 'N/A'}"
|
| 90 |
-
logger.info(log_msg)
|
| 91 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] {log_msg}")
|
| 92 |
-
|
| 93 |
-
# κ²°κ³Ό μμΈ λ‘κΉ
|
| 94 |
if isinstance(result, (list, tuple)) and len(result) >= 2:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] {log_msg}")
|
| 99 |
-
|
| 100 |
-
animation_result = result[0]
|
| 101 |
-
comparison_result = result[1]
|
| 102 |
-
|
| 103 |
-
# λΉλμ€ κ²½λ‘ μΆμΆ
|
| 104 |
-
animation_video = None
|
| 105 |
-
comparison_video = None
|
| 106 |
-
|
| 107 |
-
if isinstance(animation_result, dict) and 'video' in animation_result:
|
| 108 |
-
animation_video = animation_result['video']
|
| 109 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] μ λλ©μ΄μ
λΉλμ€ κ²½λ‘: {animation_video}")
|
| 110 |
-
|
| 111 |
-
if isinstance(comparison_result, dict) and 'video' in comparison_result:
|
| 112 |
-
comparison_video = comparison_result['video']
|
| 113 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] λΉκ΅ λΉλμ€ κ²½λ‘: {comparison_video}")
|
| 114 |
-
|
| 115 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] === μ λλ©μ΄μ
μμ± μλ£! ===")
|
| 116 |
-
|
| 117 |
-
return animation_video, comparison_video, "\n".join(logs)
|
| 118 |
else:
|
| 119 |
-
|
| 120 |
-
logger.error(error_msg)
|
| 121 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] ERROR: {error_msg}")
|
| 122 |
-
return None, None, "\n".join(logs)
|
| 123 |
-
|
| 124 |
except Exception as e:
|
| 125 |
-
|
| 126 |
-
logger.error(error_msg)
|
| 127 |
-
logger.error(traceback.format_exc())
|
| 128 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] ERROR: {error_msg}")
|
| 129 |
-
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] μμΈ μ€λ₯:\n{traceback.format_exc()}")
|
| 130 |
return None, None, "\n".join(logs)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
with gr.Blocks(title="Animation Generator
|
| 134 |
-
gr.Markdown(""
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
4. "Generate Animation" λ²νΌμ ν΄λ¦νμΈμ
|
| 144 |
-
""")
|
| 145 |
-
|
| 146 |
-
# API μ°κ²° μν νμΈ
|
| 147 |
-
with gr.Row():
|
| 148 |
-
with gr.Column():
|
| 149 |
-
connection_status = gr.Textbox(label="API μ°κ²° μν", interactive=False)
|
| 150 |
-
check_connection_btn = gr.Button("API μ°κ²° ν
μ€νΈ", variant="secondary")
|
| 151 |
-
|
| 152 |
gr.Markdown("---")
|
| 153 |
-
|
| 154 |
-
# μ
λ ₯ μΉμ
|
| 155 |
with gr.Row():
|
| 156 |
with gr.Column():
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
elem_id="audio_input"
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
with gr.Column():
|
| 169 |
-
guidance_scale = gr.Slider(
|
| 170 |
-
minimum=1,
|
| 171 |
-
maximum=10,
|
| 172 |
-
value=3,
|
| 173 |
-
step=0.1,
|
| 174 |
-
label="Guidance Scale",
|
| 175 |
-
info="Controls the strength of the guidance"
|
| 176 |
-
)
|
| 177 |
-
steps = gr.Slider(
|
| 178 |
-
minimum=1,
|
| 179 |
-
maximum=50,
|
| 180 |
-
value=10,
|
| 181 |
-
step=1,
|
| 182 |
-
label="Inference Steps",
|
| 183 |
-
info="Number of denoising steps"
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
generate_btn = gr.Button("π Generate Animation", variant="primary", size="lg")
|
| 187 |
-
|
| 188 |
-
# κ²°κ³Ό μΉμ
|
| 189 |
-
gr.Markdown("## π½οΈ Results")
|
| 190 |
-
with gr.Row():
|
| 191 |
-
with gr.Column():
|
| 192 |
-
animation_output = gr.Video(
|
| 193 |
-
label="Animation Result",
|
| 194 |
-
elem_id="animation_output"
|
| 195 |
)
|
| 196 |
with gr.Column():
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
# λ‘κ·Έ μΉμ
|
| 203 |
-
with gr.Accordion("π μ€ν λ‘κ·Έ", open=True):
|
| 204 |
-
logs_output = gr.Textbox(
|
| 205 |
-
label="Logs",
|
| 206 |
-
lines=10,
|
| 207 |
-
max_lines=20,
|
| 208 |
-
interactive=False,
|
| 209 |
-
elem_id="logs"
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
-
# μμ μΉμ
|
| 213 |
-
gr.Markdown("## π― Examples")
|
| 214 |
-
gr.Examples(
|
| 215 |
-
examples=[
|
| 216 |
-
["example_portrait.jpg", "example_audio.wav", 3.0, 10],
|
| 217 |
-
["example_portrait2.jpg", "example_audio2.wav", 2.5, 15],
|
| 218 |
-
],
|
| 219 |
-
inputs=[image_input, audio_input, guidance_scale, steps],
|
| 220 |
-
outputs=[animation_output, comparison_output, logs_output],
|
| 221 |
-
fn=generate_animation,
|
| 222 |
-
cache_examples=False
|
| 223 |
-
)
|
| 224 |
-
|
| 225 |
-
# μ΄λ²€νΈ νΈλ€λ¬
|
| 226 |
-
check_connection_btn.click(
|
| 227 |
-
fn=test_api_connection,
|
| 228 |
-
outputs=[connection_status, connection_status]
|
| 229 |
-
)
|
| 230 |
-
|
| 231 |
-
generate_btn.click(
|
| 232 |
-
fn=generate_animation,
|
| 233 |
-
inputs=[image_input, audio_input, guidance_scale, steps],
|
| 234 |
-
outputs=[animation_output, comparison_output, logs_output]
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
# νμ΄μ§ λ‘λ μ API μ°κ²° ν
μ€νΈ
|
| 238 |
-
demo.load(
|
| 239 |
-
fn=test_api_connection,
|
| 240 |
-
outputs=[connection_status, connection_status]
|
| 241 |
-
)
|
| 242 |
|
| 243 |
-
# μ± μ€ν
|
| 244 |
if __name__ == "__main__":
|
| 245 |
-
demo.launch(
|
| 246 |
-
server_name="0.0.0.0",
|
| 247 |
-
server_port=7860,
|
| 248 |
-
share=False,
|
| 249 |
-
debug=True
|
| 250 |
-
)
|
|
|
|
| 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:
|
| 49 |
+
msg = f"[{now}] μλ² μ°κ²° μ€ν¨ β : {e}"
|
| 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),
|
| 68 |
guidance_scale=guidance_scale,
|
| 69 |
steps=steps,
|
| 70 |
api_name="/generate_animation"
|
| 71 |
)
|
| 72 |
+
|
| 73 |
+
progress(0.95, desc="κ²°κ³Ό μ 리")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
if isinstance(result, (list, tuple)) and len(result) >= 2:
|
| 75 |
+
anim, comp = result[0], result[1]
|
| 76 |
+
logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] μ±κ³΅")
|
| 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 |
+
gr.Button("μλ² μ°κ²° ν
μ€νΈ").click(
|
| 91 |
+
test_api_connection,
|
| 92 |
+
outputs=[status_box, status_box]
|
| 93 |
+
)
|
| 94 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
gr.Markdown("---")
|
|
|
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
| 98 |
+
img_in = gr.Image(type="filepath", label="Portrait")
|
| 99 |
+
aud_in = gr.Audio(type="filepath", label="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 |
+
gr.Button("π Generate").click(
|
| 103 |
+
generate_animation,
|
| 104 |
+
inputs=[img_in, aud_in, scale, steps],
|
| 105 |
+
outputs=["animation_out", "comp_out", "log_out"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
with gr.Column():
|
| 108 |
+
gr.Video(label="Animation Result", elem_id="animation_out")
|
| 109 |
+
gr.Video(label="Side-by-side", elem_id="comp_out")
|
| 110 |
+
|
| 111 |
+
with gr.Accordion("λ‘κ·Έ", open=False):
|
| 112 |
+
gr.Textbox(label="μ€ν λ‘κ·Έ", lines=12, max_lines=20, interactive=False, elem_id="log_out")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
|
|
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
demo.queue(max_size=4).launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|