Skip to content

Commit def1f4b

Browse files
authored
Merge pull request #1 from Euda1mon1a/claude/setup-project-EmjdD
2 parents 1eaa046 + b8bc987 commit def1f4b

File tree

5 files changed

+529
-0
lines changed

5 files changed

+529
-0
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.dockerignore
2+
.gitignore
3+
*.md
4+
Dockerfile
5+
compose.yml
6+
data/

DOCKER.md

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# Docker Setup Guide for SHARP
2+
3+
This guide covers running SHARP using Docker with the Gradio web interface.
4+
5+
## Prerequisites
6+
7+
- **Docker** 20.10 or later
8+
- **Docker Compose** v2.0 or later
9+
- **NVIDIA GPU** with CUDA support
10+
- **NVIDIA Container Toolkit** ([installation guide](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html))
11+
12+
### Verify NVIDIA Container Toolkit
13+
14+
```bash
15+
docker run --rm --gpus all nvidia/cuda:12.9.1-base-ubuntu24.04 nvidia-smi
16+
```
17+
18+
## Quick Start
19+
20+
```bash
21+
# Clone the repository
22+
git clone https://github.com/apple/ml-sharp.git
23+
cd ml-sharp
24+
25+
# Build and run
26+
docker compose up --build
27+
```
28+
29+
Open http://localhost:7860 in your browser.
30+
31+
## Configuration
32+
33+
### Environment Variables
34+
35+
Configure the application by setting environment variables in `compose.yml`:
36+
37+
| Variable | Default | Description |
38+
|----------|---------|-------------|
39+
| `SHARP_AUTH_USERNAME` | *(none)* | Username for web authentication |
40+
| `SHARP_AUTH_PASSWORD` | *(none)* | Password for web authentication |
41+
| `SHARP_MAX_FILE_SIZE_MB` | `50` | Maximum upload file size in MB |
42+
| `SHARP_PORT` | `7860` | Web server port |
43+
| `SHARP_DATA_DIR` | `/app/data` | Data directory inside container |
44+
45+
### Enabling Authentication
46+
47+
Edit `compose.yml` to uncomment and set credentials:
48+
49+
```yaml
50+
environment:
51+
- SHARP_AUTH_USERNAME=admin
52+
- SHARP_AUTH_PASSWORD=your-secure-password
53+
```
54+
55+
**Important:** Always enable authentication when exposing the service to a network.
56+
57+
### Resource Limits
58+
59+
The default configuration limits resources to prevent abuse:
60+
61+
```yaml
62+
deploy:
63+
resources:
64+
limits:
65+
cpus: "8"
66+
memory: 32G
67+
```
68+
69+
Adjust these based on your hardware capabilities.
70+
71+
## Usage
72+
73+
### Web Interface
74+
75+
1. Navigate to http://localhost:7860
76+
2. Upload an image (JPEG, PNG, GIF, BMP, or WebP)
77+
3. Wait for processing (typically under 1 second on GPU)
78+
4. Download the generated RGB and depth videos
79+
80+
### Supported Image Formats
81+
82+
- JPEG/JPG
83+
- PNG
84+
- GIF
85+
- BMP
86+
- WebP
87+
88+
Maximum file size: 50MB (configurable)
89+
90+
### Output
91+
92+
The web interface generates two videos:
93+
- **RGB Video**: Photorealistic 3D view synthesis
94+
- **Depth Video**: Depth map visualization
95+
96+
Output files are saved to the `./data` directory on your host machine.
97+
98+
## Advanced Usage
99+
100+
### Running in Background
101+
102+
```bash
103+
docker compose up -d --build
104+
```
105+
106+
### Viewing Logs
107+
108+
```bash
109+
docker compose logs -f
110+
```
111+
112+
### Stopping the Service
113+
114+
```bash
115+
docker compose down
116+
```
117+
118+
### Rebuilding After Changes
119+
120+
```bash
121+
docker compose up --build --force-recreate
122+
```
123+
124+
### Using a Custom Port
125+
126+
Edit `compose.yml`:
127+
128+
```yaml
129+
ports:
130+
- "8080:7860" # Access at localhost:8080
131+
```
132+
133+
Or set the environment variable:
134+
135+
```yaml
136+
environment:
137+
- SHARP_PORT=8080
138+
ports:
139+
- "8080:8080"
140+
```
141+
142+
## Production Deployment
143+
144+
### Security Recommendations
145+
146+
1. **Enable Authentication**: Always set `SHARP_AUTH_USERNAME` and `SHARP_AUTH_PASSWORD`
147+
148+
2. **Use a Reverse Proxy**: Deploy behind nginx or Traefik for:
149+
- SSL/TLS termination
150+
- Rate limiting
151+
- Additional security headers
152+
153+
3. **Network Isolation**: Bind to localhost and use a reverse proxy:
154+
```yaml
155+
ports:
156+
- "127.0.0.1:7860:7860"
157+
```
158+
159+
4. **Regular Updates**: Keep the Docker image updated for security patches
160+
161+
### Example nginx Configuration
162+
163+
```nginx
164+
server {
165+
listen 443 ssl http2;
166+
server_name sharp.example.com;
167+
168+
ssl_certificate /path/to/cert.pem;
169+
ssl_certificate_key /path/to/key.pem;
170+
171+
location / {
172+
proxy_pass http://127.0.0.1:7860;
173+
proxy_http_version 1.1;
174+
proxy_set_header Upgrade $http_upgrade;
175+
proxy_set_header Connection "upgrade";
176+
proxy_set_header Host $host;
177+
proxy_set_header X-Real-IP $remote_addr;
178+
proxy_read_timeout 300s;
179+
}
180+
}
181+
```
182+
183+
## Troubleshooting
184+
185+
### GPU Not Detected
186+
187+
```bash
188+
# Verify NVIDIA Container Toolkit
189+
nvidia-ctk --version
190+
191+
# Check Docker GPU access
192+
docker run --rm --gpus all nvidia/cuda:12.9.1-base-ubuntu24.04 nvidia-smi
193+
```
194+
195+
### Out of Memory
196+
197+
Reduce resource limits or process smaller images:
198+
199+
```yaml
200+
environment:
201+
- SHARP_MAX_FILE_SIZE_MB=20
202+
```
203+
204+
### Build Fails
205+
206+
Ensure you have sufficient disk space (the image requires ~15GB):
207+
208+
```bash
209+
docker system df
210+
docker system prune # Clean unused resources
211+
```
212+
213+
### Container Won't Start
214+
215+
Check logs for errors:
216+
217+
```bash
218+
docker compose logs sharp
219+
```
220+
221+
### Permission Denied on Data Directory
222+
223+
Ensure the `./data` directory is writable:
224+
225+
```bash
226+
mkdir -p data
227+
chmod 755 data
228+
```
229+
230+
## Development
231+
232+
### Building Manually
233+
234+
```bash
235+
docker build -t sharp:latest .
236+
```
237+
238+
### Running Without Compose
239+
240+
```bash
241+
docker run --gpus all -p 7860:7860 -v $(pwd)/data:/app/data sharp:latest
242+
```
243+
244+
### Accessing Container Shell
245+
246+
```bash
247+
docker compose exec sharp bash
248+
```
249+
250+
## Architecture
251+
252+
The Docker setup includes:
253+
254+
- **Base Image**: `nvidia/cuda:12.9.1-cudnn-devel-ubuntu24.04`
255+
- **Python**: 3.13
256+
- **Security**: Runs as non-root user (`sharp`)
257+
- **Web Interface**: Gradio on port 7860
258+
- **Model**: Auto-downloaded on first build (~cached in image)
259+
260+
## License
261+
262+
See the main [README.md](README.md) for license information.

Dockerfile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
FROM nvidia/cuda:12.9.1-cudnn-devel-ubuntu24.04
2+
3+
# Install Python 3.13
4+
RUN apt-get update && apt-get install -y wget software-properties-common build-essential && apt-get clean && rm -rf /var/lib/apt/lists/*
5+
RUN add-apt-repository ppa:deadsnakes/ppa
6+
RUN apt-get update && apt-get install -y python3.13 python3.13-venv python3.13-dev ninja-build && apt-get clean && rm -rf /var/lib/apt/lists/*
7+
8+
# Create non-root user for security
9+
RUN useradd -m -u 1000 -s /bin/bash sharp
10+
11+
# Install Sharp and dependencies
12+
RUN mkdir /app && chown sharp:sharp /app
13+
COPY --chown=sharp:sharp pyproject.toml requirements.txt requirements.in /app/
14+
COPY --chown=sharp:sharp src/ /app/src/
15+
WORKDIR /app
16+
17+
# Create virtual environment and install dependencies
18+
RUN python3.13 -m venv .venv && chown -R sharp:sharp .venv
19+
ENV TORCH_CUDA_ARCH_LIST="8.0;8.6;8.7;8.9;9.0+PTX"
20+
ENV FORCE_CUDA="1"
21+
RUN .venv/bin/pip install ninja
22+
RUN .venv/bin/pip install -r requirements.txt
23+
RUN .venv/bin/pip install gradio
24+
RUN ln -s /app/.venv/bin/sharp /usr/local/bin/sharp
25+
26+
# Test run to download model and check if it works
27+
RUN wget https://apple.github.io/ml-sharp/thumbnails/Unsplash_-5wkyNA2BPc_0000-0001.jpg -O /tmp/test.jpg
28+
RUN sharp predict -i /tmp/test.jpg -o /tmp/test
29+
RUN rm /tmp/test.jpg /tmp/test -rf
30+
31+
# Copy other files and set ownership
32+
COPY --chown=sharp:sharp . /app
33+
34+
# Create data directory with proper permissions
35+
RUN mkdir -p /app/data && chown -R sharp:sharp /app/data
36+
37+
# Switch to non-root user
38+
USER sharp
39+
40+
# Expose port
41+
EXPOSE 7860
42+
43+
# Start Gradio web server
44+
CMD [".venv/bin/python3.13", "-u", "/app/gradio_web.py"]

compose.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
services:
2+
sharp:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
volumes:
7+
- ./data:/app/data
8+
ports:
9+
- "7860:7860"
10+
environment:
11+
# Optional: Set authentication credentials
12+
# SHARP_AUTH_USERNAME: "admin"
13+
# SHARP_AUTH_PASSWORD: "changeme"
14+
# Optional: Configure limits
15+
# SHARP_MAX_FILE_SIZE_MB: "50"
16+
# SHARP_PORT: "7860"
17+
- NVIDIA_VISIBLE_DEVICES=all
18+
deploy:
19+
resources:
20+
limits:
21+
cpus: "8"
22+
memory: 32G
23+
reservations:
24+
devices:
25+
- driver: nvidia
26+
count: 1
27+
capabilities: [gpu]
28+
restart: unless-stopped
29+
healthcheck:
30+
test: ["CMD", "curl", "-f", "http://localhost:7860/"]
31+
interval: 30s
32+
timeout: 10s
33+
retries: 3
34+
start_period: 60s

0 commit comments

Comments
 (0)