File size: 4,584 Bytes
08f2f78
c71a436
d2be6c5
 
 
f9db221
d2be6c5
6e9a86d
d2be6c5
 
 
 
 
 
 
 
2cb0c9e
d2be6c5
 
fead15d
fc0a8e9
6e9a86d
fead15d
d2be6c5
fead15d
d2be6c5
 
fead15d
 
 
d2be6c5
0cdac26
fead15d
0faf301
fc0a8e9
 
 
 
0faf301
fc0a8e9
1131010
fead15d
08f2f78
cbf01ca
1131010
fead15d
 
0faf301
 
d2be6c5
fead15d
d2be6c5
 
fead15d
d43f63e
 
 
 
 
fead15d
d2be6c5
 
fead15d
fc0a8e9
b1f8888
fead15d
b1f8888
fc0a8e9
b1f8888
fead15d
b1f8888
 
fc0a8e9
b1f8888
fead15d
b1f8888
 
fead15d
e187d73
 
fead15d
e187d73
 
 
b1f8888
64bb281
fead15d
 
e187d73
 
fead15d
64bb281
 
fead15d
64bb281
 
fead15d
 
 
a2af212
fead15d
64bb281
 
 
fead15d
fc0a8e9
 
 
 
 
fead15d
fc0a8e9
64bb281
fead15d
 
fc0a8e9
 
0a084dd
fead15d
 
4e2d24c
c54bd9c
4e2d24c
 
 
 
 
 
 
 
 
 
fead15d
 
cb886e8
fead15d
06e59b9
 
fead15d
6e9a86d
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
FROM pytorch/pytorch:2.1.2-cuda11.8-cudnn8-devel
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    build-essential \
    python3 \
    python3-pip \
    python3-venv \
    libssl-dev \
    libffi-dev \
    git \
    wget \
    ca-certificates \
    libgl1-mesa-glx \
    libglib2.0-0 \
    python3-dev \
    g++ \
    && rm -rf /var/lib/apt/lists/*

# Create a symlink for python
RUN ln -s /usr/bin/python3 /usr/bin/python || true

# Create a non-root user
RUN useradd -m -u 1000 user

USER user

# Verify CUDA installation path
RUN find /usr/local -type d -name "cuda*"

ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH \
    PYTHONPATH=$HOME/app \
    PYTHONUNBUFFERED=1 \
    GRADIO_ALLOW_FLAGGING=never \
    GRADIO_NUM_PORTS=1 \
    GRADIO_SERVER_NAME=0.0.0.0 \
    GRADIO_THEME=huggingface \
    GRADIO_SHARE=False \
    SYSTEM=spaces

# Set CUDA_HOME environment variable
ENV CUDA_HOME=/usr/local/cuda-11.8
ENV TORCH_CUDA_ARCH_LIST="6.0;6.1;7.0;7.5;8.0;8.6+PTX;8.9;9.0"
ENV LD_LIBRARY_PATH=${CUDA_HOME}/lib64:${LD_LIBRARY_PATH}

# Set the environment variable to specify the GPU device
ENV CUDA_DEVICE_ORDER=PCI_BUS_ID
ENV CUDA_VISIBLE_DEVICES=0

# Clone the RB-Modulation repository
RUN git clone https://github.com/google/RB-Modulation.git $HOME/app

# Ensure CSD directory exists and is in the correct location
RUN if [ ! -d "$HOME/app/third_party/CSD" ]; then \
        echo "CSD directory not found in the expected location" && \
        exit 1; \
    fi

# Set the working directory
WORKDIR $HOME/app

# Upgrade build tools
RUN python3 -m pip install --upgrade pip setuptools wheel

# Download pretrained models
RUN cd third_party/StableCascade/models && \
    bash download_models.sh essential big-big bfloat16

# Install StableCascade requirements
RUN cd third_party/StableCascade && \
    pip install --no-cache-dir -r requirements.txt && \
    pip install --no-cache-dir jupyter notebook opencv-python matplotlib ftfy

# Install gdown for Google Drive downloads
RUN pip install --no-cache-dir gdown

# Download pre-trained CSD weights
RUN gdown https://drive.google.com/uc?id=1FX0xs8p-C7Ob-h5Y4cUhTeOepHzXv_46 -O $HOME/app/third_party/CSD/checkpoint.pth

# Verify the download
RUN if [ ! -f "$HOME/app/third_party/CSD/checkpoint.pth" ]; then \
        echo "CSD checkpoint file not found" && exit 1; \
    fi

RUN ls -la $HOME/app/third_party/CSD

# Ensure CSD is a proper Python package
RUN touch $HOME/app/third_party/CSD/__init__.py

# Update PYTHONPATH
ENV PYTHONPATH=$HOME/app:$HOME/app/third_party:$PYTHONPATH

# Print Python path
RUN python -c "import sys; print('\n'.join(sys.path))"

RUN pip install --no-cache-dir pybind11>=2.12 && \
    pip install --no-cache-dir numpy==1.26.4 && \
    pip install --no-cache-dir transformers==4.31.0

# Verify CSD module can be imported (try different methods)
RUN python -c "from third_party.CSD import model; print('CSD model successfully imported')" || \
    python -c "import sys; sys.path.append('/home/user/app/third_party'); from CSD import model; print('CSD model successfully imported')"

# Verify torch is available before installing GroundingDINO
RUN python -c "import torch; print('torch ok:', torch.__version__)"

# Install LangSAM dependencies
RUN pip install --no-cache-dir segment-anything==1.0

# Important fix: disable build isolation for GroundingDINO
RUN pip install --no-cache-dir --no-build-isolation git+https://github.com/IDEA-Research/GroundingDINO.git

# Install lang-segment-anything without dependencies
# to avoid reinstalling GroundingDINO in isolated build mode
RUN git clone https://github.com/luca-medeiros/lang-segment-anything /tmp/lang-segment-anything && \
    cd /tmp/lang-segment-anything && \
    git checkout 9bdf77715fa70fca96452184abdc64a6666e0d46 && \
    pip install --no-deps -e .

# Install Gradio and accelerate
RUN python3 -m pip install --no-cache-dir gradio==6.14.0 accelerate==1.0.1

# Optional: avoids hot-reload warning on Spaces
RUN pip install --no-cache-dir spaces

# Critical fix: keep NumPy on 1.x for torch/torchvision compatibility
RUN pip install --no-cache-dir --force-reinstall "numpy<2"

# Sanity checks
RUN python -c "import numpy; print('numpy version:', numpy.__version__)"
RUN python -c "import torch; import numpy; print('torch ok / numpy ok')"
RUN python -c "import groundingdino; print('groundingdino ok')"
RUN python -c "import lang_sam; print('lang_sam ok')"

# Copy the app.py file from the host to the container
COPY --chown=user:user app.py .

# Command to run the Gradio app
CMD ["python3", "app.py"]