Spaces:
Runtime error
Runtime error
proper commit
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ import requests
|
|
| 10 |
import json
|
| 11 |
import time
|
| 12 |
|
|
|
|
|
|
|
| 13 |
# Add a new function for text-to-image generation
|
| 14 |
def generate_garment_image(prompt):
|
| 15 |
# This is a placeholder function. You'll need to implement actual text-to-image generation here.
|
|
@@ -20,32 +22,32 @@ def generate_garment_image(prompt):
|
|
| 20 |
return placeholder_image
|
| 21 |
|
| 22 |
def tryon(person_img, garment_prompt, seed, randomize_seed):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
# Generate garment image from prompt
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
|
| 50 |
print("post response code", response.status_code)
|
| 51 |
if response.status_code == 200:
|
|
@@ -54,49 +56,47 @@ def tryon(person_img, garment_prompt, seed, randomize_seed):
|
|
| 54 |
if status == "success":
|
| 55 |
uuid = result['result']
|
| 56 |
print(uuid)
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
time.sleep(9)
|
| 65 |
-
Max_Retry = 10
|
| 66 |
-
result_img = None
|
| 67 |
-
for i in range(Max_Retry):
|
| 68 |
-
try:
|
| 69 |
-
url = "http://" + os.environ['tryon_url'] + "Query?taskId=" + uuid
|
| 70 |
-
response = requests.get(url, headers=headers, timeout=15)
|
| 71 |
-
print("get response code", response.status_code)
|
| 72 |
-
if response.status_code == 200:
|
| 73 |
-
result = response.json()['result']
|
| 74 |
-
status = result['status']
|
| 75 |
-
if status == "success":
|
| 76 |
-
result = base64.b64decode(result['result'])
|
| 77 |
-
result_np = np.frombuffer(result, np.uint8)
|
| 78 |
-
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 79 |
-
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 80 |
-
info = "Success"
|
| 81 |
-
break
|
| 82 |
-
elif status == "error":
|
| 83 |
-
return None, seed, "Too many users, please try again later"
|
| 84 |
-
else:
|
| 85 |
-
print(response.text)
|
| 86 |
-
info = "URL error, please contact the admin"
|
| 87 |
-
except requests.exceptions.ReadTimeout:
|
| 88 |
-
print("timeout")
|
| 89 |
-
info = "Too many users, please try again later"
|
| 90 |
-
except Exception as err:
|
| 91 |
-
print(f"Error: {err}")
|
| 92 |
-
time.sleep(1)
|
| 93 |
-
get_end_time = time.time()
|
| 94 |
-
print(f"get time used: {get_end_time-get_start_time}")
|
| 95 |
|
| 96 |
-
return result_img, seed, info
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"An error occurred: {str(e)}")
|
| 99 |
-
return None, seed, f"An error occurred: {str(e)}"
|
| 100 |
|
| 101 |
def start_tryon(person_img, garment_prompt, seed, randomize_seed):
|
| 102 |
start_time = time.time()
|
|
@@ -106,78 +106,60 @@ def start_tryon(person_img, garment_prompt, seed, randomize_seed):
|
|
| 106 |
seed = random.randint(0, MAX_SEED)
|
| 107 |
|
| 108 |
# Generate garment image from prompt
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if response.status_code == 200:
|
| 131 |
result = response.json()['result']
|
| 132 |
status = result['status']
|
| 133 |
if status == "success":
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
else:
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
|
| 142 |
-
get_start_time = time.time()
|
| 143 |
-
time.sleep(9)
|
| 144 |
-
Max_Retry = 10
|
| 145 |
-
result_img = None
|
| 146 |
-
for i in range(Max_Retry):
|
| 147 |
-
try:
|
| 148 |
-
url = "http://" + os.environ['tryon_url'] + "Query?taskId=" + uuid
|
| 149 |
-
response = requests.get(url, headers=headers, timeout=15)
|
| 150 |
-
print("get response code", response.status_code)
|
| 151 |
-
if response.status_code == 200:
|
| 152 |
-
result = response.json()['result']
|
| 153 |
-
status = result['status']
|
| 154 |
-
if status == "success":
|
| 155 |
-
result = base64.b64decode(result['result'])
|
| 156 |
-
result_np = np.frombuffer(result, np.uint8)
|
| 157 |
-
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 158 |
-
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 159 |
-
info = "Success"
|
| 160 |
-
break
|
| 161 |
-
elif status == "error":
|
| 162 |
-
return None, seed, "Too many users, please try again later"
|
| 163 |
-
else:
|
| 164 |
-
print(response.text)
|
| 165 |
-
info = "URL error, please contact the admin"
|
| 166 |
-
except requests.exceptions.ReadTimeout:
|
| 167 |
-
print("timeout")
|
| 168 |
-
info = "Too many users, please try again later"
|
| 169 |
-
except Exception as err:
|
| 170 |
-
print(f"Error: {err}")
|
| 171 |
-
time.sleep(1)
|
| 172 |
-
get_end_time = time.time()
|
| 173 |
-
print(f"get time used: {get_end_time-get_start_time}")
|
| 174 |
|
| 175 |
-
return result_img, seed, info
|
| 176 |
-
except Exception as e:
|
| 177 |
-
print(f"An error occurred: {str(e)}")
|
| 178 |
-
return None, seed, f"An error occurred: {str(e)}"
|
| 179 |
|
| 180 |
-
MAX_SEED = 999999
|
| 181 |
|
| 182 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 183 |
|
|
|
|
| 10 |
import json
|
| 11 |
import time
|
| 12 |
|
| 13 |
+
|
| 14 |
+
|
| 15 |
# Add a new function for text-to-image generation
|
| 16 |
def generate_garment_image(prompt):
|
| 17 |
# This is a placeholder function. You'll need to implement actual text-to-image generation here.
|
|
|
|
| 22 |
return placeholder_image
|
| 23 |
|
| 24 |
def tryon(person_img, garment_prompt, seed, randomize_seed):
|
| 25 |
+
post_start_time = time.time()
|
| 26 |
+
if person_img is None or garment_prompt == "":
|
| 27 |
+
return None, None, "Empty image or prompt"
|
| 28 |
+
if randomize_seed:
|
| 29 |
+
seed = random.randint(0, MAX_SEED)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
# Generate garment image from prompt
|
| 33 |
+
garment_img = generate_garment_image(garment_prompt)
|
| 34 |
+
|
| 35 |
+
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 36 |
+
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 37 |
+
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 38 |
+
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
|
|
|
| 39 |
|
| 40 |
+
url = "http://" + os.environ['tryon_url'] + "Submit"
|
| 41 |
+
token = os.environ['token']
|
| 42 |
+
cookie = os.environ['Cookie']
|
| 43 |
+
referer = os.environ['referer']
|
| 44 |
+
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 45 |
+
data = {
|
| 46 |
+
"clothImage": encoded_garment_img,
|
| 47 |
+
"humanImage": encoded_person_img,
|
| 48 |
+
"seed": seed
|
| 49 |
+
}
|
| 50 |
+
try:
|
| 51 |
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
|
| 52 |
print("post response code", response.status_code)
|
| 53 |
if response.status_code == 200:
|
|
|
|
| 56 |
if status == "success":
|
| 57 |
uuid = result['result']
|
| 58 |
print(uuid)
|
| 59 |
+
except Exception as err:
|
| 60 |
+
print(f"Error: {err}")
|
| 61 |
+
raise gr.Error("Too many users, please try again later")
|
| 62 |
+
post_end_time = time.time()
|
| 63 |
+
print(f"post time used: {post_end_time-post_start_time}")
|
| 64 |
|
| 65 |
+
get_start_time =time.time()
|
| 66 |
+
time.sleep(9)
|
| 67 |
+
Max_Retry = 10
|
| 68 |
+
result_img = None
|
| 69 |
+
for i in range(Max_Retry):
|
| 70 |
+
try:
|
| 71 |
+
url = "http://" + os.environ['tryon_url'] + "Query?taskId=" + uuid
|
| 72 |
+
response = requests.get(url, headers=headers, timeout=15)
|
| 73 |
+
print("get response code", response.status_code)
|
| 74 |
+
if response.status_code == 200:
|
| 75 |
+
result = response.json()['result']
|
| 76 |
+
status = result['status']
|
| 77 |
+
if status == "success":
|
| 78 |
+
result = base64.b64decode(result['result'])
|
| 79 |
+
result_np = np.frombuffer(result, np.uint8)
|
| 80 |
+
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 81 |
+
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 82 |
+
info = "Success"
|
| 83 |
+
break
|
| 84 |
+
elif status == "error":
|
| 85 |
+
raise gr.Error("Too many users, please try again later")
|
| 86 |
+
else:
|
| 87 |
+
print(response.text)
|
| 88 |
+
info = "URL error, pleace contact the admin"
|
| 89 |
+
except requests.exceptions.ReadTimeout:
|
| 90 |
+
print("timeout")
|
| 91 |
+
info = "Too many users, please try again later"
|
| 92 |
+
except Exception as err:
|
| 93 |
+
print(f"Error: {err}")
|
| 94 |
+
time.sleep(1)
|
| 95 |
+
get_end_time = time.time()
|
| 96 |
+
print(f"get time used: {get_end_time-get_start_time}")
|
| 97 |
|
| 98 |
+
return result_img, seed, info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
def start_tryon(person_img, garment_prompt, seed, randomize_seed):
|
| 102 |
start_time = time.time()
|
|
|
|
| 106 |
seed = random.randint(0, MAX_SEED)
|
| 107 |
|
| 108 |
# Generate garment image from prompt
|
| 109 |
+
garment_img = generate_garment_image(garment_prompt)
|
| 110 |
+
|
| 111 |
+
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 112 |
+
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 113 |
+
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 114 |
+
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
|
|
|
| 115 |
|
| 116 |
+
url = "http://" + os.environ['tryon_url']
|
| 117 |
+
token = os.environ['token']
|
| 118 |
+
cookie = os.environ['Cookie']
|
| 119 |
+
referer = os.environ['referer']
|
| 120 |
+
|
| 121 |
+
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 122 |
+
data = {
|
| 123 |
+
"clothImage": encoded_garment_img,
|
| 124 |
+
"humanImage": encoded_person_img,
|
| 125 |
+
"seed": seed
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
result_img = None
|
| 129 |
+
try:
|
| 130 |
+
session = requests.Session()
|
| 131 |
+
response = session.post(url, headers=headers, data=json.dumps(data), timeout=60)
|
| 132 |
+
print("response code", response.status_code)
|
| 133 |
if response.status_code == 200:
|
| 134 |
result = response.json()['result']
|
| 135 |
status = result['status']
|
| 136 |
if status == "success":
|
| 137 |
+
result = base64.b64decode(result['result'])
|
| 138 |
+
result_np = np.frombuffer(result, np.uint8)
|
| 139 |
+
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 140 |
+
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 141 |
+
info = "Success"
|
| 142 |
+
else:
|
| 143 |
+
info = "Try again latter"
|
| 144 |
else:
|
| 145 |
+
print(response.text)
|
| 146 |
+
info = "URL error, pleace contact the admin"
|
| 147 |
+
except requests.exceptions.ReadTimeout:
|
| 148 |
+
print("timeout")
|
| 149 |
+
info = "Too many users, please try again later"
|
| 150 |
+
raise gr.Error("Too many users, please try again later")
|
| 151 |
+
except Exception as err:
|
| 152 |
+
print(f"其他错误: {err}")
|
| 153 |
+
info = "Error, pleace contact the admin"
|
| 154 |
+
end_time = time.time()
|
| 155 |
+
print(f"time used: {end_time-start_time}")
|
| 156 |
|
| 157 |
+
return result_img, seed, info
|
| 158 |
+
|
| 159 |
+
MAX_SEED = 999999
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
|
|
|
| 163 |
|
| 164 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
| 165 |
|