Revolution-D commited on
Commit
8301422
·
verified ·
1 Parent(s): 1c3c5e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -137
app.py CHANGED
@@ -1,149 +1,42 @@
1
  import gradio as gr
2
  import requests
3
- import json
4
- import time
5
- import os
6
 
7
  # --- 配置資訊 ---
8
- API_KEY = "d72ef10876b9403a9479c6e31f27a4be"
9
- WORKFLOW_ID = "1958842411961233410"
10
-
11
- # RunningHub API 端點
12
  UPLOAD_URL = "https://www.runninghub.cn/api/resource/openapi/upload"
13
- 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
- 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},
60
- {"nodeId": SEED_NODE_ID, "fieldName": "seed", "fieldValue": seed}
61
- ]
62
-
63
- payload = {
64
- "apiKey": API_KEY,
65
- "workflowId": WORKFLOW_ID,
66
- "nodeInfoList": node_info_list
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()
74
- create_data = create_response.json()
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
 
82
  except requests.exceptions.RequestException as e:
83
- return f"任務建立連線錯誤: {e}", None
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="生成的圖像")
139
- with gr.Row():
140
- status_output = gr.Textbox(label="任務狀態", interactive=False)
141
-
142
- process_button.click(
143
- fn=process_image_with_runninghub,
144
- inputs=[image_input, prompt_input, seed_input],
145
- outputs=[status_output, image_output]
146
- )
147
 
148
  if __name__ == "__main__":
149
- demo.launch()
 
1
  import gradio as gr
2
  import requests
 
 
 
3
 
4
  # --- 配置資訊 ---
5
+ API_KEY = "d72ef10876b9403a9479c6e31f27a4be".strip()
 
 
 
6
  UPLOAD_URL = "https://www.runninghub.cn/api/resource/openapi/upload"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # --- 核心函式 ---
9
+ def test_upload_image(image_path):
10
+ if image_path is None:
11
+ return "請先上傳圖片!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  try:
14
+ with open(image_path, "rb") as f:
15
+ files = {"file": f}
16
+ data = {"apiKey": API_KEY}
17
+ response = requests.post(UPLOAD_URL, data=data, files=files)
18
+ response.raise_for_status()
19
+ result = response.json()
20
+ print(result) # 除錯用
21
+
22
+ if result.get("code") == 0:
23
+ return f"上傳成功!路徑: {result['data']['fileName']}"
24
+ else:
25
+ return f"上傳失敗!訊息: {result.get('msg')}"
26
 
27
  except requests.exceptions.RequestException as e:
28
+ return f"連線錯誤: {e}"
29
+
30
+ # --- Gradio 介面 ---
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("# RunningHub 上傳測試")
33
+ gr.Markdown("上傳一張圖片,測試 API Key 是否有效。")
34
+
35
+ image_input = gr.Image(type="filepath", label="上傳圖片")
36
+ status_output = gr.Textbox(label="狀態", interactive=False)
37
+ upload_button = gr.Button("測試上傳")
38
+
39
+ upload_button.click(fn=test_upload_image, inputs=image_input, outputs=status_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  if __name__ == "__main__":
42
+ demo.launch(share=True)