File size: 2,202 Bytes
3ed0796
 
 
 
7ab95fc
3ed0796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ab95fc
3ed0796
 
 
 
 
 
 
96be85a
3ed0796
 
 
 
 
 
 
 
fdd45ab
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
74
75
76
77
78
# Get uv installer
FROM ghcr.io/astral-sh/uv:0.2.12 as uv

# Main application image
FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04

# Set home to the user's home directory
ENV DEBIAN_FRONTEND=noninteractive \
HOME=/home/user \
	PATH=/home/user/.local/bin:$PATH \
	GRADIO_ALLOW_FLAGGING=never \
	GRADIO_NUM_PORTS=1 \
	GRADIO_SERVER_NAME=0.0.0.0 \
	GRADIO_THEME=huggingface \
	SYSTEM=spaces

# Copy uv
COPY --from=uv /uv /uv

# Install Python, pip, venv, and system dependencies
RUN apt-get update && \
    apt-get install -y --fix-missing --no-install-recommends \
    python3.11 python3.11-venv python3-pip ffmpeg \
    build-essential \
    git \
    && apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Create virtual environment
RUN --mount=type=cache,target=/root/.cache/uv \
    /uv venv /opt/venv

# Set environment variables
ENV VIRTUAL_ENV=/opt/venv \
    PATH="/opt/venv/bin:$PATH" \
    PORT=7860 \
    SERVER_NAME=0.0.0.0

# Create user and set permissions (required for HF Spaces)
RUN useradd -m -u 1000 user && \
    chown -R user /opt/venv

# Switch to user context
USER user
WORKDIR /app

# Set home to user's home directory and other envs
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH \
    HF_HOME=/home/user/.cache/huggingface \
    UV_CACHE_DIR=/app/.uv-cache

# Create cache directory with proper permissions
RUN mkdir -p $UV_CACHE_DIR && chown -R user:user $UV_CACHE_DIR

# Copy requirements first for caching
COPY --chown=user requirements.txt .

# Install Python packages with uv caching
RUN --mount=type=cache,target=$UV_CACHE_DIR,uid=1000,gid=1000 \
    /uv pip install torch torchvision

# Install build dependencies for flash-attn
RUN --mount=type=cache,target=$UV_CACHE_DIR,uid=1000,gid=1000 \
    /uv pip install -U --no-cache-dir setuptools wheel ninja packaging

# Install flash-attn
RUN --mount=type=cache,target=$UV_CACHE_DIR,uid=1000,gid=1000 \
    MAX_JOBS=4 /uv pip install flash-attn --no-build-isolation

# Install Python packages with uv caching
RUN --mount=type=cache,target=$UV_CACHE_DIR,uid=1000,gid=1000 \
    /uv pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY --chown=user . .

ENTRYPOINT ["python", "app.py"]