Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,45 +65,50 @@ def process_image_workflow(image_path, positive_prompt, seed=-1):
|
|
| 65 |
except requests.exceptions.RequestException as e:
|
| 66 |
return f"任務建立連線錯誤: {e}", None
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
yield f"
|
| 103 |
-
return f"
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# --- Gradio 介面 ---
|
| 109 |
with gr.Blocks() as demo:
|
|
|
|
| 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:
|