from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import Response from pydantic import BaseModel import torch import trimesh import io app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) device = torch.device('cpu') print("Loading Shap-E models...") from shap_e.diffusion.sample import sample_latents from shap_e.diffusion.gaussian_diffusion import diffusion_from_config from shap_e.models.download import load_model, load_config from shap_e.util.notebooks import decode_latent_mesh xm = load_model('transmitter', device=device) model = load_model('text300M', device=device) diffusion = diffusion_from_config(load_config('diffusion')) print("Models loaded!") class Prompt(BaseModel): text: str format: str = "glb" @app.get("/") def root(): return {"status": "FORGE3D API running!", "version": "4.0"} @app.post("/generate") def generate(prompt: Prompt): latents = sample_latents( batch_size=1, model=model, diffusion=diffusion, guidance_scale=15.0, model_kwargs=dict(texts=[prompt.text]), progress=True, clip_denoised=True, use_fp16=False, use_karras=True, karras_steps=8, sigma_min=1e-3, sigma_max=160, s_churn=0, ) mesh = decode_latent_mesh(xm, latents[0]).tri_mesh() tri = trimesh.Trimesh( vertices=mesh.verts, faces=mesh.faces ) buf = io.BytesIO() fmt = prompt.format.lower() if fmt == "obj": tri.export(buf, file_type='obj') media = "text/plain" elif fmt == "stl": tri.export(buf, file_type='stl') media = "application/octet-stream" else: tri.export(buf, file_type='glb') media = "model/gltf-binary" buf.seek(0) return Response(content=buf.read(), media_type=media)