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