fix(photo-ai): build weights in an isolated stage and stop blocking app deploys
Prefetch moved to a separate python:3.10-slim stage that only sees weights.py/fetch-weights.py, so editing app.py or vendor/ no longer invalidates the 360 MB weights layer. Weight catalog and download helpers extracted to photo-ai/weights.py; fetch-weights.py no longer imports app (torch). Cache mount switched to sharing=shared: a canceled build could leave the exclusive lock held and hang the next build forever; downloads now use a pid-unique tmp file, so concurrent builds cannot corrupt it. Docs recommend 'docker compose up -d --build app' for app updates instead of rebuilding photo-ai/face-id.
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import urllib.request
|
||||
from collections import OrderedDict
|
||||
|
||||
log = logging.getLogger('photo-ai.weights')
|
||||
if not log.handlers:
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s'))
|
||||
log.addHandler(handler)
|
||||
log.setLevel(logging.INFO)
|
||||
log.propagate = False
|
||||
|
||||
|
||||
class WeightError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
MODEL_WEIGHTS = OrderedDict([
|
||||
('x2plus', {
|
||||
'scale': 2,
|
||||
'arch': 'rrdb_x2',
|
||||
'file': 'RealESRGAN_x2plus.pth',
|
||||
'url': 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
|
||||
'min_bytes': 60_000_000,
|
||||
'alias': True,
|
||||
'denoise': False,
|
||||
'title': 'Универсальный апскейл x2, дефолт',
|
||||
}),
|
||||
('general-x4v3', {
|
||||
'scale': 4,
|
||||
'arch': 'srvgg:32',
|
||||
'file': 'realesr-general-x4v3.pth',
|
||||
'url': 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth',
|
||||
'min_bytes': 2_000_000,
|
||||
'alias': False,
|
||||
'denoise': True,
|
||||
'title': 'Быстрый апскейл x4 с денойзом',
|
||||
'dni': {
|
||||
'file': 'realesr-general-wdn-x4v3.pth',
|
||||
'url': 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth',
|
||||
'min_bytes': 2_000_000,
|
||||
'weight': 0.5,
|
||||
},
|
||||
}),
|
||||
('animevideo-v3', {
|
||||
'scale': 4,
|
||||
'arch': 'srvgg:16',
|
||||
'file': 'realesr-animevideov3.pth',
|
||||
'url': 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth',
|
||||
'min_bytes': 1_000_000,
|
||||
'alias': False,
|
||||
'denoise': False,
|
||||
'title': 'Быстрый апскейл x4 для скриншотов и иллюстраций',
|
||||
}),
|
||||
])
|
||||
|
||||
FACE_WEIGHTS = OrderedDict([
|
||||
('gfpgan', {
|
||||
'module': 'gfpgan',
|
||||
'file': 'GFPGANv1.4.pth',
|
||||
'url': 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth',
|
||||
'min_bytes': 300_000_000,
|
||||
'arch': 'clean',
|
||||
'channel_multiplier': 2,
|
||||
'strength': False,
|
||||
'title': 'GFPGAN v1.4, восстановление лиц, дефолт',
|
||||
}),
|
||||
('codeformer', {
|
||||
'module': 'codeformer',
|
||||
'file': 'codeformer.pth',
|
||||
'url': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth',
|
||||
'min_bytes': 300_000_000,
|
||||
'strength': True,
|
||||
'title': 'CodeFormer, восстановление лиц с регулируемой силой',
|
||||
}),
|
||||
])
|
||||
|
||||
FACEXLIB_WEIGHTS = OrderedDict([
|
||||
('detection_Resnet50_Final.pth', {
|
||||
'url': 'https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth',
|
||||
'min_bytes': 90_000_000,
|
||||
}),
|
||||
('parsing_parsenet.pth', {
|
||||
'url': 'https://github.com/xinntao/facexlib/releases/download/v0.2.2/parsing_parsenet.pth',
|
||||
'min_bytes': 70_000_000,
|
||||
}),
|
||||
])
|
||||
|
||||
|
||||
def all_weights():
|
||||
items = OrderedDict()
|
||||
for spec in MODEL_WEIGHTS.values():
|
||||
items[spec['file']] = spec
|
||||
if spec.get('dni'):
|
||||
items[spec['dni']['file']] = spec['dni']
|
||||
for spec in FACE_WEIGHTS.values():
|
||||
items[spec['file']] = spec
|
||||
for name, spec in FACEXLIB_WEIGHTS.items():
|
||||
items[name] = spec
|
||||
return items
|
||||
|
||||
|
||||
def file_ok(path, min_bytes):
|
||||
return os.path.isfile(path) and os.path.getsize(path) >= min_bytes
|
||||
|
||||
|
||||
def link_or_copy(src, dst):
|
||||
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
||||
try:
|
||||
os.link(src, dst)
|
||||
except OSError:
|
||||
shutil.copyfile(src, dst)
|
||||
|
||||
|
||||
def download_weight(url, target, min_bytes):
|
||||
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||
tmp = '%s.%d.tmp' % (target, os.getpid())
|
||||
log.info('качаю веса %s -> %s', url, target)
|
||||
try:
|
||||
urllib.request.urlretrieve(url, tmp)
|
||||
except Exception as err:
|
||||
if os.path.exists(tmp):
|
||||
os.unlink(tmp)
|
||||
raise WeightError('не удалось скачать веса %s: %s' % (os.path.basename(target), err)) from err
|
||||
size = os.path.getsize(tmp) if os.path.isfile(tmp) else 0
|
||||
if size < min_bytes:
|
||||
os.unlink(tmp)
|
||||
raise WeightError('веса %s повреждены: %d байт, минимум %d'
|
||||
% (os.path.basename(target), size, min_bytes))
|
||||
os.replace(tmp, target)
|
||||
Reference in New Issue
Block a user