Monikashyapa commited on
Commit
a100145
·
verified ·
1 Parent(s): 8281a9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -32
app.py CHANGED
@@ -2,16 +2,22 @@ import gradio as gr
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 = "HT_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,39 +47,26 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
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
 
78
  # -----------------------------
79
  # Gradio UI
 
2
  import cv2
3
  import numpy as np
4
  from gradio_client import Client
5
+ import getpass # for secure token input
 
6
 
7
  # -----------------------------
8
+ # Prompt for Hugging Face API key
9
  # -----------------------------
10
+ API_KEY = getpass.getpass("Enter your Hugging Face API key: ").strip()
11
+
12
+ try:
13
+ hunyuan_client = Client("tencent/Hunyuan3D-2.1", hf_token=API_KEY)
14
+ print("Hunyuan3D client loaded successfully!")
15
+ except Exception as e:
16
+ print(f"Failed to load Hunyuan3D client: {e}")
17
+ hunyuan_client = None
18
 
19
  # -----------------------------
20
+ # Local "try-on" function
21
  # -----------------------------
22
  def tryon_local(person_img, garment_img, seed, randomize_seed):
23
  if person_img is None or garment_img is None:
 
47
  return overlay, seed, info
48
 
49
  # -----------------------------
50
+ # Hunyuan3D API call
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # -----------------------------
52
  def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
53
+ overlay_img, seed_used, info = tryon_local(person_img, garment_img, seed, randomize_seed)
54
+ if overlay_img is None:
55
+ return None, info
56
+
57
+ if hunyuan_client is None:
58
+ return overlay_img, "Hunyuan3D client not loaded"
59
 
60
  try:
61
+ # Convert image to bytes to send via Gradio client
62
+ _, img_encoded = cv2.imencode(".png", overlay_img)
63
+ img_bytes = img_encoded.tobytes()
64
 
65
+ # Predict with the client
66
+ result = hunyuan_client.predict(img_bytes)
67
+ return result, "Success (3D try-on)"
 
68
  except Exception as e:
69
+ return overlay_img, f"Hunyuan3D API error: {e}"
70
 
71
  # -----------------------------
72
  # Gradio UI