Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,16 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from gradio_client import Client
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
# -----------------------------
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# -----------------------------
|
| 10 |
def tryon_local(person_img, garment_img, seed, randomize_seed):
|
| 11 |
if person_img is None or garment_img is None:
|
|
@@ -35,33 +41,37 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
|
|
| 35 |
return overlay, seed, info
|
| 36 |
|
| 37 |
# -----------------------------
|
| 38 |
-
# Hunyuan3D API client
|
| 39 |
# -----------------------------
|
| 40 |
-
# Set your API key here
|
| 41 |
-
API_KEY = "YOUR_HUNYUAN3D_API_KEY"
|
| 42 |
hunyuan_client = Client("tencent/Hunyuan3D-2.1", hf_token=API_KEY)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
| 50 |
-
tryon_img, seed_used,
|
| 51 |
if tryon_img is None:
|
| 52 |
-
return None, "
|
| 53 |
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
nparr = np.frombuffer(result_bytes, np.uint8)
|
| 63 |
-
result_img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
|
| 64 |
-
return result_img, tryon_info
|
| 65 |
except Exception as e:
|
| 66 |
return None, f"Hunyuan3D API error: {e}"
|
| 67 |
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from gradio_client import Client
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
+
# Hugging Face API Key
|
| 10 |
+
# -----------------------------
|
| 11 |
+
API_KEY = "HF_Token" # <-- Replace with your Hugging Face token
|
| 12 |
+
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# Local "try-on" simulation
|
| 15 |
# -----------------------------
|
| 16 |
def tryon_local(person_img, garment_img, seed, randomize_seed):
|
| 17 |
if person_img is None or garment_img is None:
|
|
|
|
| 41 |
return overlay, seed, info
|
| 42 |
|
| 43 |
# -----------------------------
|
| 44 |
+
# Initialize Hunyuan3D API client
|
| 45 |
# -----------------------------
|
|
|
|
|
|
|
| 46 |
hunyuan_client = Client("tencent/Hunyuan3D-2.1", hf_token=API_KEY)
|
| 47 |
|
| 48 |
+
# -----------------------------
|
| 49 |
+
# Convert NumPy image to bytes for API
|
| 50 |
+
# -----------------------------
|
| 51 |
+
def np_to_bytes(img: np.ndarray):
|
| 52 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 53 |
+
pil_img = Image.fromarray(img_rgb)
|
| 54 |
+
buf = BytesIO()
|
| 55 |
+
pil_img.save(buf, format="PNG")
|
| 56 |
+
buf.seek(0)
|
| 57 |
+
return buf.read()
|
| 58 |
|
| 59 |
+
# -----------------------------
|
| 60 |
+
# Try-on using Hunyuan3D
|
| 61 |
+
# -----------------------------
|
| 62 |
def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
| 63 |
+
tryon_img, seed_used, info = tryon_local(person_img, garment_img, seed, randomize_seed)
|
| 64 |
if tryon_img is None:
|
| 65 |
+
return None, "Local try-on failed"
|
| 66 |
|
| 67 |
try:
|
| 68 |
+
person_bytes = np_to_bytes(person_img)
|
| 69 |
+
garment_bytes = np_to_bytes(garment_img)
|
| 70 |
+
|
| 71 |
+
# Use the correct endpoint without keyword args
|
| 72 |
+
result_bytes = hunyuan_client.predict(person_bytes, garment_bytes)
|
| 73 |
+
result_img = np.array(Image.open(BytesIO(result_bytes)))
|
| 74 |
+
return result_img, info + " + Hunyuan3D processed"
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
return None, f"Hunyuan3D API error: {e}"
|
| 77 |
|