ikraamkb commited on
Commit
2f8cc3e
·
verified ·
1 Parent(s): ce61d8a

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +17 -19
Dockerfile CHANGED
@@ -1,40 +1,38 @@
1
- # Use Python 3.9 as the base image
2
  FROM python:3.9
3
 
4
- # Create a user and set permissions
5
  RUN useradd -m -u 1000 user
6
  USER user
7
-
8
- # Ensure PATH includes user's local bin
9
  ENV PATH="/home/user/.local/bin:$PATH"
10
 
11
- # Set environment variables to cache Hugging Face & Torch models
12
- ENV TRANSFORMERS_CACHE="/cache/huggingface"
13
- ENV TORCH_HOME="/cache/torch"
14
-
15
- # Set working directory
16
- WORKDIR /app
17
 
18
- # Create model cache directories with correct permissions
19
- RUN mkdir -p /cache/huggingface /cache/torch && chmod -R 777 /cache
20
 
21
- # Preload models into cache
22
  RUN python -c "from transformers import AutoModelForCausalLM, AutoTokenizer; \
23
- AutoModelForCausalLM.from_pretrained('microsoft/phi-2'); \
24
- AutoTokenizer.from_pretrained('microsoft/phi-2')" \
25
  && python -c "from torchvision.models.detection import fasterrcnn_resnet50_fpn, FasterRCNN_ResNet50_FPN_Weights; \
26
  weights = FasterRCNN_ResNet50_FPN_Weights.DEFAULT; \
27
- model = fasterrcnn_resnet50_fpn(weights=weights)"
 
 
 
28
 
29
  # Copy requirements and install dependencies
30
  COPY --chown=user ./requirements.txt requirements.txt
31
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
32
 
33
- # Copy the rest of the application code
34
  COPY --chown=user . /app
35
 
36
- # Expose the port (optional)
37
  EXPOSE 7860
38
 
39
- # Start the FastAPI application with Uvicorn
40
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ # Use Python 3.9 as base image
2
  FROM python:3.9
3
 
4
+ # Create a non-root user
5
  RUN useradd -m -u 1000 user
6
  USER user
 
 
7
  ENV PATH="/home/user/.local/bin:$PATH"
8
 
9
+ # Set environment variables for model caching
10
+ ENV TRANSFORMERS_CACHE="/home/user/.cache/huggingface"
11
+ ENV TORCH_HOME="/home/user/.cache/torch"
 
 
 
12
 
13
+ # Create cache directories
14
+ RUN mkdir -p $TRANSFORMERS_CACHE $TORCH_HOME && chmod -R 777 $TRANSFORMERS_CACHE $TORCH_HOME
15
 
16
+ # Preload and cache models
17
  RUN python -c "from transformers import AutoModelForCausalLM, AutoTokenizer; \
18
+ AutoModelForCausalLM.from_pretrained('microsoft/phi-2', cache_dir='$TRANSFORMERS_CACHE'); \
19
+ AutoTokenizer.from_pretrained('microsoft/phi-2', cache_dir='$TRANSFORMERS_CACHE')" \
20
  && python -c "from torchvision.models.detection import fasterrcnn_resnet50_fpn, FasterRCNN_ResNet50_FPN_Weights; \
21
  weights = FasterRCNN_ResNet50_FPN_Weights.DEFAULT; \
22
+ model = fasterrcnn_resnet50_fpn(weights=weights, cache_dir='$TORCH_HOME')"
23
+
24
+ # Set working directory
25
+ WORKDIR /app
26
 
27
  # Copy requirements and install dependencies
28
  COPY --chown=user ./requirements.txt requirements.txt
29
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
30
 
31
+ # Copy application code
32
  COPY --chown=user . /app
33
 
34
+ # Expose the port
35
  EXPOSE 7860
36
 
37
+ # Start the FastAPI app with Uvicorn
38
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]