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.
79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
import weights as wm
|
|
|
|
WEIGHTS = wm.all_weights()
|
|
MODELS = {}
|
|
for _name, _spec in wm.MODEL_WEIGHTS.items():
|
|
MODELS[_name] = _spec
|
|
for _name, _spec in wm.FACE_WEIGHTS.items():
|
|
MODELS[_name] = _spec
|
|
|
|
|
|
def wanted():
|
|
raw = os.environ.get('PHOTO_AI_PREFETCH', '').strip()
|
|
if not raw or raw.lower() == 'all':
|
|
return dict(WEIGHTS)
|
|
picked = {}
|
|
for key in raw.split(','):
|
|
name = key.strip()
|
|
if not name:
|
|
continue
|
|
if name.lower() == 'face':
|
|
for face_name, face_spec in wm.FACE_WEIGHTS.items():
|
|
picked[face_spec['file']] = face_spec
|
|
continue
|
|
spec = MODELS.get(name) or WEIGHTS.get(name)
|
|
if spec is None:
|
|
sys.stderr.write('неизвестная модель для предзагрузки: %s\n' % name)
|
|
continue
|
|
picked[spec['file']] = spec
|
|
return picked
|
|
|
|
|
|
def fetch(name, spec, target, cache_dir):
|
|
url = spec['url']
|
|
if cache_dir:
|
|
cached = os.path.join(cache_dir, name)
|
|
if wm.file_ok(cached, spec['min_bytes']):
|
|
wm.link_or_copy(cached, target)
|
|
print('веса %s взяты из кэша сборки %s' % (name, cached))
|
|
return
|
|
try:
|
|
wm.download_weight(url, cached, spec['min_bytes'])
|
|
except Exception:
|
|
if os.path.isfile(cached):
|
|
os.unlink(cached)
|
|
raise
|
|
wm.link_or_copy(cached, target)
|
|
return
|
|
wm.download_weight(url, target, spec['min_bytes'])
|
|
|
|
|
|
def main():
|
|
target_dir = sys.argv[1] if len(sys.argv) > 1 else os.environ.get('PHOTO_AI_SEED_DIR', '/opt/photo-ai-seed')
|
|
cache_dir = sys.argv[2] if len(sys.argv) > 2 else ''
|
|
if cache_dir:
|
|
os.makedirs(cache_dir, exist_ok=True)
|
|
os.makedirs(target_dir, exist_ok=True)
|
|
failed = []
|
|
for name, spec in wanted().items():
|
|
target = os.path.join(target_dir, name)
|
|
if wm.file_ok(target, spec['min_bytes']):
|
|
continue
|
|
try:
|
|
fetch(name, spec, target, cache_dir)
|
|
except Exception as err:
|
|
failed.append('%s: %s' % (name, err))
|
|
continue
|
|
for line in failed:
|
|
sys.stderr.write('не удалось скачать %s\n' % line)
|
|
return 1 if failed else 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|