Skip to content

Commit 31498dc

Browse files
committed
Fall back to lazy load using try except block
1 parent 5d8c8b3 commit 31498dc

1 file changed

Lines changed: 29 additions & 25 deletions

File tree

server/api/apps.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@ class ApiConfig(AppConfig):
66
name = 'api'
77

88
def ready(self):
9-
import os
10-
import sys
11-
12-
# ready() runs in every Django process: migrate, test, shell, runserver, etc.
13-
# Only preload the model when we're actually going to serve requests.
14-
# Dev (docker-compose.yml) runs `manage.py runserver 0.0.0.0:8000`.
15-
# Prod (Dockerfile.prod CMD) runs `manage.py runserver 0.0.0.0:8000 --noreload`.
16-
# entrypoint.prod.sh also runs migrate, createsu, and populatedb before exec'ing
17-
# runserver — the guard below correctly skips model loading for those commands too.
18-
if sys.argv[1:2] != ['runserver']:
19-
return
20-
21-
# Dev's autoreloader spawns two processes: a parent file-watcher and a child
22-
# server. ready() runs in both, but only the child (RUN_MAIN=true) serves
23-
# requests. Skip the parent to avoid loading the model twice on each file change.
24-
# Prod uses --noreload so RUN_MAIN is never set; 'noreload' in sys.argv handles that case.
25-
if os.environ.get('RUN_MAIN') != 'true' and '--noreload' not in sys.argv:
26-
return
27-
28-
# Note: paraphrase-MiniLM-L6-v2 (~80MB) is downloaded from HuggingFace on first
29-
# use and cached to ~/.cache/torch/sentence_transformers/ inside the container.
30-
# That cache is ephemeral — every container rebuild re-downloads the model unless
31-
# a volume is mounted at that path.
32-
from .services.sentencetTransformer_model import TransformerModel
33-
TransformerModel.get_instance()
9+
10+
try:
11+
import os
12+
import sys
13+
14+
# ready() runs in every Django process: migrate, test, shell, runserver, etc.
15+
# Only preload the model when we're actually going to serve requests.
16+
# Dev (docker-compose.yml) runs `manage.py runserver 0.0.0.0:8000`.
17+
# Prod (Dockerfile.prod CMD) runs `manage.py runserver 0.0.0.0:8000 --noreload`.
18+
# entrypoint.prod.sh also runs migrate, createsu, and populatedb before exec'ing
19+
# runserver — the guard below correctly skips model loading for those commands too.
20+
if sys.argv[1:2] != ['runserver']:
21+
return
22+
23+
# Dev's autoreloader spawns two processes: a parent file-watcher and a child
24+
# server. ready() runs in both, but only the child (RUN_MAIN=true) serves
25+
# requests. Skip the parent to avoid loading the model twice on each file change.
26+
# Prod uses --noreload so RUN_MAIN is never set; 'noreload' in sys.argv handles that case.
27+
if os.environ.get('RUN_MAIN') != 'true' and '--noreload' not in sys.argv:
28+
return
29+
30+
# Note: paraphrase-MiniLM-L6-v2 (~80MB) is downloaded from HuggingFace on first
31+
# use and cached to ~/.cache/torch/sentence_transformers/ inside the container.
32+
# That cache is ephemeral — every container rebuild re-downloads the model unless
33+
# a volume is mounted at that path.
34+
from .services.sentencetTransformer_model import TransformerModel
35+
TransformerModel.get_instance()
36+
except Exception:
37+
logger.exception("Failed to preload the embedding model at startup")

0 commit comments

Comments
 (0)