-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathclient.py
More file actions
204 lines (163 loc) · 6.81 KB
/
Copy pathclient.py
File metadata and controls
204 lines (163 loc) · 6.81 KB
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
#!/usr/bin/env python3
import numpy as np
import sys
import argparse
from typing import List, Tuple
import tritonclient.http as httpclient
from tritonclient.utils import InferenceServerException
class SimpleTokenizer:
"""
A simple character-level tokenizer for demo purposes.
"""
def __init__(self, vocab_size=10000):
self.vocab_size = vocab_size
self.pad_token_id = 0
self.unk_token_id = 1
def encode(self, text: str, max_length: int = 128) -> Tuple[List[int], List[int]]:
"""
Encode text to token IDs and create attention mask.
Args:
text: Input text string
max_length: Maximum sequence length
Returns:
Tuple of (input_ids, attention_mask)
"""
# Simple character-level encoding that maps each character to an ID based on its ASCII value
input_ids = [min(ord(c), self.vocab_size - 1) for c in text.lower()]
# Truncate if too long
if len(input_ids) > max_length:
input_ids = input_ids[:max_length]
# Create attention mask (1 for real tokens, 0 for padding)
attention_mask = [1] * len(input_ids)
# Pad to max_length
padding_length = max_length - len(input_ids)
input_ids.extend([self.pad_token_id] * padding_length)
attention_mask.extend([0] * padding_length)
return input_ids, attention_mask
class SentimentClient:
"""
Client for the Transformer Sentiment Classifier on Triton Inference Server.
"""
def __init__(self, url: str = "localhost:8000", model_name: str = "transformer"):
"""
Initialize the client.
Args:
url: Triton server URL (e.g., "localhost:8000")
model_name: Name of the model
"""
self.url = url
self.model_name = model_name
self.client = httpclient.InferenceServerClient(url=url, verbose=False)
self.tokenizer = SimpleTokenizer()
self.max_seq_length = 128
self.class_names = ["Negative", "Neutral", "Positive"]
def check_server_ready(self) -> bool:
"""Check if the Triton server is ready."""
try:
if self.client.is_server_ready():
print(f"Server at {self.url} is ready")
return True
else:
print(f"Server at {self.url} is not ready")
return False
except InferenceServerException as e:
print(f"Failed to connect to server at {self.url}")
print(f" Error: {e}")
return False
def check_model_ready(self) -> bool:
"""Check if the model is ready."""
try:
if self.client.is_model_ready(self.model_name):
print(f"Model '{self.model_name}' is ready")
return True
else:
print(f"Model '{self.model_name}' is not ready")
return False
except InferenceServerException as e:
print(f"Failed to check model status")
print(f" Error: {e}")
return False
def predict(self, text: str) -> Tuple[np.ndarray, int, str]:
"""
Run inference on a single text input.
Args:
text: Input text string
Returns:
Tuple of (probabilities, predicted_class, class_name)
"""
# Tokenize input
input_ids, attention_mask = self.tokenizer.encode(text, self.max_seq_length)
# Convert to numpy arrays with batch dimension
input_ids_np = np.array([input_ids], dtype=np.int64)
attention_mask_np = np.array([attention_mask], dtype=np.int64)
# Create input objects
inputs = [
httpclient.InferInput("INPUT_IDS", input_ids_np.shape, "INT64"),
httpclient.InferInput("ATTENTION_MASK", attention_mask_np.shape, "INT64")
]
# Set data
inputs[0].set_data_from_numpy(input_ids_np)
inputs[1].set_data_from_numpy(attention_mask_np)
# Create output object
outputs = [httpclient.InferRequestedOutput("OUTPUT")]
# Send inference request
try:
response = self.client.infer(
model_name=self.model_name,
inputs=inputs,
outputs=outputs
)
# Get output
output = response.as_numpy("OUTPUT")[0] # Remove batch dimension
predicted_class = int(np.argmax(output))
class_name = self.class_names[predicted_class]
return output, predicted_class, class_name
except InferenceServerException as e:
print(f"Inference failed: {e}")
raise
def predict_batch(self, texts: List[str]) -> List[Tuple[np.ndarray, int, str]]:
"""
Run inference on a batch of text inputs.
Args:
texts: List of input text strings
Returns:
List of tuples (probabilities, predicted_class, class_name) for each input
"""
# Tokenize all inputs
input_ids_batch = []
attention_mask_batch = []
for text in texts:
input_ids, attention_mask = self.tokenizer.encode(text, self.max_seq_length)
input_ids_batch.append(input_ids)
attention_mask_batch.append(attention_mask)
# Convert to numpy arrays
input_ids_np = np.array(input_ids_batch, dtype=np.int64)
attention_mask_np = np.array(attention_mask_batch, dtype=np.int64)
# Create input objects
inputs = [
httpclient.InferInput("INPUT_IDS", input_ids_np.shape, "INT64"),
httpclient.InferInput("ATTENTION_MASK", attention_mask_np.shape, "INT64")
]
# Set data
inputs[0].set_data_from_numpy(input_ids_np)
inputs[1].set_data_from_numpy(attention_mask_np)
# Create output object
outputs = [httpclient.InferRequestedOutput("OUTPUT")]
# Send inference request
try:
response = self.client.infer(
model_name=self.model_name,
inputs=inputs,
outputs=outputs
)
# Get outputs
outputs_np = response.as_numpy("OUTPUT")
results = []
for output in outputs_np:
predicted_class = int(np.argmax(output))
class_name = self.class_names[predicted_class]
results.append((output, predicted_class, class_name))
return results
except InferenceServerException as e:
print(f"Batch inference failed: {e}")
raise