|
14 | 14 |
|
15 | 15 | from ..core.auth import get_auth
|
16 | 16 | from ..core.const import API_URL
|
17 |
| -from ..core.endpoint import EndpointPolicy |
18 |
| -from ..core.endpoint import connect_endpoint as _connect_endpoint |
19 |
| -from ..core.endpoint import connect_local_endpoint as _connect_local_endpoint |
| 17 | +from ..core.endpoint import DirectPolicy, LocalServerPolicy, RemoteServerPolicy |
| 18 | +from ..core.endpoint import policy as _policy |
| 19 | +from ..core.endpoint import policy_local_server as _policy_local_server |
| 20 | +from ..core.endpoint import policy_remote_server as _policy_remote_server |
20 | 21 |
|
21 | 22 |
|
22 |
| -def connect_endpoint( |
23 |
| - endpoint_name: str, robot_name: Optional[str] = None, instance: int = 0 |
24 |
| -) -> EndpointPolicy: |
25 |
| - """Connect to a deployed model endpoint for inference. |
| 23 | +def policy( |
| 24 | + train_run_name: Optional[str] = None, |
| 25 | + model_file: Optional[str] = None, |
| 26 | + robot_name: Optional[str] = None, |
| 27 | + instance: int = 0, |
| 28 | +) -> DirectPolicy: |
| 29 | + """Launch a direct policy that runs the model in-process without any server. |
26 | 30 |
|
27 |
| - Establishes a connection to a model endpoint that has been deployed on the |
28 |
| - Neuracore platform. The endpoint can be used to make predictions with the |
29 |
| - deployed model, and data logging is associated with the specified robot. |
| 31 | + This is the fastest option with lowest latency since there's no network overhead. |
| 32 | + The model runs directly in your Python process. |
30 | 33 |
|
31 | 34 | Args:
|
32 |
| - endpoint_name: Name of the deployed endpoint to connect to. |
| 35 | + train_run_name: Name of the training run to load the model from. |
33 | 36 | robot_name: Robot name that predictions and data will be associated with.
|
34 | 37 | If not provided, uses the last initialized robot from global state.
|
35 | 38 | instance: Instance number of the robot for multi-instance deployments.
|
36 | 39 |
|
37 | 40 | Returns:
|
38 |
| - Policy object that provides an interface for making predictions |
39 |
| - with the deployed model. |
| 41 | + DirectPolicy object that provides direct in-process model inference. |
40 | 42 |
|
41 | 43 | Raises:
|
42 |
| - EndpointError: If the endpoint connection fails due to invalid endpoint |
43 |
| - name, authentication issues, or network problems. |
44 |
| - ConfigError: If there is an error trying to get the current org |
| 44 | + EndpointError: If the model download or initialization fails. |
| 45 | + ConfigError: If there is an error trying to get the current org. |
45 | 46 | """
|
46 |
| - return _connect_endpoint( |
47 |
| - endpoint_name=endpoint_name, robot_name=robot_name, instance=instance |
48 |
| - ) |
| 47 | + return _policy(train_run_name, model_file, robot_name, instance) |
49 | 48 |
|
50 | 49 |
|
51 |
| -def connect_local_endpoint( |
52 |
| - path_to_model: Optional[str] = None, |
| 50 | +def policy_local_server( |
53 | 51 | train_run_name: Optional[str] = None,
|
| 52 | + model_file: Optional[str] = None, |
54 | 53 | port: int = 8080,
|
55 | 54 | robot_name: Optional[str] = None,
|
56 | 55 | instance: int = 0,
|
57 |
| -) -> EndpointPolicy: |
58 |
| - """Connect to a local model endpoint (run locally on your hardware). |
| 56 | + host: str = "127.0.0.1", |
| 57 | +) -> LocalServerPolicy: |
| 58 | + """Launch and connect to a local server policy. |
59 | 59 |
|
60 |
| - Establishes a connection to a locally hosted model endpoint. The model can |
61 |
| - be specified either by providing a direct path to a .mar model file or by |
62 |
| - referencing a training run name. Only one of these options should be provided. |
| 60 | + This option provides server-like architecture while maintaining local control. |
63 | 61 |
|
64 | 62 | Args:
|
65 |
| - path_to_model: Direct file path to a local .mar (Model ARchive) model file. |
66 |
| - Mutually exclusive with train_run_name. |
67 |
| - train_run_name: Name of a training run to load the model from. The system |
68 |
| - will locate and load the model from the specified training run. |
69 |
| - Mutually exclusive with path_to_model. |
70 |
| - port: TCP port number where the local endpoint is running. |
| 63 | + train_run_name: Name of the training run to load the model from. |
| 64 | + model_file: Path to the model file to load. |
| 65 | + port: TCP port number where the local server will run. |
71 | 66 | robot_name: Robot name that predictions and data will be associated with.
|
72 | 67 | If not provided, uses the last initialized robot from global state.
|
73 | 68 | instance: Instance number of the robot for multi-instance deployments.
|
| 69 | + host: Host address to bind the server to. Defaults to localhost. |
74 | 70 |
|
75 | 71 | Returns:
|
76 |
| - Policy object that provides an interface for making predictions |
77 |
| - with the local model. |
| 72 | + LocalServerPolicy object that manages a local FastAPI server. |
78 | 73 |
|
79 | 74 | Raises:
|
80 |
| - EndpointError: If the endpoint connection fails due to invalid model path, |
81 |
| - inaccessible port, or conflicting parameters. |
82 |
| - ValueError: If both path_to_model and train_run_name are provided, or if |
83 |
| - neither is provided. |
84 |
| - FileNotFoundError: If the specified model file doesn't exist. |
85 |
| - ConfigError: If there is an error trying to get the current org |
| 75 | + EndpointError: If the server startup or model initialization fails. |
| 76 | + ConfigError: If there is an error trying to get the current org. |
86 | 77 | """
|
87 |
| - return _connect_local_endpoint( |
88 |
| - robot_name=robot_name, |
89 |
| - instance=instance, |
90 |
| - path_to_model=path_to_model, |
91 |
| - train_run_name=train_run_name, |
92 |
| - port=port, |
| 78 | + return _policy_local_server( |
| 79 | + train_run_name, model_file, port, robot_name, instance, host |
93 | 80 | )
|
94 | 81 |
|
95 | 82 |
|
| 83 | +def policy_remote_server( |
| 84 | + endpoint_name: str, |
| 85 | + robot_name: Optional[str] = None, |
| 86 | + instance: int = 0, |
| 87 | +) -> RemoteServerPolicy: |
| 88 | + """Connects to a policy that is remotely running on neuracore. |
| 89 | +
|
| 90 | + Connects to a model endpoint deployed on the Neuracore cloud platform. |
| 91 | + The endpoint must be active and accessible. |
| 92 | +
|
| 93 | + Args: |
| 94 | + endpoint_name: Name of the deployed endpoint to connect to. |
| 95 | + robot_name: Robot name that predictions and data will be associated with. |
| 96 | + If not provided, uses the last initialized robot from global state. |
| 97 | + instance: Instance number of the robot for multi-instance deployments. |
| 98 | +
|
| 99 | + Returns: |
| 100 | + RemoteServerPolicy object for making predictions with the remote endpoint. |
| 101 | +
|
| 102 | + Raises: |
| 103 | + EndpointError: If the endpoint connection fails due to invalid endpoint |
| 104 | + name, authentication issues, or network problems. |
| 105 | + ConfigError: If there is an error trying to get the current org. |
| 106 | + """ |
| 107 | + return _policy_remote_server(endpoint_name, robot_name, instance) |
| 108 | + |
| 109 | + |
| 110 | +# Deployment management functions |
96 | 111 | def deploy_model(job_id: str, name: str) -> dict:
|
97 | 112 | """Deploy a trained model to a managed endpoint.
|
98 | 113 |
|
|
0 commit comments