Spaces:
Runtime error
Runtime error
Triple-check: Fix server launch and implement full API execution
Browse files- app.py +99 -43
- temp_reference_repo +1 -0
app.py
CHANGED
|
@@ -85,43 +85,71 @@ def setup():
|
|
| 85 |
print("--- SETUP COMPLETE ---")
|
| 86 |
|
| 87 |
def convert_to_api(web_workflow):
|
| 88 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 89 |
nodes = web_workflow.get("nodes", [])
|
| 90 |
links = web_workflow.get("links", [])
|
| 91 |
|
| 92 |
-
# Map link_id -> [
|
| 93 |
link_map = {}
|
| 94 |
for link in links:
|
| 95 |
if link:
|
| 96 |
-
|
| 97 |
-
link_map[
|
| 98 |
|
| 99 |
api_prompt = {}
|
| 100 |
for node in nodes:
|
| 101 |
node_id = str(node["id"])
|
| 102 |
-
|
| 103 |
inputs = {}
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
if "widgets_values" in node:
|
| 107 |
-
# This is tricky as we don't know the names, but most nodes
|
| 108 |
-
# handle them in order. For API, many nodes use named inputs.
|
| 109 |
-
# We'll rely on the 'inputs' section for connections.
|
| 110 |
-
pass
|
| 111 |
-
|
| 112 |
-
# Add connections
|
| 113 |
for inp in node.get("inputs", []):
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
#
|
| 119 |
-
#
|
| 120 |
-
#
|
| 121 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
api_prompt[node_id] = {
|
| 123 |
-
"class_type":
|
| 124 |
-
"inputs":
|
| 125 |
}
|
| 126 |
return api_prompt
|
| 127 |
|
|
@@ -147,7 +175,7 @@ def remove_watermark(input_image):
|
|
| 147 |
# 2. Launch ComfyUI (Headless)
|
| 148 |
print("Launching Headless ComfyUI server...")
|
| 149 |
# Using the correct CWD is critical for ComfyUI to find its models and custom nodes
|
| 150 |
-
cmd = [sys.executable, "main.py", "--listen", "127.0.0.1", "--port", "8188", "--
|
| 151 |
proc = subprocess.Popen(cmd, cwd=COMFYUI_DIR)
|
| 152 |
|
| 153 |
# Wait for server to be ready (increased timeout and added logging)
|
|
@@ -171,8 +199,6 @@ def remove_watermark(input_image):
|
|
| 171 |
|
| 172 |
try:
|
| 173 |
# 3. Prepare Prompt
|
| 174 |
-
# Since manual conversion is prone to errors, we use the known API structure for this workflow
|
| 175 |
-
# or load the pre-converted one if we can.
|
| 176 |
workflow_path = os.path.join(BYPASS_REPO_DIR, "Synthid_Bypass.json")
|
| 177 |
with open(workflow_path, 'r') as f:
|
| 178 |
web_wf = json.load(f)
|
|
@@ -182,29 +208,59 @@ def remove_watermark(input_image):
|
|
| 182 |
if node.get("id") == 11:
|
| 183 |
node["widgets_values"] = [input_filename, "image"]
|
| 184 |
|
| 185 |
-
#
|
| 186 |
-
|
| 187 |
-
# if possible, but here we'll try to trigger it.
|
| 188 |
-
|
| 189 |
-
# TRICK: Most ComfyUI scripts for ZeroGPU use a specific 'api_workflow.json'
|
| 190 |
-
# I will create a basic one based on the web one.
|
| 191 |
-
print("Sending prompt to ComfyUI...")
|
| 192 |
-
# (Conversion logic omitted for brevity in this block, but implied to be robust)
|
| 193 |
-
# For the prototype, we return the image if the API fails, but in reality
|
| 194 |
-
# the user wants the result.
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
|
|
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
finally:
|
| 205 |
-
print("Shutting down ComfyUI...")
|
| 206 |
proc.terminate()
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
# Premium UI with Fixed Height and No Share Buttons
|
| 210 |
css = """
|
|
|
|
| 85 |
print("--- SETUP COMPLETE ---")
|
| 86 |
|
| 87 |
def convert_to_api(web_workflow):
|
| 88 |
+
"""
|
| 89 |
+
Robustly converts ComfyUI Web JSON (UI format) to API Prompt format.
|
| 90 |
+
Requires mapping links to actual node connections.
|
| 91 |
+
"""
|
| 92 |
nodes = web_workflow.get("nodes", [])
|
| 93 |
links = web_workflow.get("links", [])
|
| 94 |
|
| 95 |
+
# Map link_id -> [origin_node_id, origin_slot_index]
|
| 96 |
link_map = {}
|
| 97 |
for link in links:
|
| 98 |
if link:
|
| 99 |
+
l_id, node_from, slot_from, node_to, slot_to, l_type = link
|
| 100 |
+
link_map[l_id] = [str(node_from), slot_from]
|
| 101 |
|
| 102 |
api_prompt = {}
|
| 103 |
for node in nodes:
|
| 104 |
node_id = str(node["id"])
|
| 105 |
+
class_type = node["type"]
|
| 106 |
inputs = {}
|
| 107 |
|
| 108 |
+
# 1. Handle Connections (from links)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
for inp in node.get("inputs", []):
|
| 110 |
+
l_id = inp.get("link")
|
| 111 |
+
if l_id and l_id in link_map:
|
| 112 |
+
inputs[inp["name"]] = link_map[l_id]
|
| 113 |
+
|
| 114 |
+
# 2. Handle Widgets (from widgets_values)
|
| 115 |
+
# This is where it gets tricky since Web format stores values in a list
|
| 116 |
+
# and API format expects them as named keys.
|
| 117 |
+
# We'll use a known mapping for core nodes if possible.
|
| 118 |
+
# For custom nodes, it depends on the node's implementation of 'INPUT_TYPES'.
|
| 119 |
+
|
| 120 |
+
# Note: If the workflow was saved with 'widgets_values', we inject them.
|
| 121 |
+
# We'll try to guess common input names or just pass them as indices if the server allows.
|
| 122 |
+
# For SynthID-Bypass, we'll hardcode the critical ones if needed.
|
| 123 |
+
|
| 124 |
+
# Fallback: Many nodes put widgets after connections in their registration.
|
| 125 |
+
# If we don't have names, it might fail.
|
| 126 |
+
# However, many modern workflows save 'widgets_values' which we need to map.
|
| 127 |
+
|
| 128 |
+
# For this specific bypass tool, we'll use the pre-known node names for key nodes.
|
| 129 |
+
w_values = node.get("widgets_values", [])
|
| 130 |
+
if class_type == "CLIPTextEncode" and w_values:
|
| 131 |
+
inputs["text"] = w_values[0]
|
| 132 |
+
elif class_type == "KSampler" and len(w_values) >= 7:
|
| 133 |
+
inputs["seed"] = w_values[0]
|
| 134 |
+
inputs["steps"] = w_values[2]
|
| 135 |
+
inputs["cfg"] = w_values[3]
|
| 136 |
+
inputs["sampler_name"] = w_values[4]
|
| 137 |
+
inputs["scheduler"] = w_values[5]
|
| 138 |
+
inputs["denoise"] = w_values[6]
|
| 139 |
+
elif class_type == "VAELoader" and w_values:
|
| 140 |
+
inputs["vae_name"] = w_values[0]
|
| 141 |
+
elif class_type == "UNETLoader" and w_values:
|
| 142 |
+
inputs["unet_name"] = w_values[0]
|
| 143 |
+
elif class_type == "LoadImage" and w_values:
|
| 144 |
+
inputs["image"] = w_values[0]
|
| 145 |
+
inputs["upload"] = w_values[1] if len(w_values) > 1 else "image"
|
| 146 |
+
|
| 147 |
+
# Add any other widget values that might be present
|
| 148 |
+
# This is a guestimate, but is usually how API conversion works
|
| 149 |
+
|
| 150 |
api_prompt[node_id] = {
|
| 151 |
+
"class_type": class_type,
|
| 152 |
+
"inputs": inputs
|
| 153 |
}
|
| 154 |
return api_prompt
|
| 155 |
|
|
|
|
| 175 |
# 2. Launch ComfyUI (Headless)
|
| 176 |
print("Launching Headless ComfyUI server...")
|
| 177 |
# Using the correct CWD is critical for ComfyUI to find its models and custom nodes
|
| 178 |
+
cmd = [sys.executable, "main.py", "--listen", "127.0.0.1", "--port", "8188", "--disable-auto-launch"]
|
| 179 |
proc = subprocess.Popen(cmd, cwd=COMFYUI_DIR)
|
| 180 |
|
| 181 |
# Wait for server to be ready (increased timeout and added logging)
|
|
|
|
| 199 |
|
| 200 |
try:
|
| 201 |
# 3. Prepare Prompt
|
|
|
|
|
|
|
| 202 |
workflow_path = os.path.join(BYPASS_REPO_DIR, "Synthid_Bypass.json")
|
| 203 |
with open(workflow_path, 'r') as f:
|
| 204 |
web_wf = json.load(f)
|
|
|
|
| 208 |
if node.get("id") == 11:
|
| 209 |
node["widgets_values"] = [input_filename, "image"]
|
| 210 |
|
| 211 |
+
# Convert to API format
|
| 212 |
+
api_prompt = convert_to_api(web_wf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
print("Queueing prompt to ComfyUI...")
|
| 215 |
+
prompt_data = {"prompt": api_prompt}
|
| 216 |
+
resp = requests.post("http://127.0.0.1:8188/prompt", json=prompt_data)
|
| 217 |
|
| 218 |
+
if resp.status_code != 200:
|
| 219 |
+
raise RuntimeError(f"Failed to queue prompt: {resp.text}")
|
| 220 |
+
|
| 221 |
+
prompt_id = resp.json().get("prompt_id")
|
| 222 |
+
print(f"Prompt queued successfully (ID: {prompt_id})")
|
| 223 |
+
|
| 224 |
+
# 4. Wait for completion
|
| 225 |
+
# We poll the history endpoint until the prompt_id appears
|
| 226 |
+
max_poll = 120 # 120 seconds for processing
|
| 227 |
+
finished = False
|
| 228 |
+
output_filename = None
|
| 229 |
|
| 230 |
+
for p in range(max_poll):
|
| 231 |
+
history_resp = requests.get(f"http://127.0.0.1:8188/history/{prompt_id}")
|
| 232 |
+
if history_resp.status_code == 200:
|
| 233 |
+
history = history_resp.json()
|
| 234 |
+
if prompt_id in history:
|
| 235 |
+
# Success!
|
| 236 |
+
print("Processing complete!")
|
| 237 |
+
# Extract output filename from the SaveImage node (ID 62)
|
| 238 |
+
output_data = history[prompt_id]['outputs'].get('62')
|
| 239 |
+
if output_data and 'images' in output_data:
|
| 240 |
+
output_filename = output_data['images'][0]['filename']
|
| 241 |
+
finished = True
|
| 242 |
+
break
|
| 243 |
+
|
| 244 |
+
if p % 10 == 0: print(f"Still processing... ({p}s)")
|
| 245 |
+
time.sleep(1)
|
| 246 |
+
|
| 247 |
+
if not finished:
|
| 248 |
+
raise RuntimeError("Processing timed out or failed to save image.")
|
| 249 |
+
|
| 250 |
+
# 5. Return result
|
| 251 |
+
output_path = os.path.join(output_dir, output_filename)
|
| 252 |
+
return Image.open(output_path).copy() # Copy to avoid library closing issues
|
| 253 |
|
| 254 |
finally:
|
| 255 |
+
print("Shutting down ComfyUI server...")
|
| 256 |
proc.terminate()
|
| 257 |
+
try:
|
| 258 |
+
proc.wait(timeout=5)
|
| 259 |
+
except subprocess.TimeoutExpired:
|
| 260 |
+
proc.kill()
|
| 261 |
+
|
| 262 |
+
# Cleanup input file
|
| 263 |
+
if os.path.exists(input_path): os.remove(input_path)
|
| 264 |
|
| 265 |
# Premium UI with Fixed Height and No Share Buttons
|
| 266 |
css = """
|
temp_reference_repo
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Subproject commit 9bfeff1fe71c5b33c3757e2c280158e00bf5ff5e
|