python client for grid cortex
Project description
Grid Cortex Client
This Python client library provides a robust and developer-friendly interface for interacting with models deployed on the Grid Cortex Ray Serve infrastructure. It simplifies model inference calls by abstracting away the complexities of request/response handling and data transformations, offering an extensible architecture for various model types.
Key Features
- Simplified Model Interaction: Use the
CortexClient.run(model_id, **kwargs)method for straightforward model execution. - Automatic Handler Dispatch: The client automatically selects the correct model handler based on the
model_id. - Extensible Architecture: Easily add support for new models by implementing task-specific handlers derived from
BaseModel. - Flexible Input Handling: Supports various input types (image paths, URLs, PIL objects) and model-specific parameters via keyword arguments.
- Standardized Pre/Post-processing: Model handlers manage their own data transformations, ensuring correct payload formats and user-friendly outputs.
- Robust Error Handling: Custom exceptions for API and network issues.
Design Overview
The client is designed with a clear separation of concerns:
-
CortexClient(src/grid_cortex_client/cortex_client.py):- The primary user-facing class.
- Orchestrates model execution by dispatching to the appropriate model handler.
- The main method is
run(model_id: str, **kwargs: Any) -> Any;:- Identifies the correct model handler (e.g.,
DepthModel,DetectionModel) using an internal registry that maps keywords in themodel_idto handler classes. - Instantiates the handler with the given
model_id. - Calls the internal
run_model()method, passing the handler instance and all**kwargsasinput_datato the handler'spreprocessmethod.
- Identifies the correct model handler (e.g.,
- The
run_model(model: BaseModel, input_data: Dict[str, Any], ...)method handles the detailed steps:- Calls the handler's
preprocessmethod withinput_data. - Makes the HTTP POST request to the appropriate model endpoint (e.g.,
/model_id/run). - Calls the handler's
postprocessmethod with the server's response.
- Calls the handler's
-
HTTPClient(src/grid_cortex_client/client.py):- Manages low-level HTTP communication, API key authentication, and request/response cycles.
-
Model Abstraction (
src/grid_cortex_client/models/):BaseModel(models/base_model.py): An abstract base class defining the contract for all model handlers, requiringpreprocess(self, input_data: Dict[str, Any], **kwargs: Any)andpostprocess(self, response_data: Dict[str, Any], **kwargs: Any)methods.- Task-Specific Model Handlers (e.g.,
DepthModel,DetectionModel,SegmentationModelinmodels/depth.py,models/detection.py,models/segmentation.py):- Concrete implementations of
BaseModel. preprocess: Converts theinput_datadictionary (from**kwargsinclient.run()) into the JSON payload expected by the specific model server.postprocess: Transforms the server's JSON response into a user-friendly Python object (e.g., PIL Image, list of detections, etc.).
- Concrete implementations of
-
Reusable Utilities (
src/grid_cortex_client/preprocessing.py,src/grid_cortex_client/postprocessing.py):- Contain stateless helper functions for common tasks like image loading/encoding and parsing specific response formats (e.g., base64 encoded NumPy arrays or images).
Project Structure
pyproject.toml: Project metadata and dependencies.src/grid_cortex_client/: Main library source code.cortex_client.py:CortexClient.client.py:HTTPClient, custom errors.preprocessing.py&postprocessing.py: Utility functions. -models/: Model-specific handlers. -base_model.py:BaseModelABC. -depth.py:DepthModel. -detection.py:DetectionModel. -segmentation.py:SegmentationModel. -stereo.py:FoundationStereoModel. -grasp.py:GraspModel. -graspgen.py:GraspGenModel.
examples/: Example usage scripts.example.py: General usage examples.grasp.py: Grasp generation with M2T2 model.foundationstereo.py: Stereo depth estimation with FoundationStereo.graspgen.py: Grasp generation with GraspGen from point clouds.
README.md: This file.
Installation
Prerequisites
- Python 3.8+ (Recommended: 3.10+)
Steps
- Navigate to the
grid-cortex-clientdirectory. - Install using pip:
- For development (editable install):
pip install -e .
This installs the package in editable mode and includes all dependencies frompyproject.toml. - To install from a built wheel (if available in
dist/):pip install dist/grid_cortex_client-*.whl
- For development (editable install):
Configuration
The CortexClient requires an API key and the base URL for the Cortex API.
-
Environment Variables (Recommended):
GRID_CORTEX_API_KEY: Your API key.GRID_CORTEX_BASE_URL: Base URL (e.g.,https://cortex-stage.generalrobotics.dev). Defaults are provided if not set. The client automatically uses these ifapi_keyorbase_urlare not passed during instantiation.
-
Direct Instantiation:
from grid_cortex_client import CortexClient client = CortexClient(api_key="your_api_key_here", base_url="your_custom_base_url_here")
Available Models & Handlers
The client uses a keyword-based system to map a given model_id to its appropriate handler. The model_id you use should typically contain one of the keywords associated with a handler.
| Model Task | Handler Class | Keywords for model_id |
Key Input Parameters (**kwargs for client.run) |
Expected Output Type (from client.run) |
|---|---|---|---|---|
| Depth Estimation | DepthModel |
midas, marigold, depthpro, metric3d, depthanything2, zoedepth |
image_input (path, URL, or PIL.Image) |
PIL.Image.Image (depth map) |
| Stereo Depth Estimation | FoundationStereoModel |
foundationstereo |
left_image, right_image, K (camera intrinsics), baseline (stereo baseline) |
numpy.ndarray (depth map) |
| Object Detection | DetectionModel |
owlv2 |
image_input (path, URL, or PIL.Image), prompt (str or List[str]), box_threshold (float, optional) |
List[Dict] (list of detections) |
| Image Segmentation | SegmentationModel |
clipseg, lseg, gsam2 |
image_input (path, URL, or PIL.Image), prompt (str) |
PIL.Image.Image (mask) |
| Grasp Generation | GraspModel |
m2t2 |
xyz (point cloud), rgb (RGB values), seg (segmentation labels) |
Dict[str, numpy.ndarray] (grasps and confidence) |
| Grasp Generation (Depth Images) | GraspGenModel |
graspgen |
depth_image, seg_image, camera_intrinsics, aux_args (num_grasps, gripper_config, camera_extrinsics) |
Dict[str, numpy.ndarray] (grasps, confidence, latency_ms) |
Logging
The client uses the standard Python logging module. You can configure the logging level to control verbosity:
import logging
# For verbose output from the client and underlying HTTP library:
# logging.basicConfig(level=logging.INFO)
# To reduce verbosity (show only warnings and errors):
logging.getLogger("grid_cortex_client").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING) # For the HTTPX library
Contributing
Contributions are welcome! Please refer to the main repository guidelines. When adding support for new models:
- Create a new handler class in
src/grid_cortex_client/models/inheriting fromBaseModel. - Implement the
preprocessandpostprocessmethods. - Add relevant keywords and the new handler class to the
_MODEL_ID_TO_HANDLER_CLASSdictionary insrc/grid_cortex_client/cortex_client.py. - Add tests and update documentation (including the "Available Models" table).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grid_cortex_client-0.1.265-py3-none-any.whl.
File metadata
- Download URL: grid_cortex_client-0.1.265-py3-none-any.whl
- Upload date:
- Size: 35.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
739b9fb69c38218d1b5867ae36d6bfe032ab8957215a655f126d6371030aa099
|
|
| MD5 |
702df609c42cbd050e458359b4a91ebd
|
|
| BLAKE2b-256 |
4add75da97e23958e1b742be1b13f039f7f3d4ff050852ad46a35fd80080aee8
|