File size: 14,691 Bytes
b2fbb41 |
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 |
from pathlib import Path
import sys
import argparse
import os.path as osp
import os
import importlib
import subprocess
from platform import platform
BRANCH = 'dev'
VERSION = '1.4.0'
python = sys.executable
git = os.environ.get('GIT', "git")
skip_install = False
index_url = os.environ.get('INDEX_URL', "")
QT_APIS = ['pyqt6', 'pyside6', 'pyqt5', 'pyside2']
stored_commit_hash = None
REQ_WIN = [
'pywin32'
]
PATH_ROOT=Path(__file__).parent
PATH_FONTS=str(PATH_ROOT/'fonts')
FONT_EXTS = {'.ttf','.otf','.ttc','.pfb'}
IS_WIN7 = "Windows-7" in platform()
import utils.shared as shared # Earlier import of shared to use default for config_path argument
parser = argparse.ArgumentParser()
parser.add_argument("--reinstall-torch", action='store_true', help="launch.py argument: install the appropriate version of torch even if you have some version already installed")
parser.add_argument("--proj-dir", default='', type=str, help='Open project directory on startup')
if IS_WIN7:
parser.add_argument("--qt-api", default='pyqt5', choices=QT_APIS, help='Set qt api')
else:
parser.add_argument("--qt-api", default='pyqt6', choices=QT_APIS, help='Set qt api')
parser.add_argument("--debug", action='store_true')
parser.add_argument("--requirements", default='requirements.txt')
parser.add_argument("--headless", action='store_true', help='run without GUI')
parser.add_argument("--exec_dirs", default='', help='translation queue (project directories) separated by comma')
parser.add_argument("--ldpi", default=None, type=float, help='logical dots perinch')
parser.add_argument("--export-translation-txt", action='store_true', help='save translation to txt file once RUN completed')
parser.add_argument("--export-source-txt", action='store_true', help='save source to txt file once RUN completed')
parser.add_argument("--frozen", action='store_true', help='run without checking requirements')
parser.add_argument("--update", action='store_true', help="Update the repository before launching") # Add argument --update
parser.add_argument("--config_path", default=shared.CONFIG_PATH, help='Config file to use for translation') # Named config_path to avoid conflict with existing name config
parser.add_argument('--nightly', action='store_true', help="Enable AMD Nightly ROCm")
args, _ = parser.parse_known_args()
def is_installed(package):
try:
spec = importlib.util.find_spec(package)
except ModuleNotFoundError:
return False
return spec is not None
def run(command, desc=None, errdesc=None, custom_env=None, live=False):
if desc is not None:
print(desc)
if live:
result = subprocess.run(command, shell=True, env=os.environ if custom_env is None else custom_env)
if result.returncode != 0:
raise RuntimeError(f"""{errdesc or 'Error running command'}.
Command: {command}
Error code: {result.returncode}""")
return ""
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=os.environ if custom_env is None else custom_env)
if result.returncode != 0:
message = f"""{errdesc or 'Error running command'}.
Command: {command}
Error code: {result.returncode}
stdout: {result.stdout.decode(encoding="utf8", errors="ignore") if len(result.stdout)>0 else '<empty>'}
stderr: {result.stderr.decode(encoding="utf8", errors="ignore") if len(result.stderr)>0 else '<empty>'}
"""
raise RuntimeError(message)
return result.stdout.decode(encoding="utf8", errors="ignore")
def run_pip(args, desc=None):
if skip_install:
return
index_url_line = f' --index-url {index_url}' if index_url != '' else ''
return run(f'"{python}" -m pip {args} --prefer-binary{index_url_line} --disable-pip-version-check --no-warn-script-location', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}", live=True)
def commit_hash():
global stored_commit_hash
if stored_commit_hash is not None:
return stored_commit_hash
try:
stored_commit_hash = run(f"{git} rev-parse HEAD").strip()
except Exception:
stored_commit_hash = "<none>"
return stored_commit_hash
BT = None
APP = None
def restart():
global BT
print('restarting...\n')
if BT:
BT.close()
os.execv(sys.executable, ['python'] + sys.argv)
def main():
if args.debug:
os.environ['BALLOONTRANS_DEBUG'] = '1'
os.environ['QT_API'] = args.qt_api
commit = commit_hash()
print('Python version: ', sys.version)
print('Python executable: ', sys.executable)
print(f'Version: {VERSION}')
print(f'Branch: {BRANCH}')
print(f"Commit hash: {commit}")
APP_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(APP_DIR)
prepare_environment()
from utils.zluda_config import enable_zluda_config
enable_zluda_config()
if args.update:
if getattr(sys, 'frozen', False):
print('Running as app, skipping update.')
else:
print('Checking for updates...')
try:
current_commit = commit_hash()
run(f"{git} fetch origin {BRANCH}", desc="Fetching updates from git...", errdesc="Failed to fetch updates.")
latest_commit = run(f"{git} rev-parse origin/{BRANCH}").strip()
if current_commit != latest_commit:
print("New updates found. Updating repository...")
run(f"{git} pull origin {BRANCH}", desc="Updating repository...", errdesc="Failed to update repository.")
print("Repository updated. Restarting to apply updates...")
restart()
return
else:
print("No updates found.")
except Exception as e:
print(f"Update check failed: {e}")
print("Continuing with the current version.")
from utils.logger import setup_logging, logger as LOGGER
from utils.io_utils import find_all_files_recursive
from utils import config as program_config
from qtpy.QtCore import QTranslator, QLocale, Qt
shared.args = args
shared.DEFAULT_DISPLAY_LANG = QLocale.system().name().replace('en_CN', 'zh_CN')
shared.HEADLESS = args.headless
shared.load_cache()
program_config.load_config(args.config_path)
config = program_config.pcfg
if args.headless:
config.module.load_model_on_demand = True
config.module.empty_runcache = False
if sys.platform == 'win32':
import ctypes
myappid = u'BalloonsTranslator' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
import qtpy
from qtpy.QtWidgets import QApplication
from qtpy.QtGui import QIcon, QFontDatabase, QGuiApplication, QFont
from qtpy import API, QT_VERSION
LOGGER.info(f'QT_API: {API}, QT Version: {QT_VERSION}')
shared.DEBUG = args.debug
shared.USE_PYSIDE6 = API == 'pyside6'
if qtpy.API_NAME[-1] == '6':
shared.FLAG_QT6 = True
else:
shared.FLAG_QT6 = False
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True) #enable high dpi scaling
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True) #use high dpi icons
QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
os.chdir(shared.PROGRAM_PATH)
setup_logging(shared.LOGGING_PATH)
app_args = sys.argv
if args.headless:
app_args = sys.argv + ['-platform', 'offscreen']
app = QApplication(app_args)
app.setApplicationName('BalloonsTranslator')
app.setApplicationVersion(VERSION)
# import msl.loadlib (required by translators/trans_eztrans) before init QApplication
# yield QWindowsContext: OleInitialize() failed on py3.10,
from modules.base import init_module_registries
from modules.prepare_local_files import prepare_local_files_forall
init_module_registries()
prepare_local_files_forall()
if not args.headless:
ps = QGuiApplication.primaryScreen()
shared.LDPI = ps.logicalDotsPerInch()
shared.SCREEN_W = ps.geometry().width()
shared.SCREEN_H = ps.geometry().height()
lang = config.display_lang
langp = osp.join(shared.TRANSLATE_DIR, lang + '.qm')
if osp.exists(langp):
translator = QTranslator()
translator.load(lang, osp.dirname(osp.abspath(__file__)) + "/translate")
app.installTranslator(translator)
elif lang not in ('en_US', 'English'):
LOGGER.warning(f'target display language file {langp} doesnt exist.')
LOGGER.info(f'set display language to {lang}')
# Fonts
# Load custom fonts if they exist
if osp.exists(PATH_FONTS):
for fp in find_all_files_recursive(PATH_FONTS, FONT_EXTS):
fnt_idx = QFontDatabase.addApplicationFont(fp)
if fnt_idx >= 0:
shared.CUSTOM_FONTS.append(QFontDatabase.applicationFontFamilies(fnt_idx)[0])
if sys.platform == 'win32' and args.headless:
# font database does not initialise on windows with qpa -offscreen:
# whttps://github.com/dmMaze/BallonsTranslator/issues/519
from qtpy.QtCore import QStandardPaths
font_dir_list = QStandardPaths.standardLocations(QStandardPaths.StandardLocation.FontsLocation)
for fd in font_dir_list:
fp_list = find_all_files_recursive(fd, FONT_EXTS)
for fp in fp_list:
fnt_idx = QFontDatabase.addApplicationFont(fp)
if shared.FLAG_QT6:
shared.FONT_FAMILIES = set(f for f in QFontDatabase.families())
else:
fdb = QFontDatabase()
shared.FONT_FAMILIES = set(fdb.families())
app_font = QFont('Microsoft YaHei UI')
if not app_font.exactMatch() or sys.platform == 'darwin':
app_font = app.font()
app_font.setHintingPreference(QFont.HintingPreference.PreferNoHinting)
app_font.setStyleStrategy(QFont.StyleStrategy.PreferAntialias | QFont.StyleStrategy.NoSubpixelAntialias)
QGuiApplication.setFont(app_font)
shared.DEFAULT_FONT_FAMILY = app_font.family()
shared.APP_DEFAULT_FONT = app_font.family()
if args.ldpi:
shared.LDPI = args.ldpi
from ui.mainwindow import MainWindow
ballontrans = MainWindow(app, config, open_dir=args.proj_dir, **vars(args))
global BT
BT = ballontrans
BT.restart_signal.connect(restart)
if not args.headless:
if shared.SCREEN_W > 1707 and sys.platform == 'win32': # higher than 2560 (1440p) / 1.5
# https://github.com/dmMaze/BallonsTranslator/issues/220
BT.comicTransSplitter.setHandleWidth(7)
ballontrans.setWindowIcon(QIcon(shared.ICON_PATH))
ballontrans.show()
ballontrans.resetStyleSheet()
sys.exit(app.exec())
def is_amd_gpu():
try:
if sys.platform == 'win32':
# Windows: use wmic
cmd = 'wmic path win32_VideoController get name'
output = subprocess.check_output(cmd, shell=True, text=True, stderr=subprocess.DEVNULL)
return any(keyword in output for keyword in ["AMD", "Radeon"])
else:
return False
except Exception:
return False
def supported_amd_nightly_gpu():
try:
if sys.platform == 'win32':
# Windows: use wmic
cmd = 'wmic path win32_VideoController get name'
output = subprocess.check_output(cmd, shell=True, text=True, stderr=subprocess.DEVNULL)
if any(keyword in output for keyword in
["RX 7900", "RX 7800", "RX 7700", "RX 7600", "PRO W7900", "PRO W7800", "PRO W7700"]):
return "RDNA3"
if any(keyword in output for keyword in
["RX 9070", "RX 9060"]):
return "RDNA4"
else:
return "None"
except Exception:
return "None"
def prepare_environment():
try:
import packaging
except ModuleNotFoundError:
run_pip(f"install packaging", "install packaging")
from utils.package import check_req_file, check_reqs
if getattr(sys, 'frozen', False):
print('Running as app, skip dependency installation')
return
if args.frozen:
return
req_updated = False
if sys.platform == 'win32':
for req in REQ_WIN:
if not check_reqs([req]):
run_pip(f"install {req}", req)
req_updated = True
if is_amd_gpu():
print('AMD GPU: Yes')
if args.nightly:
amd_nightly_gpu = supported_amd_nightly_gpu()
if amd_nightly_gpu == "None":
Exception("No AMD Nightly GPU supported")
if amd_nightly_gpu == "RDNA3":
torch_command = os.environ.get('TORCH_COMMAND',
"pip install rocm==7.0.0rc20250818 rocm-sdk-core==7.0.0rc20250818 rocm-sdk-libraries-gfx110X-dgpu==7.0.0rc20250818 torch==2.9.0a0+rocm7.0.0rc20250818 torchvision==0.24.0a0+rocm7.0.0rc20250818 --index-url https://d2awnip2yjpvqn.cloudfront.net/v2/gfx110X-dgpu/ intel-openmp==2025.1.1 --extra-index-url https://pypi.org/simple --disable-pip-version-check")
if amd_nightly_gpu == "RDNA4":
torch_command = os.environ.get('TORCH_COMMAND',
"pip install rocm==7.0.0rc20250817 rocm-sdk-core==7.0.0rc20250817 rocm-sdk-libraries-gfx120X-all==7.0.0rc20250817 torch==2.9.0a0+rocm7.0.0rc20250817 torchvision==0.24.0a0+rocm7.0.0rc20250817 --index-url https://d2awnip2yjpvqn.cloudfront.net/v2/gfx120X-all/ intel-openmp==2025.1.1 --extra-index-url https://pypi.org/simple --disable-pip-version-check")
else:
# AMD GPU: Cuda 11.8, Pytorch 2.2.2
torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cu118 --disable-pip-version-check")
else:
torch_command = os.environ.get('TORCH_COMMAND', "pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128 --disable-pip-version-check")
if args.reinstall_torch or not is_installed("torch") or not is_installed("torchvision"):
run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True)
req_updated = True
if not check_req_file(args.requirements):
run_pip(f"install -r {args.requirements}", "requirements")
req_updated = True
if req_updated:
import site
importlib.reload(site)
if __name__ == '__main__':
main()
|