Spaces:
Paused
Paused
dario-abbondanza commited on
Commit Β·
dd90875
1
Parent(s): c6466a2
Uploading fastAPI
Browse files- Dockerfile +17 -0
- main.py +78 -0
- app.py β old_app.py +0 -0
- requirements.txt +3 -1
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
# Set the working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install dependencies
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
# Copy application files
|
| 11 |
+
COPY . /app/
|
| 12 |
+
|
| 13 |
+
# Expose the port FastAPI will run on
|
| 14 |
+
EXPOSE 8000
|
| 15 |
+
|
| 16 |
+
# Run FastAPI when the container starts
|
| 17 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
main.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Header
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from typing import List
|
| 9 |
+
from trellis.pipelines import TrellisImageTo3DPipeline
|
| 10 |
+
from trellis.utils import render_utils, postprocessing_utils
|
| 11 |
+
from trellis.representations import Gaussian, MeshExtractResult
|
| 12 |
+
import imageio
|
| 13 |
+
|
| 14 |
+
# Define working directories
|
| 15 |
+
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")
|
| 16 |
+
os.makedirs(TMP_DIR, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
# β
Load pipeline once to avoid reloading for every request
|
| 19 |
+
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
|
| 20 |
+
pipeline.cuda()
|
| 21 |
+
|
| 22 |
+
# β
Preload model (to prevent cold starts)
|
| 23 |
+
try:
|
| 24 |
+
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8)))
|
| 25 |
+
except:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
# β
API Key for private access
|
| 29 |
+
HF_API_KEY = os.getenv("HF_API_KEY", "your-secure-api-key")
|
| 30 |
+
|
| 31 |
+
app = FastAPI()
|
| 32 |
+
|
| 33 |
+
def preprocess_image(image: Image.Image) -> Image.Image:
|
| 34 |
+
"""Preprocess a single input image using the Trellis pipeline."""
|
| 35 |
+
return pipeline.preprocess_image(image)
|
| 36 |
+
|
| 37 |
+
@app.post("/generate_3d/")
|
| 38 |
+
async def generate_3d(
|
| 39 |
+
image: UploadFile = File(...),
|
| 40 |
+
authorization: str = Header(None)
|
| 41 |
+
):
|
| 42 |
+
"""Accepts an image upload, runs inference, and returns a GLB file."""
|
| 43 |
+
|
| 44 |
+
# π API Key authentication
|
| 45 |
+
if authorization != f"Bearer {HF_API_KEY}":
|
| 46 |
+
raise HTTPException(status_code=403, detail="Invalid API key")
|
| 47 |
+
|
| 48 |
+
if not image.filename.lower().endswith(("png", "jpg", "jpeg")):
|
| 49 |
+
raise HTTPException(status_code=400, detail="Invalid image format. Upload a PNG or JPG.")
|
| 50 |
+
|
| 51 |
+
# Save the uploaded image
|
| 52 |
+
image_path = os.path.join(TMP_DIR, image.filename)
|
| 53 |
+
with open(image_path, "wb") as f:
|
| 54 |
+
f.write(image.file.read())
|
| 55 |
+
|
| 56 |
+
# Load and preprocess the image
|
| 57 |
+
img = Image.open(image_path).convert("RGBA")
|
| 58 |
+
processed_image = preprocess_image(img)
|
| 59 |
+
|
| 60 |
+
# Run the Trellis pipeline
|
| 61 |
+
outputs = pipeline.run(
|
| 62 |
+
processed_image,
|
| 63 |
+
seed=np.random.randint(0, np.iinfo(np.int32).max),
|
| 64 |
+
formats=["gaussian", "mesh"],
|
| 65 |
+
preprocess_image=False,
|
| 66 |
+
sparse_structure_sampler_params={"steps": 12, "cfg_strength": 7.5},
|
| 67 |
+
slat_sampler_params={"steps": 12, "cfg_strength": 3.0},
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Extract the GLB file
|
| 71 |
+
gs, mesh = outputs["gaussian"][0], outputs["mesh"][0]
|
| 72 |
+
glb = postprocessing_utils.to_glb(gs, mesh, simplify=0.95, texture_size=1024, verbose=False)
|
| 73 |
+
glb_path = os.path.join(TMP_DIR, "sample.glb")
|
| 74 |
+
glb.export(glb_path)
|
| 75 |
+
|
| 76 |
+
torch.cuda.empty_cache()
|
| 77 |
+
|
| 78 |
+
return FileResponse(glb_path, media_type="model/gltf-binary", filename="sample.glb")
|
app.py β old_app.py
RENAMED
|
File without changes
|
requirements.txt
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
--extra-index-url https://download.pytorch.org/whl/cu121
|
| 2 |
|
|
|
|
|
|
|
| 3 |
torch==2.4.0
|
| 4 |
torchvision==0.19.0
|
| 5 |
pillow==10.4.0
|
|
@@ -23,4 +25,4 @@ transformers==4.46.3
|
|
| 23 |
gradio_litmodel3d==0.0.1
|
| 24 |
https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
|
| 25 |
https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl?download=true
|
| 26 |
-
https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl?download=true
|
|
|
|
| 1 |
--extra-index-url https://download.pytorch.org/whl/cu121
|
| 2 |
|
| 3 |
+
fastapi
|
| 4 |
+
uvicorn
|
| 5 |
torch==2.4.0
|
| 6 |
torchvision==0.19.0
|
| 7 |
pillow==10.4.0
|
|
|
|
| 25 |
gradio_litmodel3d==0.0.1
|
| 26 |
https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
|
| 27 |
https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl?download=true
|
| 28 |
+
https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl?download=true
|