Update app.py
Browse files
app.py
CHANGED
@@ -2,58 +2,57 @@ import os
|
|
2 |
import requests
|
3 |
import json
|
4 |
import time
|
|
|
|
|
|
|
|
|
5 |
import gradio as gr
|
6 |
|
7 |
MAX_SEED = 999999
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
if upload_response.status_code == 200:
|
15 |
-
return upload_response.json().get("data", {}).get("url", None)
|
16 |
-
else:
|
17 |
-
print("Image upload failed:", upload_response.text)
|
18 |
-
return None
|
19 |
|
20 |
# Main try-on function using Pixelcut API
|
21 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
22 |
start_time = time.time()
|
23 |
|
24 |
-
# Check if images are provided
|
25 |
if person_img is None or garment_img is None:
|
26 |
return None, None, "Empty image"
|
27 |
|
28 |
-
# Handle seed randomization
|
29 |
if randomize_seed:
|
30 |
seed = random.randint(0, MAX_SEED)
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# Load Pixelcut API key from environment
|
40 |
-
pixelcut_api_key = os.environ.get('pixelcut_api_key')
|
41 |
-
if not pixelcut_api_key:
|
42 |
-
return None, None, "API key missing β check environment settings"
|
43 |
-
|
44 |
-
# Setup API endpoint and headers
|
45 |
-
url = "https://api.developer.pixelcut.ai/v1/try-on"
|
46 |
headers = {
|
47 |
'Content-Type': 'application/json',
|
48 |
'Accept': 'application/json',
|
49 |
'X-API-KEY': pixelcut_api_key
|
50 |
}
|
51 |
|
52 |
-
# Setup request payload
|
53 |
data = {
|
54 |
-
"
|
55 |
-
"garment_image_url": garment_url,
|
56 |
-
"seed": seed,
|
57 |
"format": "png"
|
58 |
}
|
59 |
|
@@ -64,12 +63,12 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
64 |
try:
|
65 |
session = requests.Session()
|
66 |
|
67 |
-
# Retry loop for "Too many users" error
|
68 |
for attempt in range(max_retries):
|
69 |
response = session.post(url, headers=headers, json=data, timeout=60)
|
70 |
print("Response code:", response.status_code)
|
71 |
|
72 |
-
#
|
73 |
if response.status_code == 200:
|
74 |
result_url = response.json().get('result_url', '')
|
75 |
print("β
Success:", result_url)
|
@@ -77,13 +76,13 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
77 |
# If Pixelcut returns a result URL, fetch the final image
|
78 |
if result_url:
|
79 |
result_img_data = requests.get(result_url).content
|
80 |
-
|
81 |
-
result_img
|
82 |
info = "Success"
|
83 |
break
|
84 |
|
85 |
else:
|
86 |
-
# If API returns "Too many users" error, retry
|
87 |
print("Full response:", response.text)
|
88 |
if "Too many users" in response.text:
|
89 |
print(f"Attempt {attempt + 1}/{max_retries}: API busy. Retrying in {retry_delay} seconds...")
|
@@ -93,16 +92,16 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
93 |
break
|
94 |
|
95 |
else:
|
96 |
-
# If all retries fail
|
97 |
info = "API still overloaded β please try again later."
|
98 |
|
99 |
-
# Handle timeouts specifically
|
100 |
except requests.exceptions.ReadTimeout:
|
101 |
print("Timeout!")
|
102 |
info = "Too many users, please try again later"
|
103 |
raise gr.Error("Too many users, please try again later")
|
104 |
|
105 |
-
# Handle any other unexpected errors
|
106 |
except Exception as err:
|
107 |
print(f"β Other error: {err}")
|
108 |
info = "Error, please contact the admin"
|
@@ -112,7 +111,6 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
112 |
|
113 |
return result_img, seed, info
|
114 |
|
115 |
-
|
116 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
117 |
|
118 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import time
|
5 |
+
import cv2
|
6 |
+
import base64
|
7 |
+
import random
|
8 |
+
import numpy as np
|
9 |
import gradio as gr
|
10 |
|
11 |
MAX_SEED = 999999
|
12 |
|
13 |
+
# β
Hardcoded Pixelcut API key
|
14 |
+
pixelcut_api_key = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
|
15 |
+
|
16 |
+
|
17 |
+
# Convert images to PNG format
|
18 |
+
def convert_to_png(image):
|
19 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
20 |
+
|
21 |
+
|
22 |
+
# Encode images to base64 PNG format
|
23 |
+
def encode_image(image):
|
24 |
+
image = convert_to_png(image)
|
25 |
+
_, buffer = cv2.imencode('.png', image)
|
26 |
+
return base64.b64encode(buffer).decode('utf-8')
|
27 |
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Main try-on function using Pixelcut API
|
30 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
31 |
start_time = time.time()
|
32 |
|
33 |
+
# π Check if images are provided
|
34 |
if person_img is None or garment_img is None:
|
35 |
return None, None, "Empty image"
|
36 |
|
37 |
+
# π² Handle seed randomization
|
38 |
if randomize_seed:
|
39 |
seed = random.randint(0, MAX_SEED)
|
40 |
|
41 |
+
# π₯ Convert images to base64 PNG
|
42 |
+
encoded_person_img = encode_image(person_img)
|
43 |
+
encoded_garment_img = encode_image(garment_img)
|
44 |
|
45 |
+
# π Setup Pixelcut API endpoint and headers
|
46 |
+
url = "https://api.developer.pixelcut.ai/v1/remove-background"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
headers = {
|
48 |
'Content-Type': 'application/json',
|
49 |
'Accept': 'application/json',
|
50 |
'X-API-KEY': pixelcut_api_key
|
51 |
}
|
52 |
|
53 |
+
# π© Setup request payload (matches the curl command structure)
|
54 |
data = {
|
55 |
+
"image_url": "https://cdn3.pixelcut.app/product.jpg",
|
|
|
|
|
56 |
"format": "png"
|
57 |
}
|
58 |
|
|
|
63 |
try:
|
64 |
session = requests.Session()
|
65 |
|
66 |
+
# π Retry loop for "Too many users" error
|
67 |
for attempt in range(max_retries):
|
68 |
response = session.post(url, headers=headers, json=data, timeout=60)
|
69 |
print("Response code:", response.status_code)
|
70 |
|
71 |
+
# β
Success β process the result
|
72 |
if response.status_code == 200:
|
73 |
result_url = response.json().get('result_url', '')
|
74 |
print("β
Success:", result_url)
|
|
|
76 |
# If Pixelcut returns a result URL, fetch the final image
|
77 |
if result_url:
|
78 |
result_img_data = requests.get(result_url).content
|
79 |
+
result_np = np.frombuffer(result_img_data, np.uint8)
|
80 |
+
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
81 |
info = "Success"
|
82 |
break
|
83 |
|
84 |
else:
|
85 |
+
# π₯ If API returns "Too many users" error, retry with delay
|
86 |
print("Full response:", response.text)
|
87 |
if "Too many users" in response.text:
|
88 |
print(f"Attempt {attempt + 1}/{max_retries}: API busy. Retrying in {retry_delay} seconds...")
|
|
|
92 |
break
|
93 |
|
94 |
else:
|
95 |
+
# β If all retries fail
|
96 |
info = "API still overloaded β please try again later."
|
97 |
|
98 |
+
# π Handle timeouts specifically
|
99 |
except requests.exceptions.ReadTimeout:
|
100 |
print("Timeout!")
|
101 |
info = "Too many users, please try again later"
|
102 |
raise gr.Error("Too many users, please try again later")
|
103 |
|
104 |
+
# β Handle any other unexpected errors
|
105 |
except Exception as err:
|
106 |
print(f"β Other error: {err}")
|
107 |
info = "Error, please contact the admin"
|
|
|
111 |
|
112 |
return result_img, seed, info
|
113 |
|
|
|
114 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
115 |
|
116 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|