Mohammad Zeeshan commited on
Commit
801ea6e
·
verified ·
1 Parent(s): 7c3e785

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +21 -14
Dockerfile CHANGED
@@ -1,26 +1,33 @@
1
- # Use the official Python image as the base image
2
- FROM python:3.9
3
 
4
- # Set the working directory
5
  WORKDIR /app
 
6
 
7
- # Copy the requirements file into the container
8
- COPY requirements.txt .
9
-
10
- # Install the dependencies
11
  RUN pip install --no-cache-dir -r requirements.txt
12
 
13
- # Create a writable cache directory for Hugging Face models
14
- RUN mkdir -p /app/cache && chmod -R 777 /app/cache
 
 
15
 
16
- # Set Hugging Face cache directory to the new writable location
 
 
 
 
 
17
  ENV TRANSFORMERS_CACHE=/app/cache
 
18
 
19
- # Copy the rest of the application code into the container
20
  COPY . .
21
 
22
- # Expose the port the app runs on
 
 
 
23
  EXPOSE 7860
24
 
25
- # Command to run the application
26
- CMD ["python", "main.py"]
 
1
+ # 🏗️ Stage 1: Build dependencies in a lightweight Python image
2
+ FROM python:3.9-slim AS builder
3
 
 
4
  WORKDIR /app
5
+ COPY requirements.txt .
6
 
 
 
 
 
7
  RUN pip install --no-cache-dir -r requirements.txt
8
 
9
+ # 🚀 Stage 2: Final optimized production image
10
+ FROM python:3.9-slim
11
+
12
+ WORKDIR /app
13
 
14
+ # Copy dependencies from builder stage
15
+ COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
16
+ COPY --from=builder /usr/local/bin /usr/local/bin
17
+
18
+ # Set up a persistent cache for Hugging Face models
19
+ RUN mkdir -p /app/cache && chmod -R 777 /app/cache
20
  ENV TRANSFORMERS_CACHE=/app/cache
21
+ ENV HF_HOME=/app/cache
22
 
23
+ # Copy application code
24
  COPY . .
25
 
26
+ # Preload the model (avoid startup delays)
27
+ RUN python -c "from transformers import pipeline; pipeline('translation', model='facebook/m2m100_418M')"
28
+
29
+ # Expose port
30
  EXPOSE 7860
31
 
32
+ # Start the FastAPI server using Uvicorn
33
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "2", "--timeout-keep-alive", "5"]