Spaces:
Running
Running
Commit
·
5e1c9fb
1
Parent(s):
9a354ff
init
Browse files
app.py
CHANGED
@@ -44,9 +44,9 @@ def is_http_resource_accessible(url):
|
|
44 |
|
45 |
|
46 |
def onPoseChange(prompt_text, source_image, request: gr.Request):
|
47 |
-
"""
|
48 |
if source_image is None:
|
49 |
-
return "
|
50 |
|
51 |
if not prompt_text or prompt_text.strip() == "":
|
52 |
prompt_text = "Change the pose: two hands on hips.#Change the pose: arms extended to show outfit."
|
@@ -57,21 +57,21 @@ def onPoseChange(prompt_text, source_image, request: gr.Request):
|
|
57 |
if x_forwarded_for:
|
58 |
client_ip = x_forwarded_for
|
59 |
|
60 |
-
#
|
61 |
if isinstance(source_image, str) and not source_image.startswith('http'):
|
62 |
timeId = int(str(time.time()).replace(".", "")) + random.randint(1000, 9999)
|
63 |
image_url = upload_pose_img(client_ip, timeId, source_image)
|
64 |
if not image_url:
|
65 |
-
return "
|
66 |
else:
|
67 |
image_url = source_image
|
68 |
|
69 |
-
#
|
70 |
pose_result = public_pose_changer(image_url, prompt_text)
|
71 |
if pose_result is None:
|
72 |
-
return "
|
73 |
|
74 |
-
#
|
75 |
max_try = 120
|
76 |
wait_s = 1
|
77 |
for i in range(max_try):
|
@@ -83,23 +83,23 @@ def onPoseChange(prompt_text, source_image, request: gr.Request):
|
|
83 |
elif result['status'] == 'PROCESSING':
|
84 |
continue
|
85 |
elif result['status'] == 'SUCCEED':
|
86 |
-
#
|
87 |
output_images = [None, None, None]
|
88 |
-
for j in range(1, 4): # output1
|
89 |
output_key = f'output{j}'
|
90 |
if output_key in result and result[output_key] and result[output_key].strip():
|
91 |
timestamp = int(time.time() * 1000)
|
92 |
output_images[j-1] = result[output_key] + f"?t={timestamp}"
|
93 |
|
94 |
-
return f"
|
95 |
elif result['status'] == 'FAILED':
|
96 |
-
return f"
|
97 |
|
98 |
-
return "
|
99 |
|
100 |
except Exception as e:
|
101 |
-
print(f"
|
102 |
-
return f"
|
103 |
|
104 |
def onClick(cloth_image, pose_image, high_resolution, request: gr.Request):
|
105 |
if pose_image is None:
|
@@ -226,35 +226,35 @@ with gr.Blocks() as demo:
|
|
226 |
res_image = gr.Image(label="result image", value=None, type="filepath")
|
227 |
MK01 = gr.Markdown()
|
228 |
|
229 |
-
#
|
230 |
with gr.Accordion('pose changer', open=False):
|
231 |
-
#
|
232 |
with gr.Row():
|
233 |
pose_prompt = gr.Textbox(
|
234 |
value="Change the pose: two hands on hips.#Change the pose: arms extended to show outfit.",
|
235 |
-
label="
|
236 |
-
placeholder="
|
237 |
lines=2,
|
238 |
scale=4
|
239 |
)
|
240 |
change_button = gr.Button(value="Change", scale=1)
|
241 |
|
242 |
-
#
|
243 |
with gr.Row():
|
244 |
with gr.Column(scale=1):
|
245 |
-
pose_changer_image = gr.Image(value=None, type="filepath", label="
|
246 |
-
pose_change_info = gr.Textbox(value="", interactive=False, label="
|
247 |
|
248 |
with gr.Column(scale=2):
|
249 |
with gr.Row():
|
250 |
-
pose_result_1 = gr.Image(label="
|
251 |
-
pose_result_2 = gr.Image(label="
|
252 |
-
pose_result_3 = gr.Image(label="
|
253 |
|
254 |
run_button.click(fn=onClick, inputs=[cloth_image, pose_image, high_resolution],
|
255 |
outputs=[res_image, info_text, MK01, pose_changer_image])
|
256 |
|
257 |
-
#
|
258 |
change_button.click(
|
259 |
fn=onPoseChange,
|
260 |
inputs=[pose_prompt, pose_changer_image],
|
|
|
44 |
|
45 |
|
46 |
def onPoseChange(prompt_text, source_image, request: gr.Request):
|
47 |
+
"""Handle pose change request"""
|
48 |
if source_image is None:
|
49 |
+
return "Please provide source image first!", None, None, None
|
50 |
|
51 |
if not prompt_text or prompt_text.strip() == "":
|
52 |
prompt_text = "Change the pose: two hands on hips.#Change the pose: arms extended to show outfit."
|
|
|
57 |
if x_forwarded_for:
|
58 |
client_ip = x_forwarded_for
|
59 |
|
60 |
+
# If source_image is a local file path, upload it first
|
61 |
if isinstance(source_image, str) and not source_image.startswith('http'):
|
62 |
timeId = int(str(time.time()).replace(".", "")) + random.randint(1000, 9999)
|
63 |
image_url = upload_pose_img(client_ip, timeId, source_image)
|
64 |
if not image_url:
|
65 |
+
return "Image upload failed!", None, None, None
|
66 |
else:
|
67 |
image_url = source_image
|
68 |
|
69 |
+
# Initiate pose change request
|
70 |
pose_result = public_pose_changer(image_url, prompt_text)
|
71 |
if pose_result is None:
|
72 |
+
return "Pose change request failed!", None, None, None
|
73 |
|
74 |
+
# Poll for results
|
75 |
max_try = 120
|
76 |
wait_s = 1
|
77 |
for i in range(max_try):
|
|
|
83 |
elif result['status'] == 'PROCESSING':
|
84 |
continue
|
85 |
elif result['status'] == 'SUCCEED':
|
86 |
+
# Extract the first 3 valid output images
|
87 |
output_images = [None, None, None]
|
88 |
+
for j in range(1, 4): # output1 to output3
|
89 |
output_key = f'output{j}'
|
90 |
if output_key in result and result[output_key] and result[output_key].strip():
|
91 |
timestamp = int(time.time() * 1000)
|
92 |
output_images[j-1] = result[output_key] + f"?t={timestamp}"
|
93 |
|
94 |
+
return f"Pose change completed! {result.get('msg', '')}", output_images[0], output_images[1], output_images[2]
|
95 |
elif result['status'] == 'FAILED':
|
96 |
+
return f"Pose change failed: {result.get('msg', '')}", None, None, None
|
97 |
|
98 |
+
return "Pose change timeout!", None, None, None
|
99 |
|
100 |
except Exception as e:
|
101 |
+
print(f"Pose change exception: {e}")
|
102 |
+
return f"Processing exception: {str(e)}", None, None, None
|
103 |
|
104 |
def onClick(cloth_image, pose_image, high_resolution, request: gr.Request):
|
105 |
if pose_image is None:
|
|
|
226 |
res_image = gr.Image(label="result image", value=None, type="filepath")
|
227 |
MK01 = gr.Markdown()
|
228 |
|
229 |
+
# Add pose changer module
|
230 |
with gr.Accordion('pose changer', open=False):
|
231 |
+
# Top: text box and button
|
232 |
with gr.Row():
|
233 |
pose_prompt = gr.Textbox(
|
234 |
value="Change the pose: two hands on hips.#Change the pose: arms extended to show outfit.",
|
235 |
+
label="Pose Change Prompt",
|
236 |
+
placeholder="Enter pose change description...",
|
237 |
lines=2,
|
238 |
scale=4
|
239 |
)
|
240 |
change_button = gr.Button(value="Change", scale=1)
|
241 |
|
242 |
+
# Bottom: source image on left, result images on right
|
243 |
with gr.Row():
|
244 |
with gr.Column(scale=1):
|
245 |
+
pose_changer_image = gr.Image(value=None, type="filepath", label="Source Image", interactive=True)
|
246 |
+
pose_change_info = gr.Textbox(value="", interactive=False, label="Processing Info")
|
247 |
|
248 |
with gr.Column(scale=2):
|
249 |
with gr.Row():
|
250 |
+
pose_result_1 = gr.Image(label="Result 1", value=None, type="filepath")
|
251 |
+
pose_result_2 = gr.Image(label="Result 2", value=None, type="filepath")
|
252 |
+
pose_result_3 = gr.Image(label="Result 3", value=None, type="filepath")
|
253 |
|
254 |
run_button.click(fn=onClick, inputs=[cloth_image, pose_image, high_resolution],
|
255 |
outputs=[res_image, info_text, MK01, pose_changer_image])
|
256 |
|
257 |
+
# Bind pose changer change button
|
258 |
change_button.click(
|
259 |
fn=onPoseChange,
|
260 |
inputs=[pose_prompt, pose_changer_image],
|