save code
This commit is contained in:
+76
-4
@@ -15,6 +15,9 @@ import redis
|
||||
# from check_img_body import is_thigh_visible
|
||||
import logging
|
||||
from config import *
|
||||
import websockets
|
||||
import asyncio
|
||||
import traceback
|
||||
|
||||
|
||||
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
@@ -311,8 +314,75 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, human_url, no2)
|
||||
prompt_id = response.json()["prompt_id"]
|
||||
|
||||
|
||||
# 2. 轮询队列,直到任务完成
|
||||
async def listen_node_output(prompt_id, target_node_id):
|
||||
uri = "ws://localhost:8188/ws"
|
||||
async with websockets.connect(uri) as websocket:
|
||||
await websocket.send(json.dumps({"prompt_id": prompt_id}))
|
||||
|
||||
while True:
|
||||
message = await websocket.recv()
|
||||
|
||||
# 1. 处理二进制数据
|
||||
if isinstance(message, bytes):
|
||||
try:
|
||||
message = message.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
print(f"[{datetime.now().strftime('%H:%M:%S')}] 收到二进制数据 (长度: {len(message)} bytes)")
|
||||
continue
|
||||
|
||||
# 2. 解析JSON
|
||||
try:
|
||||
data = json.loads(message)
|
||||
print(f"cur data {data}")
|
||||
except json.JSONDecodeError:
|
||||
print(f"[{datetime.now().strftime('%H:%M:%S')}] 非JSON数据: {message[:100]}...")
|
||||
continue
|
||||
|
||||
if data.get("type") == "progress":
|
||||
pdata = data.get('data', {})
|
||||
print(f"cur progress {pdata}")
|
||||
# if value >= max:
|
||||
# return f"finished value:{value} max:{max}"
|
||||
|
||||
if data.get("type") == "progress_state":
|
||||
nodes = data.get('data', {}).get('nodes', {})
|
||||
for node in nodes:
|
||||
if node.get("state", None) != 'finished':
|
||||
print(f"progress_state node:{node}")
|
||||
|
||||
# 3. 输出运行状态
|
||||
if data.get("type") == "status":
|
||||
print(f"[{datetime.now().strftime('%H:%M:%S')}] 系统状态: {data.get('data', {}).get('status', {})}")
|
||||
status = data.get('data', {}).get('status', None)
|
||||
print(f"statue:{status}")
|
||||
if status:
|
||||
exec_info = status.get('exec_info', None)
|
||||
print(f"exec_info:{exec_info}")
|
||||
if exec_info:
|
||||
queue_remaining = exec_info.get('queue_remaining', None)
|
||||
print(f"queue_remaining:{queue_remaining}")
|
||||
if queue_remaining == 0:
|
||||
return "process end"
|
||||
continue
|
||||
|
||||
if data.get("type") == "executing":
|
||||
node_id = data.get("data", {}).get("node")
|
||||
progress = data.get("data", {}).get("progress", 0)
|
||||
print(f"[{datetime.now().strftime('%H:%M:%S')}] 正在执行节点 {node_id} (进度: {progress:.0%})")
|
||||
|
||||
# 4. 目标节点完成
|
||||
if data.get("type") == "executed" and data.get("data", {}).get("node") == target_node_id:
|
||||
output = data.get("data", {}).get("output")
|
||||
print(f"[{datetime.now().strftime('%H:%M:%S')}] 节点 {target_node_id} 完成!")
|
||||
return output
|
||||
|
||||
|
||||
# 调用方式保持不变
|
||||
output = asyncio.run(listen_node_output(prompt_id, "104"))
|
||||
print("最终输出:", output)
|
||||
|
||||
while True:
|
||||
# response = requests.post("http://localhost:8188/interrupt")
|
||||
queue = requests.get("http://localhost:8188/queue").json()
|
||||
# print(queue)
|
||||
if not queue["queue_running"] and not queue["queue_pending"]:
|
||||
@@ -556,7 +626,9 @@ def do_change_cloth():
|
||||
|
||||
try:
|
||||
return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url, human_url, no2)
|
||||
except:
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
print(f"错误详情:{e}")
|
||||
return jsonify({
|
||||
"ret":-1,
|
||||
"state": -1,
|
||||
@@ -663,7 +735,7 @@ def change_cloth_base64():
|
||||
return jsonify({"ret":-1, 'msg': 'Failed to save cloth image'}), 500
|
||||
data['cloth_img'] = None
|
||||
|
||||
cloth_url = f"http://112.126.94.241:{remote_port}/static/imgs/{cloth_filename}"
|
||||
cloth_url = f"http://112.126.94.241:{base64_test_port}/static/imgs/{cloth_filename}"
|
||||
|
||||
data["human_url"] = human_url
|
||||
data["cloth_url"] = cloth_url
|
||||
@@ -675,7 +747,7 @@ def change_cloth_base64():
|
||||
# 在内部调用第二个HTTP请求
|
||||
try:
|
||||
# 调用第二个API(可以是外部服务或自己的另一个端点)
|
||||
response = requests.post(f'http://112.126.94.241:{remote_port}/do_change_cloth', json=data)
|
||||
response = requests.post(f'http://112.126.94.241:{base64_test_port}/do_change_cloth', json=data)
|
||||
|
||||
return Response(
|
||||
response=response.content,
|
||||
|
||||
Reference in New Issue
Block a user