Soumik Bose commited on
Commit
c492bb4
·
1 Parent(s): 9ab4c8b
Files changed (1) hide show
  1. Dockerfile +32 -18
Dockerfile CHANGED
@@ -1,31 +1,45 @@
1
  # Use the official Python 3.11 slim image
2
  FROM python:3.11-slim
3
 
4
- # Install curl for the keep-alive script (and clean up after)
5
- RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
6
-
7
- # Set the working directory inside the container
 
 
 
 
 
 
 
 
 
 
8
  WORKDIR /app
9
 
10
- # Environment variables for optimization and logging
11
- ENV PYTHONUNBUFFERED=1
12
- ENV PYTHONIOENCODING=UTF-8
13
- ENV HF_HOME=/tmp/cache
14
 
15
- # Copy the requirements file first
16
- COPY requirements.txt .
 
 
 
17
 
18
- # Install dependencies
19
- RUN pip install --no-cache-dir -r requirements.txt
20
 
21
- # Copy the rest of the application code
22
- COPY . .
23
 
24
- # Create cache directory
25
- RUN mkdir -p ${HF_HOME} && chmod 777 ${HF_HOME}
26
 
27
- # Expose port 7860 (required by Hugging Face Spaces)
28
  EXPOSE 7860
29
 
30
- # Keep-alive script + start Uvicorn with optimized workers
 
 
31
  CMD bash -c "while true; do curl -s https://sasasas635-database-chat.hf.space/ping >/dev/null && sleep 300; done & uvicorn main:app --host 0.0.0.0 --port 7860 --workers 4 --loop asyncio"
 
1
  # Use the official Python 3.11 slim image
2
  FROM python:3.11-slim
3
 
4
+ # Set environment variables
5
+ ENV PYTHONDONTWRITEBYTECODE=1 \
6
+ PYTHONUNBUFFERED=1 \
7
+ PYTHONIOENCODING=UTF-8 \
8
+ # Point HF cache to a writable directory inside /app
9
+ HF_HOME=/app/cache \
10
+ TRANSFORMERS_CACHE=/app/cache
11
+
12
+ # Install system dependencies (curl) and clean up in the same layer to reduce image size
13
+ RUN apt-get update && apt-get install -y --no-install-recommends curl \
14
+ && rm -rf /var/lib/apt/lists/* \
15
+ && useradd -m -u 1000 user
16
+
17
+ # Set working directory
18
  WORKDIR /app
19
 
20
+ # --- LAYER 1: Dependencies (Cached unless requirements.txt changes) ---
21
+ COPY --chown=user:user requirements.txt .
22
+ RUN pip install --no-cache-dir -r requirements.txt
 
23
 
24
+ # --- LAYER 2: Model Files (Cached unless model weights change) ---
25
+ # We explicitly copy the models folder before the rest of the code.
26
+ # This prevents the heavy model layer from breaking cache when you just edit main.py.
27
+ # Note: Ensure you have a 'models' folder locally. If not, you can remove this line.
28
+ COPY --chown=user:user models ./models
29
 
30
+ # --- LAYER 3: Application Code (Cached until you edit code) ---
31
+ COPY --chown=user:user . .
32
 
33
+ # Ensure the cache directory exists and the user owns it
34
+ RUN mkdir -p $HF_HOME && chown -R user:user /app/cache
35
 
36
+ # Switch to the non-root user
37
+ USER user
38
 
39
+ # Expose port 7860
40
  EXPOSE 7860
41
 
42
+ # Start script
43
+ # 1. Background loop pings the HF Space URL to keep it alive
44
+ # 2. Uvicorn runs the app
45
  CMD bash -c "while true; do curl -s https://sasasas635-database-chat.hf.space/ping >/dev/null && sleep 300; done & uvicorn main:app --host 0.0.0.0 --port 7860 --workers 4 --loop asyncio"