Update app.py
Browse files
app.py
CHANGED
|
@@ -9,148 +9,109 @@ import json
|
|
| 9 |
import time
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
if person_img is None or garment_img is None:
|
| 15 |
-
gr.Warning("Empty image")
|
| 16 |
return None, None, "Empty image"
|
|
|
|
|
|
|
| 17 |
if randomize_seed:
|
| 18 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
|
|
|
| 19 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 20 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 21 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 22 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
token = os.environ['token']
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
cookie = os.environ.get('Cookie', '')
|
| 29 |
-
referer = os.environ.get('referer', '')
|
| 30 |
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 31 |
data = {
|
| 32 |
"clothImage": encoded_garment_img,
|
| 33 |
"humanImage": encoded_person_img,
|
| 34 |
"seed": seed
|
| 35 |
}
|
| 36 |
-
try:
|
| 37 |
-
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
|
| 38 |
-
# print("post response code", response.status_code)
|
| 39 |
-
if response.status_code == 200:
|
| 40 |
-
result = response.json()['result']
|
| 41 |
-
status = result['status']
|
| 42 |
-
if status == "success":
|
| 43 |
-
uuid = result['result']
|
| 44 |
-
# print(uuid)
|
| 45 |
-
except Exception as err:
|
| 46 |
-
print(f"Post Exception Error: {err}")
|
| 47 |
-
raise gr.Error("Too many users, please try again later")
|
| 48 |
-
post_end_time = time.time()
|
| 49 |
-
print(f"post time used: {post_end_time-post_start_time}")
|
| 50 |
|
| 51 |
-
get_start_time =time.time()
|
| 52 |
-
time.sleep(9)
|
| 53 |
-
Max_Retry = 12
|
| 54 |
result_img = None
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if response.status_code == 200:
|
| 63 |
result = response.json()['result']
|
| 64 |
status = result['status']
|
|
|
|
| 65 |
if status == "success":
|
|
|
|
| 66 |
result = base64.b64decode(result['result'])
|
| 67 |
result_np = np.frombuffer(result, np.uint8)
|
| 68 |
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 69 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 70 |
info = "Success"
|
| 71 |
-
break
|
| 72 |
-
elif status == "error":
|
| 73 |
-
err_log = f"Status is Error"
|
| 74 |
-
info = "Error"
|
| 75 |
-
break
|
| 76 |
-
else:
|
| 77 |
-
# print(response.text)
|
| 78 |
-
err_log = "URL error, pleace contact the admin"
|
| 79 |
-
info = "URL error, pleace contact the admin"
|
| 80 |
-
break
|
| 81 |
-
except requests.exceptions.ReadTimeout:
|
| 82 |
-
err_log = "Http Timeout"
|
| 83 |
-
info = "Http Timeout, please try again later"
|
| 84 |
-
except Exception as err:
|
| 85 |
-
err_log = f"Get Exception Error: {err}"
|
| 86 |
-
time.sleep(1)
|
| 87 |
-
get_end_time = time.time()
|
| 88 |
-
print(f"get time used: {get_end_time-get_start_time}")
|
| 89 |
-
print(f"all time used: {get_end_time-get_start_time+post_end_time-post_start_time}")
|
| 90 |
-
if info == "":
|
| 91 |
-
err_log = f"No image after {Max_Retry} retries"
|
| 92 |
-
info = "Too many users, please try again later"
|
| 93 |
-
if info != "Success":
|
| 94 |
-
print(f"Error Log: {err_log}")
|
| 95 |
-
gr.Warning("Too many users, please try again later")
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
def start_tryon(person_img, garment_img, seed, randomize_seed):
|
| 100 |
-
start_time = time.time()
|
| 101 |
-
if person_img is None or garment_img is None:
|
| 102 |
-
return None, None, "Empty image"
|
| 103 |
-
if randomize_seed:
|
| 104 |
-
seed = random.randint(0, MAX_SEED)
|
| 105 |
-
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 106 |
-
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 107 |
-
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 108 |
-
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 109 |
-
|
| 110 |
-
url = "http://" + os.environ['tryon_url']
|
| 111 |
-
token = os.environ['token']
|
| 112 |
-
cookie = os.environ['Cookie']
|
| 113 |
-
referer = os.environ['referer']
|
| 114 |
-
|
| 115 |
-
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 116 |
-
data = {
|
| 117 |
-
"clothImage": encoded_garment_img,
|
| 118 |
-
"humanImage": encoded_person_img,
|
| 119 |
-
"seed": seed
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
result_img = None
|
| 123 |
-
try:
|
| 124 |
-
session = requests.Session()
|
| 125 |
-
response = session.post(url, headers=headers, data=json.dumps(data), timeout=60)
|
| 126 |
-
print("response code", response.status_code)
|
| 127 |
-
if response.status_code == 200:
|
| 128 |
-
result = response.json()['result']
|
| 129 |
-
status = result['status']
|
| 130 |
-
if status == "success":
|
| 131 |
-
result = base64.b64decode(result['result'])
|
| 132 |
-
result_np = np.frombuffer(result, np.uint8)
|
| 133 |
-
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 134 |
-
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 135 |
-
info = "Success"
|
| 136 |
else:
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
else:
|
| 139 |
-
|
| 140 |
-
info = "
|
|
|
|
|
|
|
| 141 |
except requests.exceptions.ReadTimeout:
|
| 142 |
print("timeout")
|
| 143 |
info = "Too many users, please try again later"
|
| 144 |
raise gr.Error("Too many users, please try again later")
|
|
|
|
|
|
|
| 145 |
except Exception as err:
|
| 146 |
-
print(f"
|
| 147 |
-
info = "Error,
|
|
|
|
| 148 |
end_time = time.time()
|
| 149 |
print(f"time used: {end_time-start_time}")
|
| 150 |
|
| 151 |
return result_img, seed, info
|
| 152 |
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 156 |
|
|
|
|
| 9 |
import time
|
| 10 |
|
| 11 |
|
| 12 |
+
import os
|
| 13 |
+
import cv2
|
| 14 |
+
import base64
|
| 15 |
+
import random
|
| 16 |
+
import requests
|
| 17 |
+
import json
|
| 18 |
+
import time
|
| 19 |
+
import numpy as np
|
| 20 |
+
import gradio as gr
|
| 21 |
+
|
| 22 |
+
MAX_SEED = 999999
|
| 23 |
+
|
| 24 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
| 25 |
+
start_time = time.time()
|
| 26 |
+
|
| 27 |
+
# Check if images are provided
|
| 28 |
if person_img is None or garment_img is None:
|
|
|
|
| 29 |
return None, None, "Empty image"
|
| 30 |
+
|
| 31 |
+
# Handle seed randomization
|
| 32 |
if randomize_seed:
|
| 33 |
seed = random.randint(0, MAX_SEED)
|
| 34 |
+
|
| 35 |
+
# Encode images to base64
|
| 36 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 37 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 38 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 39 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 40 |
|
| 41 |
+
# Load environment variables for API details
|
| 42 |
+
url = "http://" + os.environ['tryon_url']
|
| 43 |
token = os.environ['token']
|
| 44 |
+
cookie = os.environ.get('Cookie', '') # Optional cookie fallback
|
| 45 |
+
referer = os.environ.get('referer', '') # Optional referer fallback
|
| 46 |
|
| 47 |
+
# Define headers and data payload
|
|
|
|
|
|
|
| 48 |
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 49 |
data = {
|
| 50 |
"clothImage": encoded_garment_img,
|
| 51 |
"humanImage": encoded_person_img,
|
| 52 |
"seed": seed
|
| 53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
result_img = None
|
| 56 |
+
max_retries = 5
|
| 57 |
+
retry_delay = 5 # 5 seconds between retries
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
session = requests.Session()
|
| 61 |
+
|
| 62 |
+
# Retry loop for "Too many users" error
|
| 63 |
+
for attempt in range(max_retries):
|
| 64 |
+
response = session.post(url, headers=headers, data=json.dumps(data), timeout=60)
|
| 65 |
+
print("response code", response.status_code)
|
| 66 |
+
|
| 67 |
if response.status_code == 200:
|
| 68 |
result = response.json()['result']
|
| 69 |
status = result['status']
|
| 70 |
+
|
| 71 |
if status == "success":
|
| 72 |
+
# Decode the result image
|
| 73 |
result = base64.b64decode(result['result'])
|
| 74 |
result_np = np.frombuffer(result, np.uint8)
|
| 75 |
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 76 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 77 |
info = "Success"
|
| 78 |
+
break # β
Exit the loop when successful!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
else:
|
| 81 |
+
info = "Try again later"
|
| 82 |
+
print("Status returned:", status)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
+
print(response.text)
|
| 86 |
+
if "Too many users" in response.text:
|
| 87 |
+
print(f"Attempt {attempt + 1}/{max_retries}: API busy. Retrying in {retry_delay} seconds...")
|
| 88 |
+
time.sleep(retry_delay)
|
| 89 |
+
else:
|
| 90 |
+
info = "URL error, please contact the admin"
|
| 91 |
+
break # β If it's a different error, stop retrying
|
| 92 |
+
|
| 93 |
else:
|
| 94 |
+
# If all retries fail, return this message
|
| 95 |
+
info = "API still overloaded β please try again later."
|
| 96 |
+
|
| 97 |
+
# Handle timeouts specifically
|
| 98 |
except requests.exceptions.ReadTimeout:
|
| 99 |
print("timeout")
|
| 100 |
info = "Too many users, please try again later"
|
| 101 |
raise gr.Error("Too many users, please try again later")
|
| 102 |
+
|
| 103 |
+
# Handle any other unexpected errors
|
| 104 |
except Exception as err:
|
| 105 |
+
print(f"Other error: {err}")
|
| 106 |
+
info = "Error, please contact the admin"
|
| 107 |
+
|
| 108 |
end_time = time.time()
|
| 109 |
print(f"time used: {end_time-start_time}")
|
| 110 |
|
| 111 |
return result_img, seed, info
|
| 112 |
|
| 113 |
+
|
| 114 |
+
|
| 115 |
|
| 116 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 117 |
|