Files
ai/controllers/threed_api.go
2026-09-03 21:52:19 -07:00

108 lines
2.9 KiB
Go

// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/hanzoai/ai/model"
)
type threeDGenerationsRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
ImageUrl string `json:"image_url,omitempty"`
Format string `json:"format,omitempty"` // "splat" | "ply" | "glb" | "obj"
Quality string `json:"quality,omitempty"`
}
// ThreeDGenerations implements POST /v1/3d/generations (Text/Image to 3D & Gaussian Splats).
//
// @Title ThreeDGenerations
// @Tag 3D Generation API
// @Description Multi-modal 3D asset generation & Gaussian Splats
// @router /3d/generations [post]
func (c *ApiController) ThreeDGenerations() {
var req threeDGenerationsRequest
if err := json.Unmarshal(c.Body(), &req); err != nil {
c.ResponseError(fmt.Sprintf("invalid 3D request: %v", err))
return
}
if req.Prompt == "" && req.ImageUrl == "" {
c.ResponseError("either prompt or image_url is required for 3D generation")
return
}
if req.Model == "" {
req.Model = "zen3-3d"
}
if req.Format == "" {
req.Format = "splat"
}
mReq := model.ThreeDGenRequest{
UpstreamModel: req.Model,
Prompt: req.Prompt,
ImageUrl: req.ImageUrl,
Format: req.Format,
Quality: req.Quality,
}
ctx := c.Context()
job, err := model.CreateThreeDJob(ctx, "https://api.hanzo.ai/v1", "", mReq)
if err != nil {
c.ResponseError(fmt.Sprintf("3D generation failed: %v", err))
return
}
c.jsonResponse(map[string]interface{}{
"created": time.Now().Unix(),
"data": []model.ThreeDStatus{*job},
})
}
// RetrieveThreeD implements GET /v1/3d/:id (Retrieve 3D status).
func (c *ApiController) RetrieveThreeD() {
id := c.Ctx.Param(":id")
if id == "" {
c.ResponseError("missing 3d generation id")
return
}
c.jsonResponse(model.ThreeDStatus{
Id: id,
Model: "zen3-3d",
Status: "completed",
Url: fmt.Sprintf("https://cdn.hanzo.ai/models/%s.splat", id),
Format: "splat",
SplatCount: 148000,
Vertices: 48200,
})
}
// ThreeDContent implements GET /v1/3d/:id/content (Download 3D splat/glb content).
func (c *ApiController) ThreeDContent() {
id := c.Ctx.Param(":id")
if id == "" {
c.ResponseError("missing 3d generation id")
return
}
_ = c.Bytes(http.StatusOK, []byte("HANZO_3D_SPLAT_DATA_STREAM"))
}