Spaces:
Runtime error
Runtime error
hamzaanwar12 commited on
Commit ·
2c41964
1
Parent(s): b9ca341
only gradio
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0"
|
| 3 |
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
| 4 |
os.environ["HTTP_PROXY"] = ""
|
|
@@ -283,23 +286,23 @@ def pose_transfer(source_image, test_pair_index):
|
|
| 283 |
bsz = 1
|
| 284 |
|
| 285 |
c_new = torch.cat([c_new[:bsz], c_new[:bsz], c_new[bsz:]])
|
| 286 |
-
down_block_additional_residuals = [torch.cat([torch.zeros_like(sample), sample, sample]).to(dtype=weight_dtype)
|
| 287 |
-
|
| 288 |
-
up_block_additional_residuals = {k: torch.cat([torch.zeros_like(v), torch.zeros_like(v), v]).to(dtype=weight_dtype)
|
| 289 |
-
|
| 290 |
|
| 291 |
noise_scheduler.set_timesteps(cfg.TEST.NUM_INFERENCE_STEPS)
|
| 292 |
for t in noise_scheduler.timesteps:
|
| 293 |
inputs = torch.cat([noisy_latents, noisy_latents, noisy_latents], dim=0)
|
| 294 |
inputs = noise_scheduler.scale_model_input(inputs, timestep=t)
|
| 295 |
noise_pred = unet(sample=inputs, timestep=t, encoder_hidden_states=c_new,
|
| 296 |
-
|
| 297 |
-
|
| 298 |
|
| 299 |
noise_pred_uc, noise_pred_down, noise_pred_full = noise_pred.chunk(3)
|
| 300 |
noise_pred = noise_pred_uc + \
|
| 301 |
-
|
| 302 |
-
|
| 303 |
noisy_latents = noise_scheduler.step(noise_pred, t, noisy_latents)[0]
|
| 304 |
|
| 305 |
sampling_imgs = vae.decode(noisy_latents) * 0.5 + 0.5 # denormalize
|
|
@@ -342,52 +345,7 @@ def cancel_download_fn():
|
|
| 342 |
return "Download cancelled."
|
| 343 |
|
| 344 |
# ==============================
|
| 345 |
-
#
|
| 346 |
-
# ==============================
|
| 347 |
-
def api_pose_transfer(source_image_base64: str, test_pair_index: int):
|
| 348 |
-
"""API function for pose transfer."""
|
| 349 |
-
try:
|
| 350 |
-
if not model_ready:
|
| 351 |
-
return {
|
| 352 |
-
"output_image": "",
|
| 353 |
-
"success": False,
|
| 354 |
-
"message": "Models not ready. Please download models first."
|
| 355 |
-
}
|
| 356 |
-
|
| 357 |
-
# Decode base64 image
|
| 358 |
-
image_data = base64.b64decode(source_image_base64)
|
| 359 |
-
source_image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 360 |
-
|
| 361 |
-
# Perform pose transfer
|
| 362 |
-
output_image = pose_transfer(source_image, test_pair_index)
|
| 363 |
-
|
| 364 |
-
# Convert output to base64
|
| 365 |
-
buffered = io.BytesIO()
|
| 366 |
-
output_image.save(buffered, format="PNG")
|
| 367 |
-
output_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 368 |
-
|
| 369 |
-
return {
|
| 370 |
-
"output_image": output_base64,
|
| 371 |
-
"success": True,
|
| 372 |
-
"message": "Pose transfer successful"
|
| 373 |
-
}
|
| 374 |
-
|
| 375 |
-
except Exception as e:
|
| 376 |
-
return {
|
| 377 |
-
"output_image": "",
|
| 378 |
-
"success": False,
|
| 379 |
-
"message": f"Error during pose transfer: {str(e)}"
|
| 380 |
-
}
|
| 381 |
-
|
| 382 |
-
def api_status():
|
| 383 |
-
"""API function to check model status."""
|
| 384 |
-
return {
|
| 385 |
-
"model_ready": model_ready,
|
| 386 |
-
"test_pairs_count": len(test_pairs) if test_pairs is not None else 0
|
| 387 |
-
}
|
| 388 |
-
|
| 389 |
-
# ==============================
|
| 390 |
-
# GRADIO UI
|
| 391 |
# ==============================
|
| 392 |
def gradio_pose_transfer(source_image, test_pair_index):
|
| 393 |
"""Gradio interface for pose transfer."""
|
|
@@ -433,9 +391,6 @@ with gr.Blocks() as demo:
|
|
| 433 |
with gr.Tab("Pose Transfer"):
|
| 434 |
gr.Markdown("## Pose Transfer")
|
| 435 |
|
| 436 |
-
# Initialize test_pairs_count after models are loaded
|
| 437 |
-
test_pairs_count = gr.State(value=len(test_pairs) if test_pairs is not None else 0)
|
| 438 |
-
|
| 439 |
with gr.Row():
|
| 440 |
with gr.Column():
|
| 441 |
source_image = gr.Image(label="Source Image", type="pil")
|
|
@@ -443,7 +398,7 @@ with gr.Blocks() as demo:
|
|
| 443 |
label="Test Pair Index",
|
| 444 |
value=0,
|
| 445 |
minimum=0,
|
| 446 |
-
maximum=4039
|
| 447 |
)
|
| 448 |
generate_btn = gr.Button("🚀 Generate Pose Transfer")
|
| 449 |
|
|
@@ -456,24 +411,6 @@ with gr.Blocks() as demo:
|
|
| 456 |
inputs=[source_image, test_pair_index],
|
| 457 |
outputs=[output_image, status_message]
|
| 458 |
)
|
| 459 |
-
|
| 460 |
-
# Add API endpoints using Gradio's built-in API
|
| 461 |
-
demo.add_api(
|
| 462 |
-
[
|
| 463 |
-
gr.Endpoint(
|
| 464 |
-
fn=api_pose_transfer,
|
| 465 |
-
inputs=[gr.Textbox(label="source_image"), gr.Number(label="test_pair_index")],
|
| 466 |
-
outputs=gr.JSON(label="response")
|
| 467 |
-
),
|
| 468 |
-
gr.Endpoint(
|
| 469 |
-
fn=api_status,
|
| 470 |
-
inputs=[],
|
| 471 |
-
outputs=gr.JSON(label="status")
|
| 472 |
-
)
|
| 473 |
-
],
|
| 474 |
-
title="Pose Transfer API",
|
| 475 |
-
description="API for pose transfer functionality"
|
| 476 |
-
)
|
| 477 |
|
| 478 |
# ==============================
|
| 479 |
# START APP
|
|
@@ -484,4 +421,4 @@ if __name__ == "__main__":
|
|
| 484 |
model_ready = True
|
| 485 |
initialize_models()
|
| 486 |
|
| 487 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
# Prevent libgomp crashes in Spaces
|
| 3 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
| 4 |
+
|
| 5 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0"
|
| 6 |
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
| 7 |
os.environ["HTTP_PROXY"] = ""
|
|
|
|
| 286 |
bsz = 1
|
| 287 |
|
| 288 |
c_new = torch.cat([c_new[:bsz], c_new[:bsz], c_new[bsz:]])
|
| 289 |
+
down_block_additional_residuals = [torch.cat([torch.zeros_like(sample), sample, sample]).to(dtype=weight_dtype)
|
| 290 |
+
for sample in down_block_additional_residuals]
|
| 291 |
+
up_block_additional_residuals = {k: torch.cat([torch.zeros_like(v), torch.zeros_like(v), v]).to(dtype=weight_dtype)
|
| 292 |
+
for k, v in up_block_additional_residuals.items()}
|
| 293 |
|
| 294 |
noise_scheduler.set_timesteps(cfg.TEST.NUM_INFERENCE_STEPS)
|
| 295 |
for t in noise_scheduler.timesteps:
|
| 296 |
inputs = torch.cat([noisy_latents, noisy_latents, noisy_latents], dim=0)
|
| 297 |
inputs = noise_scheduler.scale_model_input(inputs, timestep=t)
|
| 298 |
noise_pred = unet(sample=inputs, timestep=t, encoder_hidden_states=c_new,
|
| 299 |
+
down_block_additional_residuals=copy.deepcopy(down_block_additional_residuals),
|
| 300 |
+
up_block_additional_residuals=copy.deepcopy(up_block_additional_residuals))
|
| 301 |
|
| 302 |
noise_pred_uc, noise_pred_down, noise_pred_full = noise_pred.chunk(3)
|
| 303 |
noise_pred = noise_pred_uc + \
|
| 304 |
+
cfg.TEST.DOWN_BLOCK_GUIDANCE_SCALE * (noise_pred_down - noise_pred_uc) + \
|
| 305 |
+
cfg.TEST.FULL_GUIDANCE_SCALE * (noise_pred_full - noise_pred_down)
|
| 306 |
noisy_latents = noise_scheduler.step(noise_pred, t, noisy_latents)[0]
|
| 307 |
|
| 308 |
sampling_imgs = vae.decode(noisy_latents) * 0.5 + 0.5 # denormalize
|
|
|
|
| 345 |
return "Download cancelled."
|
| 346 |
|
| 347 |
# ==============================
|
| 348 |
+
# GRADIO UI ONLY (NO API ENDPOINTS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
# ==============================
|
| 350 |
def gradio_pose_transfer(source_image, test_pair_index):
|
| 351 |
"""Gradio interface for pose transfer."""
|
|
|
|
| 391 |
with gr.Tab("Pose Transfer"):
|
| 392 |
gr.Markdown("## Pose Transfer")
|
| 393 |
|
|
|
|
|
|
|
|
|
|
| 394 |
with gr.Row():
|
| 395 |
with gr.Column():
|
| 396 |
source_image = gr.Image(label="Source Image", type="pil")
|
|
|
|
| 398 |
label="Test Pair Index",
|
| 399 |
value=0,
|
| 400 |
minimum=0,
|
| 401 |
+
maximum=4039
|
| 402 |
)
|
| 403 |
generate_btn = gr.Button("🚀 Generate Pose Transfer")
|
| 404 |
|
|
|
|
| 411 |
inputs=[source_image, test_pair_index],
|
| 412 |
outputs=[output_image, status_message]
|
| 413 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
|
| 415 |
# ==============================
|
| 416 |
# START APP
|
|
|
|
| 421 |
model_ready = True
|
| 422 |
initialize_models()
|
| 423 |
|
| 424 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|