diff --git a/change1/service1.py b/change1/service1.py index 236824d..90ca5bc 100644 --- a/change1/service1.py +++ b/change1/service1.py @@ -29,6 +29,11 @@ COMFYUI_URL = "http://112.126.94.241:28188" COMFYUI_URL_BACKUP = "http://112.126.94.241:38188" WORKFLOW_PATH = Path(__file__).parent / "change1.json" +_cred_dir = Path(__file__).parent.parent +COMFYUI_USER = (_cred_dir / "user.txt").read_text(encoding="utf-8").strip() +COMFYUI_PASS = (_cred_dir / "password.txt").read_text(encoding="utf-8").strip() +COMFYUI_AUTH = (COMFYUI_USER, COMFYUI_PASS) + NODE_MODEL = "11" # 输入图(LoadImage) NODE_OUTPUT = "33" # 输出(SaveImage) NODE_PROMPT = "31" # 文本提示(PrimitiveStringMultiline) @@ -82,11 +87,11 @@ def decode_base64_image(b64_str: str) -> bytes: async def check_comfyui_alive(base_url: str, timeout: float = 3.0) -> bool: - """检查指定的 ComfyUI 服务器是否可用""" + """检查指定的 ComfyUI 服务器是否可用(能连通且非 5xx 即视为可用)""" try: - async with httpx.AsyncClient(timeout=timeout) as client: + async with httpx.AsyncClient(timeout=timeout, auth=COMFYUI_AUTH) as client: resp = await client.get(f"{base_url}/system_stats") - return resp.status_code == 200 + return resp.status_code < 500 except Exception as e: print(f"ComfyUI 健康检查失败 {base_url}: {e}") return False @@ -169,7 +174,7 @@ async def try_on(req: TryOnRequest): if desc: workflow[NODE_PROMPT]["inputs"] = {"value": desc} - async with httpx.AsyncClient(timeout=60.0) as client: + async with httpx.AsyncClient(timeout=60.0, auth=COMFYUI_AUTH) as client: try: model_bytes = decode_base64_image(req.model_image) except Exception as e: @@ -190,7 +195,7 @@ async def try_on(req: TryOnRequest): except httpx.HTTPError as e: raise HTTPException(status_code=502, detail=f"提交工作流失败: {e}") - async with httpx.AsyncClient(timeout=30.0) as poll_client: + async with httpx.AsyncClient(timeout=30.0, auth=COMFYUI_AUTH) as poll_client: outputs = await wait_for_result(poll_client, comfyui_url, prompt_id, timeout=300) node_output = outputs.get(NODE_OUTPUT) @@ -206,7 +211,7 @@ async def try_on(req: TryOnRequest): subfolder = img_info.get("subfolder", "") type_ = img_info.get("type", "output") - async with httpx.AsyncClient(timeout=30.0) as dl_client: + async with httpx.AsyncClient(timeout=30.0, auth=COMFYUI_AUTH) as dl_client: result_bytes = await fetch_image_bytes(dl_client, comfyui_url, filename, subfolder, type_) suffix = Path(filename).suffix or ".png" diff --git a/change2/service2.py b/change2/service2.py index 8c12e6c..f45afae 100644 --- a/change2/service2.py +++ b/change2/service2.py @@ -31,6 +31,11 @@ COMFYUI_URL = "http://112.126.94.241:28188" COMFYUI_URL_BACKUP = "http://112.126.94.241:38188" WORKFLOW_PATH = Path(__file__).parent / "change2_1_ex.json" +_cred_dir = Path(__file__).parent.parent +COMFYUI_USER = (_cred_dir / "user.txt").read_text(encoding="utf-8").strip() +COMFYUI_PASS = (_cred_dir / "password.txt").read_text(encoding="utf-8").strip() +COMFYUI_AUTH = (COMFYUI_USER, COMFYUI_PASS) + # WORKFLOW_PATH = Path(__file__).parent / "change_fast2.json" # 节点 ID 映射 @@ -99,11 +104,11 @@ def decode_base64_image(b64_str: str) -> bytes: async def check_comfyui_alive(base_url: str, timeout: float = 3.0) -> bool: - """检查指定的 ComfyUI 服务器是否可用""" + """检查指定的 ComfyUI 服务器是否可用(能连通且非 5xx 即视为可用)""" try: - async with httpx.AsyncClient(timeout=timeout) as client: + async with httpx.AsyncClient(timeout=timeout, auth=COMFYUI_AUTH) as client: resp = await client.get(f"{base_url}/system_stats") - return resp.status_code == 200 + return resp.status_code < 500 except Exception as e: print(f"ComfyUI 健康检查失败 {base_url}: {e}") return False @@ -187,7 +192,7 @@ async def try_on(req: TryOnRequest): if desc: workflow[NODE_PROMPT]["inputs"] = {"value": desc} - async with httpx.AsyncClient(timeout=60.0) as client: + async with httpx.AsyncClient(timeout=60.0, auth=COMFYUI_AUTH) as client: try: model_bytes = decode_base64_image(req.model_image) shirt_bytes = decode_base64_image(req.shirt_image) @@ -214,7 +219,7 @@ async def try_on(req: TryOnRequest): except httpx.HTTPError as e: raise HTTPException(status_code=502, detail=f"提交工作流失败: {e}") - async with httpx.AsyncClient(timeout=30.0) as poll_client: + async with httpx.AsyncClient(timeout=30.0, auth=COMFYUI_AUTH) as poll_client: outputs = await wait_for_result(poll_client, comfyui_url, prompt_id, timeout=300) node_output = outputs.get(NODE_OUTPUT) @@ -230,7 +235,7 @@ async def try_on(req: TryOnRequest): subfolder = img_info.get("subfolder", "") type_ = img_info.get("type", "output") - async with httpx.AsyncClient(timeout=30.0) as dl_client: + async with httpx.AsyncClient(timeout=30.0, auth=COMFYUI_AUTH) as dl_client: result_bytes = await fetch_image_bytes(dl_client, comfyui_url, filename, subfolder, type_) # 缩放到 768x1024 diff --git a/change3/service3.py b/change3/service3.py index 5c3b0bd..7d492cf 100644 --- a/change3/service3.py +++ b/change3/service3.py @@ -30,6 +30,11 @@ COMFYUI_URL = "http://112.126.94.241:28188" COMFYUI_URL_BACKUP = "http://112.126.94.241:38188" WORKFLOW_PATH = Path(__file__).parent / "change2_2_0308.json" +_cred_dir = Path(__file__).parent.parent +COMFYUI_USER = (_cred_dir / "user.txt").read_text(encoding="utf-8").strip() +COMFYUI_PASS = (_cred_dir / "password.txt").read_text(encoding="utf-8").strip() +COMFYUI_AUTH = (COMFYUI_USER, COMFYUI_PASS) + # WORKFLOW_PATH = Path(__file__).parent / "change_fast3.json" # 节点 ID 映射 @@ -90,11 +95,11 @@ def decode_base64_image(b64_str: str) -> bytes: async def check_comfyui_alive(base_url: str, timeout: float = 3.0) -> bool: - """检查指定的 ComfyUI 服务器是否可用""" + """检查指定的 ComfyUI 服务器是否可用(能连通且非 5xx 即视为可用)""" try: - async with httpx.AsyncClient(timeout=timeout) as client: + async with httpx.AsyncClient(timeout=timeout, auth=COMFYUI_AUTH) as client: resp = await client.get(f"{base_url}/system_stats") - return resp.status_code == 200 + return resp.status_code < 500 except Exception as e: print(f"ComfyUI 健康检查失败 {base_url}: {e}") return False @@ -178,7 +183,7 @@ async def try_on(req: TryOnRequest): if desc: workflow[NODE_PROMPT]["inputs"] = {"value": desc} - async with httpx.AsyncClient(timeout=60.0) as client: + async with httpx.AsyncClient(timeout=60.0, auth=COMFYUI_AUTH) as client: try: model_bytes = decode_base64_image(req.model_image) shirt_bytes = decode_base64_image(req.shirt_image) @@ -209,7 +214,7 @@ async def try_on(req: TryOnRequest): except httpx.HTTPError as e: raise HTTPException(status_code=502, detail=f"提交工作流失败: {e}") - async with httpx.AsyncClient(timeout=30.0) as poll_client: + async with httpx.AsyncClient(timeout=30.0, auth=COMFYUI_AUTH) as poll_client: outputs = await wait_for_result(poll_client, comfyui_url, prompt_id, timeout=300) node_output = outputs.get(NODE_OUTPUT) @@ -225,7 +230,7 @@ async def try_on(req: TryOnRequest): subfolder = img_info.get("subfolder", "") type_ = img_info.get("type", "output") - async with httpx.AsyncClient(timeout=30.0) as dl_client: + async with httpx.AsyncClient(timeout=30.0, auth=COMFYUI_AUTH) as dl_client: result_bytes = await fetch_image_bytes(dl_client, comfyui_url, filename, subfolder, type_) # 缩放到 768x1024 diff --git a/password.txt b/password.txt new file mode 100644 index 0000000..712e6fa --- /dev/null +++ b/password.txt @@ -0,0 +1 @@ +your888password \ No newline at end of file diff --git a/user.txt b/user.txt new file mode 100644 index 0000000..f77b004 --- /dev/null +++ b/user.txt @@ -0,0 +1 @@ +admin \ No newline at end of file