import requests
import time
import json

# 1. 提交任务

with open('/home/xsl/code/ComfyUI/change_cloth/basic_api.json', 'r', encoding='utf-8') as file:
    prompt_text = file.read()

prompt = json.loads(prompt_text)
#set the text prompt for our positive CLIPTextEncode
prompt["6"]["inputs"]["text"] = "one girl"

#set the seed for our KSampler node
prompt["3"]["inputs"]["seed"] = 5

p = {"prompt": prompt}

data = json.dumps(p).encode('utf-8')

response = requests.post("http://localhost:8188/prompt", data=data)
prompt_id = response.json()["prompt_id"]



# 2. 轮询队列，直到任务完成
while True:
    queue = requests.get("http://localhost:8188/queue").json()
    print(queue)
    if not queue["queue_running"] and not queue["queue_pending"]:
        break  # 队列为空，任务已完成
    time.sleep(0.5)  # 避免频繁请求

# 3. 从历史记录中获取结果
history = requests.get("http://localhost:8188/history").json()
print("History:", history)
outputs = history[prompt_id]["outputs"]
print("Generated outputs:", outputs)