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
|
| 7 |
+
# --- 配置資訊 ---
|
| 8 |
+
API_KEY = "46b4a365e01c454ebcfbe41537af23d4"
|
| 9 |
+
WORKFLOW_ID = "958842411961233410"
|
| 10 |
+
|
| 11 |
+
# RunningHub API 端點
|
| 12 |
+
UPLOAD_URL = "https://www.runninghub.cn/api/resource/openapi/upload"
|
| 13 |
+
CREATE_TASK_URL = "https://www.runninghub.cn/task/openapi/create"
|
| 14 |
+
GET_TASK_INFO_URL = "https://www.runninghub.cn/task/openapi/getTaskInfo"
|
| 15 |
+
|
| 16 |
+
# 你的工作流中需要修改的節點ID
|
| 17 |
+
# 節點 141 用來載入圖片,我們需要修改它的 'image' 欄位
|
| 18 |
+
LOAD_IMAGE_NODE_ID = "141"
|
| 19 |
+
POSITIVE_PROMPT_NODE_ID = "144" # 正向提示詞
|
| 20 |
+
SEED_NODE_ID = "132" # 種子值
|
| 21 |
+
|
| 22 |
+
# --- 核心邏輯函式 ---
|
| 23 |
+
# (此部分與您之前收到的程式碼相同,無需修改)
|
| 24 |
+
|
| 25 |
+
def process_image_with_runninghub(uploaded_image, positive_prompt, seed):
|
| 26 |
+
"""
|
| 27 |
+
處理上傳圖片,調用 RunningHub API 進行圖像生成。
|
| 28 |
+
"""
|
| 29 |
+
if uploaded_image is None:
|
| 30 |
+
return "請先上傳一張圖片!", None
|
| 31 |
+
|
| 32 |
+
# 步驟 1: 上傳圖片到 RunningHub
|
| 33 |
+
yield "正在上傳圖片...", None
|
| 34 |
+
try:
|
| 35 |
+
files = {'file': open(uploaded_image, 'rb')}
|
| 36 |
+
upload_response = requests.post(
|
| 37 |
+
UPLOAD_URL,
|
| 38 |
+
headers={"apiKey": API_KEY},
|
| 39 |
+
files=files
|
| 40 |
+
)
|
| 41 |
+
upload_response.raise_for_status()
|
| 42 |
+
upload_data = upload_response.json()
|
| 43 |
+
|
| 44 |
+
if upload_data.get("code") != 0:
|
| 45 |
+
return f"圖片上傳失敗!訊息: {upload_data.get('msg')}", None
|
| 46 |
+
|
| 47 |
+
rh_image_path = upload_data["data"]["filePath"]
|
| 48 |
+
yield f"圖片上傳成功!路徑: {rh_image_path}", None
|
| 49 |
+
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
return f"上傳連線錯誤: {e}", None
|
| 52 |
+
|
| 53 |
+
# 步驟 2: 建立 ComfyUI 任務
|
| 54 |
+
yield "正在建立算圖任務...", None
|
| 55 |
+
|
| 56 |
+
node_info_list = [
|
| 57 |
+
{"nodeId": LOAD_IMAGE_NODE_ID, "fieldName": "image", "fieldValue": rh_image_path},
|
| 58 |
+
{"nodeId": POSITIVE_PROMPT_NODE_ID, "fieldName": "ShowText_0", "fieldValue": positive_prompt},
|
| 59 |
+
{"nodeId": SEED_NODE_ID, "fieldName": "seed", "fieldValue": seed}
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
payload = {
|
| 63 |
+
"apiKey": API_KEY,
|
| 64 |
+
"workflowId": WORKFLOW_ID,
|
| 65 |
+
"nodeInfoList": node_info_list
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
headers = {'Content-Type': 'application/json'}
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
create_response = requests.post(CREATE_TASK_URL, headers=headers, data=json.dumps(payload))
|
| 72 |
+
create_response.raise_for_status()
|
| 73 |
+
create_data = create_response.json()
|
| 74 |
+
|
| 75 |
+
if create_data.get("code") != 0:
|
| 76 |
+
return f"任務建立失敗!錯誤碼: {create_data.get('code')}, 訊息: {create_data.get('msg')}", None
|
| 77 |
+
|
| 78 |
+
task_id = create_data["data"]["taskId"]
|
| 79 |
+
yield f"任務已建立!任務ID: {task_id}", None
|
| 80 |
+
|
| 81 |
+
except requests.exceptions.RequestException as e:
|
| 82 |
+
return f"任務建立連線錯誤: {e}", None
|
| 83 |
+
|
| 84 |
+
# 步驟 3: 輪詢任務狀態
|
| 85 |
+
max_retries = 30
|
| 86 |
+
retry_interval = 5 # 5 秒
|
| 87 |
+
|
| 88 |
+
for i in range(max_retries):
|
| 89 |
+
yield f"正在查詢任務狀態... (第 {i+1} 次)", None
|
| 90 |
+
time.sleep(retry_interval)
|
| 91 |
+
|
| 92 |
+
status_payload = {"apiKey": API_KEY, "taskId": task_id}
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
status_response = requests.post(GET_TASK_INFO_URL, headers=headers, data=json.dumps(status_payload))
|
| 96 |
+
status_response.raise_for_status()
|
| 97 |
+
status_data = status_response.json()
|
| 98 |
+
|
| 99 |
+
current_status = status_data["data"]["taskStatus"]
|
| 100 |
+
|
| 101 |
+
if current_status == "SUCCESS":
|
| 102 |
+
image_url = status_data["data"]["imageInfoList"][0]["fileUrl"]
|
| 103 |
+
yield f"任務完成!", image_url
|
| 104 |
+
return f"任務完成!", image_url
|
| 105 |
+
|
| 106 |
+
elif current_status == "FAILED":
|
| 107 |
+
error_msg = status_data.get("data", {}).get("errorMsg", "Unknown error")
|
| 108 |
+
yield f"任務失敗!錯誤訊息: {error_msg}", None
|
| 109 |
+
return f"任務失敗!錯誤訊息: {error_msg}", None
|
| 110 |
+
|
| 111 |
+
except requests.exceptions.RequestException as e:
|
| 112 |
+
yield f"查詢連線錯誤: {e}", None
|
| 113 |
+
return f"查詢連線錯誤: {e}", None
|
| 114 |
+
|
| 115 |
+
yield "查詢任務超時,請稍後使用任務ID手動查詢。", None
|
| 116 |
+
return "查詢任務超時,請稍後使用任務ID手動查詢。", None
|
| 117 |
+
|
| 118 |
+
# --- Gradio 介面設定 ---
|
| 119 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 120 |
+
gr.Markdown("# RunningHub - 圖像處理工作流")
|
| 121 |
+
gr.Markdown("上傳一張圖片,輸入提示詞和種子值,然後啟動遠端圖像處理任務。")
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
with gr.Column():
|
| 125 |
+
with gr.Row():
|
| 126 |
+
image_input = gr.Image(type="filepath", label="上傳圖片")
|
| 127 |
+
|
| 128 |
+
with gr.Column():
|
| 129 |
+
prompt_input = gr.Textbox(lines=3, label="正向提示詞 (Positive Prompt)", placeholder="輸入你想要生成的內容...")
|
| 130 |
+
seed_input = gr.Number(label="種子值 (Seed)", value=-1, precision=0)
|
| 131 |
+
|
| 132 |
+
process_button = gr.Button("開始處理圖像", variant="primary")
|
| 133 |
+
|
| 134 |
+
with gr.Column():
|
| 135 |
+
with gr.Row():
|
| 136 |
+
image_output = gr.Image(label="生成的圖像")
|
| 137 |
+
with gr.Row():
|
| 138 |
+
status_output = gr.Textbox(label="任務狀態", interactive=False)
|
| 139 |
+
|
| 140 |
+
process_button.click(
|
| 141 |
+
fn=process_image_with_runninghub,
|
| 142 |
+
inputs=[image_input, prompt_input, seed_input],
|
| 143 |
+
outputs=[status_output, image_output]
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
demo.launch()
|