File size: 2,445 Bytes
209e402
2b61f9d
209e402
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b61f9d
 
 
 
209e402
 
 
 
 
 
 
 
 
 
 
2b61f9d
209e402
 
75e2ca4
 
 
 
2b61f9d
209e402
 
 
 
 
 
 
 
 
 
 
 
 
 
2b61f9d
209e402
 
2b61f9d
209e402
 
2b61f9d
209e402
2b61f9d
 
4b9a663
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
FROM ghcr.io/astral-sh/uv:python3.10-bookworm-slim as builder

WORKDIR /app

# Copy project files for dependency installation
COPY pyproject.toml /app/
COPY README.md /app/

# Show pyproject.toml content
RUN cat pyproject.toml

# Create a virtual environment and install dependencies using uv sync
RUN uv venv /app/.venv && \
    . /app/.venv/bin/activate && \
    uv sync

# Verify installations with the virtual environment
RUN . /app/.venv/bin/activate && \
    python -c "import numpy; print(f'NumPy version: {numpy.__version__}')" && \
    python -c "import pandas; print(f'Pandas version: {pandas.__version__}')" && \
    python -c "import uvicorn; print(f'Uvicorn version: {uvicorn.__version__}')"

# Second stage for the final image
FROM ghcr.io/astral-sh/uv:python3.10-bookworm-slim

# Add user - this is the user that will run the app
RUN useradd -m -u 1000 user

# Copy the virtual environment from the builder stage
COPY --from=builder /app/.venv /home/user/app/.venv

# Install Node.js for building the frontend
RUN apt-get update && apt-get install -y \
    curl \
    gnupg \
    && curl -sL https://deb.nodesource.com/setup_18.x | bash - \
    && apt-get install -y nodejs \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Set up user environment
ENV HOME=/home/user \
    PATH=/home/user/app/.venv/bin:$PATH \
    UVICORN_WS_PROTOCOL=websockets \
    FORWARDED_ALLOW_IPS="*" \
    HTTPTOOLS_LOG_DEBUG=1

# Verify dependencies are available in the final image
RUN python -c "import numpy; print(f'NumPy version: {numpy.__version__}')" && \
    python -c "import pandas; print(f'Pandas version: {pandas.__version__}')" && \
    python -c "import uvicorn; print(f'Uvicorn version: {uvicorn.__version__}')"

# Copy frontend code and build it
COPY --chown=user frontend /home/user/app/frontend
USER user
WORKDIR /home/user/app/frontend
RUN npm install && npm run build

# Copy backend code
WORKDIR /home/user/app
COPY --chown=user backend /home/user/app/backend

# Copy aimakerspace module
COPY --chown=user aimakerspace /home/user/app/aimakerspace

# Set the working directory to the backend folder
WORKDIR /home/user/app/backend

# Expose port for FastAPI on Hugging Face
EXPOSE 7860

# Start the FastAPI server with optimized settings for Hugging Face Spaces
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--proxy-headers", "--forwarded-allow-ips=*", "--timeout-keep-alive", "75"]