Yeetek commited on
Commit
2e4795a
Β·
verified Β·
1 Parent(s): 9d62a97

Update app.py

Browse files

2 Runtime patch that really disables numba caching
Why the previous attempts still failed
NUMBA_DISABLE_CACHE=1 prevents writing to the on-disk cache,
but numba still creates a FunctionCache object.
In 0 .56 that creation raises if it can’t locate the source file.

So we need to patch Dispatcher.enable_caching() to a no-op before
UMAP (and therefore BERTopic) is imported.

Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -1,22 +1,38 @@
1
  # ---------- BEGIN app.py ----------
2
  import os, sys, json, uuid
3
 
4
- # ── 1. Diagnostics ────────────────────────────────────────────────────────────
5
- # Print the first 20 environment variables to the Space logs (delete later)
6
- print("ENV-snapshot:", json.dumps(dict(list(os.environ.items())[:20])))
7
  sys.stdout.flush()
8
 
9
- # ── 2. Numba cache workaround (must run BEFORE bertopic import) ───────────────
10
- os.environ["NUMBA_DISABLE_CACHE"] = "1" # hard off-switch
11
- os.environ.setdefault("NUMBA_CACHE_DIR", "/tmp/numba") # fallback dir
12
  os.makedirs(os.environ["NUMBA_CACHE_DIR"], exist_ok=True)
13
 
14
- # ── 3. Heavy imports ──────────────────────────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  from typing import List
16
  from fastapi import FastAPI, HTTPException
17
  from pydantic import BaseModel
18
  from bertopic import BERTopic
19
  from sentence_transformers import SentenceTransformer
 
 
 
20
 
21
  # ── 4. Configuration via env vars ─────────────────────────────────────────────
22
  MODEL_NAME = os.getenv("EMBED_MODEL", "Seznam/simcse-small-e-czech")
 
1
  # ---------- BEGIN app.py ----------
2
  import os, sys, json, uuid
3
 
4
+ # ── 0. Quick env print – keep for now ───────────────────────────────────
5
+ print("ENV-snapshot:", json.dumps(dict(list(os.environ.items())[:25])))
 
6
  sys.stdout.flush()
7
 
8
+ # ── 1. Safety: make a writable cache dir (still useful) ────────────────
9
+ os.environ.setdefault("NUMBA_CACHE_DIR", "/tmp/numba_cache")
 
10
  os.makedirs(os.environ["NUMBA_CACHE_DIR"], exist_ok=True)
11
 
12
+ # ── 2. **Hard-patch numba** so enable_caching does nothing ────────────
13
+ try:
14
+ import numba
15
+ from numba.core import dispatcher
16
+
17
+ def _disable_cache(self):
18
+ """Monkey-patch: skip creating FunctionCache altogether."""
19
+ return
20
+
21
+ dispatcher.Dispatcher.enable_caching = _disable_cache
22
+ os.environ["NUMBA_DISABLE_CACHE"] = "1" # belt & braces
23
+ except ImportError:
24
+ # numba isn't installed yet (first pip install) – safe to ignore
25
+ pass
26
+
27
+ # ── 3. Heavy imports (UMAP, BERTopic) ──────────────────────────────────
28
  from typing import List
29
  from fastapi import FastAPI, HTTPException
30
  from pydantic import BaseModel
31
  from bertopic import BERTopic
32
  from sentence_transformers import SentenceTransformer
33
+ ...
34
+ # ---------- rest of the file unchanged ----------
35
+
36
 
37
  # ── 4. Configuration via env vars ─────────────────────────────────────────────
38
  MODEL_NAME = os.getenv("EMBED_MODEL", "Seznam/simcse-small-e-czech")