Monikashyapa commited on
Commit
b793cb4
·
verified ·
1 Parent(s): 9d9631e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -48
app.py CHANGED
@@ -1,68 +1,61 @@
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
- # Local "try-on" function
9
  # -----------------------------
10
- def tryon_local(person_img, garment_img, seed, randomize_seed):
11
- if person_img is None or garment_img is None:
12
- return None, None, "Empty image"
13
-
14
- h_person, w_person = person_img.shape[:2]
15
- h_garment, w_garment = garment_img.shape[:2]
16
-
17
- scale = w_person / w_garment
18
- new_w = int(w_garment * scale)
19
- new_h = int(h_garment * scale)
20
- garment_resized = cv2.resize(garment_img, (new_w, new_h))
21
-
22
- overlay = person_img.copy()
23
- y_offset = 0
24
- x_offset = max(0, (w_person - new_w) // 2)
25
-
26
- if garment_resized.shape[2] == 4:
27
- alpha = garment_resized[:, :, 3] / 255.0
28
- for c in range(3):
29
- overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c] = \
30
- alpha * garment_resized[:, :, c] + (1 - alpha) * overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w, c]
31
- else:
32
- overlay[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = garment_resized
33
-
34
- info = "Success (local simulation)"
35
- return overlay, seed, info
36
-
37
- # -----------------------------
38
- # Hunyuan3D API client
39
- # -----------------------------
40
- API_KEY = os.getenv("HF_API_KEY", "hf_YOUR_TOKEN_HERE") # Replace with your token if not using env var
41
- hunyuan_client = None
42
  try:
43
  hunyuan_client = Client("tencent/Hunyuan3D-2.1", hf_token=API_KEY)
44
- print("Hunyuan3D client loaded successfully!")
 
 
45
  except Exception as e:
46
  print(f"Failed to load Hunyuan3D client: {e}")
47
  hunyuan_client = None
 
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
50
- # If Hunyuan3D client is not loaded, fall back to local
51
- if hunyuan_client is None:
52
- result_img, seed_used, info = tryon_local(person_img, garment_img, seed, randomize_seed)
53
- return result_img, info
 
54
 
55
- # Call Hunyuan3D API
56
  try:
57
- # Convert images to bytes for API
58
- _, person_bytes = cv2.imencode(".png", person_img)
59
- _, garment_bytes = cv2.imencode(".png", garment_img)
60
- result_3d = hunyuan_client.predict(person_bytes.tobytes(), garment_bytes.tobytes())
61
- return result_3d, "Success (Hunyuan3D)"
 
 
 
 
 
 
 
 
 
62
  except Exception as e:
63
- # Fallback to local try-on on API error
64
- result_img, seed_used, info = tryon_local(person_img, garment_img, seed, randomize_seed)
65
- return result_img, f"Hunyuan3D API error: {e}. Used local simulation instead."
66
 
67
  # -----------------------------
68
  # Gradio UI
 
 
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
+ # Hunyuan3D API setup
10
  # -----------------------------
11
+ API_KEY = "HT_Token" # Replace with your valid 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"Failed to load Hunyuan3D client: {e}")
19
  hunyuan_client = None
20
+ fn_index_default = 0
21
+ client_loaded = False
22
 
23
+ # -----------------------------
24
+ # Convert numpy image to bytes
25
+ # -----------------------------
26
+ def np_to_bytes(img_np):
27
+ img_pil = Image.fromarray(cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB))
28
+ buf = io.BytesIO()
29
+ img_pil.save(buf, format="PNG")
30
+ return buf.getvalue()
31
+
32
+ # -----------------------------
33
+ # Try-on function using Hunyuan3D API
34
+ # -----------------------------
35
  def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
36
+ if not client_loaded:
37
+ return None, "Hunyuan3D client not loaded. Please check your API key."
38
+
39
+ if person_img is None or garment_img is None:
40
+ return None, "Empty image provided."
41
 
 
42
  try:
43
+ # Convert to bytes
44
+ person_bytes = np_to_bytes(person_img)
45
+ garment_bytes = np_to_bytes(garment_img)
46
+
47
+ # Call the 3D try-on endpoint
48
+ result = hunyuan_client.predict(
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 None, f"Hunyuan3D API error: {e}"
 
 
59
 
60
  # -----------------------------
61
  # Gradio UI