Hemang Thakur commited on
Commit
1364348
·
1 Parent(s): 6cd3938

updated dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +20 -30
Dockerfile CHANGED
@@ -1,84 +1,74 @@
1
- # ----------------------------
2
  # Stage 1: Build the React Frontend
3
- # ----------------------------
4
  FROM node:20-alpine AS builder
5
  RUN apk add --no-cache libc6-compat
6
  WORKDIR /app
7
 
8
- # Create writable directories for Hugging Face Spaces
9
  RUN mkdir -p /tmp/huggingface && \
10
  chmod -R 777 /tmp/huggingface && \
11
  mkdir -p /app/workspace && \
12
  chmod -R 777 /app/workspace
13
 
14
- # Set cache environment variables at build time
15
  ENV HF_HOME=/tmp/huggingface \
16
  TRANSFORMERS_CACHE=/tmp/huggingface \
17
  XDG_CACHE_HOME=/tmp \
18
  WRITABLE_DIR=/app/workspace
19
 
20
- # Copy the 'frontend' folder from the project root into the container
21
  COPY frontend ./frontend
22
-
23
- # Switch to the frontend directory
24
  WORKDIR /app/frontend
25
-
26
- # Install dependencies (using yarn, npm, or pnpm)
27
  RUN if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
28
  elif [ -f package-lock.json ]; then npm ci; \
29
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
30
  else echo "No lockfile found. Exiting." && exit 1; \
31
  fi
32
-
33
- # Build the React app (produces a production-ready build in the "build" folder)
34
  RUN npm run build
35
 
36
- # ----------------------------
37
- # Stage 2: Set Up the FastAPI Backend and Serve the React App
38
- # ----------------------------
39
  FROM python:3.12-slim AS backend
40
  WORKDIR /app
41
 
42
- # Install OS-level dependencies
43
  RUN apt-get update --fix-missing && \
44
- apt-get install --no-install-recommends -y git curl && \
 
 
45
  apt-get clean && rm -rf /var/lib/apt/lists/*
46
 
47
- # Install Node.js (if needed for any backend tasks)
 
 
 
 
 
 
48
  RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
49
  apt-get update --fix-missing && \
50
  apt-get install --no-install-recommends -y nodejs && \
51
  apt-get clean && rm -rf /var/lib/apt/lists/*
52
 
53
- # Copy requirements.txt and install Python dependencies
54
  COPY requirements.txt .
55
  RUN pip install --no-cache-dir -r requirements.txt
56
-
57
- # Install additional dependencies for torch and spaCy
58
  RUN pip install --no-cache-dir torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu124
59
  RUN python -m spacy download en_core_web_sm
60
 
61
- # Disable telemetry for deepeval
62
  ENV DEEPEVAL_TELEMETRY_OPT_OUT=YES
63
 
64
- # Copy the rest of your backend code and resources
65
  COPY . .
66
-
67
- # Copy the built React app from the builder stage into the same folder structure as used in your FastAPI code
68
  COPY --from=builder /app/frontend/build ./frontend/build
69
 
70
- # Run HuggingFace Spaces as a root user
71
- # Re-create writable directories in this stage
72
  RUN mkdir -p /tmp/huggingface /app/workspace && \
73
  chmod -R 777 /tmp/huggingface /app/workspace && \
74
  useradd -m spaces-user && \
75
  chown -R spaces-user:spaces-user /tmp/huggingface /app/workspace
76
-
77
- # Set the user to spaces-user for running the application
78
  USER spaces-user
79
 
80
- # Expose the port
81
  EXPOSE ${PORT:-7860}
82
 
83
- # Start the FastAPI backend using Uvicorn, reading the PORT env variable
84
  CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860}"]
 
 
1
  # Stage 1: Build the React Frontend
 
2
  FROM node:20-alpine AS builder
3
  RUN apk add --no-cache libc6-compat
4
  WORKDIR /app
5
 
6
+ # Create writable directories
7
  RUN mkdir -p /tmp/huggingface && \
8
  chmod -R 777 /tmp/huggingface && \
9
  mkdir -p /app/workspace && \
10
  chmod -R 777 /app/workspace
11
 
12
+ # Set cache environment variables
13
  ENV HF_HOME=/tmp/huggingface \
14
  TRANSFORMERS_CACHE=/tmp/huggingface \
15
  XDG_CACHE_HOME=/tmp \
16
  WRITABLE_DIR=/app/workspace
17
 
18
+ # Copy and build frontend
19
  COPY frontend ./frontend
 
 
20
  WORKDIR /app/frontend
 
 
21
  RUN if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
22
  elif [ -f package-lock.json ]; then npm ci; \
23
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
24
  else echo "No lockfile found. Exiting." && exit 1; \
25
  fi
 
 
26
  RUN npm run build
27
 
28
+ # Stage 2: FastAPI Backend
 
 
29
  FROM python:3.12-slim AS backend
30
  WORKDIR /app
31
 
32
+ # Install OS dependencies and set up locales
33
  RUN apt-get update --fix-missing && \
34
+ apt-get install --no-install-recommends -y git curl locales && \
35
+ locale-gen en_US.UTF-8 && \
36
+ update-locale LANG=en_US.UTF-8 && \
37
  apt-get clean && rm -rf /var/lib/apt/lists/*
38
 
39
+ # Set locale environment variables
40
+ ENV LANG=en_US.UTF-8 \
41
+ LANGUAGE=en_US:en \
42
+ LC_ALL=en_US.UTF-8 \
43
+ PYTHONIOENCODING=utf-8
44
+
45
+ # Install Node.js
46
  RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
47
  apt-get update --fix-missing && \
48
  apt-get install --no-install-recommends -y nodejs && \
49
  apt-get clean && rm -rf /var/lib/apt/lists/*
50
 
51
+ # Copy and install Python dependencies
52
  COPY requirements.txt .
53
  RUN pip install --no-cache-dir -r requirements.txt
 
 
54
  RUN pip install --no-cache-dir torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu124
55
  RUN python -m spacy download en_core_web_sm
56
 
57
+ # Disable telemetry
58
  ENV DEEPEVAL_TELEMETRY_OPT_OUT=YES
59
 
60
+ # Copy backend code and frontend build
61
  COPY . .
 
 
62
  COPY --from=builder /app/frontend/build ./frontend/build
63
 
64
+ # Set up user and permissions
 
65
  RUN mkdir -p /tmp/huggingface /app/workspace && \
66
  chmod -R 777 /tmp/huggingface /app/workspace && \
67
  useradd -m spaces-user && \
68
  chown -R spaces-user:spaces-user /tmp/huggingface /app/workspace
69
+
 
70
  USER spaces-user
71
 
 
72
  EXPOSE ${PORT:-7860}
73
 
 
74
  CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860}"]