ADD 增加认证
PERF 调整部署参数
This commit is contained in:
@@ -3,6 +3,7 @@ import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import traceback
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
@@ -52,16 +53,14 @@ image = (
|
||||
.run_commands("comfy node install https://github.com/rgthree/rgthree-comfy")
|
||||
.run_commands("comfy node install https://github.com/cubiq/ComfyUI_essentials")
|
||||
.run_commands("comfy node install https://github.com/melMass/comfy_mtb")
|
||||
.run_commands("echo 3 && comfy node install https://e.coding.net/g-ldyi2063/dev/ComfyUI_SparkTTS.git")
|
||||
.run_commands("comfy node install https://e.coding.net/g-ldyi2063/dev/ComfyUI-CustomNode.git")
|
||||
.run_commands("comfy node install https://e.coding.net/g-ldyi2063/dev/ComfyUI_SparkTTS.git")
|
||||
.run_commands("comfy node install https://e.coding.net/g-ldyi2063/dev/cosyvoice_comfyui.git")
|
||||
.run_commands("comfy node install https://e.coding.net/g-ldyi2063/dev/ComfyUI-LatentSync-Node.git")
|
||||
.run_commands("echo 4 && comfy node install https://e.coding.net/g-ldyi2063/dev/ComfyUI-LatentSync-Node.git")
|
||||
.run_commands(
|
||||
"mkdir -p /root/comfy/ComfyUI/models/ComfyUI-CustomNode/model && rm -rf /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode/model && ln -s /root/comfy/ComfyUI/models/ComfyUI-CustomNode/model /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode/model"
|
||||
).run_commands(
|
||||
"mkdir -p /root/comfy/ComfyUI/models/ComfyUI-LatentSync-Node/checkpoints && rm -rf /root/comfy/ComfyUI/custom_nodes/ComfyUI-LatentSync-Node/checkpoints && ln -s /root/comfy/ComfyUI/models/ComfyUI-LatentSync-Node/checkpoints /root/comfy/ComfyUI/custom_nodes/ComfyUI-LatentSync-Node/checkpoints"
|
||||
).run_commands(
|
||||
"mkdir -p /root/comfy/ComfyUI/models/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M-SFT && mkdir -p /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M-SFT && rm -rf /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M-SFT && ln -s /root/comfy/ComfyUI/models/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M-SFT /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M-SFT"
|
||||
).run_commands(
|
||||
"mkdir -p /root/comfy/ComfyUI/models/.cache/torch/hub/checkpoints && mkdir -p /root/.cache/torch/hub/checkpoints && rm -rf /root/.cache/torch/hub/checkpoints && ln -s /root/comfy/ComfyUI/models/.cache/torch/hub/checkpoints /root/.cache/torch/hub/checkpoints"
|
||||
).run_commands(
|
||||
@@ -92,13 +91,17 @@ output_dir = "/root/comfy/ComfyUI/output"
|
||||
@app.cls(
|
||||
allow_concurrent_inputs=1, # allow 10 concurrent API calls
|
||||
max_containers=200,
|
||||
min_containers=0,
|
||||
buffer_containers=0,
|
||||
scaledown_window=120,
|
||||
# 5 minute container keep alive after it processes an input; increasing this value is a great way to reduce ComfyUI cold start times
|
||||
timeout=900,
|
||||
gpu="L4",
|
||||
cpu=8,
|
||||
gpu=["L4", "T4"],
|
||||
cpu=(2,16),
|
||||
# memory=(32768, 32768), # (内存预留量, 内存使用上限)
|
||||
memory=32768,
|
||||
memory=(20480,40960),
|
||||
retries=1,
|
||||
enable_memory_snapshot=True,
|
||||
secrets=[secret],
|
||||
volumes={
|
||||
"/root/comfy/ComfyUI/models": vol,
|
||||
@@ -121,15 +124,17 @@ class ComfyUI:
|
||||
def launch_comfy_background(self):
|
||||
# starts the ComfyUI server in the background exactly once when the first input is received
|
||||
self.session_id = str(uuid.uuid4())
|
||||
cmd = "echo client_uuid: {}&&rm -rf /root/comfy/ComfyUI/user&&mkdir -p /root/comfy/ComfyUI/output_s3/logs/{}&&ln -s /root/comfy/ComfyUI/output_s3/logs/{} /root/comfy/ComfyUI/user".format(self.session_id,self.session_id, self.session_id)
|
||||
subprocess.run(cmd, shell=True, check=True)
|
||||
cmd = "comfy launch --background"
|
||||
cmd = "echo client_uuid: {}&&rm -rf /root/comfy/ComfyUI/user&&mkdir -p /root/comfy/ComfyUI/output_s3/logs/{}&&ln -s /root/comfy/ComfyUI/output_s3/logs/{} /root/comfy/ComfyUI/user && comfy launch --background".format(self.session_id,self.session_id, self.session_id)
|
||||
subprocess.run(cmd, shell=True, check=True)
|
||||
|
||||
@modal.method()
|
||||
def infer(self, workflow_json: str = ""):
|
||||
self.poll_server_health()
|
||||
self.prompt_uuid = str(uuid.uuid4())
|
||||
# runs the comfy run --workflow command as a subprocess
|
||||
workflow = json.loads(workflow_json)
|
||||
print("Workflow JSON:")
|
||||
print(json.dumps(workflow, indent=4, ensure_ascii=False))
|
||||
for node in workflow.values():
|
||||
if node.get("class_type") == "ComfyUIDeployExternalText":
|
||||
if node.get("inputs").get("input_id") == "file_path":
|
||||
@@ -138,10 +143,10 @@ class ComfyUI:
|
||||
os.makedirs(os.path.dirname(file_to_move.replace("input_s3", "input")), exist_ok=True)
|
||||
shutil.copy(file_to_move, file_to_move.replace("input_s3", "input"))
|
||||
node["inputs"]["default_value"] = node["inputs"]["default_value"].replace("input_s3", "input")
|
||||
with open(f"/root/{self.session_id}.json", "w", encoding="utf-8") as f:
|
||||
with open(f"/root/{self.prompt_uuid}.json", "w", encoding="utf-8") as f:
|
||||
f.write(json.dumps(workflow, ensure_ascii=False))
|
||||
cmd = f"comfy run --workflow /root/{self.session_id}.json --wait --timeout 900 --verbose"
|
||||
subprocess.run(cmd, shell=True, check=True)
|
||||
cmd = f"comfy run --workflow /root/{self.prompt_uuid}.json --wait --timeout 890 --verbose"
|
||||
subprocess.run(cmd, shell=True, check=True, timeout=895)
|
||||
|
||||
# looks up the name of the output image file based on the workflow
|
||||
file_prefix = [
|
||||
@@ -165,16 +170,42 @@ class ComfyUI:
|
||||
@modal.fastapi_endpoint(method="POST")
|
||||
def api(self, item: Dict):
|
||||
from fastapi import Response
|
||||
try:
|
||||
# save this updated workflow to a new file
|
||||
new_workflow_file = item["prompt"]
|
||||
# run inference on the currently running container
|
||||
fname = self.infer.local(new_workflow_file)
|
||||
j = {"file_name": fname}
|
||||
print("json", j)
|
||||
return Response(content=json.dumps(j), media_type="application/json")
|
||||
except Exception as e:
|
||||
raise RuntimeError(str(e))
|
||||
finally:
|
||||
print("Purge Garbage By Restart Comfy")
|
||||
cmd = "comfy stop"
|
||||
subprocess.run(cmd, shell=True, check=True)
|
||||
cmd = "comfy launch --background"
|
||||
subprocess.run(cmd, shell=True, check=True)
|
||||
|
||||
# save this updated workflow to a new file
|
||||
new_workflow_file = item["prompt"]
|
||||
|
||||
# run inference on the currently running container
|
||||
fname = self.infer.local(new_workflow_file)
|
||||
j = {"file_name": fname}
|
||||
print("json", j)
|
||||
def poll_server_health(self):
|
||||
import socket
|
||||
import urllib
|
||||
|
||||
return Response(content=json.dumps(j), media_type="application/json")
|
||||
try:
|
||||
# dummy request to check if the server is healthy
|
||||
req = urllib.request.Request("http://127.0.0.1:8188/system_stats")
|
||||
urllib.request.urlopen(req, timeout=5)
|
||||
print("ComfyUI server is healthy")
|
||||
except (socket.timeout, urllib.error.URLError) as e:
|
||||
# if no response in 5 seconds, stop the container; Modal will schedule queued inputs on a new container
|
||||
print(f"Server health check failed: {str(e)} restarting ComfyUI...")
|
||||
try:
|
||||
cmd = "comfy stop && comfy launch --background"
|
||||
subprocess.run(cmd, shell=True, check=True)
|
||||
except:
|
||||
modal.experimental.stop_fetching_inputs()
|
||||
raise Exception("ComfyUI server is not healthy, restart failed, stopping container")
|
||||
|
||||
# @modal.web_endpoint(method="POST", label="tk")
|
||||
# def tk_api(self, item: Dict):
|
||||
|
||||
Reference in New Issue
Block a user