Revolution-D commited on
Commit
ef0a146
·
verified ·
1 Parent(s): df1a70f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -46
app.py CHANGED
@@ -8,7 +8,7 @@ API_KEY = "d72ef10876b9403a9479c6e31f27a4be".strip()
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_INFO_URL = "https://www.runninghub.cn/task/openapi/getTaskInfo"
12
  HEADERS = {"Host": "www.runninghub.cn"}
13
 
14
  # 工作流節點 ID
@@ -65,50 +65,51 @@ def process_image_workflow(image_path, positive_prompt, seed=-1):
65
  except requests.exceptions.RequestException as e:
66
  return f"任務建立連線錯誤: {e}", None
67
 
68
- # --- 步驟 3: 輪詢任務狀態 (修改版) ---
69
- max_retries = 30
70
- retry_interval = 5
71
-
72
- for i in range(max_retries):
73
- yield f"查詢任務狀態... (第 {i+1} 次)", None
74
- time.sleep(retry_interval)
75
- try:
76
- status_payload = {"apiKey": API_KEY, "taskId": task_id}
77
- status_response = requests.post(GET_TASK_INFO_URL, headers={"Content-Type": "application/json"}, data=json.dumps(status_payload))
78
-
79
- # 檢查 HTTP 狀態碼
80
- if status_response.status_code == 404:
81
- print("Task not found yet, retrying...")
82
- continue # 繼續下一次迴圈
83
-
84
- status_response.raise_for_status() # 如果不是 200 或 404,則拋出錯誤
85
- status_data = status_response.json()
86
- print("查詢回傳:", status_data)
87
-
88
- if status_data.get("code") != 0:
89
- return f"查詢任務失敗!訊息: {status_data.get('msg')}", None
90
-
91
- data = status_data.get("data")
92
- if not data:
93
- return "查詢回傳 data 為空,請稍後重試。", None
94
-
95
- current_status = data.get("taskStatus")
96
- if current_status == "SUCCESS":
97
- image_url = data["imageInfoList"][0]["fileUrl"]
98
- yield "任務完成!", image_url
99
- return "任務完成!", image_url
100
- elif current_status == "FAILED":
101
- error_msg = data.get("errorMsg", "Unknown error")
102
- yield f"任務失敗!錯誤訊息: {error_msg}", None
103
- return f"任務失敗!錯誤訊息: {error_msg}", None
104
-
105
- except requests.exceptions.RequestException as e:
106
- # 如果是連線錯誤,直接返回
107
- yield f"查詢連線錯誤: {e}", None
108
- return f"查詢連線錯誤: {e}", None
109
-
110
- yield "任務查詢超時,請使用 TaskID 手動查詢。", None
111
- return "任務查詢超時,請使用 TaskID 手動查詢。", None
 
112
 
113
  # --- Gradio 介面 ---
114
  with gr.Blocks() as demo:
@@ -132,4 +133,4 @@ with gr.Blocks() as demo:
132
  )
133
 
134
  if __name__ == "__main__":
135
- demo.launch()
 
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_INFO_URL = "https://www.runninghub.ai/task/openapi/getTaskInfo" # 修正了網址
12
  HEADERS = {"Host": "www.runninghub.cn"}
13
 
14
  # 工作流節點 ID
 
65
  except requests.exceptions.RequestException as e:
66
  return f"任務建立連線錯誤: {e}", None
67
 
68
+ # --- 步驟 3: 輪詢任務狀態 ---
69
+ max_retries = 40 # 增加重試次數以應對長任務
70
+ retry_interval = 5
71
+
72
+ for i in range(max_retries):
73
+ yield f"查詢任務狀態... (第 {i+1} 次)", None
74
+ time.sleep(retry_interval)
75
+ try:
76
+ status_payload = {"apiKey": API_KEY, "taskId": task_id}
77
+ status_response = requests.post(GET_TASK_INFO_URL, headers={"Content-Type": "application/json"}, data=json.dumps(status_payload))
78
+
79
+ # 檢查 HTTP 狀態碼
80
+ if status_response.status_code == 404:
81
+ print("Task not found yet, retrying...")
82
+ continue # 繼續下一次迴圈
83
+
84
+ status_response.raise_for_status() # 如果不是 200 或 404,則拋出錯誤
85
+ status_data = status_response.json()
86
+ print("查詢回傳:", status_data)
87
+
88
+ if status_data.get("code") != 0:
89
+ yield f"查詢任務失敗!訊息: {status_data.get('msg')}", None
90
+ return f"查詢任務失敗!訊息: {status_data.get('msg')}", None
91
+
92
+ data = status_data.get("data")
93
+ if not data:
94
+ yield "查詢回傳 data 為空,請稍後重試。", None
95
+ return "查詢回傳 data 為空,請稍後重試。", None
96
+
97
+ current_status = data.get("taskStatus")
98
+ if current_status == "SUCCESS":
99
+ image_url = data["imageInfoList"][0]["fileUrl"]
100
+ yield "任務完成!", image_url
101
+ return "任務完成!", image_url
102
+ elif current_status == "FAILED":
103
+ error_msg = data.get("errorMsg", "Unknown error")
104
+ yield f"任務失敗!錯誤訊息: {error_msg}", None
105
+ return f"任務失敗!錯誤訊息: {error_msg}", None
106
+
107
+ except requests.exceptions.RequestException as e:
108
+ yield f"查詢連線錯誤: {e}", None
109
+ return f"查詢連線錯誤: {e}", None
110
+
111
+ yield "任務查詢超時,請使用 TaskID 手動查詢。", None
112
+ return "任務查詢超時,請使用 TaskID 手動查詢。", None
113
 
114
  # --- Gradio 介面 ---
115
  with gr.Blocks() as demo:
 
133
  )
134
 
135
  if __name__ == "__main__":
136
+ demo.launch()