add loader and versioning

This commit is contained in:
Alex Bezdieniezhnykh
2025-06-10 08:53:57 +03:00
parent 7750025631
commit dcd0fabc1f
31 changed files with 284 additions and 123 deletions
+1 -3
View File
@@ -4,10 +4,8 @@ from PyInstaller.utils.hooks import collect_all
datas = [('venv\\Lib\\site-packages\\cv2', 'cv2')]
binaries = []
hiddenimports = ['constants', 'annotation', 'credentials', 'file_data', 'user', 'security', 'secure_model', 'cdn_manager', 'api_client', 'hardware_service', 'remote_command', 'ai_config', 'tensorrt_engine', 'onnx_engine', 'inference_engine', 'inference', 'remote_command_handler']
hiddenimports = ['constants', 'file_data', 'remote_command', 'remote_command_handler', 'annotation', 'loader_client', 'ai_config', 'tensorrt_engine', 'onnx_engine', 'inference_engine', 'inference', 'main-inf']
hiddenimports += collect_submodules('cv2')
tmp_ret = collect_all('requests')
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
tmp_ret = collect_all('psutil')
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
tmp_ret = collect_all('msgpack')
+15 -9
View File
@@ -34,23 +34,29 @@ venv\Scripts\pyinstaller --name=azaion-inference ^
--collect-all pynvml ^
--collect-all jwt ^
--hidden-import constants ^
--hidden-import annotation ^
--hidden-import file_data ^
--hidden-import remote_command ^
--hidden-import remote_command_handler ^
--hidden-import annotation ^
--hidden-import loader_client ^
--hidden-import ai_config ^
--hidden-import tensorrt_engine ^
--hidden-import onnx_engine ^
--hidden-import inference_engine ^
--hidden-import inference ^
--hidden-import remote_command_handler ^
--hidden-import main-inf ^
start.py
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "ai_config.cp312-win_amd64.pyd" "annotation.cp312-win_amd64.pyd" "api_client.cp312-win_amd64.pyd" /MOV
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "cdn_manager.cp312-win_amd64.pyd" "constants.cp312-win_amd64.pyd" "credentials.cp312-win_amd64.pyd" /MOV
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "file_data.cp312-win_amd64.pyd" "hardware_service.cp312-win_amd64.pyd" "inference.cp312-win_amd64.pyd" "inference_engine.cp312-win_amd64.pyd" /MOV
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "main.cp312-win_amd64.pyd" "onnx_engine.cp312-win_amd64.pyd" "remote_command.cp312-win_amd64.pyd" "remote_command_handler.cp312-win_amd64.pyd" /MOV
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "security.cp312-win_amd64.pyd" "tensorrt_engine.cp312-win_amd64.pyd" "user.cp312-win_amd64.pyd" /MOV
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "ai_config.cp312-win_amd64.pyd" "annotation.cp312-win_amd64.pyd"
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "constants.cp312-win_amd64.pyd" "file_data.cp312-win_amd64.pyd"
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "remote_command.cp312-win_amd64.pyd" "remote_command_handler.cp312-win_amd64.pyd"
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "inference.cp312-win_amd64.pyd" "inference_engine.cp312-win_amd64.pyd"
robocopy "dist\azaion-inference\_internal" "..\dist-azaion\_internal" "tensorrt_engine.cp312-win_amd64.pyd" "onnx_engine.cp312-win_amd64.pyd" "main_inference.cp312-win_amd64.pyd"
robocopy "dist\azaion-inference\_internal" "..\dist-dlls\_internal" /E /MOVE
robocopy "dist\azaion-inference" "..\dist-azaion" "azaion-inference.exe" /E /MOVE
robocopy "dist\azaion-inference\_internal" "..\dist-dlls\_internal" /E
robocopy "dist\azaion-inference" "..\dist-azaion" "azaion-inference.exe" /E
REM copy for local run
robocopy "c:\share\" "dist\azaion-inference\_internal /E
cd /d %CURRENT_DIR%
+28 -17
View File
@@ -58,28 +58,39 @@ cdef class Inference:
self.is_building_engine = False
cdef build_tensor_engine(self, object updater_callback):
if not tensor_gpu_index == -1:
if tensor_gpu_index == -1:
return
engine_filename = TensorRTEngine.get_engine_filename(0)
models_dir = constants.MODELS_FOLDER
try:
engine_filename = TensorRTEngine.get_engine_filename(0)
models_dir = constants.MODELS_FOLDER
self.is_building_engine = True
updater_callback('downloading')
self.is_building_engine = True
updater_callback('downloading')
if self.loader_client.load_big_small_resource(engine_filename, models_dir):
print('tensor rt engine is here, no need to build')
res = self.loader_client.load_big_small_resource(engine_filename, models_dir)
if res.err is None:
print('tensor rt engine is here, no need to build')
self.is_building_engine = False
return
print(res.err)
# time.sleep(8) # prevent simultaneously loading dll and models
updater_callback('converting')
print('try to load onnx')
res = self.loader_client.load_big_small_resource(constants.AI_ONNX_MODEL_FILE, models_dir)
if res.err is not None:
updater_callback(f'Error. {res.err}')
model_bytes = TensorRTEngine.convert_from_onnx(res.data)
updater_callback('uploading')
res = self.loader_client.upload_big_small_resource(model_bytes, <str> engine_filename, models_dir)
if res.err is not None:
updater_callback(f'Error. {res.err}')
print(f'uploaded {engine_filename} to CDN and API')
self.is_building_engine = False
return
# time.sleep(8) # prevent simultaneously loading dll and models
updater_callback('converting')
onnx_model = self.loader_client.load_big_small_resource(constants.AI_ONNX_MODEL_FILE, models_dir)
model_bytes = TensorRTEngine.convert_from_onnx(onnx_model)
updater_callback('uploading')
self.loader_client.upload_big_small_resource(model_bytes, <str> engine_filename, models_dir)
print(f'uploaded {engine_filename} to CDN and API')
self.is_building_engine = False
updater_callback('enabled')
except Exception as e:
updater_callback(f'Error. {str(e)}')
cdef init_ai(self):
if self.engine is not None:
+7 -2
View File
@@ -1,7 +1,12 @@
from remote_command cimport RemoteCommand
cdef class LoadResult:
cdef public str err
cdef public bytes data
cdef class LoaderClient:
cdef object _context
cdef object _loader_context
cdef object _socket
cdef RemoteCommand _send_receive_command(self, RemoteCommand command)
@@ -10,4 +15,4 @@ cdef class LoaderClient:
cdef upload_big_small_resource(self, bytes content, str filename, str directory)
cdef close(self)
cdef stop(self)
+16 -11
View File
@@ -2,10 +2,15 @@ import zmq
from remote_command cimport RemoteCommand, CommandType
from file_data cimport FileData, UploadFileData
cdef class LoadResult:
def __init__(self, str err, bytes data=None):
self.err = err
self.data = data
cdef class LoaderClient:
def __init__(self, str zmq_host, int zmq_port):
self._context = zmq.Context().instance()
self._socket = self._context.socket(zmq.DEALER)
self._loader_context = zmq.Context()
self._socket = self._loader_context.socket(zmq.DEALER)
self._socket.connect(f'tcp://{zmq_host}:{zmq_port}')
cdef RemoteCommand _send_receive_command(self, RemoteCommand command):
@@ -16,24 +21,24 @@ cdef class LoaderClient:
cdef FileData file_data = FileData(folder=directory, filename=filename)
cdef RemoteCommand response = self._send_receive_command(RemoteCommand(CommandType.LOAD_BIG_SMALL, data=file_data.serialize()))
if response.command_type == CommandType.DATA_BYTES:
return response.data
return LoadResult(None, response.data)
elif response.command_type == CommandType.ERROR:
raise Exception(f"Error from server: {response.message}")
return LoadResult(f"Error from server: {response.message}")
else:
raise Exception(f"Unexpected response command type: {response.command_type}")
return LoadResult(f"Unexpected response command type: {response.command_type}")
cdef upload_big_small_resource(self, bytes content, str filename, str directory):
cdef UploadFileData upload_file_data = UploadFileData(content, filename, directory)
cdef RemoteCommand upload_resp = self._send_receive_command(RemoteCommand(CommandType.UPLOAD_BIG_SMALL, data=upload_file_data.serialize()))
if upload_resp.command_type == CommandType.OK:
return
return LoadResult(None, None)
elif upload_resp.command_type == CommandType.ERROR:
raise Exception(f"Error from server: {upload_resp.message}")
return LoadResult(f"Error from server: {upload_resp.message}")
else:
raise Exception(f"Unexpected response command type: {upload_resp.command_type}")
return LoadResult(f"Unexpected response command type: {upload_resp.command_type}")
cdef close(self):
cdef stop(self):
if self._socket and not self._socket.closed:
self._socket.close()
if self._context and not self._context.closed:
self._context.term()
if self._loader_context and not self._loader_context.closed:
self._loader_context.term()
@@ -24,7 +24,6 @@ cdef class CommandProcessor:
self.remote_handler.start()
self.running = True
self.loader_client = LoaderClient(loader_zmq_host, loader_zmq_port)
#TODO: replace api_client to azaion_loader.exe call
self.inference = Inference(self.loader_client, self.on_annotation)
def start(self):
@@ -45,9 +44,8 @@ cdef class CommandProcessor:
if command.command_type == CommandType.INFERENCE:
self.inference_queue.put(command)
elif command.command_type == CommandType.AI_AVAILABILITY_CHECK:
self.inference.build_tensor_engine(lambda status: self.remote_handler.send(
command.client_id, RemoteCommand(CommandType.AI_AVAILABILITY_RESULT, None, status).serialize()))
self.remote_handler.send(command.client_id, RemoteCommand(CommandType.AI_AVAILABILITY_RESULT, None, 'enabled').serialize())
self.inference.build_tensor_engine(lambda status: self.remote_handler.send(command.client_id,
RemoteCommand(CommandType.AI_AVAILABILITY_RESULT, None, status).serialize()))
elif command.command_type == CommandType.STOP_INFERENCE:
self.inference.stop()
elif command.command_type == CommandType.EXIT:
@@ -65,4 +63,5 @@ cdef class CommandProcessor:
def stop(self):
self.inference.stop()
self.remote_handler.stop()
self.loader_client.stop()
self.running = False
+2 -2
View File
@@ -8,7 +8,7 @@ import yaml
cdef class RemoteCommandHandler:
def __init__(self, int zmq_port, object on_command):
self._on_command = on_command
self._context = zmq.Context.instance()
self._context = zmq.Context()
self._router = self._context.socket(zmq.ROUTER)
self._router.setsockopt(zmq.LINGER, 0)
@@ -83,6 +83,7 @@ cdef class RemoteCommandHandler:
self._control.send(b"TERMINATE", flags=zmq.DONTWAIT)
except zmq.error.ZMQError:
pass
self._router.close(linger=0)
self._dealer.close(linger=0)
self._control.close(linger=0)
@@ -90,5 +91,4 @@ cdef class RemoteCommandHandler:
self._proxy_thread.join(timeout=2)
while any(w.is_alive() for w in self._workers):
time.sleep(0.1)
self._context.term()
+3 -3
View File
@@ -4,17 +4,17 @@ import numpy as np
extensions = [
Extension('constants', ['constants.pyx']),
Extension('annotation', ['annotation.pyx']),
Extension('file_data', ['file_data.pyx']),
Extension('loader_client', ['loader_client.pyx']),
Extension('remote_command', ['remote_command.pyx']),
Extension('remote_command_handler', ['remote_command_handler.pyx']),
Extension('annotation', ['annotation.pyx']),
Extension('loader_client', ['loader_client.pyx']),
Extension('ai_config', ['ai_config.pyx']),
Extension('tensorrt_engine', ['tensorrt_engine.pyx'], include_dirs=[np.get_include()]),
Extension('onnx_engine', ['onnx_engine.pyx'], include_dirs=[np.get_include()]),
Extension('inference_engine', ['inference_engine.pyx'], include_dirs=[np.get_include()]),
Extension('inference', ['inference.pyx'], include_dirs=[np.get_include()]),
Extension('main', ['main.pyx']),
Extension('main_inference', ['main_inference.pyx']),
]
setup(
+1 -1
View File
@@ -1,4 +1,4 @@
from main import CommandProcessor
from main_inference import CommandProcessor
import argparse
-1
View File
@@ -1 +0,0 @@
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiI5N2U5MWI2OC1hNmRlLTQ3YTgtOTgzYi0xOTU3YzViNDQ2MTkiLCJ1bmlxdWVfbmFtZSI6ImFkbWluLXJlbW90ZUBhemFpb24uY29tIiwicm9sZSI6IkFwaUFkbWluIiwibmJmIjoxNzM5MzUxNDEwLCJleHAiOjE3MzkzNjU4MTAsImlhdCI6MTczOTM1MTQxMCwiaXNzIjoiQXphaW9uQXBpIiwiYXVkIjoiQW5ub3RhdG9ycy9PcmFuZ2VQaS9BZG1pbnMifQ.P32xRe6nk-0u2jjBi3rdsd3YwlmGXL0NX_eE2xb7OUI