Spaces:
Build error
Build error
File size: 6,629 Bytes
28451f7 |
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 |
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.
from contextlib import asynccontextmanager
from dataclasses import dataclass
import logging
import os
import pickle
import traceback
from fastapi import FastAPI
from fastapi.requests import Request
from fastapi.responses import Response
import imageio.v3 as imageio
import numpy as np
from api_types import CompressedSeedingRequest
from server_base import InferenceModel
from server_cosmos import CosmosModel
# ------------------------------
@dataclass
class ServerSettings():
"""
Note: we use a dataclass + env variables because we can't
easily pass command line arguments through the `fastapi` launcher.
"""
model: str = os.environ.get("GEN3C_MODEL", "cosmos-predict1")
checkpoint_path: str | None = os.environ.get("GEN3C_CKPT_PATH")
data_path: str | None = os.environ.get("GEN3C_DATA_PATH")
#: Additional latency to add to any inference request, in milliseconds.
inference_latency: int = int(os.environ.get("GEN3C_INFERENCE_LATENCY", 0))
#: Number of inference results to keep in cache.
#: This may be useful when multiple requests are in flight and the user hasn't
#: retrieved the results yet.
inference_cache_size: int = int(os.environ.get("GEN3C_INFERENCE_CACHE_SIZE", 15))
#: Number of GPUs to use for inference. Leave at 0 to automatically select
#: based on available hardware.
gpu_count: int = int(os.environ.get("GEN3C_GPU_COUNT", 0))
settings = ServerSettings()
model: InferenceModel | None = None
@asynccontextmanager
async def lifespan(app: FastAPI):
global model
model_name = settings.model.lower()
if model_name in ("cosmos", "cosmos-predict1"):
cls = CosmosModel
else:
raise ValueError(f"Unsupported model type: '{settings.model}'")
model = cls(checkpoint_path=settings.checkpoint_path,
data_path=settings.data_path,
fake_delay_ms=settings.inference_latency,
inference_cache_size=settings.inference_cache_size,
gpu_count=settings.gpu_count)
# --- Startup code
# Pre-render at least one image to make sure everything is running
if not model.requires_seeding():
await model.make_test_image()
yield
# --- Shutdown code
model.cleanup()
del model
app = FastAPI(lifespan=lifespan)
logger = logging.getLogger('uvicorn.error')
# ------------------------------
def get_bool_query_param(request: Request, name: str, default: bool) -> bool:
b_str = request.query_params.get(name, "1" if default else "0")
return b_str.lower() in ("1", "true", "yes", "")
@app.post("/request-inference", response_class=Response, response_model=None)
async def request_inference(request: Request):
"""
Start a new asynchronous inference job.
"""
sync = get_bool_query_param(request, "sync", default=False)
req: bytes = await request.body()
req = pickle.loads(req)
try:
if sync:
result = await model.request_inference_sync(req)
return Response(content=pickle.dumps(result),
media_type="application/octet-stream")
else:
model.request_inference(req)
except Exception as e:
logging.error("Inference request failed with exception:"
f"\n{e}\n{traceback.format_exc()}")
return Response(str(e), status_code=400)
return Response("Request accepted.", status_code=202)
@app.post("/seed-model", response_class=Response, response_model=None)
async def seed_model(request: Request):
"""
Start a new asynchronous inference job.
"""
sync = get_bool_query_param(request, "sync", default=False)
req: bytes = await request.body()
req = pickle.loads(req)
if isinstance(req, CompressedSeedingRequest):
req.decompress()
try:
# There isn't really anything async about the seeding request being done on the server
# so far, so we just await. This could be changed in the future.
result = await model.seed_model(req)
except Exception as e:
logging.error(f"Seeding request failed with exception:"
f"\n{e}\n{traceback.format_exc()}")
return Response(str(e), status_code=400)
# return Response("Seeding request accepted.", status_code=(200 if sync else 202))
return Response(content=pickle.dumps(result),
media_type="application/octet-stream")
@app.get("/inference-result", response_class=Response, response_model=None)
async def inference_results_or_none(request_id: str):
try:
result = model.inference_result_or_none(request_id)
except Exception as e:
# TODO: try to differentiate the status codes (doesn't exist, inference failed, etc)
logging.error(f"Inference results request failed with exception:"
f"\n{e}\n{traceback.format_exc()}")
return Response(str(e), status_code=500)
if result is None:
return Response(content="Result not ready",
status_code=503)
else:
return Response(content=pickle.dumps(result),
media_type="application/octet-stream")
@app.get("/image", response_class=Response)
def latest_rgb(format: str = "jpg"):
# We return the data as pickled bytes to avoid the JSON serialization / deserialization overhead.
image = model.get_latest_rgb()
if image is None:
return Response(content="No image available yet.", status_code=404)
if format == "pickle":
content = pickle.dumps(
{
"image": image,
}
)
return Response(content=content, media_type="application/octet-stream")
elif format in ("jpg", "png"):
image = image.copy()
# Allow alpha channel to be omitted for faster transfers
if image.shape[-1] == 3:
image = np.concatenate([
image,
np.ones((*image.shape[:2], 1))
], axis=-1)
if image.dtype != np.uint8:
# TODO: proper handling of gamma compression, etc
image[:, :, :3] = np.power(image[:, :, :3], 1 / 2.2) * 255
image[:, :, 3] = image[:, :, 3] * 255
if format != "png":
image = image[:, :, :3]
content = imageio.imwrite(uri="<bytes>", image=image.astype(np.uint8), extension="." + format)
return Response(content=content, media_type=f"image/{format}")
else:
return Response(f"Unsupported image format: {format}", status_code=400)
@app.get("/metadata")
def metadata():
return model.metadata()
|