mirror of
https://github.com/harry0703/MoneyPrinterTurbo.git
synced 2026-11-14 18:22:59 +01:00
When the output of the WebUI or the API server goes to a file or a pipe on Windows, as with "webui.bat > webui.log 2>&1", Python writes stdout and stderr in the ANSI code page. The terminal log sink then raises UnicodeEncodeError for every record with a character outside that code page, and Loguru drops the record and prints "Logging error in Loguru Handler" in its place. On English Windows (cp1252) each render loses the "① video" to "⑤ font" lines of generate_video() and every record with Chinese text, such as the task parameters and the video script. On Chinese Windows (cp936) the records with emoji or with letters that GBK lacks, such as German ä, ö and ß, are lost, and the log is written in GBK. cli.py already switched both streams to UTF-8, but only for the CLI. The helper moves to app.utils.logging_utils as force_utf8_console(), and app/config/__init__.py calls it before config.py logs anything, which covers the API, the WebUI and the CLI. cli.py and the mpt launcher call the shared helper.
76 lines
2.9 KiB
Python
76 lines
2.9 KiB
Python
"""pip 安装后的 mpt 启动入口,复用现有 WebUI 和 CLI。"""
|
|
|
|
import argparse
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from app import __version__, paths
|
|
|
|
|
|
def _port_number(value: str) -> int:
|
|
try:
|
|
port = int(value)
|
|
except ValueError as exc:
|
|
raise argparse.ArgumentTypeError("port must be an integer") from exc
|
|
if not 1 <= port <= 65535:
|
|
raise argparse.ArgumentTypeError("port must be between 1 and 65535")
|
|
return port
|
|
|
|
|
|
def _available_port(host: str) -> int:
|
|
# 与源码启动脚本保持一致:默认端口被占用时继续寻找,显式指定的端口则
|
|
# 交给 Streamlit 原样绑定和报错,避免把用户带到另一个服务上。
|
|
family = socket.AF_INET6 if ":" in host else socket.AF_INET
|
|
for port in range(8501, 8600):
|
|
try:
|
|
with socket.socket(family, socket.SOCK_STREAM) as probe:
|
|
probe.bind((host, port))
|
|
return port
|
|
except OSError:
|
|
continue
|
|
raise OSError(f"no available WebUI port on {host} between 8501 and 8599")
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
argv = list(sys.argv[1:] if argv is None else argv)
|
|
# CLI 自己负责完整参数校验、帮助和退出码,避免两层解析器吞掉业务参数。
|
|
if argv and argv[0] == "cli":
|
|
if paths.is_source_checkout():
|
|
import cli
|
|
else:
|
|
from app import cli
|
|
from app.utils.logging_utils import force_utf8_console
|
|
|
|
force_utf8_console()
|
|
return cli.run_cli(argv[1:])
|
|
|
|
parser = argparse.ArgumentParser(prog="mpt", description="MoneyPrinterTurbo")
|
|
parser.add_argument("--version", action="version", version=__version__)
|
|
commands = parser.add_subparsers(dest="command")
|
|
commands.add_parser("cli", help="generate videos (mpt cli --help)")
|
|
webui = commands.add_parser("webui", help="start the WebUI")
|
|
webui.add_argument("--host", default="127.0.0.1", help="listen address")
|
|
webui.add_argument("--port", type=_port_number, help="listen port (default: first free port from 8501)")
|
|
webui.add_argument("--headless", action="store_true", help="do not open a browser")
|
|
args = parser.parse_args(argv)
|
|
if args.command != "webui":
|
|
parser.print_help()
|
|
return 0
|
|
|
|
try:
|
|
port = args.port if args.port is not None else _available_port(args.host)
|
|
ui = Path(__file__).resolve().parent.parent / "webui" / "Main.py"
|
|
return subprocess.call([
|
|
sys.executable, "-m", "streamlit", "run", str(ui),
|
|
f"--server.address={args.host}", f"--server.port={port}",
|
|
f"--server.headless={str(args.headless).lower()}",
|
|
"--browser.gatherUsageStats=false", "--client.toolbarMode=minimal",
|
|
"--logger.hideWelcomeMessage=true", "--server.showEmailPrompt=false",
|
|
])
|
|
except OSError as exc:
|
|
parser.exit(1, f"mpt: {exc}\n")
|
|
except KeyboardInterrupt:
|
|
return 130
|