Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
-
import asyncio
|
| 5 |
-
import websockets
|
| 6 |
import time
|
| 7 |
|
| 8 |
# --- 配置資訊 ---
|
|
@@ -10,6 +8,8 @@ API_KEY = "d72ef10876b9403a9479c6e31f27a4be".strip()
|
|
| 10 |
WORKFLOW_ID = "1958842411961233410"
|
| 11 |
UPLOAD_URL = "https://www.runninghub.cn/task/openapi/upload"
|
| 12 |
CREATE_TASK_URL = "https://www.runninghub.cn/task/openapi/create"
|
|
|
|
|
|
|
| 13 |
HEADERS = {"Host": "www.runninghub.cn"}
|
| 14 |
|
| 15 |
# 工作流節點 ID
|
|
@@ -18,33 +18,33 @@ POSITIVE_PROMPT_NODE_ID = "144"
|
|
| 18 |
SEED_NODE_ID = "132"
|
| 19 |
|
| 20 |
# --- 核心函式 ---
|
| 21 |
-
|
| 22 |
positive_prompt = "a charming young woman with bright eyes and a subtle smile"
|
| 23 |
|
| 24 |
if image_path is None:
|
| 25 |
return "請先上傳圖片!", None
|
| 26 |
|
| 27 |
-
# --- 步驟 1: 上傳圖片
|
| 28 |
yield "正在上傳圖片...", None
|
| 29 |
try:
|
| 30 |
with open(image_path, "rb") as f:
|
| 31 |
files = {"file": f}
|
| 32 |
data = {"apiKey": API_KEY, "fileType": "image"}
|
| 33 |
-
# 使用 requests 函式庫發送同步請求
|
| 34 |
upload_response = requests.post(UPLOAD_URL, headers=HEADERS, data=data, files=files)
|
| 35 |
upload_response.raise_for_status()
|
| 36 |
upload_data = upload_response.json()
|
|
|
|
| 37 |
|
| 38 |
if upload_data.get("code") != 0:
|
| 39 |
return f"圖片上傳失敗!訊息: {upload_data.get('msg')}", None
|
| 40 |
-
|
| 41 |
rh_image_path = upload_data["data"]["fileName"]
|
| 42 |
yield f"圖片上傳成功!fileName: {rh_image_path}", None
|
| 43 |
|
| 44 |
except requests.exceptions.RequestException as e:
|
| 45 |
return f"上傳連線錯誤: {e}", None
|
| 46 |
|
| 47 |
-
# --- 步驟 2: 建立工作流任務
|
| 48 |
yield "正在建立任務...", None
|
| 49 |
node_info_list = [
|
| 50 |
{"nodeId": LOAD_IMAGE_NODE_ID, "fieldName": "image", "fieldValue": rh_image_path},
|
|
@@ -57,50 +57,69 @@ async def process_image_workflow_async(image_path, seed=-1):
|
|
| 57 |
create_response = requests.post(CREATE_TASK_URL, headers={"Content-Type": "application/json"}, data=json.dumps(payload))
|
| 58 |
create_response.raise_for_status()
|
| 59 |
create_data = create_response.json()
|
|
|
|
| 60 |
|
| 61 |
if create_data.get("code") != 0:
|
| 62 |
return f"任務建立失敗!訊息: {create_data.get('msg')}", None
|
| 63 |
-
|
| 64 |
task_id = create_data["data"]["taskId"]
|
| 65 |
-
net_wss_url = create_data["data"]["netWssUrl"]
|
| 66 |
yield f"任務建立成功!TaskID: {task_id}", None
|
| 67 |
|
| 68 |
except requests.exceptions.RequestException as e:
|
| 69 |
return f"任務建立連線錯誤: {e}", None
|
| 70 |
|
| 71 |
-
# --- 步驟 3:
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
try:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
return "WebSocket 連線超時,請使用 TaskID 手動查詢。", None
|
| 90 |
-
except websockets.exceptions.ConnectionClosedError as e:
|
| 91 |
-
return f"WebSocket 連線關閉,錯誤: {e}", None
|
| 92 |
except Exception as e:
|
| 93 |
-
return f"
|
| 94 |
-
|
| 95 |
-
# Gradio 需要一個包裝函式來處理非同步
|
| 96 |
-
def process_image_workflow(image_path, seed=-1):
|
| 97 |
-
loop = asyncio.get_event_loop()
|
| 98 |
-
if loop.is_running():
|
| 99 |
-
# 如果事件迴圈已經在運行,直接在現有迴圈中執行
|
| 100 |
-
return asyncio.run(process_image_workflow_async(image_path, seed))
|
| 101 |
-
else:
|
| 102 |
-
# 如果事件迴圈沒有運行,則啟動一個
|
| 103 |
-
return loop.run_until_complete(process_image_workflow_async(image_path, seed))
|
| 104 |
|
| 105 |
# --- Gradio 介面 ---
|
| 106 |
with gr.Blocks() as demo:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
# --- 配置資訊 ---
|
|
|
|
| 8 |
WORKFLOW_ID = "1958842411961233410"
|
| 9 |
UPLOAD_URL = "https://www.runninghub.cn/task/openapi/upload"
|
| 10 |
CREATE_TASK_URL = "https://www.runninghub.cn/task/openapi/create"
|
| 11 |
+
GET_TASK_STATUS_URL = "https://www.runninghub.cn/task/openapi/status"
|
| 12 |
+
GET_TASK_OUTPUTS_URL = "https://www.runninghub.cn/task/openapi/outputs"
|
| 13 |
HEADERS = {"Host": "www.runninghub.cn"}
|
| 14 |
|
| 15 |
# 工作流節點 ID
|
|
|
|
| 18 |
SEED_NODE_ID = "132"
|
| 19 |
|
| 20 |
# --- 核心函式 ---
|
| 21 |
+
def process_image_workflow(image_path, seed=-1):
|
| 22 |
positive_prompt = "a charming young woman with bright eyes and a subtle smile"
|
| 23 |
|
| 24 |
if image_path is None:
|
| 25 |
return "請先上傳圖片!", None
|
| 26 |
|
| 27 |
+
# --- 步驟 1: 上傳圖片 ---
|
| 28 |
yield "正在上傳圖片...", None
|
| 29 |
try:
|
| 30 |
with open(image_path, "rb") as f:
|
| 31 |
files = {"file": f}
|
| 32 |
data = {"apiKey": API_KEY, "fileType": "image"}
|
|
|
|
| 33 |
upload_response = requests.post(UPLOAD_URL, headers=HEADERS, data=data, files=files)
|
| 34 |
upload_response.raise_for_status()
|
| 35 |
upload_data = upload_response.json()
|
| 36 |
+
print("上傳回傳:", upload_data)
|
| 37 |
|
| 38 |
if upload_data.get("code") != 0:
|
| 39 |
return f"圖片上傳失敗!訊息: {upload_data.get('msg')}", None
|
| 40 |
+
|
| 41 |
rh_image_path = upload_data["data"]["fileName"]
|
| 42 |
yield f"圖片上傳成功!fileName: {rh_image_path}", None
|
| 43 |
|
| 44 |
except requests.exceptions.RequestException as e:
|
| 45 |
return f"上傳連線錯誤: {e}", None
|
| 46 |
|
| 47 |
+
# --- 步驟 2: 建立工作流任務 ---
|
| 48 |
yield "正在建立任務...", None
|
| 49 |
node_info_list = [
|
| 50 |
{"nodeId": LOAD_IMAGE_NODE_ID, "fieldName": "image", "fieldValue": rh_image_path},
|
|
|
|
| 57 |
create_response = requests.post(CREATE_TASK_URL, headers={"Content-Type": "application/json"}, data=json.dumps(payload))
|
| 58 |
create_response.raise_for_status()
|
| 59 |
create_data = create_response.json()
|
| 60 |
+
print("建立任務回傳:", create_data)
|
| 61 |
|
| 62 |
if create_data.get("code") != 0:
|
| 63 |
return f"任務建立失敗!訊息: {create_data.get('msg')}", None
|
| 64 |
+
|
| 65 |
task_id = create_data["data"]["taskId"]
|
|
|
|
| 66 |
yield f"任務建立成功!TaskID: {task_id}", None
|
| 67 |
|
| 68 |
except requests.exceptions.RequestException as e:
|
| 69 |
return f"任務建立連線錯誤: {e}", None
|
| 70 |
|
| 71 |
+
# --- 步驟 3: 輪詢任務狀態 ---
|
| 72 |
+
max_retries = 40
|
| 73 |
+
retry_interval = 5
|
| 74 |
+
|
| 75 |
+
for i in range(max_retries):
|
| 76 |
+
yield f"查詢任務狀態... (第 {i+1} 次)", None
|
| 77 |
+
time.sleep(retry_interval)
|
| 78 |
+
try:
|
| 79 |
+
status_payload = {"apiKey": API_KEY, "taskId": task_id}
|
| 80 |
+
status_response = requests.post(GET_TASK_STATUS_URL, headers={"Content-Type": "application/json"}, data=json.dumps(status_payload))
|
| 81 |
+
status_response.raise_for_status()
|
| 82 |
+
status_data = status_response.json()
|
| 83 |
+
print("查詢狀態回傳:", status_data)
|
| 84 |
+
|
| 85 |
+
if status_data.get("code") != 0:
|
| 86 |
+
yield f"查詢任務失敗!訊息: {status_data.get('msg')}", None
|
| 87 |
+
return f"查詢任務失敗!訊息: {status_data.get('msg')}", None
|
| 88 |
+
|
| 89 |
+
current_status = status_data.get("data")
|
| 90 |
+
if current_status == "SUCCESS":
|
| 91 |
+
yield "任務狀態為成功,正在取得圖片結果...", None
|
| 92 |
+
break # 跳出輪詢迴圈
|
| 93 |
+
elif current_status == "FAILED":
|
| 94 |
+
yield f"任務失敗!", None
|
| 95 |
+
return f"任務失敗!", None
|
| 96 |
+
|
| 97 |
+
except requests.exceptions.RequestException as e:
|
| 98 |
+
yield f"查詢連線錯誤: {e}", None
|
| 99 |
+
return f"查詢連線錯誤: {e}", None
|
| 100 |
+
else:
|
| 101 |
+
yield "任務查詢超時,請使用 TaskID 手動查詢。", None
|
| 102 |
+
return "任務查詢超時,請使用 TaskID 手動查詢。", None
|
| 103 |
+
|
| 104 |
+
# --- 步驟 4: 取得任務結果 ---
|
| 105 |
try:
|
| 106 |
+
outputs_payload = {"apiKey": API_KEY, "taskId": task_id}
|
| 107 |
+
outputs_response = requests.post(GET_TASK_OUTPUTS_URL, headers={"Content-Type": "application/json"}, data=json.dumps(outputs_payload))
|
| 108 |
+
outputs_response.raise_for_status()
|
| 109 |
+
outputs_data = outputs_response.json()
|
| 110 |
+
print("取得結果回傳:", outputs_data)
|
| 111 |
+
|
| 112 |
+
if outputs_data.get("code") != 0:
|
| 113 |
+
return f"取得結果失敗!訊息: {outputs_data.get('msg')}", None
|
| 114 |
+
|
| 115 |
+
image_url = outputs_data["data"][0]["fileUrl"]
|
| 116 |
+
yield "任務完成!", image_url
|
| 117 |
+
return "任務完成!", image_url
|
| 118 |
+
|
| 119 |
+
except requests.exceptions.RequestException as e:
|
| 120 |
+
return f"取得結果連線錯誤: {e}", None
|
|
|
|
|
|
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
+
return f"處理結果時發生錯誤: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
# --- Gradio 介面 ---
|
| 125 |
with gr.Blocks() as demo:
|