# Initialize the REST TorchServeClient object
= TorchServeClientREST()
ts_client ts_client
TorchServeClientREST(base_url=http://localhost, management_port=8081, inference_port=8080)
TorchServeClientREST (base_url=None, management_port=8081, inference_port=8080)
Initialize self. See help(type(self)) for accurate signature.
To make calls to REST endpoint, simply initialize a TorchServeClientREST
object as shown below:
TorchServeClientREST(base_url=http://localhost, management_port=8081, inference_port=8080)
If you wish to customize the base URL, management port, or inference port of your TorchServe server, you can pass them as arguments during initialization:
# Customize the base URL, management port, and inference port
ts_client = TorchServeClientREST(base_url='http://your-torchserve-server.com',
management_port=8081, inference_port=8080)
ts_client
TorchServeClientREST(base_url=http://your-torchserve-server.com, management_port=8081, inference_port=8080)
Alternatively, if you don’t provide a base URL during initialization, the client will check for the presence of TORCHSERVE_URL
in the environment variables. If the variable is not found, it will gracefully fall back to using localhost as the default.
With TorchServe Management APIs, you can effortlessly manage your models at runtime. Here’s a quick rundown of the actions you can perform using our TorchServeClient
SDK:
ts_client.management.register_model()
method.ts_client.management.scale_workers()
.{'status': 'Processing worker updates...'}
ts_client.management.describe_model()
.[{'modelName': 'squeezenet1_1',
'modelVersion': '1.0',
'modelUrl': 'https://torchserve.pytorch.org/mar_files/squeezenet1_1.mar',
'runtime': 'python',
'minWorkers': 1,
'maxWorkers': 1,
'batchSize': 1,
'maxBatchDelay': 100,
'loadedAtStartup': False,
'workers': [{'id': '9001',
'startTime': '2023-07-17T22:55:40.155Z',
'status': 'UNLOADING',
'memoryUsage': 0,
'pid': -1,
'gpu': False,
'gpuUsage': 'N/A'}]}]
ts_client.management.list_models()
.{'models': [{'modelName': 'squeezenet1_1',
'modelUrl': 'https://torchserve.pytorch.org/mar_files/squeezenet1_1.mar'}]}
ts_client.management.set_model_version()
method.{'status': 'Default vesion succsesfully updated for model "squeezenet1_1" to "1.0"'}
ts_client.management.unregister_model()
function to gracefully remove it from TorchServe.{'status': 'Model "squeezenet1_1" unregistered'}
Remember, all these management APIs can be accessed conveniently under the namespace ts_client.management
.
TorchServeClient allows you to interact with the Inference API, which listens on port 8080, enabling you to run inference on your samples effortlessly. Here are the available APIs under the ts_client.inference
namespace:
ts_client.inference.api_description()
to get a comprehensive list.ts_client.inference.health_check()
method.ts_client.inference.predictions()
.ts_client.inference.prediction('squeezenet1_1', data={'data': open('/Users/ankursingh/Downloads/kitten_small.jpg', 'rb')})
{'lynx': 0.5455798506736755,
'tabby': 0.2794159948825836,
'Egyptian_cat': 0.10391879826784134,
'tiger_cat': 0.06263326108455658,
'leopard': 0.0050191376358270645}
ts_client.inference.explanations()
.