Zuii commited on
Commit
593aefc
Β·
verified Β·
1 Parent(s): a474bb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -24
app.py CHANGED
@@ -10,8 +10,11 @@ import gradio as gr
10
 
11
  MAX_SEED = 999999
12
 
13
- # βœ… New Pixelcut API key
14
- pixelcut_api_key = "sk_3a15d5b79d5944f3995c54615f8928f8"
 
 
 
15
 
16
  # 🎯 Convert image to PNG format
17
  def convert_to_png(image):
@@ -23,15 +26,16 @@ def encode_image(image):
23
  _, buffer = cv2.imencode('.png', image)
24
  return base64.b64encode(buffer).decode('utf-8')
25
 
26
- # πŸ› οΈ Upload images to a temporary hosting service
27
- def upload_image(image_data):
28
- files = {"file": ("image.png", image_data, "image/png")}
29
- upload_response = requests.post("https://tmpfiles.org/api/v1/upload", files=files)
30
-
31
- if upload_response.status_code == 200:
32
- return upload_response.json().get("data", {}).get("url", None)
 
33
  else:
34
- print("❌ Image upload failed:", upload_response.text)
35
  return None
36
 
37
  # πŸš€ Main try-on function using Pixelcut API
@@ -50,12 +54,12 @@ def tryon(person_img, garment_img, seed, randomize_seed):
50
  _, person_encoded = cv2.imencode('.png', convert_to_png(person_img))
51
  _, garment_encoded = cv2.imencode('.png', convert_to_png(garment_img))
52
 
53
- # βœ… Upload person and garment images to get URLs
54
- person_url = upload_image(person_encoded)
55
- garment_url = upload_image(garment_encoded)
56
 
57
  if not person_url or not garment_url:
58
- return None, None, "Image upload failed"
59
 
60
  # 🌟 Setup Pixelcut API endpoint and headers
61
  url = "https://api.developer.pixelcut.ai/v1/remove-background"
@@ -65,16 +69,9 @@ def tryon(person_img, garment_img, seed, randomize_seed):
65
  'X-API-KEY': pixelcut_api_key
66
  }
67
 
68
- # πŸ”§ Use uploaded URLs in the payload
69
- person_data = {
70
- "image_url": person_url,
71
- "format": "png"
72
- }
73
-
74
- garment_data = {
75
- "image_url": garment_url,
76
- "format": "png"
77
- }
78
 
79
  result_img = None
80
  max_retries = 5
@@ -135,6 +132,7 @@ def tryon(person_img, garment_img, seed, randomize_seed):
135
 
136
 
137
 
 
138
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
139
 
140
  garm_list = os.listdir(os.path.join(example_path,"cloth"))
 
10
 
11
  MAX_SEED = 999999
12
 
13
+ # βœ… Pixelcut API Key
14
+ pixelcut_api_key = "YOUR_PIXELCUT_API_KEY"
15
+
16
+ # βœ… ImgBB API Key (for uploading images to get valid URLs)
17
+ imgbb_api_key = "YOUR_IMGBB_API_KEY"
18
 
19
  # 🎯 Convert image to PNG format
20
  def convert_to_png(image):
 
26
  _, buffer = cv2.imencode('.png', image)
27
  return base64.b64encode(buffer).decode('utf-8')
28
 
29
+ # πŸ› οΈ Upload images to ImgBB to get permanent URLs
30
+ def upload_image_to_imgbb(image_data):
31
+ url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
32
+ files = {"image": base64.b64encode(image_data).decode('utf-8')}
33
+ response = requests.post(url, files=files)
34
+
35
+ if response.status_code == 200:
36
+ return response.json().get("data", {}).get("url")
37
  else:
38
+ print("❌ ImgBB upload failed:", response.text)
39
  return None
40
 
41
  # πŸš€ Main try-on function using Pixelcut API
 
54
  _, person_encoded = cv2.imencode('.png', convert_to_png(person_img))
55
  _, garment_encoded = cv2.imencode('.png', convert_to_png(garment_img))
56
 
57
+ # βœ… Upload person and garment images to get valid URLs
58
+ person_url = upload_image_to_imgbb(person_encoded)
59
+ garment_url = upload_image_to_imgbb(garment_encoded)
60
 
61
  if not person_url or not garment_url:
62
+ return None, None, "Image upload failed β€” check API keys or connection"
63
 
64
  # 🌟 Setup Pixelcut API endpoint and headers
65
  url = "https://api.developer.pixelcut.ai/v1/remove-background"
 
69
  'X-API-KEY': pixelcut_api_key
70
  }
71
 
72
+ # πŸ”§ Use the uploaded URLs in the payload
73
+ person_data = {"image_url": person_url, "format": "png"}
74
+ garment_data = {"image_url": garment_url, "format": "png"}
 
 
 
 
 
 
 
75
 
76
  result_img = None
77
  max_retries = 5
 
132
 
133
 
134
 
135
+
136
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
137
 
138
  garm_list = os.listdir(os.path.join(example_path,"cloth"))