ateetvatan commited on
Commit
dc24051
·
1 Parent(s): a0d9a38

Hf cache issue

Browse files
Files changed (2) hide show
  1. Dockerfile +24 -15
  2. model_loader.py +2 -2
Dockerfile CHANGED
@@ -1,25 +1,34 @@
1
- # Base image
2
  FROM python:3.10-slim
3
 
4
- # Set cache envs for Transformers & HF Hub (must come before any Python install)
5
- ENV HF_HOME=/data/hf_home
6
- ENV TRANSFORMERS_CACHE=/data/cache
7
 
8
- # Install system dependencies
9
- RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
 
 
 
 
 
 
10
 
11
- # Create app directory
12
- WORKDIR /app
 
 
 
 
13
 
14
- # Copy requirements and install
15
- COPY requirements.txt .
16
- RUN pip install --upgrade pip && pip install -r requirements.txt
17
 
18
- # Copy all project files
19
- COPY . .
20
 
21
- # Expose port (default for HF Spaces)
22
  EXPOSE 7860
 
23
 
24
- # Run the app
25
  CMD ["python", "app.py"]
 
 
1
  FROM python:3.10-slim
2
 
3
+ # Create a non-root user with UID 1000
4
+ RUN useradd -m -u 1000 user
 
5
 
6
+ # Switch to that user
7
+ USER user
8
+ ENV HOME=/home/user
9
+ WORKDIR /home/user/app
10
+
11
+ # Set cache dirs inside user home
12
+ ENV HF_HOME=$HOME/.hf_home
13
+ ENV TRANSFORMERS_CACHE=$HOME/.cache/transformers
14
 
15
+ # Create cache directories
16
+ RUN mkdir -p $HF_HOME $TRANSFORMERS_CACHE
17
+
18
+ # Switch back to root to install dependencies
19
+ USER root
20
+ RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
21
 
22
+ # Install Python deps under user home
23
+ COPY --chown=user:user requirements.txt .
24
+ RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
25
 
26
+ # Copy app files
27
+ COPY --chown=user:user . .
28
 
29
+ # Expose port and switch user
30
  EXPOSE 7860
31
+ USER user
32
 
33
+ # Entrypoint
34
  CMD ["python", "app.py"]
model_loader.py CHANGED
@@ -2,8 +2,8 @@
2
  import os
3
 
4
  # Safe fallback if ENV vars are not set (e.g., during local dev)
5
- os.environ.setdefault("HF_HOME", "/data/hf_home")
6
- os.environ.setdefault("TRANSFORMERS_CACHE", "/data/cache")
7
 
8
  from transformers import AutoTokenizer, AutoModelForCausalLM
9
  import torch, os
 
2
  import os
3
 
4
  # Safe fallback if ENV vars are not set (e.g., during local dev)
5
+ os.environ.setdefault("HF_HOME", os.path.expanduser("~/.hf_home"))
6
+ os.environ.setdefault("TRANSFORMERS_CACHE", os.path.expanduser("~/.cache/transformers"))
7
 
8
  from transformers import AutoTokenizer, AutoModelForCausalLM
9
  import torch, os