Revolution-D commited on
Commit
02b0218
·
verified ·
1 Parent(s): c78ac30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -30
app.py CHANGED
@@ -14,45 +14,46 @@ 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},
@@ -66,7 +67,7 @@ def process_image_with_runninghub(uploaded_image, positive_prompt, seed):
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()
@@ -74,7 +75,7 @@ def process_image_with_runninghub(uploaded_image, positive_prompt, seed):
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
 
@@ -83,54 +84,55 @@ def process_image_with_runninghub(uploaded_image, positive_prompt, seed):
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="生成的圖像")
 
14
  GET_TASK_INFO_URL = "https://www.runninghub.cn/task/openapi/getTaskInfo"
15
 
16
  # 你的工作流中需要修改的節點ID
 
17
  LOAD_IMAGE_NODE_ID = "141"
18
+ POSITIVE_PROMPT_NODE_ID = "144"
19
+ SEED_NODE_ID = "132"
20
 
21
  # --- 核心邏輯函式 ---
 
 
22
  def process_image_with_runninghub(uploaded_image, positive_prompt, seed):
23
  """
24
  處理上傳圖片,調用 RunningHub API 進行圖像生成。
25
  """
26
  if uploaded_image is None:
27
  return "請先上傳一張圖片!", None
28
+
29
  # 步驟 1: 上傳圖片到 RunningHub
30
  yield "正在上傳圖片...", None
31
  try:
32
+ # 準備 multipart/form-data 的數據
33
  files = {'file': open(uploaded_image, 'rb')}
34
+ data = {'apiKey': API_KEY}
35
+
36
+ # requests 函式庫會自動處理正確的 content-type
37
+ upload_response = requests.post(UPLOAD_URL, data=data, files=files)
 
38
  upload_response.raise_for_status()
39
  upload_data = upload_response.json()
40
+
41
+ # 除錯:印出上傳 API 的回傳內容
42
+ print(f"上傳 API 回傳: {upload_data}")
43
+
44
  if upload_data.get("code") != 0:
45
  return f"圖片上傳失敗!訊息: {upload_data.get('msg')}", None
46
+
47
+ # 從回傳結果中取得檔案名稱
48
+ rh_image_path = upload_data["data"]["fileName"]
49
  yield f"圖片上傳成功!路徑: {rh_image_path}", None
50
 
51
  except requests.exceptions.RequestException as e:
52
  return f"上傳連線錯誤: {e}", None
53
+
54
  # 步驟 2: 建立 ComfyUI 任務
55
  yield "正在建立算圖任務...", None
56
+
57
  node_info_list = [
58
  {"nodeId": LOAD_IMAGE_NODE_ID, "fieldName": "image", "fieldValue": rh_image_path},
59
  {"nodeId": POSITIVE_PROMPT_NODE_ID, "fieldName": "ShowText_0", "fieldValue": positive_prompt},
 
67
  }
68
 
69
  headers = {'Content-Type': 'application/json'}
70
+
71
  try:
72
  create_response = requests.post(CREATE_TASK_URL, headers=headers, data=json.dumps(payload))
73
  create_response.raise_for_status()
 
75
 
76
  if create_data.get("code") != 0:
77
  return f"任務建立失敗!錯誤碼: {create_data.get('code')}, 訊息: {create_data.get('msg')}", None
78
+
79
  task_id = create_data["data"]["taskId"]
80
  yield f"任務已建立!任務ID: {task_id}", None
81
 
 
84
 
85
  # 步驟 3: 輪詢任務狀態
86
  max_retries = 30
87
+ retry_interval = 5
88
 
89
  for i in range(max_retries):
90
  yield f"正在查詢任務狀態... (第 {i+1} 次)", None
91
  time.sleep(retry_interval)
92
+
93
  status_payload = {"apiKey": API_KEY, "taskId": task_id}
94
+
95
  try:
96
  status_response = requests.post(GET_TASK_INFO_URL, headers=headers, data=json.dumps(status_payload))
97
  status_response.raise_for_status()
98
  status_data = status_response.json()
99
+
100
  current_status = status_data["data"]["taskStatus"]
101
+
102
  if current_status == "SUCCESS":
103
  image_url = status_data["data"]["imageInfoList"][0]["fileUrl"]
104
  yield f"任務完成!", image_url
105
  return f"任務完成!", image_url
106
+
107
  elif current_status == "FAILED":
108
  error_msg = status_data.get("data", {}).get("errorMsg", "Unknown error")
109
  yield f"任務失敗!錯誤訊息: {error_msg}", None
110
  return f"任務失敗!錯誤訊息: {error_msg}", None
111
+
112
  except requests.exceptions.RequestException as e:
113
  yield f"查詢連線錯誤: {e}", None
114
  return f"查詢連線錯誤: {e}", None
115
+
116
  yield "查詢任務超時,請稍後使用任務ID手動查詢。", None
117
  return "查詢任務超時,請稍後使用任務ID手動查詢。", None
118
 
119
+
120
  # --- Gradio 介面設定 ---
121
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
122
  gr.Markdown("# RunningHub - 圖像處理工作流")
123
  gr.Markdown("上傳一張圖片,輸入提示詞和種子值,然後啟動遠端圖像處理任務。")
124
+
125
  with gr.Row():
126
  with gr.Column():
127
  with gr.Row():
128
  image_input = gr.Image(type="filepath", label="上傳圖片")
129
+
130
  with gr.Column():
131
  prompt_input = gr.Textbox(lines=3, label="正向提示詞 (Positive Prompt)", placeholder="輸入你想要生成的內容...")
132
  seed_input = gr.Number(label="種子值 (Seed)", value=-1, precision=0)
133
+
134
  process_button = gr.Button("開始處理圖像", variant="primary")
135
+
136
  with gr.Column():
137
  with gr.Row():
138
  image_output = gr.Image(label="生成的圖像")