Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,68 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from gradio_client import Client
|
| 5 |
-
import io
|
| 6 |
-
from PIL import Image
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
-
#
|
| 10 |
# -----------------------------
|
| 11 |
-
API_KEY = "
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
hunyuan_client = Client("tencent/Hunyuan3D-2.1", hf_token=API_KEY)
|
| 14 |
-
# Grab first endpoint index for the 3D try-on function
|
| 15 |
-
fn_index_default = hunyuan_client.endpoints[0].fn_index if hunyuan_client.endpoints else 0
|
| 16 |
-
client_loaded = True
|
| 17 |
except Exception as e:
|
| 18 |
-
print(f"
|
| 19 |
-
hunyuan_client = None
|
| 20 |
-
fn_index_default = 0
|
| 21 |
-
client_loaded = False
|
| 22 |
|
| 23 |
# -----------------------------
|
| 24 |
-
#
|
| 25 |
# -----------------------------
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# -----------------------------
|
| 33 |
-
# Try-on
|
| 34 |
# -----------------------------
|
| 35 |
def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
| 36 |
-
|
| 37 |
-
return None, "Hunyuan3D client not loaded. Please check your API key."
|
| 38 |
|
| 39 |
-
if
|
| 40 |
-
return
|
| 41 |
|
| 42 |
try:
|
| 43 |
-
# Convert to bytes
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
result
|
| 49 |
-
person_bytes,
|
| 50 |
-
garment_bytes,
|
| 51 |
-
fn_index=fn_index_default
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
# Convert returned bytes to image
|
| 55 |
-
result_img = np.array(Image.open(io.BytesIO(result)))
|
| 56 |
-
return cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR), "3D try-on successful!"
|
| 57 |
except Exception as e:
|
| 58 |
-
return
|
| 59 |
|
| 60 |
# -----------------------------
|
| 61 |
# Gradio UI
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
from gradio_client import Client
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# -----------------------------
|
| 8 |
+
# Load Hugging Face API key safely
|
| 9 |
# -----------------------------
|
| 10 |
+
API_KEY = os.environ.get("HF_TOKEN") # read from environment or Secrets
|
| 11 |
+
hunyuan_client = None
|
| 12 |
+
|
| 13 |
try:
|
| 14 |
hunyuan_client = Client("tencent/Hunyuan3D-2.1", hf_token=API_KEY)
|
|
|
|
|
|
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
+
print(f"Hunyuan3D client not loaded: {e}")
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# -----------------------------
|
| 19 |
+
# Local "try-on" function
|
| 20 |
# -----------------------------
|
| 21 |
+
def tryon_local(person_img, garment_img, seed, randomize_seed):
|
| 22 |
+
if person_img is None or garment_img is None:
|
| 23 |
+
return None, None, "Empty image"
|
| 24 |
+
|
| 25 |
+
h_person, w_person = person_img.shape[:2]
|
| 26 |
+
h_garment, w_garment = garment_img.shape[:2]
|
| 27 |
+
|
| 28 |
+
scale = w_person / w_garment
|
| 29 |
+
new_w = int(w_garment * scale)
|
| 30 |
+
new_h = int(h_garment * scale)
|
| 31 |
+
garment_resized = cv2.resize(garment_img, (new_w, new_h))
|
| 32 |
+
|
| 33 |
+
overlay = person_img.copy()
|
| 34 |
+
y_offset = 0
|
| 35 |
+
x_offset = max(0, (w_person - new_w) // 2)
|
| 36 |
+
|
| 37 |
+
if garment_resized.shape[2] == 4:
|
| 38 |
+
alpha = garment_resized[:, :, 3] / 255.0
|
| 39 |
+
for c in range(3):
|
| 40 |
+
overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c] = \
|
| 41 |
+
alpha * garment_resized[:, :, c] + (1 - alpha) * overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c]
|
| 42 |
+
else:
|
| 43 |
+
overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = garment_resized
|
| 44 |
+
|
| 45 |
+
info = "Success (local simulation)"
|
| 46 |
+
return overlay, seed, info
|
| 47 |
|
| 48 |
# -----------------------------
|
| 49 |
+
# Try-on via Hunyuan3D
|
| 50 |
# -----------------------------
|
| 51 |
def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
| 52 |
+
overlay_img, seed_used, info = tryon_local(person_img, garment_img, seed, randomize_seed)
|
|
|
|
| 53 |
|
| 54 |
+
if hunyuan_client is None:
|
| 55 |
+
return overlay_img, "Hunyuan3D client not loaded. Please check your API key."
|
| 56 |
|
| 57 |
try:
|
| 58 |
+
# Convert image to bytes before sending to API
|
| 59 |
+
_, buffer = cv2.imencode(".png", overlay_img)
|
| 60 |
+
img_bytes = buffer.tobytes()
|
| 61 |
+
# Call the API (most HF endpoints accept a single positional argument)
|
| 62 |
+
result = hunyuan_client.predict(img_bytes)
|
| 63 |
+
return result, "3D try-on completed!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
+
return overlay_img, f"Hunyuan3D API error: {e}"
|
| 66 |
|
| 67 |
# -----------------------------
|
| 68 |
# Gradio UI
|