Monikashyapa commited on
Commit
fe4ca82
·
verified ·
1 Parent(s): 7bf4efc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -1,18 +1,11 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- import base64
5
  from gradio_client import Client
 
6
 
7
  # -----------------------------
8
- # Helper: Convert NumPy image to base64
9
- # -----------------------------
10
- def np_to_base64(img):
11
- _, buffer = cv2.imencode(".png", img)
12
- return base64.b64encode(buffer).decode("utf-8")
13
-
14
- # -----------------------------
15
- # Local "try-on" simulation
16
  # -----------------------------
17
  def tryon_local(person_img, garment_img, seed, randomize_seed):
18
  if person_img is None or garment_img is None:
@@ -44,24 +37,31 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
44
  # -----------------------------
45
  # Hunyuan3D API client
46
  # -----------------------------
47
- hunyuan_client = Client("tencent/Hunyuan3D-2.1")
 
 
48
 
49
- # Get default fn_index
50
- fn_index_default = hunyuan_client.endpoints[0].fn_index if hunyuan_client.endpoints else 0
 
 
51
 
52
  def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
53
- # Run local try-on first
54
  tryon_img, seed_used, tryon_info = tryon_local(person_img, garment_img, seed, randomize_seed)
55
  if tryon_img is None:
56
  return None, "Try-on failed"
57
 
58
  try:
59
- # Convert image to base64 for API
60
- img_b64 = np_to_base64(tryon_img)
61
-
62
- # Hunyuan3D API only accepts positional arguments (list)
63
- result = hunyuan_client.predict([img_b64], fn_index=fn_index_default)
64
- return result, tryon_info
 
 
 
 
65
  except Exception as e:
66
  return None, f"Hunyuan3D API error: {e}"
67
 
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
4
  from gradio_client import Client
5
+ import base64
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:
 
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
+ def image_to_bytes(img):
45
+ """Convert numpy image to PNG bytes for API upload"""
46
+ _, buf = cv2.imencode(".png", img)
47
+ return buf.tobytes()
48
 
49
  def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
 
50
  tryon_img, seed_used, tryon_info = tryon_local(person_img, garment_img, seed, randomize_seed)
51
  if tryon_img is None:
52
  return None, "Try-on failed"
53
 
54
  try:
55
+ # Convert images to bytes to avoid JSON serialization errors
56
+ person_bytes = image_to_bytes(person_img)
57
+ garment_bytes = image_to_bytes(garment_img)
58
+
59
+ # Call Hunyuan3D API
60
+ result_bytes = hunyuan_client.predict([person_bytes, garment_bytes])
61
+ # Convert returned bytes back to numpy image
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