File size: 22,289 Bytes
bd993f3 5050a56 bd993f3 5050a56 bd993f3 42be709 bd993f3 42be709 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 42be709 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 42be709 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 42be709 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 42be709 bd993f3 6f5bb4a 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 107ee7d bd993f3 107ee7d bd993f3 107ee7d bd993f3 107ee7d bd993f3 107ee7d bd993f3 42be709 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 594dbe4 bd993f3 5050a56 bd993f3 5050a56 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 5050a56 bd993f3 594dbe4 bd993f3 594dbe4 5061a9b bd993f3 594dbe4 bd993f3 594dbe4 bd993f3 594dbe4 bd993f3 594dbe4 bd993f3 42be709 bd993f3 594dbe4 bd993f3 594dbe4 bd993f3 594dbe4 bd993f3 594dbe4 bd993f3 8d8a162 bd993f3 8d8a162 bd993f3 8d8a162 bd993f3 42be709 bd993f3 5050a56 bd993f3 5050a56 bd993f3 5050a56 bd993f3 8d8a162 bd993f3 8d8a162 bd993f3 5061a9b bd993f3 5050a56 bd993f3 5050a56 bd993f3 f8b16b3 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 bd993f3 42be709 804bfe8 fb6b387 804bfe8 790cdd9 bd993f3 0f9f4fe 790cdd9 bd993f3 790cdd9 bd993f3 790cdd9 804bfe8 bd993f3 5373ae2 bd993f3 5373ae2 804bfe8 5373ae2 bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 5373ae2 790cdd9 804bfe8 bd993f3 0f9f4fe bd993f3 0f9f4fe 790cdd9 804bfe8 790cdd9 88d0709 804bfe8 bd993f3 5373ae2 804bfe8 0f9f4fe 804bfe8 bd993f3 804bfe8 bd993f3 804bfe8 bd993f3 804bfe8 bd993f3 0f9f4fe bd993f3 804bfe8 bd993f3 804bfe8 bd993f3 804bfe8 bd993f3 804bfe8 bd993f3 0f9f4fe 804bfe8 bd993f3 804bfe8 790cdd9 804bfe8 790cdd9 804bfe8 790cdd9 804bfe8 790cdd9 804bfe8 790cdd9 804bfe8 790cdd9 804bfe8 790cdd9 bd993f3 804bfe8 790cdd9 804bfe8 8d50ebd 88d0709 804bfe8 790cdd9 fb6b387 804bfe8 790cdd9 bd993f3 804bfe8 790cdd9 804bfe8 790cdd9 804bfe8 dc85679 804bfe8 42be709 bd993f3 804bfe8 bd993f3 42be709 88d0709 804bfe8 bd993f3 42be709 804bfe8 bd993f3 42be709 bd993f3 42be709 804bfe8 bd993f3 804bfe8 bd993f3 804bfe8 42be709 bd993f3 804bfe8 bd993f3 107ee7d 804bfe8 bd993f3 107ee7d bd993f3 107ee7d bd993f3 42be709 804bfe8 bd993f3 42be709 bd993f3 1afb45f bd993f3 22b1d0c bd993f3 51a8816 bd993f3 6f5bb4a bd993f3 16d9f7c bd993f3 16d9f7c bd993f3 897ef7a bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 790cdd9 bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 0f9f4fe bd993f3 92fa2eb 1afb45f bd993f3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | import os
import subprocess
import threading
import shutil
import json
import requests
import gradio as gr
REPO_URL = "https://hf-mirror.com/HOPStudio/modelscope"
REPO_PATH = "/root/modelscope"
FRP_VERSION = "0.69.0"
CURRENT_DIR = REPO_PATH
LAST_OUTPUT = ""
def clone_repository():
if os.path.exists(REPO_PATH):
print(f"检测到已有仓库,正在删除: {REPO_PATH}")
try:
shutil.rmtree(REPO_PATH)
print("旧仓库已删除。")
except Exception as e:
print(f"删除旧仓库失败: {e}")
return False
try:
print(f"正在克隆仓库到 {REPO_PATH}...")
env = os.environ.copy()
env["GIT_LFS_SKIP_SMUDGE"] = "1"
env["HF_HUB_DISABLE_XET"] = "1"
subprocess.run(
["git", "clone", REPO_URL, REPO_PATH],
check=True,
env=env,
timeout=120
)
print("仓库克隆成功。")
return True
except subprocess.TimeoutExpired:
print("克隆仓库超时。")
return False
except subprocess.CalledProcessError as e:
print(f"克隆仓库失败: {e}")
return False
except Exception as e:
print(f"克隆仓库异常: {e}")
return False
def execute_shell_command(command):
global CURRENT_DIR
command = command.strip()
if command == "pwd":
return CURRENT_DIR
if command.startswith("cd "):
path = command[3:].strip()
path = os.path.expanduser(path)
if not os.path.isabs(path):
path = os.path.join(CURRENT_DIR, path)
path = os.path.abspath(path)
if os.path.isdir(path):
CURRENT_DIR = path
return f"已切换目录: {CURRENT_DIR}"
return f"目录不存在: {path}"
try:
result = subprocess.run(
command,
shell=True,
cwd=CURRENT_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=120
)
output = result.stdout + result.stderr
if not output.strip():
output = "命令执行完成,无输出。"
return f"当前目录: {CURRENT_DIR}\n\n{output}"
except subprocess.TimeoutExpired:
return "命令执行超时。"
except Exception as e:
return f"执行命令失败: {e}"
def _api_call_impl(input_text, model="internlm2.5-latest", temperature=0.8, top_p=0.9, n=1):
if input_text.startswith("cmd_run "):
command = input_text[len("cmd_run "):].strip()
return execute_shell_command(command)
api_token = os.getenv("API_TOKEN")
if not api_token:
return "API_TOKEN 未设置。"
url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_token}"
}
data = {
"model": model,
"messages": [
{
"role": "user",
"content": input_text
}
],
"n": int(n),
"temperature": float(temperature),
"top_p": float(top_p)
}
try:
response = requests.post(
url,
headers=headers,
data=json.dumps(data),
timeout=60
)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
except Exception as e:
return f"API 请求失败: {e}"
def api_call(input_text, model="internlm2.5-latest", temperature=0.8, top_p=0.9, n=1):
global LAST_OUTPUT
LAST_OUTPUT = _api_call_impl(
input_text,
model,
temperature,
top_p,
n
)
return LAST_OUTPUT
def refresh_frontend():
return LAST_OUTPUT
def create_frpc_toml():
os.makedirs(REPO_PATH, exist_ok=True)
frpc_toml_path = os.path.join(REPO_PATH, "frpc.toml")
toml_content = """
serverAddr = "103.71.69.203"
serverPort = 7000
auth.token = "hop20030219"
[[proxies]]
name = "jupyterlab"
type = "tcp"
localIP = "127.0.0.1"
localPort = 18888
remotePort = 18888
[[proxies]]
name = "comfyui"
type = "tcp"
localIP = "127.0.0.1"
localPort = 8188
remotePort = 8188
"""
try:
with open(frpc_toml_path, "w", encoding="utf-8") as f:
f.write(toml_content)
print(f"frpc.toml 已创建: {frpc_toml_path}")
except Exception as e:
print(f"创建 frpc.toml 失败: {e}")
def install_frp():
frp_tar_name = f"frp_{FRP_VERSION}_linux_amd64.tar.gz"
frp_dir_name = f"frp_{FRP_VERSION}_linux_amd64"
frp_tar_path = os.path.join(REPO_PATH, frp_tar_name)
frp_install_path = os.path.join(REPO_PATH, frp_dir_name)
frp_download_urls = [
f"https://github.com/fatedier/frp/releases/download/v{FRP_VERSION}/{frp_tar_name}",
f"https://gh.llkk.cc/https://github.com/fatedier/frp/releases/download/v{FRP_VERSION}/{frp_tar_name}",
f"https://gh-proxy.com/https://github.com/fatedier/frp/releases/download/v{FRP_VERSION}/{frp_tar_name}"
]
if os.path.exists(frp_install_path):
print(f"frp 已安装: {frp_install_path}")
return True
def is_valid_tar_gz(path):
if not os.path.exists(path):
return False
try:
result = subprocess.run(
["tar", "-tzf", path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=20
)
return result.returncode == 0
except Exception:
return False
if os.path.exists(frp_tar_path) and not is_valid_tar_gz(frp_tar_path):
print("检测到损坏的 frp 压缩包,正在删除。")
os.remove(frp_tar_path)
if not os.path.exists(frp_tar_path):
print("开始下载 frp。")
download_success = False
for url in frp_download_urls:
try:
print(f"尝试下载: {url}")
result = subprocess.run(
[
"wget",
"--timeout=30",
"--tries=1",
"--no-check-certificate",
"-O",
frp_tar_path,
url
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=90
)
if result.returncode == 0 and is_valid_tar_gz(frp_tar_path):
print("frp 下载成功。")
download_success = True
break
print("下载失败或文件无效。")
if os.path.exists(frp_tar_path):
os.remove(frp_tar_path)
except subprocess.TimeoutExpired:
print("frp 下载超时。")
if os.path.exists(frp_tar_path):
os.remove(frp_tar_path)
except Exception as e:
print(f"frp 下载异常: {e}")
if os.path.exists(frp_tar_path):
os.remove(frp_tar_path)
if not download_success:
print("frp 下载失败,跳过 frp。")
return False
try:
print(f"正在解压 frp 到 {REPO_PATH}...")
subprocess.run(
["tar", "-xzf", frp_tar_path, "-C", REPO_PATH],
check=True,
timeout=30
)
frpc_path = os.path.join(frp_install_path, "frpc")
frps_path = os.path.join(frp_install_path, "frps")
if os.path.exists(frpc_path):
subprocess.run(["chmod", "+x", frpc_path], check=False)
if os.path.exists(frps_path):
subprocess.run(["chmod", "+x", frps_path], check=False)
print("frp 安装成功。")
return True
except subprocess.TimeoutExpired:
print("frp 解压超时。")
return False
except subprocess.CalledProcessError as e:
print(f"frp 解压失败: {e}")
return False
except Exception as e:
print(f"frp 安装异常: {e}")
return False
def start_frp():
frp_dir_name = f"frp_{FRP_VERSION}_linux_amd64"
frpc_path = os.path.join(REPO_PATH, frp_dir_name, "frpc")
frpc_config_path = os.path.join(REPO_PATH, "frpc.toml")
if not os.path.exists(frpc_path):
print(f"frpc 不存在: {frpc_path}")
return
if not os.path.exists(frpc_config_path):
create_frpc_toml()
try:
subprocess.Popen(
[frpc_path, "-c", frpc_config_path],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
print("frp 已启动。")
except Exception as e:
print(f"启动 frp 失败: {e}")
def start_jupyterlab_process():
jupyter_script = os.path.join(REPO_PATH, "start_jupyterlab.py")
if not os.path.exists(jupyter_script):
print(f"未找到 JupyterLab 启动脚本: {jupyter_script}")
return
try:
subprocess.Popen(
["python3", jupyter_script],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
print("JupyterLab 已启动。")
except Exception as e:
print(f"启动 JupyterLab 失败: {e}")
# ===== 新增:ModelScope 下载 comfy_portable.zip,解压后使用自带 Python 启动 ComfyUI =====
MODELSCOPE_MODEL_ID = "HOPStudio/forgeui"
COMFY_PORTABLE_ZIP_NAME = "comfy_portable.zip"
COMFY_PORTABLE_ZIP_PATH = os.path.join("/root", COMFY_PORTABLE_ZIP_NAME)
COMFY_PORTABLE_DIR = "/root/comfy_portable"
COMFY_PORTABLE_PYTHON = os.path.join(COMFY_PORTABLE_DIR, "python", "bin", "python3")
COMFYUI_MAIN = os.path.join(COMFY_PORTABLE_DIR, "ComfyUI", "main.py")
COMFYUI_PORT = 8188
COMFYUI_LOG_PATH = os.path.join(REPO_PATH, "comfyui.log")
PROXY_URL = "http://103.71.69.203:17890"
MODELSCOPE_DOWNLOAD_TIMEOUT = 21600
def get_proxy_env():
env = os.environ.copy()
env["http_proxy"] = PROXY_URL
env["https_proxy"] = PROXY_URL
env["HTTP_PROXY"] = PROXY_URL
env["HTTPS_PROXY"] = PROXY_URL
env["ALL_PROXY"] = PROXY_URL
env["no_proxy"] = "localhost,127.0.0.1"
env["NO_PROXY"] = "localhost,127.0.0.1"
return env
def get_modelscope_env(use_proxy=False):
env = os.environ.copy()
env["MODELSCOPE_ENDPOINT"] = "https://www.modelscope.cn"
env["MODELSCOPE_SDK_ENDPOINT"] = "https://www.modelscope.cn"
proxy_keys = [
"http_proxy",
"https_proxy",
"HTTP_PROXY",
"HTTPS_PROXY",
"ALL_PROXY",
"all_proxy"
]
if use_proxy:
env["http_proxy"] = PROXY_URL
env["https_proxy"] = PROXY_URL
env["HTTP_PROXY"] = PROXY_URL
env["HTTPS_PROXY"] = PROXY_URL
env["ALL_PROXY"] = PROXY_URL
env["all_proxy"] = PROXY_URL
else:
for key in proxy_keys:
env.pop(key, None)
env["no_proxy"] = "localhost,127.0.0.1"
env["NO_PROXY"] = "localhost,127.0.0.1"
return env
def configure_git_proxy():
try:
subprocess.run(
["git", "config", "--global", "http.proxy", PROXY_URL],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=10
)
subprocess.run(
["git", "config", "--global", "https.proxy", PROXY_URL],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=10
)
print("git 代理已配置。")
except Exception as e:
print(f"配置 git 代理失败: {e}")
def is_valid_zip(path):
if not os.path.exists(path):
return False
try:
import zipfile
if not zipfile.is_zipfile(path):
return False
with zipfile.ZipFile(path, "r") as zf:
return len(zf.namelist()) > 0
except Exception:
return False
def is_comfy_portable_ready():
return os.path.exists(COMFY_PORTABLE_PYTHON) and os.path.exists(COMFYUI_MAIN)
def ensure_comfy_portable_permissions():
"""修复 zip 解压后丢失的可执行权限,只作用于 comfy_portable。"""
if not os.path.isdir(COMFY_PORTABLE_DIR):
return
paths_to_chmod = [
COMFY_PORTABLE_PYTHON,
os.path.join(COMFY_PORTABLE_DIR, "python", "bin", "python"),
os.path.join(COMFY_PORTABLE_DIR, "python", "bin", "python3.10"),
os.path.join(COMFY_PORTABLE_DIR, "start.sh"),
]
for path in paths_to_chmod:
if not os.path.exists(path):
continue
try:
current_mode = os.stat(path).st_mode
os.chmod(path, current_mode | 0o755)
print(f"已修复执行权限: {path}")
except Exception as e:
print(f"修复执行权限失败: {path}, {e}")
python_bin_dir = os.path.join(COMFY_PORTABLE_DIR, "python", "bin")
if os.path.isdir(python_bin_dir):
try:
subprocess.run(
[
"bash",
"-lc",
f"find {python_bin_dir!r} -type f -exec chmod +x {{}} \\;"
],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=60
)
print(f"已修复 python/bin 下文件执行权限: {python_bin_dir}")
except Exception as e:
print(f"批量修复 python/bin 权限失败: {e}")
def download_comfy_portable_from_modelscope():
if is_comfy_portable_ready():
print(f"检测到已解压的 comfy_portable: {COMFY_PORTABLE_DIR}")
return True
if is_valid_zip(COMFY_PORTABLE_ZIP_PATH):
print(f"检测到已有 comfy_portable 压缩包: {COMFY_PORTABLE_ZIP_PATH}")
return True
if os.path.exists(COMFY_PORTABLE_ZIP_PATH):
print(f"检测到无效 comfy_portable 压缩包,正在删除: {COMFY_PORTABLE_ZIP_PATH}")
try:
os.remove(COMFY_PORTABLE_ZIP_PATH)
except Exception as e:
print(f"删除无效压缩包失败: {e}")
return False
if not shutil.which("modelscope"):
print("未检测到 modelscope 命令。为避免改动系统 Python 依赖,本脚本不会自动安装 modelscope。")
print("请先在外部环境安装好 modelscope CLI,或手动把 comfy_portable.zip 放到 /root/comfy_portable.zip。")
return False
commands = [
(False, "直连"),
(True, "代理")
]
for use_proxy, mode_name in commands:
command = [
"modelscope",
"download",
"--model",
MODELSCOPE_MODEL_ID,
COMFY_PORTABLE_ZIP_NAME,
"--local_dir",
"/root"
]
try:
print(f"执行 ModelScope 下载 comfy_portable.zip [{mode_name}]: " + " ".join(command))
result = subprocess.run(
command,
env=get_modelscope_env(use_proxy=use_proxy),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=MODELSCOPE_DOWNLOAD_TIMEOUT
)
if result.stdout.strip():
print(result.stdout[-2000:])
if result.stderr.strip():
print(result.stderr[-4000:])
if result.returncode == 0 and is_valid_zip(COMFY_PORTABLE_ZIP_PATH):
print(f"comfy_portable.zip 下载完成: {COMFY_PORTABLE_ZIP_PATH}")
return True
print("本次 ModelScope 下载失败或压缩包无效,继续尝试下一个方式。")
except subprocess.TimeoutExpired:
print("ModelScope 下载 comfy_portable.zip 超时,继续尝试下一个方式。")
except Exception as e:
print(f"ModelScope 下载 comfy_portable.zip 异常: {e}")
print("ModelScope 下载 comfy_portable.zip 失败。")
return False
def extract_comfy_portable():
if is_comfy_portable_ready():
print(f"comfy_portable 已存在,跳过解压: {COMFY_PORTABLE_DIR}")
return True
if not is_valid_zip(COMFY_PORTABLE_ZIP_PATH):
print(f"comfy_portable.zip 不存在或无效: {COMFY_PORTABLE_ZIP_PATH}")
return False
if os.path.exists(COMFY_PORTABLE_DIR):
print(f"检测到不完整的 comfy_portable 目录,正在删除后重新解压: {COMFY_PORTABLE_DIR}")
try:
shutil.rmtree(COMFY_PORTABLE_DIR)
except Exception as e:
print(f"删除不完整 comfy_portable 目录失败: {e}")
return False
try:
print(f"正在解压 comfy_portable.zip 到 /root: {COMFY_PORTABLE_ZIP_PATH}")
import zipfile
with zipfile.ZipFile(COMFY_PORTABLE_ZIP_PATH, "r") as zf:
zf.extractall("/root")
ensure_comfy_portable_permissions()
if is_comfy_portable_ready():
print(f"comfy_portable 解压完成: {COMFY_PORTABLE_DIR}")
return True
print("解压完成,但未找到 /root/comfy_portable/python/bin/python3 或 /root/comfy_portable/ComfyUI/main.py。")
return False
except Exception as e:
print(f"解压 comfy_portable.zip 失败: {e}")
return False
def prepare_comfy_portable():
if is_comfy_portable_ready():
print(f"comfy_portable 已准备完成: {COMFY_PORTABLE_DIR}")
return True
if not download_comfy_portable_from_modelscope():
return False
return extract_comfy_portable()
def start_comfyui_process():
if not prepare_comfy_portable():
print("comfy_portable 准备失败,跳过 ComfyUI 启动。")
return
ensure_comfy_portable_permissions()
if not os.path.exists(COMFY_PORTABLE_PYTHON):
print(f"未找到便携 Python: {COMFY_PORTABLE_PYTHON}")
return
if not os.path.exists(COMFYUI_MAIN):
print(f"未找到 ComfyUI main.py: {COMFYUI_MAIN}")
return
configure_git_proxy()
python_lib_dir = os.path.join(COMFY_PORTABLE_DIR, "python", "lib")
comfyui_dir = os.path.join(COMFY_PORTABLE_DIR, "ComfyUI")
command = f"""
set -e
export LD_LIBRARY_PATH=\"{python_lib_dir}:${{LD_LIBRARY_PATH:-}}\"
export http_proxy={PROXY_URL}
export https_proxy={PROXY_URL}
export HTTP_PROXY={PROXY_URL}
export HTTPS_PROXY={PROXY_URL}
export ALL_PROXY={PROXY_URL}
export no_proxy=localhost,127.0.0.1
export NO_PROXY=localhost,127.0.0.1
git config --global http.proxy {PROXY_URL}
git config --global https.proxy {PROXY_URL}
cd \"{comfyui_dir}\"
\"{COMFY_PORTABLE_PYTHON}\" main.py --listen 0.0.0.0 --port {COMFYUI_PORT}
"""
try:
os.makedirs(REPO_PATH, exist_ok=True)
log_file = open(COMFYUI_LOG_PATH, "a", encoding="utf-8")
process = subprocess.Popen(
["bash", "-lc", command],
cwd=COMFY_PORTABLE_DIR,
env=get_proxy_env(),
stdout=log_file,
stderr=subprocess.STDOUT
)
import time
time.sleep(3)
if process.poll() is None:
print(f"ComfyUI 已启动,端口: {COMFYUI_PORT},日志: {COMFYUI_LOG_PATH}")
else:
print(f"ComfyUI 启动后立即退出,返回码: {process.returncode},请查看日志: {COMFYUI_LOG_PATH}")
except Exception as e:
print(f"启动 ComfyUI 失败: {e}")
def start_gradio():
with gr.Blocks() as demo:
gr.Markdown("# InternLM 控制台")
input_box = gr.Textbox(
label="输入",
placeholder="输入对话内容"
)
model_box = gr.Textbox(
value="internlm2.5-latest",
label="模型"
)
temperature_slider = gr.Slider(
minimum=0,
maximum=1,
value=0.8,
step=0.01,
label="Temperature"
)
top_p_slider = gr.Slider(
minimum=0,
maximum=1,
value=0.9,
step=0.01,
label="Top-p"
)
n_slider = gr.Slider(
minimum=1,
maximum=10,
value=1,
step=1,
label="生成回复数量"
)
with gr.Row():
send_button = gr.Button("发送", variant="primary")
refresh_button = gr.Button("刷新前端")
output_box = gr.Textbox(
label="回复",
lines=20,
interactive=False
)
send_button.click(
api_call,
inputs=[
input_box,
model_box,
temperature_slider,
top_p_slider,
n_slider
],
outputs=output_box
)
refresh_button.click(
refresh_frontend,
outputs=output_box
)
timer = gr.Timer(5)
timer.tick(
refresh_frontend,
outputs=output_box
)
print("Gradio 正在启动。")
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)
def main():
global CURRENT_DIR
clone_success = clone_repository()
if clone_success:
CURRENT_DIR = REPO_PATH
install_frp()
create_frpc_toml()
try:
frp_thread = threading.Thread(
target=start_frp,
daemon=True
)
frp_thread.start()
except Exception as e:
print(f"frp 线程启动失败: {e}")
try:
jupyter_thread = threading.Thread(
target=start_jupyterlab_process,
daemon=True
)
jupyter_thread.start()
except Exception as e:
print(f"JupyterLab 线程启动失败: {e}")
try:
comfyui_thread = threading.Thread(
target=start_comfyui_process,
daemon=True
)
comfyui_thread.start()
except Exception as e:
print(f"ComfyUI 线程启动失败: {e}")
print("准备启动 Gradio...")
start_gradio()
if __name__ == "__main__":
main() |