Update app.py
Browse files
app.py
CHANGED
@@ -8,68 +8,92 @@ import requests
|
|
8 |
import json
|
9 |
import time
|
10 |
|
11 |
-
#
|
12 |
-
TRYON_URL = os.getenv('TRYON_URL'
|
13 |
-
TOKEN = os.getenv('TOKEN'
|
14 |
-
COOKIE = os.getenv('COOKIE'
|
15 |
-
REFERER = os.getenv('REFERER'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
|
|
|
|
|
|
18 |
post_start_time = time.time()
|
19 |
if person_img is None or garment_img is None:
|
20 |
gr.Warning("Empty image")
|
21 |
return None, None, "Empty image"
|
|
|
22 |
if randomize_seed:
|
23 |
seed = random.randint(0, MAX_SEED)
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
'
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
try:
|
48 |
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
|
49 |
-
|
|
|
50 |
if response.status_code == 200:
|
51 |
result = response.json()['result']
|
52 |
status = result['status']
|
53 |
if status == "success":
|
54 |
uuid = result['result']
|
55 |
-
|
|
|
56 |
except Exception as err:
|
57 |
print(f"Post Exception Error: {err}")
|
58 |
raise gr.Error("Too many users, please try again later")
|
|
|
59 |
post_end_time = time.time()
|
60 |
print(f"post time used: {post_end_time-post_start_time}")
|
61 |
|
62 |
-
get_start_time =time.time()
|
63 |
time.sleep(9)
|
64 |
Max_Retry = 12
|
65 |
result_img = None
|
66 |
info = ""
|
67 |
err_log = ""
|
|
|
68 |
for i in range(Max_Retry):
|
69 |
try:
|
70 |
-
|
71 |
-
response = requests.get(
|
72 |
-
|
|
|
73 |
if response.status_code == 200:
|
74 |
result = response.json()['result']
|
75 |
status = result['status']
|
@@ -85,19 +109,21 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
85 |
info = "Error"
|
86 |
break
|
87 |
else:
|
88 |
-
|
89 |
-
|
90 |
-
info = "URL error, pleace contact the admin"
|
91 |
break
|
|
|
92 |
except requests.exceptions.ReadTimeout:
|
93 |
err_log = "Http Timeout"
|
94 |
info = "Http Timeout, please try again later"
|
95 |
except Exception as err:
|
96 |
err_log = f"Get Exception Error: {err}"
|
97 |
time.sleep(1)
|
|
|
98 |
get_end_time = time.time()
|
99 |
print(f"get time used: {get_end_time-get_start_time}")
|
100 |
print(f"all time used: {get_end_time-get_start_time+post_end_time-post_start_time}")
|
|
|
101 |
if info == "":
|
102 |
err_log = f"No image after {Max_Retry} retries"
|
103 |
info = "Too many users, please try again later"
|
@@ -113,17 +139,24 @@ def start_tryon(person_img, garment_img, seed, randomize_seed):
|
|
113 |
return None, None, "Empty image"
|
114 |
if randomize_seed:
|
115 |
seed = random.randint(0, MAX_SEED)
|
|
|
116 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
117 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
118 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
119 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
|
126 |
-
headers = {
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
data = {
|
128 |
"clothImage": encoded_garment_img,
|
129 |
"humanImage": encoded_person_img,
|
@@ -133,7 +166,7 @@ def start_tryon(person_img, garment_img, seed, randomize_seed):
|
|
133 |
result_img = None
|
134 |
try:
|
135 |
session = requests.Session()
|
136 |
-
response = session.post(
|
137 |
print("response code", response.status_code)
|
138 |
if response.status_code == 200:
|
139 |
result = response.json()['result']
|
@@ -145,24 +178,22 @@ def start_tryon(person_img, garment_img, seed, randomize_seed):
|
|
145 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
146 |
info = "Success"
|
147 |
else:
|
148 |
-
info = "Try again
|
149 |
else:
|
150 |
print(response.text)
|
151 |
-
info = "URL error,
|
152 |
except requests.exceptions.ReadTimeout:
|
153 |
print("timeout")
|
154 |
info = "Too many users, please try again later"
|
155 |
raise gr.Error("Too many users, please try again later")
|
156 |
except Exception as err:
|
157 |
-
print(f"
|
158 |
-
info = "Error,
|
159 |
end_time = time.time()
|
160 |
print(f"time used: {end_time-start_time}")
|
161 |
|
162 |
return result_img, seed, info
|
163 |
|
164 |
-
MAX_SEED = 999999
|
165 |
-
|
166 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
167 |
|
168 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
@@ -198,9 +229,6 @@ def load_description(fp):
|
|
198 |
content = f.read()
|
199 |
return content
|
200 |
|
201 |
-
def change_imgs(image1, image2):
|
202 |
-
return image1, image2
|
203 |
-
|
204 |
with gr.Blocks(css=css) as Tryon:
|
205 |
gr.HTML(load_description("assets/title.md"))
|
206 |
with gr.Row():
|
@@ -224,14 +252,13 @@ with gr.Blocks(css=css) as Tryon:
|
|
224 |
gr.HTML("""
|
225 |
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
|
226 |
<div>
|
227 |
-
Step 3. Press
|
228 |
</div>
|
229 |
</div>
|
230 |
""")
|
231 |
with gr.Row():
|
232 |
with gr.Column(elem_id = "col-left"):
|
233 |
imgs = gr.Image(label="Person image", sources='upload', type="numpy")
|
234 |
-
# category = gr.Dropdown(label="Garment category", choices=['upper_body', 'lower_body', 'dresses'], value="upper_body")
|
235 |
example = gr.Examples(
|
236 |
inputs=imgs,
|
237 |
examples_per_page=12,
|
@@ -258,12 +285,15 @@ with gr.Blocks(css=css) as Tryon:
|
|
258 |
with gr.Row():
|
259 |
seed_used = gr.Number(label="Seed used")
|
260 |
result_info = gr.Text(label="Response")
|
261 |
-
# try_button = gr.Button(value="Run", elem_id="button")
|
262 |
test_button = gr.Button(value="Run", elem_id="button")
|
263 |
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
|
|
267 |
|
268 |
with gr.Column(elem_id = "col-showcase"):
|
269 |
gr.HTML("""
|
@@ -285,9 +315,9 @@ with gr.Blocks(css=css) as Tryon:
|
|
285 |
label=None
|
286 |
)
|
287 |
|
|
|
288 |
Tryon.queue(api_open=False).launch(
|
289 |
-
server_name="0.0.0.0",
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
)
|
|
|
8 |
import json
|
9 |
import time
|
10 |
|
11 |
+
# Environment variables
|
12 |
+
TRYON_URL = os.getenv('TRYON_URL')
|
13 |
+
TOKEN = os.getenv('TOKEN')
|
14 |
+
COOKIE = os.getenv('COOKIE')
|
15 |
+
REFERER = os.getenv('REFERER')
|
16 |
+
|
17 |
+
# Environment variables kontrolü
|
18 |
+
if not all([TRYON_URL, TOKEN, COOKIE, REFERER]):
|
19 |
+
print("Warning: Some environment variables are missing!")
|
20 |
+
print(f"TRYON_URL: {'Set' if TRYON_URL else 'Missing'}")
|
21 |
+
print(f"TOKEN: {'Set' if TOKEN else 'Missing'}")
|
22 |
+
print(f"COOKIE: {'Set' if COOKIE else 'Missing'}")
|
23 |
+
print(f"REFERER: {'Set' if REFERER else 'Missing'}")
|
24 |
+
|
25 |
+
MAX_SEED = 999999
|
26 |
|
27 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
28 |
+
if not all([TRYON_URL, TOKEN, COOKIE, REFERER]):
|
29 |
+
raise gr.Error("Server configuration is missing. Please contact administrator.")
|
30 |
+
|
31 |
post_start_time = time.time()
|
32 |
if person_img is None or garment_img is None:
|
33 |
gr.Warning("Empty image")
|
34 |
return None, None, "Empty image"
|
35 |
+
|
36 |
if randomize_seed:
|
37 |
seed = random.randint(0, MAX_SEED)
|
38 |
+
|
39 |
+
try:
|
40 |
+
# Resim kodlama
|
41 |
+
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
42 |
+
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
43 |
+
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
44 |
+
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
45 |
|
46 |
+
# URL oluşturma
|
47 |
+
if not TRYON_URL.startswith(('http://', 'https://')):
|
48 |
+
base_url = f"https://{TRYON_URL}"
|
49 |
+
else:
|
50 |
+
base_url = TRYON_URL
|
51 |
+
|
52 |
+
url = f"{base_url.rstrip('/')}/Submit"
|
53 |
+
|
54 |
+
headers = {
|
55 |
+
'Content-Type': 'application/json',
|
56 |
+
'token': TOKEN,
|
57 |
+
'Cookie': COOKIE,
|
58 |
+
'referer': REFERER
|
59 |
+
}
|
60 |
+
|
61 |
+
data = {
|
62 |
+
"clothImage": encoded_garment_img,
|
63 |
+
"humanImage": encoded_person_img,
|
64 |
+
"seed": seed
|
65 |
+
}
|
66 |
|
|
|
|
|
67 |
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
|
68 |
+
print("post response code", response.status_code)
|
69 |
+
|
70 |
if response.status_code == 200:
|
71 |
result = response.json()['result']
|
72 |
status = result['status']
|
73 |
if status == "success":
|
74 |
uuid = result['result']
|
75 |
+
print(uuid)
|
76 |
+
|
77 |
except Exception as err:
|
78 |
print(f"Post Exception Error: {err}")
|
79 |
raise gr.Error("Too many users, please try again later")
|
80 |
+
|
81 |
post_end_time = time.time()
|
82 |
print(f"post time used: {post_end_time-post_start_time}")
|
83 |
|
84 |
+
get_start_time = time.time()
|
85 |
time.sleep(9)
|
86 |
Max_Retry = 12
|
87 |
result_img = None
|
88 |
info = ""
|
89 |
err_log = ""
|
90 |
+
|
91 |
for i in range(Max_Retry):
|
92 |
try:
|
93 |
+
query_url = f"{base_url.rstrip('/')}/Query?taskId={uuid}"
|
94 |
+
response = requests.get(query_url, headers=headers, timeout=20)
|
95 |
+
print("get response code", response.status_code)
|
96 |
+
|
97 |
if response.status_code == 200:
|
98 |
result = response.json()['result']
|
99 |
status = result['status']
|
|
|
109 |
info = "Error"
|
110 |
break
|
111 |
else:
|
112 |
+
err_log = "URL error, please contact the admin"
|
113 |
+
info = "URL error, please contact the admin"
|
|
|
114 |
break
|
115 |
+
|
116 |
except requests.exceptions.ReadTimeout:
|
117 |
err_log = "Http Timeout"
|
118 |
info = "Http Timeout, please try again later"
|
119 |
except Exception as err:
|
120 |
err_log = f"Get Exception Error: {err}"
|
121 |
time.sleep(1)
|
122 |
+
|
123 |
get_end_time = time.time()
|
124 |
print(f"get time used: {get_end_time-get_start_time}")
|
125 |
print(f"all time used: {get_end_time-get_start_time+post_end_time-post_start_time}")
|
126 |
+
|
127 |
if info == "":
|
128 |
err_log = f"No image after {Max_Retry} retries"
|
129 |
info = "Too many users, please try again later"
|
|
|
139 |
return None, None, "Empty image"
|
140 |
if randomize_seed:
|
141 |
seed = random.randint(0, MAX_SEED)
|
142 |
+
|
143 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
144 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
145 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
146 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
147 |
|
148 |
+
if not TRYON_URL.startswith(('http://', 'https://')):
|
149 |
+
base_url = f"https://{TRYON_URL}"
|
150 |
+
else:
|
151 |
+
base_url = TRYON_URL
|
152 |
|
153 |
+
headers = {
|
154 |
+
'Content-Type': 'application/json',
|
155 |
+
'token': TOKEN,
|
156 |
+
'Cookie': COOKIE,
|
157 |
+
'referer': REFERER
|
158 |
+
}
|
159 |
+
|
160 |
data = {
|
161 |
"clothImage": encoded_garment_img,
|
162 |
"humanImage": encoded_person_img,
|
|
|
166 |
result_img = None
|
167 |
try:
|
168 |
session = requests.Session()
|
169 |
+
response = session.post(base_url, headers=headers, data=json.dumps(data), timeout=60)
|
170 |
print("response code", response.status_code)
|
171 |
if response.status_code == 200:
|
172 |
result = response.json()['result']
|
|
|
178 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
179 |
info = "Success"
|
180 |
else:
|
181 |
+
info = "Try again later"
|
182 |
else:
|
183 |
print(response.text)
|
184 |
+
info = "URL error, please contact the admin"
|
185 |
except requests.exceptions.ReadTimeout:
|
186 |
print("timeout")
|
187 |
info = "Too many users, please try again later"
|
188 |
raise gr.Error("Too many users, please try again later")
|
189 |
except Exception as err:
|
190 |
+
print(f"Error: {err}")
|
191 |
+
info = "Error, please contact the admin"
|
192 |
end_time = time.time()
|
193 |
print(f"time used: {end_time-start_time}")
|
194 |
|
195 |
return result_img, seed, info
|
196 |
|
|
|
|
|
197 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
198 |
|
199 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
|
|
229 |
content = f.read()
|
230 |
return content
|
231 |
|
|
|
|
|
|
|
232 |
with gr.Blocks(css=css) as Tryon:
|
233 |
gr.HTML(load_description("assets/title.md"))
|
234 |
with gr.Row():
|
|
|
252 |
gr.HTML("""
|
253 |
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;">
|
254 |
<div>
|
255 |
+
Step 3. Press "Run" to get try-on results
|
256 |
</div>
|
257 |
</div>
|
258 |
""")
|
259 |
with gr.Row():
|
260 |
with gr.Column(elem_id = "col-left"):
|
261 |
imgs = gr.Image(label="Person image", sources='upload', type="numpy")
|
|
|
262 |
example = gr.Examples(
|
263 |
inputs=imgs,
|
264 |
examples_per_page=12,
|
|
|
285 |
with gr.Row():
|
286 |
seed_used = gr.Number(label="Seed used")
|
287 |
result_info = gr.Text(label="Response")
|
|
|
288 |
test_button = gr.Button(value="Run", elem_id="button")
|
289 |
|
290 |
+
test_button.click(
|
291 |
+
fn=tryon,
|
292 |
+
inputs=[imgs, garm_img, seed, randomize_seed],
|
293 |
+
outputs=[image_out, seed_used, result_info],
|
294 |
+
api_name=False,
|
295 |
+
concurrency_limit=45
|
296 |
+
)
|
297 |
|
298 |
with gr.Column(elem_id = "col-showcase"):
|
299 |
gr.HTML("""
|
|
|
315 |
label=None
|
316 |
)
|
317 |
|
318 |
+
# Uygulamayı başlat
|
319 |
Tryon.queue(api_open=False).launch(
|
320 |
+
server_name="0.0.0.0",
|
321 |
+
show_api=False,
|
322 |
+
debug=True
|
323 |
+
)
|
|