Revolution-D commited on
Commit
1509c59
·
verified ·
1 Parent(s): 979f4b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -21
app.py CHANGED
@@ -1,47 +1,118 @@
1
  import gradio as gr
2
  import requests
 
 
3
 
4
  # --- 配置資訊 ---
5
  API_KEY = "d72ef10876b9403a9479c6e31f27a4be".strip()
 
6
  UPLOAD_URL = "https://www.runninghub.cn/task/openapi/upload"
 
 
7
  HEADERS = {"Host": "www.runninghub.cn"}
8
 
 
 
 
 
 
9
  # --- 核心函式 ---
10
- def test_upload_image(image_path):
 
 
 
 
 
 
11
  if image_path is None:
12
- return "請先上傳圖片!"
13
 
 
 
14
  try:
15
  with open(image_path, "rb") as f:
16
  files = {"file": f}
17
- data = {
18
- "apiKey": API_KEY,
19
- "fileType": "image" # 圖片固定為 image
20
- }
 
 
 
 
21
 
22
- response = requests.post(UPLOAD_URL, headers=HEADERS, data=data, files=files)
23
- response.raise_for_status()
24
- result = response.json()
25
- print(result) # 除錯用
26
 
27
- if result.get("code") == 0:
28
- return f"上傳成功!fileName: {result['data']['fileName']}"
29
- else:
30
- return f"上傳失敗!訊息: {result.get('msg')}"
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  except requests.exceptions.RequestException as e:
33
- return f"連線錯誤: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # --- Gradio 介面 ---
36
  with gr.Blocks() as demo:
37
- gr.Markdown("# RunningHub 上傳測試")
38
- gr.Markdown("上傳一張圖片,測試 API Key 是否有效。")
39
 
40
- image_input = gr.Image(type="filepath", label="上傳圖片")
41
- status_output = gr.Textbox(label="狀態", interactive=False)
42
- upload_button = gr.Button("測試上傳")
 
 
 
 
 
 
43
 
44
- upload_button.click(fn=test_upload_image, inputs=image_input, outputs=status_output)
 
 
 
 
45
 
46
  if __name__ == "__main__":
47
  demo.launch(share=True)
 
1
  import gradio as gr
2
  import requests
3
+ import json
4
+ import time
5
 
6
  # --- 配置資訊 ---
7
  API_KEY = "d72ef10876b9403a9479c6e31f27a4be".strip()
8
+ WORKFLOW_ID = "1958842411961233410" # 你的工作流ID
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
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
+ """
22
+ 1. 上傳圖片
23
+ 2. 建立 RunningHub 工作流任務
24
+ 3. 輪詢任務狀態
25
+ 4. 返回生成圖片
26
+ """
27
  if image_path is None:
28
+ return "請先上傳圖片!", None
29
 
30
+ # --- 步驟 1: 上傳圖片 ---
31
+ yield "正在上傳圖片...", None
32
  try:
33
  with open(image_path, "rb") as f:
34
  files = {"file": f}
35
+ data = {"apiKey": API_KEY, "fileType": "image"}
36
+ upload_response = requests.post(UPLOAD_URL, headers=HEADERS, data=data, files=files)
37
+ upload_response.raise_for_status()
38
+ upload_data = upload_response.json()
39
+ print(upload_data) # 除錯
40
+
41
+ if upload_data.get("code") != 0:
42
+ return f"圖片上傳失敗!訊息: {upload_data.get('msg')}", None
43
 
44
+ rh_image_path = upload_data["data"]["fileName"]
45
+ yield f"圖片上傳成功!fileName: {rh_image_path}", None
 
 
46
 
47
+ except requests.exceptions.RequestException as e:
48
+ return f"上傳連線錯誤: {e}", None
 
 
49
 
50
+ # --- 步驟 2: 建立工作流任務 ---
51
+ yield "正在建立任務...", None
52
+ node_info_list = [
53
+ {"nodeId": LOAD_IMAGE_NODE_ID, "fieldName": "image", "fieldValue": rh_image_path},
54
+ {"nodeId": POSITIVE_PROMPT_NODE_ID, "fieldName": "ShowText_0", "fieldValue": positive_prompt},
55
+ {"nodeId": SEED_NODE_ID, "fieldName": "seed", "fieldValue": seed}
56
+ ]
57
+ payload = {"apiKey": API_KEY, "workflowId": WORKFLOW_ID, "nodeInfoList": node_info_list}
58
+ try:
59
+ create_response = requests.post(CREATE_TASK_URL, headers={"Content-Type": "application/json"}, data=json.dumps(payload))
60
+ create_response.raise_for_status()
61
+ create_data = create_response.json()
62
+ if create_data.get("code") != 0:
63
+ return f"任務建立失敗!訊息: {create_data.get('msg')}", None
64
+ task_id = create_data["data"]["taskId"]
65
+ yield f"任務建立成功!TaskID: {task_id}", None
66
  except requests.exceptions.RequestException as e:
67
+ return f"任務建立連線錯誤: {e}", None
68
+
69
+ # --- 步驟 3: 輪詢任務狀態 ---
70
+ max_retries = 30
71
+ retry_interval = 5
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
+ status_response.raise_for_status()
79
+ status_data = status_response.json()
80
+ current_status = status_data["data"]["taskStatus"]
81
+ if current_status == "SUCCESS":
82
+ image_url = status_data["data"]["imageInfoList"][0]["fileUrl"]
83
+ yield f"任務完成!", image_url
84
+ return f"任務完成!", image_url
85
+ elif current_status == "FAILED":
86
+ error_msg = status_data.get("data", {}).get("errorMsg", "Unknown error")
87
+ yield f"任務失敗!錯誤訊息: {error_msg}", None
88
+ return f"任務失敗!錯誤訊息: {error_msg}", None
89
+ except requests.exceptions.RequestException as e:
90
+ yield f"查詢連線錯誤: {e}", None
91
+ return f"查詢連線錯誤: {e}", None
92
+
93
+ yield "任務查詢超時,請使用 TaskID 手動查詢。", None
94
+ return "任務查詢超時,請使用 TaskID 手動查詢。", None
95
 
96
  # --- Gradio 介面 ---
97
  with gr.Blocks() as demo:
98
+ gr.Markdown("# RunningHub 工作流圖生圖")
99
+ gr.Markdown("上傳圖片,輸入正向提示詞與種子值,啟動遠端工作流任務。")
100
 
101
+ with gr.Row():
102
+ with gr.Column():
103
+ image_input = gr.Image(type="filepath", label="上傳圖片")
104
+ prompt_input = gr.Textbox(lines=3, label="正向提示詞 (Positive Prompt)", placeholder="輸入你想生成的內容...")
105
+ seed_input = gr.Number(label="種子值 (Seed)", value=-1, precision=0)
106
+ process_button = gr.Button("開始處理圖像")
107
+ with gr.Column():
108
+ status_output = gr.Textbox(label="任務狀態", interactive=False)
109
+ image_output = gr.Image(label="生成圖片")
110
 
111
+ process_button.click(
112
+ fn=process_image_workflow,
113
+ inputs=[image_input, prompt_input, seed_input],
114
+ outputs=[status_output, image_output]
115
+ )
116
 
117
  if __name__ == "__main__":
118
  demo.launch(share=True)