Skip to content

Commit 9d0a276

Browse files
committed
Add Docker support and Gradio web UI
Add containerized deployment with NVIDIA CUDA support and a Gradio-based web interface for easier usage. Based on community PR apple#13.
1 parent 1eaa046 commit 9d0a276

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

.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/

Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
# Install Sharp and dependencies
9+
RUN mkdir /app
10+
COPY pyproject.toml requirements.txt requirements.in /app/
11+
COPY src/ /app/src/
12+
WORKDIR /app
13+
RUN python3.13 -m venv .venv
14+
ENV TORCH_CUDA_ARCH_LIST="8.0;8.6;8.7;8.9;9.0+PTX"
15+
ENV FORCE_CUDA="1"
16+
RUN .venv/bin/pip install ninja
17+
RUN .venv/bin/pip install -r requirements.txt
18+
RUN .venv/bin/pip install gradio
19+
RUN ln -s /app/.venv/bin/sharp /usr/local/bin/sharp
20+
21+
# Test run to download model and check if it works
22+
RUN wget https://apple.github.io/ml-sharp/thumbnails/Unsplash_-5wkyNA2BPc_0000-0001.jpg -O /tmp/test.jpg
23+
RUN sharp predict -i /tmp/test.jpg -o /tmp/test
24+
RUN rm /tmp/test.jpg /tmp/test -rf
25+
26+
# Copy other files
27+
COPY . /app
28+
29+
# Start Gradio web server
30+
CMD [".venv/bin/python3.13", "-u", "/app/gradio_web.py"]

compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
sharp:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
volumes:
7+
- ./data:/app/data
8+
ports:
9+
- "7860:7860"
10+
deploy:
11+
resources:
12+
reservations:
13+
devices:
14+
- driver: nvidia
15+
count: 1
16+
capabilities: [gpu]

gradio_web.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import gradio as gr
2+
import subprocess
3+
import os
4+
import shutil
5+
import time
6+
7+
8+
def predict(image):
9+
# Ensure data directory exists
10+
os.makedirs("/app/data", exist_ok=True)
11+
12+
input_path = "/app/data/input.jpg"
13+
14+
# Save/Copy input image
15+
# image provided by gradio (type='filepath') is a temp path
16+
shutil.copy(image, input_path)
17+
18+
# Run sharp command
19+
# sharp predict -i /app/data/input.jpg -o /app/data/output --render
20+
cmd = [
21+
"sharp",
22+
"predict",
23+
"-i",
24+
input_path,
25+
"-o",
26+
"/app/data/output",
27+
"--render",
28+
]
29+
30+
# Execute command
31+
try:
32+
t = time.time()
33+
print("Sharp started")
34+
subprocess.run(cmd, check=True, capture_output=True)
35+
print(f"Sharp command took {round(time.time() - t, 3)} seconds")
36+
except subprocess.CalledProcessError as e:
37+
print(f"Error running sharp: {e}")
38+
print(f"Stdout: {e.stdout.decode()}")
39+
print(f"Stderr: {e.stderr.decode()}")
40+
return None
41+
42+
# Find output videos
43+
rgb_video = "/app/data/output/input.mp4"
44+
depth_video = "/app/data/output/input.depth.mp4"
45+
46+
if os.path.exists(rgb_video) and os.path.exists(depth_video):
47+
return rgb_video, depth_video
48+
elif os.path.exists(rgb_video):
49+
return rgb_video, None
50+
51+
return None, None
52+
53+
54+
demo = gr.Interface(
55+
fn=predict,
56+
inputs=gr.Image(type="filepath", label="Input Image"),
57+
outputs=[gr.Video(label="RGB Video"), gr.Video(label="Depth Video")],
58+
title="Sharp 3D View Synthesis",
59+
description="Upload an image to generate a 3D view synthesis video.",
60+
)
61+
62+
if __name__ == "__main__":
63+
print(
64+
"Sharp Monocular View Synthesis in Less Than a Second (https://github.com/apple/ml-sharp)"
65+
)
66+
demo.launch(server_name="0.0.0.0", server_port=7860)

0 commit comments

Comments
 (0)