Dev-ks04 commited on
Commit
c9b24fa
Β·
1 Parent(s): 36720a3

fix: pin numpy<2.0 to resolve faiss-cpu ImportError

Browse files

faiss-cpu 1.8.0 uses numpy.core.multiarray which was removed in NumPy 2.0.
Fix: install numpy>=1.24,<2.0 explicitly BEFORE torch and faiss in Dockerfile.

Files changed (2) hide show
  1. Dockerfile +16 -13
  2. requirements_hf.txt +7 -7
Dockerfile CHANGED
@@ -1,40 +1,43 @@
1
  # ── Contexto Backend – Hugging Face Spaces (CPU) ──────────────────────────────
2
  FROM python:3.10-slim
3
 
4
- # HF Spaces requires port 7860
5
  EXPOSE 7860
6
 
7
- # System deps
8
  RUN apt-get update && apt-get install -y --no-install-recommends \
9
  gcc g++ git curl \
10
  && rm -rf /var/lib/apt/lists/*
11
 
12
  WORKDIR /app
13
 
14
- # ── Step 1: Install PyTorch CPU-only FIRST (separate layer for caching) ────────
15
- RUN pip install --no-cache-dir --upgrade pip && \
16
- pip install --no-cache-dir \
17
- torch==2.2.2 \
18
- --index-url https://download.pytorch.org/whl/cpu
19
 
20
- # ── Step 2: Copy requirements and install remaining deps ───────────────────────
 
 
 
 
 
 
 
 
21
  COPY requirements_hf.txt ./requirements_hf.txt
22
  RUN pip install --no-cache-dir -r requirements_hf.txt
23
 
24
- # ── Step 3: Copy all source files ─────────────────────────────────────────────
25
  COPY . .
26
 
27
- # ── Step 4: Set HuggingFace cache to writable path ────────────────────────────
28
  ENV TRANSFORMERS_CACHE=/app/.cache/huggingface
29
  ENV HF_HOME=/app/.cache/huggingface
30
  ENV TOKENIZERS_PARALLELISM=false
31
 
32
- # Pre-download T5-small to bake into image (avoids cold-start delay)
33
  RUN python -c "\
34
  from transformers import T5Tokenizer, T5ForConditionalGeneration; \
35
  T5Tokenizer.from_pretrained('t5-small'); \
36
  T5ForConditionalGeneration.from_pretrained('t5-small'); \
37
- print('T5-small cached.')" || echo "Model pre-download skipped (will download at runtime)"
38
 
39
- # ── Run FastAPI on port 7860 ───────────────────────────────────────────────────
40
  CMD ["uvicorn", "src.api:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
 
1
  # ── Contexto Backend – Hugging Face Spaces (CPU) ──────────────────────────────
2
  FROM python:3.10-slim
3
 
 
4
  EXPOSE 7860
5
 
 
6
  RUN apt-get update && apt-get install -y --no-install-recommends \
7
  gcc g++ git curl \
8
  && rm -rf /var/lib/apt/lists/*
9
 
10
  WORKDIR /app
11
 
12
+ # ── Step 1: Upgrade pip ────────────────────────────────────────────────────────
13
+ RUN pip install --no-cache-dir --upgrade pip
 
 
 
14
 
15
+ # ── Step 2: Pin NumPy <2.0 FIRST (faiss-cpu needs numpy.core, removed in 2.0) ─
16
+ RUN pip install --no-cache-dir "numpy>=1.24.0,<2.0.0"
17
+
18
+ # ── Step 3: Install PyTorch CPU-only ──────────────────────────────────────────
19
+ RUN pip install --no-cache-dir \
20
+ torch==2.2.2 \
21
+ --index-url https://download.pytorch.org/whl/cpu
22
+
23
+ # ── Step 4: Install remaining dependencies ────────────────────────────────────
24
  COPY requirements_hf.txt ./requirements_hf.txt
25
  RUN pip install --no-cache-dir -r requirements_hf.txt
26
 
27
+ # ── Step 5: Copy source ───────────────────────────────────────────────────────
28
  COPY . .
29
 
30
+ # ── Step 6: HuggingFace cache config ─────────────────────────────────────────
31
  ENV TRANSFORMERS_CACHE=/app/.cache/huggingface
32
  ENV HF_HOME=/app/.cache/huggingface
33
  ENV TOKENIZERS_PARALLELISM=false
34
 
35
+ # Pre-download T5-small to bake into image
36
  RUN python -c "\
37
  from transformers import T5Tokenizer, T5ForConditionalGeneration; \
38
  T5Tokenizer.from_pretrained('t5-small'); \
39
  T5ForConditionalGeneration.from_pretrained('t5-small'); \
40
+ print('T5-small cached.')" || echo "Model will download at runtime"
41
 
42
+ # ── Step 7: Run on port 7860 ──────────────────────────────────────────────────
43
  CMD ["uvicorn", "src.api:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
requirements_hf.txt CHANGED
@@ -1,4 +1,9 @@
1
- # ── Contexto Backend – HF Spaces requirements (torch installed separately in Dockerfile) ──
 
 
 
 
 
2
 
3
  # NLP core
4
  transformers==4.40.1
@@ -6,6 +11,7 @@ sentencepiece
6
  nltk
7
 
8
  # Embedding + vector search (RAG)
 
9
  sentence-transformers==2.7.0
10
  faiss-cpu==1.8.0
11
 
@@ -16,7 +22,6 @@ pydantic==2.7.1
16
  python-multipart
17
 
18
  # Text processing
19
- numpy
20
  scikit-learn
21
  rouge-score
22
 
@@ -25,8 +30,3 @@ deep-translator==1.11.4
25
 
26
  # Document parsing helpers
27
  requests
28
-
29
- # Document reading
30
- python-docx
31
- PyMuPDF
32
- mammoth
 
1
+ # ── Contexto Backend – HF Spaces requirements ─────────────────────────────────
2
+ # NOTE: torch is installed separately in the Dockerfile via --index-url
3
+
4
+ # NumPy MUST be <2.0 β€” faiss-cpu and sentence-transformers use numpy.core
5
+ # which was removed in NumPy 2.0
6
+ numpy>=1.24.0,<2.0.0
7
 
8
  # NLP core
9
  transformers==4.40.1
 
11
  nltk
12
 
13
  # Embedding + vector search (RAG)
14
+ # faiss-cpu 1.8.0 requires numpy<2.0 (pinned above)
15
  sentence-transformers==2.7.0
16
  faiss-cpu==1.8.0
17
 
 
22
  python-multipart
23
 
24
  # Text processing
 
25
  scikit-learn
26
  rouge-score
27
 
 
30
 
31
  # Document parsing helpers
32
  requests