File size: 7,394 Bytes
92318e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246a3a7
92318e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
import uvicorn
import os
import shutil
import torch
import numpy as np
from PIL import Image
from typing import List, Optional
from pydantic import BaseModel
import imageio
from trellis.pipelines import TrellisImageTo3DPipeline
from trellis.representations import Gaussian, MeshExtractResult
from trellis.utils import render_utils, postprocessing_utils
from easydict import EasyDict as edict

app = FastAPI(title="TRELLIS 3D API")

# 添加 CORS 中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 在生产环境中应该设置具体的域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 配置
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
os.makedirs(TMP_DIR, exist_ok=True)
MAX_SEED = np.iinfo(np.int32).max

# 初始化 pipeline
pipeline = TrellisImageTo3DPipeline.from_pretrained("cavargas10/TRELLIS")
pipeline.cuda()
try:
    pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8)))  # 预加载 rembg
except:
    pass

class GenerationParams(BaseModel):
    seed: int = 0
    ss_guidance_strength: float = 7.5
    ss_sampling_steps: int = 12
    slat_guidance_strength: float = 3.0
    slat_sampling_steps: int = 12
    multiimage_algo: str = "stochastic"

class GLBParams(BaseModel):
    mesh_simplify: float = 0.95
    texture_size: int = 1024

def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
    return {
        'gaussian': {
            **gs.init_params,
            '_xyz': gs._xyz.cpu().numpy(),
            '_features_dc': gs._features_dc.cpu().numpy(),
            '_scaling': gs._scaling.cpu().numpy(),
            '_rotation': gs._rotation.cpu().numpy(),
            '_opacity': gs._opacity.cpu().numpy(),
        },
        'mesh': {
            'vertices': mesh.vertices.cpu().numpy(),
            'faces': mesh.faces.cpu().numpy(),
        },
    }

def unpack_state(state: dict) -> tuple[Gaussian, edict]:
    gs = Gaussian(
        aabb=state['gaussian']['aabb'],
        sh_degree=state['gaussian']['sh_degree'],
        mininum_kernel_size=state['gaussian']['mininum_kernel_size'],
        scaling_bias=state['gaussian']['scaling_bias'],
        opacity_bias=state['gaussian']['opacity_bias'],
        scaling_activation=state['gaussian']['scaling_activation'],
    )
    gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')
    gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')
    gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')
    gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')
    gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')
    mesh = edict(
        vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),
        faces=torch.tensor(state['mesh']['faces'], device='cuda'),
    )
    return gs, mesh

@app.post("/generate")
async def generate_3d(
    files: List[UploadFile] = File(...),
    params: GenerationParams = None
):
    if not params:
        params = GenerationParams()
    
    # 创建临时目录
    session_id = str(np.random.randint(0, MAX_SEED))
    user_dir = os.path.join(TMP_DIR, session_id)
    os.makedirs(user_dir, exist_ok=True)
    
    try:
        # 处理上传的图片
        images = []
        for file in files:
            image = Image.open(file.file)
            images.append(image)
        
        # 运行生成
        outputs = pipeline.run_multi_image(
            images,
            seed=params.seed,
            formats=["gaussian", "mesh"],
            preprocess_image=False,
            sparse_structure_sampler_params={
                "steps": params.ss_sampling_steps,
                "cfg_strength": params.ss_guidance_strength,
            },
            slat_sampler_params={
                "steps": params.slat_sampling_steps,
                "cfg_strength": params.slat_guidance_strength,
            },
            mode=params.multiimage_algo,
        )
        
        # 生成预览视频
        video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
        video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
        video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
        video_path = os.path.join(user_dir, 'preview.mp4')
        imageio.mimsave(video_path, video, fps=15)
        
        # 保存状态
        state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
        state_path = os.path.join(user_dir, 'state.npz')
        np.savez(state_path, **state)
        
        return {
            "session_id": session_id,
            "preview_url": f"/preview/{session_id}",
            "state_url": f"/state/{session_id}"
        }
        
    except Exception as e:
        shutil.rmtree(user_dir)
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/extract_glb")
async def extract_glb(
    session_id: str,
    params: GLBParams = None
):
    if not params:
        params = GLBParams()
    
    user_dir = os.path.join(TMP_DIR, session_id)
    if not os.path.exists(user_dir):
        raise HTTPException(status_code=404, detail="Session not found")
    
    try:
        # 加载状态
        state_path = os.path.join(user_dir, 'state.npz')
        state = np.load(state_path, allow_pickle=True, encoding='latin1')
        state = {k: state[k] for k in state.files}
        
        # 生成 GLB
        gs, mesh = unpack_state(state)
        glb = postprocessing_utils.to_glb(
            gs, 
            mesh, 
            simplify=params.mesh_simplify, 
            texture_size=params.texture_size, 
            verbose=False
        )
        
        glb_path = os.path.join(user_dir, 'model.glb')
        glb.export(glb_path)
        
        return {"glb_url": f"/glb/{session_id}"}
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/preview/{session_id}")
async def get_preview(session_id: str):
    preview_path = os.path.join(TMP_DIR, session_id, 'preview.mp4')
    if not os.path.exists(preview_path):
        raise HTTPException(status_code=404, detail="Preview not found")
    return FileResponse(preview_path)

@app.get("/glb/{session_id}")
async def get_glb(session_id: str):
    glb_path = os.path.join(TMP_DIR, session_id, 'model.glb')
    if not os.path.exists(glb_path):
        raise HTTPException(status_code=404, detail="GLB not found")
    return FileResponse(glb_path)

@app.get("/state/{session_id}")
async def get_state(session_id: str):
    state_path = os.path.join(TMP_DIR, session_id, 'state.npz')
    if not os.path.exists(state_path):
        raise HTTPException(status_code=404, detail="State not found")
    return FileResponse(state_path)

if __name__ == "__main__":
    # 在 Hugging Face Spaces 中,我们需要使用 0.0.0.0 作为主机
    # 端口 7860 是 Hugging Face Spaces 的默认端口
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=7860,
        # 添加以下配置以提高性能
        workers=1,  # 由于 GPU 限制,使用单工作进程
        loop="uvloop",
        http="httptools"
    )