fix hardware service

This commit is contained in:
Alex Bezdieniezhnykh
2025-05-02 13:25:33 +03:00
parent 7842fe1067
commit 472ed6533e
15 changed files with 118 additions and 142 deletions
+8 -7
View File
@@ -14,11 +14,12 @@ from user cimport User, RoleEnum
cdef class ApiClient:
"""Handles API authentication and downloading of the AI model."""
def __init__(self):
def __init__(self, str api_url):
self.credentials = None
self.user = None
self.token = None
self.cdn_manager = None
self.api_url = api_url
cdef set_credentials(self, Credentials credentials):
self.credentials = credentials
@@ -33,12 +34,13 @@ cdef class ApiClient:
self.cdn_manager = CDNManager(creds)
cdef login(self):
response = requests.post(f"{constants.API_URL}/login",
json={"email": self.credentials.email, "password": self.credentials.password})
response = requests.post(f"{self.api_url}/login",
json={"email": self.credentials.email, "password": self.credentials.password})
response.raise_for_status()
token = response.json()["token"]
self.set_token(token)
cdef set_token(self, str token):
self.token = token
claims = jwt.decode(token, options={"verify_signature": False})
@@ -73,23 +75,22 @@ cdef class ApiClient:
cdef upload_file(self, str filename, bytes resource, str folder):
if self.token is None:
self.login()
url = f"{constants.API_URL}/resources/{folder}"
url = f"{self.api_url}/resources/{folder}"
headers = { "Authorization": f"Bearer {self.token}" }
files = {'data': (filename, resource)}
try:
r = requests.post(url, headers=headers, files=files, allow_redirects=True)
r.raise_for_status()
constants.log(f"Uploaded {filename} to {constants.API_URL}/{folder} successfully: {r.status_code}.")
constants.log(f"Uploaded {filename} to {self.api_url}/{folder} successfully: {r.status_code}.")
except Exception as e:
constants.log(f"Upload fail: {e}")
cdef load_bytes(self, str filename, str folder):
hardware_service = HardwareService()
cdef str hardware = hardware_service.get_hardware_info()
if self.token is None:
self.login()
url = f"{constants.API_URL}/resources/get/{folder}"
url = f"{self.api_url}/resources/get/{folder}"
headers = {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json"
-2
View File
@@ -28,8 +28,6 @@ tmp_ret = collect_all('pynvml')
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
tmp_ret = collect_all('boto3')
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
tmp_ret = collect_all('re')
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
tmp_ret = collect_all('jwt')
datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2]
+2 -1
View File
@@ -1 +1,2 @@
zmq_port: 5131
zmq_port: 5131
api_url: https://api.azaion.com
+2 -1
View File
@@ -1 +1,2 @@
zmq_port: 5127
zmq_port: 5127
api_url: http://localhost:5219
-1
View File
@@ -4,7 +4,6 @@ cdef int QUEUE_MAXSIZE # Maximum size of the command queue
cdef str COMMANDS_QUEUE # Name of the commands queue in rabbit
cdef str ANNOTATIONS_QUEUE # Name of the annotations queue in rabbit
cdef str API_URL # Base URL for the external API
cdef str QUEUE_CONFIG_FILENAME # queue config filename to load from api
cdef str AI_ONNX_MODEL_FILE
-1
View File
@@ -6,7 +6,6 @@ cdef int QUEUE_MAXSIZE = 1000 # Maximum size of the command queue
cdef str COMMANDS_QUEUE = "azaion-commands"
cdef str ANNOTATIONS_QUEUE = "azaion-annotations"
cdef str API_URL = "https://api.azaion.com" # Base URL for the external API
cdef str QUEUE_CONFIG_FILENAME = "secured-config.json"
cdef str AI_ONNX_MODEL_FILE = "azaion.onnx"
+7 -3
View File
@@ -59,13 +59,17 @@ cdef class HardwareService:
# in case of subprocess error do:
# cdef bytes os_command_bytes = os_command.encode('utf-8')
# and use os_command_bytes
result = subprocess.check_output(os_command, shell=True).decode('utf-8')
lines = [line.strip() for line in result.splitlines() if line.strip()]
cdef str cpu = lines[0].replace("Name=", "").replace(" ", " ")
cdef str gpu = lines[1].replace("Name=", "").replace(" ", " ")
# could be multiple gpus
cdef str memory = lines[-2].replace("TotalVisibleMemorySize=", "").replace(" ", " ")
cdef str drive_serial = lines[-1]
return f'CPU: {cpu}. GPU: {gpu}. Memory: {memory}. Drive Serial: {drive_serial}'
len_lines = len(lines)
cdef str memory = lines[len_lines-2].replace("TotalVisibleMemorySize=", "").replace(" ", " ")
cdef str drive_serial = lines[len_lines-1]
cdef str res = f'CPU: {cpu}. GPU: {gpu}. Memory: {memory}. DriveSerial: {drive_serial}'
return res
+7 -2
View File
@@ -23,8 +23,13 @@ cdef class CommandProcessor:
cdef Inference inference
def __init__(self):
self.api_client = ApiClient()
self.remote_handler = RemoteCommandHandler(self.on_command)
with open(<str>constants.CONFIG_FILE, "r") as f:
config = yaml.safe_load(f)
zmq_port = config["zmq_port"]
api_url = config["api_url"]
self.api_client = ApiClient(api_url)
self.remote_handler = RemoteCommandHandler(zmq_port, self.on_command)
self.inference_queue = Queue(maxsize=constants.QUEUE_MAXSIZE)
self.remote_handler.start()
self.running = True
+3 -6
View File
@@ -6,16 +6,13 @@ cimport constants
import yaml
cdef class RemoteCommandHandler:
def __init__(self, object on_command):
def __init__(self, int zmq_port, object on_command):
self._on_command = on_command
self._context = zmq.Context.instance()
self._router = self._context.socket(zmq.ROUTER)
self._router.setsockopt(zmq.LINGER, 0)
with open(<str>constants.CONFIG_FILE, "r") as f:
config = yaml.safe_load(f)
port = config["zmq_port"]
self._router.bind(f'tcp://*:{port}')
self._router.bind(f'tcp://*:{zmq_port}')
self._dealer = self._context.socket(zmq.DEALER)
self._dealer.setsockopt(zmq.LINGER, 0)
@@ -31,7 +28,7 @@ cdef class RemoteCommandHandler:
for _ in range(4): # 4 worker threads
worker = Thread(target=self._worker_loop, daemon=True)
self._workers.append(worker)
print(f'Listening to commands on port {port}...')
print(f'Listening to commands on port {zmq_port}...')
cdef start(self):
self._proxy_thread.start()