mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 09:46:30 +00:00
62623b7123
rewrite zmq to DEALER and ROUTER add GET_USER command to get CurrentUser from Python all auth is on the python side inference run and validate annotations on python
92 lines
3.2 KiB
Cython
92 lines
3.2 KiB
Cython
import traceback
|
|
from queue import Queue
|
|
cimport constants
|
|
|
|
from api_client cimport ApiClient
|
|
from annotation cimport Annotation
|
|
from inference cimport Inference
|
|
from remote_command cimport RemoteCommand, CommandType
|
|
from remote_command_handler cimport RemoteCommandHandler
|
|
from user cimport User
|
|
import argparse
|
|
|
|
cdef class ParsedArguments:
|
|
cdef str email, password, folder;
|
|
cdef bint persist_token
|
|
|
|
def __init__(self, str email, str password, str folder, bint persist_token):
|
|
self.email = email
|
|
self.password = password
|
|
self.folder = folder
|
|
self.persist_token = persist_token
|
|
|
|
cdef class CommandProcessor:
|
|
cdef ApiClient api_client
|
|
cdef RemoteCommandHandler remote_handler
|
|
cdef object command_queue
|
|
cdef bint running
|
|
|
|
def __init__(self, args: ParsedArguments):
|
|
self.api_client = ApiClient(args.email, args.password, args.folder)
|
|
self.remote_handler = RemoteCommandHandler(self.on_command)
|
|
self.command_queue = Queue(maxsize=constants.QUEUE_MAXSIZE)
|
|
self.remote_handler.start()
|
|
self.running = True
|
|
|
|
def start(self):
|
|
while self.running:
|
|
try:
|
|
command = self.command_queue.get()
|
|
model = self.api_client.load_ai_model()
|
|
Inference(model, self.on_annotation).run_inference(command)
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
|
|
cdef on_command(self, RemoteCommand command):
|
|
try:
|
|
if command.command_type == CommandType.INFERENCE:
|
|
self.command_queue.put(command)
|
|
elif command.command_type == CommandType.LOAD:
|
|
response = self.api_client.load_bytes(command.filename)
|
|
self.remote_handler.send(command.client_id, response)
|
|
elif command.command_type == CommandType.GET_USER:
|
|
self.get_user(command, self.api_client.get_user())
|
|
else:
|
|
pass
|
|
except Exception as e:
|
|
print(f"Error handling client: {e}")
|
|
|
|
cdef get_user(self, RemoteCommand command, User user):
|
|
self.remote_handler.send(command.client_id, user.serialize())
|
|
|
|
cdef on_annotation(self, RemoteCommand cmd, Annotation annotation):
|
|
data = annotation.serialize()
|
|
self.remote_handler.send(cmd.client_id, data)
|
|
|
|
def stop(self):
|
|
self.running = False
|
|
|
|
def parse_arguments():
|
|
parser = argparse.ArgumentParser(description="Command Processor")
|
|
parser.add_argument("-e", "--email", type=str, default="", help="Email")
|
|
parser.add_argument("-p", "--pw", type=str, default="", help="Password")
|
|
parser.add_argument("-f", "--folder", type=str, default="", help="Folder to API inner folder to download file from")
|
|
parser.add_argument("-t", "--persist_token", type=bool, default=True, help="True for persisting token from API")
|
|
cdef args = parser.parse_args()
|
|
|
|
cdef str email = args.email
|
|
cdef str password = args.pw
|
|
cdef str folder = args.folder
|
|
cdef bint persist_token = args.persist_token
|
|
|
|
return ParsedArguments(email, password, folder, persist_token)
|
|
|
|
if __name__ == '__main__':
|
|
args = parse_arguments()
|
|
processor = CommandProcessor(args)
|
|
try:
|
|
processor.start()
|
|
except KeyboardInterrupt:
|
|
processor.stop()
|
|
|