Skip to content

Commit

Permalink
Added fastapi server and Docker files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Banbury committed Sep 4, 2024
1 parent 47a99bd commit 5941269
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:slim

Check failure on line 1 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Missing User Instruction

Rule ID: da8c7b4f-5324-4d73-9d52-09c5ce91e8e7 Severity: High Resource: FROM={{python:slim}} A user should be specified in the dockerfile, otherwise the image will run as root
Raw output
Expected: The 'Dockerfile' should contain the 'USER' instruction
Found: The 'Dockerfile' does not contain any 'USER' instruction

Check notice on line 1 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Healthcheck Instruction Missing

Rule ID: 5f039761-874b-4dac-94cc-89067fb2315b Severity: Low Resource: FROM={{python:slim}} Ensure that HEALTHCHECK is being used. The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working
Raw output
Expected: Dockerfile should contain instruction 'HEALTHCHECK'
Found: Dockerfile doesn't contain instruction 'HEALTHCHECK'

WORKDIR /app
RUN apt-get update && \

Check warning on line 4 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Apt Get Install Pin Version Not Defined

Rule ID: ea2428c4-65af-4ac2-b4fb-a7c9babde2dd Severity: Medium Resource: FROM={{python:slim}}.RUN={{apt-get update && apt-get upgrade -y && apt-get install -y git build-essential python3-setuptools}} When installing a package, its pin version should be defined
Raw output
Expected: Package 'git' has version defined
Found: Package 'git' does not have version defined

Check warning on line 4 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Apt Get Install Pin Version Not Defined

Rule ID: ea2428c4-65af-4ac2-b4fb-a7c9babde2dd Severity: Medium Resource: FROM={{python:slim}}.RUN={{apt-get update && apt-get upgrade -y && apt-get install -y git build-essential python3-setuptools}} When installing a package, its pin version should be defined
Raw output
Expected: Package 'python3-setuptools' has version defined
Found: Package 'python3-setuptools' does not have version defined

Check warning on line 4 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Apt Get Install Pin Version Not Defined

Rule ID: ea2428c4-65af-4ac2-b4fb-a7c9babde2dd Severity: Medium Resource: FROM={{python:slim}}.RUN={{apt-get update && apt-get upgrade -y && apt-get install -y git build-essential python3-setuptools}} When installing a package, its pin version should be defined
Raw output
Expected: Package 'build-essential' has version defined
Found: Package 'build-essential' does not have version defined

Check notice on line 4 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

APT-GET Not Avoiding Additional Packages

Rule ID: bdec9117-4d5d-4d8f-bebe-93174383d99e Severity: None Resource: FROM={{python:slim}}.{{RUN apt-get update && apt-get upgrade -y && apt-get install -y git build-essential python3-setuptools}} Check if any apt-get installs don't use '--no-install-recommends' flag to avoid installing additional packages.
Raw output
Expected: 'RUN apt-get update &&     apt-get upgrade -y &&     apt-get install -y git build-essential python3-setuptools' uses '--no-install-recommends' flag to avoid installing additional packages
Found: 'RUN apt-get update &&     apt-get upgrade -y &&     apt-get install -y git build-essential python3-setuptools' does not use '--no-install-recommends' flag to avoid installing additional packages

Check notice on line 4 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Apt Get Install Lists Were Not Deleted

Rule ID: 366ed13d-f2bb-40b8-85c9-894ce13e43c7 Severity: None Resource: FROM={{python:slim}}.RUN={{apt-get update && apt-get upgrade -y && apt-get install -y git build-essential python3-setuptools}} After using apt-get install, it is needed to delete apt-get lists
Raw output
Expected: After using apt-get install, the apt-get lists should be deleted
Found: After using apt-get install, the apt-get lists were not deleted
apt-get upgrade -y && \
apt-get install -y git build-essential python3-setuptools
RUN git clone https://github.com/Stability-AI/stable-fast-3d.git
WORKDIR /app/stable-fast-3d
COPY server.py .
COPY env.server .env
COPY requirements.txt .
RUN mkdir model

Check notice on line 12 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Multiple RUN, ADD, COPY, Instructions Listed

Rule ID: c9b556f6-4b22-40b7-88bc-f25a8ed7b646 Severity: Low Resource: FROM={{python:slim}}.{{RUN mkdir model}} Multiple commands (RUN, Copy, And) should be grouped in order to reduce the number of layers.
Raw output
Expected: There isn´t any RUN instruction that could be grouped
Found: There are RUN instructions that could be grouped
RUN rm __init__.py
RUN python -m pip install torch torchvision torchaudio setuptools==69.5.1 wheel

Check warning on line 14 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Pip install keeping cached packages

Rule ID: 86805785-1b59-4f12-9718-3867bc187cc1 Severity: Medium Resource: FROM={{python:slim}}.{{python -m pip install torch torchvision torchaudio setuptools==69.5.1 wheel}} When installing packages with pip, the '--no-cache-dir' flag should be set to make Docker images smaller
Raw output
Expected: The '--no-cache-dir' flag should be set when running 'pip/pip3 install'
Found: The '--no-cache-dir' flag isn't set when running 'pip/pip3 install'

Check warning on line 14 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Unpinned Package Version in Pip Install

Rule ID: 441843a8-0981-4faf-a188-6deeaf3d0be4 Severity: Medium Resource: FROM={{python:slim}}.{{RUN python -m pip install torch torchvision torchaudio setuptools==69.5.1 wheel}} Package version pinning reduces the range of versions that can be installed, reducing the chances of failure due to unanticipated changes
Raw output
Expected: RUN instruction with 'pip/pip3 install <package>' should use package pinning form 'pip/pip3 install <package>=<version>'
Found: RUN instruction python -m pip install torch torchvision torchaudio setuptools==69.5.1 wheel does not use package pinning form
RUN python -m pip install -r requirements.txt

Check warning on line 15 in Dockerfile

View check run for this annotation

Wiz Inc. (a28a8b7b4c) / Wiz IaC Scanner

Pip install keeping cached packages

Rule ID: 86805785-1b59-4f12-9718-3867bc187cc1 Severity: Medium Resource: FROM={{python:slim}}.{{python -m pip install -r requirements.txt}} When installing packages with pip, the '--no-cache-dir' flag should be set to make Docker images smaller
Raw output
Expected: The '--no-cache-dir' flag should be set when running 'pip/pip3 install'
Found: The '--no-cache-dir' flag isn't set when running 'pip/pip3 install'
CMD ["fastapi", "run", "/app/stable-fast-3d/server.py", "--port", "8000"]
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.4'

services:
stablefast3d:
image: stablefast3d
ports:
- 8100:8000
build:
context: .
dockerfile: ./Dockerfile
volumes:
- ./model:/app/stable-fast-3d/model
7 changes: 7 additions & 0 deletions env.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DEVICE=cpu
PRETRAINED_MODEL=model
FOREGROUND_RATIO=0.85
TEXTURE_RESOLUTION=1024
REMESSH_OPTION=none
TARGET_VERTEX_COUNT=-1
BATCH_SIZE=1
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ huggingface-hub==0.23.4
rembg[gpu]==2.0.57; sys_platform != 'darwin'
rembg==2.0.57; sys_platform == 'darwin'
git+https://github.com/vork/PyNanoInstantMeshes.git
gpytoolbox==0.2.0
gpytoolbox==0.3.2
fastapi[standard]>=0.112.2
./texture_baker/
72 changes: 72 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import base64
import os
import io
from io import BytesIO
from contextlib import nullcontext
from dotenv import load_dotenv
from fastapi import FastAPI, UploadFile
from fastapi.responses import Response
from PIL import Image
import rembg
import torch
from sf3d.system import SF3D
from sf3d.utils import get_device, remove_background, resize_foreground

load_dotenv()

app = FastAPI()

model = SF3D.from_pretrained(
os.getenv("PRETRAINED_MODEL"),
config_name="config.yaml",
weight_name="model.safetensors",
)
model.to(os.getenv("DEVICE"))
model.eval()

rembg_session = rembg.new_session()

@app.post("/generate",
responses = { 200: { "content": { "model/gltf-binary": {} } } },
response_class=Response
)
async def generate(file: UploadFile):
# load the image using Pillow
image = Image.open(file.file)
image = remove_bg(image)
mesh = generate_model(image)

# return the image as a binary stream with a suitable content-disposition header for download
return Response(
content=mesh,
media_type="model/gltf-binary",
headers={"Content-Disposition": "attachment; filename=mesh.glb"},
)

def remove_bg(img: Image) -> Image:
img = remove_background(img, rembg_session)
img = resize_foreground(img, float(os.getenv("FOREGROUND_RATIO")))
return img

def generate_model(image: Image):
device = os.getenv("DEVICE")
if torch.cuda.is_available():
torch.cuda.reset_peak_memory_stats()
with torch.no_grad():
with torch.autocast(
device_type=device, dtype=torch.float16
) if "cuda" in device else nullcontext():
mesh, glob_dict = model.run_image(
image,
bake_resolution=int(os.getenv("TEXTURE_RESOLUTION")),
remesh=os.getenv("REMESSH_OPTION"),
vertex_count=int(os.getenv("TARGET_VERTEX_COUNT")),
)
if torch.cuda.is_available():
print("Peak Memory:", torch.cuda.max_memory_allocated() / 1024 / 1024, "MB")
elif torch.backends.mps.is_available():
print(
"Peak Memory:", torch.mps.driver_allocated_memory() / 1024 / 1024, "MB"
)

return mesh.export(include_normals=True, file_type='glb')

0 comments on commit 5941269

Please sign in to comment.