mirror of
https://github.com/azaion/annotations.git
synced 2026-04-23 00:06:31 +00:00
82b3b526a7
file load works, suite can start up
89 lines
3.1 KiB
Cython
89 lines
3.1 KiB
Cython
from queue import Queue
|
|
cimport constants
|
|
import msgpack
|
|
|
|
from api_client cimport ApiClient
|
|
from annotation cimport Annotation
|
|
from inference import Inference
|
|
from remote_command cimport RemoteCommand, CommandType
|
|
from remote_command_handler cimport RemoteCommandHandler
|
|
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()
|
|
print(f'command is : {command}')
|
|
model = self.api_client.load_ai_model()
|
|
Inference(model, self.on_annotations).run_inference(command)
|
|
except Exception as e:
|
|
print(f"Error processing queue: {e}")
|
|
|
|
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)
|
|
print(f'loaded file: {command.filename}, {len(response)} bytes')
|
|
self.remote_handler.send(response)
|
|
print(f'{len(response)} bytes was sent.')
|
|
|
|
except Exception as e:
|
|
print(f"Error handling client: {e}")
|
|
|
|
cdef on_annotations(self, RemoteCommand cmd, annotations: [Annotation]):
|
|
data = msgpack.packb(annotations)
|
|
self.remote_handler.send(data)
|
|
print(f'{len(data)} bytes was sent.')
|
|
|
|
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()
|
|
|