File size: 2,292 Bytes
903b444
 
464d4fa
7c679a2
464d4fa
7c679a2
 
 
903b444
 
464d4fa
 
fe49550
464d4fa
7c679a2
 
 
 
 
 
 
 
 
d796f80
 
 
 
fe49550
464d4fa
7c679a2
903b444
464d4fa
7c679a2
903b444
464d4fa
7c679a2
 
5181b3c
903b444
7c679a2
 
 
 
e60c7a5
 
d796f80
d6a554f
 
 
464d4fa
 
 
 
 
 
 
d6a554f
903b444
5181b3c
 
 
 
 
 
 
 
 
 
 
903b444
5181b3c
903b444
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
FROM python:3.12-slim

ENV DEBIAN_FRONTEND=noninteractive PIP_NO_CACHE_DIR=1

# Create non-root user
RUN useradd -m -u 1000 appuser
ENV PATH="/home/appuser/.local/bin:$PATH"

WORKDIR /app

# Minimal OS deps
RUN apt-get update && apt-get install -y --no-install-recommends libgomp1 && rm -rf /var/lib/apt/lists/*

# Put caches in user's home + sane threading + pythonpath
ENV HF_HOME=/home/appuser/.cache \
    TRANSFORMERS_CACHE=/home/appuser/.cache/transformers \
    HUGGINGFACE_HUB_CACHE=/home/appuser/.cache/huggingface \
    SENTENCE_TRANSFORMERS_HOME=/home/appuser/.cache/sentence-transformers \
    XDG_CACHE_HOME=/home/appuser/.cache \
    NLTK_DATA=/home/appuser/nltk_data \
    OMP_NUM_THREADS=1 \
    OPENBLAS_NUM_THREADS=1 \
    MKL_NUM_THREADS=1 \
    PYTHONPATH=/app/backend:$PYTHONPATH

# Disable HF fast downloader (avoids missing hf_transfer during build)
ENV HF_HUB_ENABLE_HF_TRANSFER=0

# Copy code as appuser
COPY --chown=appuser:appuser . .

# Switch to non-root BEFORE installs so caches/dirs are writable
USER appuser

# Python deps (user site)
RUN pip install --no-cache-dir --user -r requirements.txt

# NLTK data (you use wordnet + tokenizers). spaCy removed since unused.
RUN python - <<'PY'
import nltk
for pkg in ["punkt","punkt_tab","wordnet","omw-1.4"]:
    nltk.download(pkg)
print("NLTK OK")
PY

# Prefetch models (non-fatal if network hiccups)
RUN python - <<'PY'
from sentence_transformers import SentenceTransformer
from transformers import AutoTokenizer, AutoModelForTokenClassification
try:
    SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
    AutoTokenizer.from_pretrained("oliverguhr/fullstop-punctuation-multilang-large")
    AutoModelForTokenClassification.from_pretrained("oliverguhr/fullstop-punctuation-multilang-large")
    print("HF models cached")
except Exception as e:
    print("Prefetch skipped:", e)
PY

# (Optional) smoke test to catch FAISS/torch issues early
RUN python - <<'PY'
import sys
print("PY:", sys.version)
import faiss, torch
print("FAISS:", faiss.__version__)
print("Torch:", torch.__version__, "CUDA:", torch.cuda.is_available())
PY

# Spaces port + gunicorn binding
ENV PORT=7860
EXPOSE 7860
CMD ["bash","-lc","gunicorn -w 1 -k gthread --threads 4 --timeout 300 -b 0.0.0.0:${PORT:-7860} backend.app:app"]