50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
import triton_python_backend_utils as pb_utils
|
|
import numpy as np
|
|
import json
|
|
import random
|
|
import string
|
|
|
|
class TritonPythonModel:
|
|
def initialize(self, args):
|
|
"""
|
|
모델이 로드될 때 딱 한 번만 호출됩니다.
|
|
`initialize` 함수를 구현하는 것은 선택 사항입니다. 이 함수를 통해 모델은
|
|
이 모델과 관련된 모든 상태를 초기화할 수 있습니다.
|
|
"""
|
|
self.logger = pb_utils.Logger
|
|
|
|
self.model_name = args["model_name"]
|
|
self.model_config = json.loads(args["model_config"])
|
|
|
|
self.logger.log_info(f"'{self.model_name}' 모델 초기화 완료")
|
|
|
|
|
|
def execute(self, requests):
|
|
responses = []
|
|
|
|
for request in requests:
|
|
# 랜덤 문자열 생성
|
|
random_string = ''.join(
|
|
random.choice(string.ascii_letters + string.digits) for _ in range(16)
|
|
)
|
|
self.logger.log_info(f"OUTPUT 출력:\n{random_string}")
|
|
|
|
# Triton이 요구하는 shape [1] STRING 출력
|
|
output_tensor = pb_utils.Tensor(
|
|
"OUTPUT",
|
|
np.array([random_string.encode("utf-8")], dtype=np.object_)
|
|
)
|
|
|
|
responses.append(pb_utils.InferenceResponse(
|
|
output_tensors=[output_tensor]
|
|
))
|
|
|
|
return responses
|
|
|
|
def finalize(self):
|
|
"""
|
|
모델 실행이 완료된 후 Triton 서버가 종료될 때 한 번 호출되는 함수입니다.
|
|
`finalize` 함수를 구현하는 것은 선택 사항입니다. 이 함수를 통해 모델은
|
|
종료 전에 필요한 모든 정리 작업을 수행할 수 있습니다.
|
|
"""
|
|
pass |