-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrellis_server.py
More file actions
238 lines (191 loc) · 7.85 KB
/
trellis_server.py
File metadata and controls
238 lines (191 loc) · 7.85 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python
"""Trellis API server for generating 3D models from text prompts."""
import os
from pathlib import Path
import tempfile
import shutil
import uuid
import structlog
# Configuration for the Trellis backends.
os.environ["ATTN_BACKEND"] = "flash-attn" # Can be 'flash-attn' or 'xformers'.
os.environ["SPCONV_ALGO"] = "native" # Can be 'native' or 'auto', default is 'auto'.
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import imageio
from PIL import Image
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import FileResponse
from pydantic import BaseModel
from typing import Optional
import uvicorn
# Import Trellis modules - adjust these paths as needed based on your setup
from third_party.TRELLIS.trellis.pipelines import (
TrellisImageTo3DPipeline,
TrellisTextTo3DPipeline,
)
from third_party.TRELLIS.trellis.utils import postprocessing_utils, render_utils
# Initialize logger
logger = structlog.get_logger(__name__)
# Initialize FastAPI app
app = FastAPI(title="Trellis API", description="API for generating 3D models from text or images")
# Initialize pipelines
text_pipeline = None
image_pipeline = None
# Define output directory
OUTPUT_DIR = Path("./output")
OUTPUT_DIR.mkdir(exist_ok=True)
# Request model for text input
class TextPromptRequest(BaseModel):
prompt: str
seed: int = 1
save_additional_files: bool = False
# Load text pipeline
def load_text_pipeline():
global text_pipeline
if text_pipeline is None:
logger.info("Loading text-to-3D pipeline...")
text_pipeline = TrellisTextTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-text-xlarge")
text_pipeline.cuda()
logger.info("Text-to-3D pipeline loaded successfully")
# Load image pipeline
def load_image_pipeline():
global image_pipeline
if image_pipeline is None:
logger.info("Loading image-to-3D pipeline...")
image_pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
image_pipeline.cuda()
logger.info("Image-to-3D pipeline loaded successfully")
# Define endpoints
@app.post("/generate/text")
async def generate_from_text(request: TextPromptRequest):
"""Generate a 3D model from text prompt."""
# Load the pipeline on demand
load_text_pipeline()
if text_pipeline is None:
raise HTTPException(status_code=503, detail="Failed to load text-to-3D pipeline")
# Create a unique ID for this request
request_id = str(uuid.uuid4())
output_path = OUTPUT_DIR / request_id
output_path.mkdir(exist_ok=True)
try:
logger.info("Processing text prompt",
text_prompt=request.prompt,
seed=request.seed,
request_id=request_id)
# Run the pipeline
outputs = text_pipeline.run(
request.prompt,
seed=request.seed,
)
# Process outputs
glb_path = output_path / "model.glb"
# Save GLB file
glb = postprocessing_utils.to_glb(
outputs["gaussian"][0],
outputs["mesh"][0],
simplify=0.95,
texture_size=1024,
)
glb.export(glb_path)
# Optionally save additional files
if request.save_additional_files:
# Render and save videos
video = render_utils.render_video(outputs["gaussian"][0])["color"]
imageio.mimsave(output_path / "gaussian.mp4", video, fps=30)
video = render_utils.render_video(outputs["radiance_field"][0])["color"]
imageio.mimsave(output_path / "radiance_field.mp4", video, fps=30)
video = render_utils.render_video(outputs["mesh"][0])["normal"]
imageio.mimsave(output_path / "mesh.mp4", video, fps=30)
# Save PLY file
outputs["gaussian"][0].save_ply(output_path / "model.ply")
logger.info("Processing complete", request_id=request_id)
# Return the GLB file
return FileResponse(
path=glb_path,
filename="model.glb",
media_type="model/gltf-binary"
)
except Exception as e:
logger.error("Error processing request", error=str(e), request_id=request_id)
raise HTTPException(status_code=500, detail=f"Error processing request: {str(e)}")
@app.post("/generate/image")
async def generate_from_image(
file: UploadFile = File(...),
seed: int = Form(1),
save_additional_files: bool = Form(False)
):
"""Generate a 3D model from an image."""
# Load the pipeline on demand
load_image_pipeline()
if image_pipeline is None:
raise HTTPException(status_code=503, detail="Failed to load image-to-3D pipeline")
# Create a unique ID for this request
request_id = str(uuid.uuid4())
output_path = OUTPUT_DIR / request_id
output_path.mkdir(exist_ok=True)
# Save uploaded image
temp_file = output_path / file.filename
with open(temp_file, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
try:
logger.info("Processing image prompt",
image_file=file.filename,
seed=seed,
request_id=request_id)
# Load image
image = Image.open(temp_file)
# Run the pipeline
outputs = image_pipeline.run(image, seed=seed)
# Process outputs
glb_path = output_path / "model.glb"
# Save GLB file
glb = postprocessing_utils.to_glb(
outputs["gaussian"][0],
outputs["mesh"][0],
simplify=0.95,
texture_size=1024,
)
glb.export(glb_path)
# Optionally save additional files
if save_additional_files:
# Render and save videos
video = render_utils.render_video(outputs["gaussian"][0])["color"]
imageio.mimsave(output_path / "gaussian.mp4", video, fps=30)
video = render_utils.render_video(outputs["radiance_field"][0])["color"]
imageio.mimsave(output_path / "radiance_field.mp4", video, fps=30)
video = render_utils.render_video(outputs["mesh"][0])["normal"]
imageio.mimsave(output_path / "mesh.mp4", video, fps=30)
# Save PLY file
outputs["gaussian"][0].save_ply(output_path / "model.ply")
logger.info("Processing complete", request_id=request_id)
# Return the GLB file
return FileResponse(
path=glb_path,
filename="model.glb",
media_type="model/gltf-binary"
)
except Exception as e:
logger.error("Error processing request", error=str(e), request_id=request_id)
raise HTTPException(status_code=500, detail=f"Error processing request: {str(e)}")
# Health check endpoint
@app.get("/health")
async def health_check():
"""Check if the service is healthy."""
return {
"status": "healthy",
"text_pipeline_loaded": text_pipeline is not None,
"image_pipeline_loaded": image_pipeline is not None
}
# Run the server
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Run the Trellis API server")
parser.add_argument("--host", default="localhost", help="Host to bind to")
parser.add_argument("--port", type=int, default=8000, help="Port to bind to")
parser.add_argument("--preload-models", action="store_true", help="Preload models at startup")
args = parser.parse_args()
# Optionally preload models
if args.preload_models:
load_text_pipeline()
# load_image_pipeline()
print(f"Starting Trellis API server on {args.host}:{args.port}")
uvicorn.run(app, host=args.host, port=args.port)