fixed inference bugs

add DONE during inference, correct handling on C# side
This commit is contained in:
Alex Bezdieniezhnykh
2025-02-01 02:09:11 +02:00
parent e7afa96a0b
commit 739759628a
23 changed files with 324 additions and 95 deletions
+16 -34
View File
@@ -1,6 +1,7 @@
import traceback
from queue import Queue
cimport constants
import msgpack
from api_client cimport ApiClient
from annotation cimport Annotation
@@ -8,23 +9,21 @@ 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):
def __init__(self, str email, str password, str folder):
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
cdef Inference inference
def __init__(self, args: ParsedArguments):
self.api_client = ApiClient(args.email, args.password, args.folder)
@@ -32,25 +31,31 @@ cdef class CommandProcessor:
self.command_queue = Queue(maxsize=constants.QUEUE_MAXSIZE)
self.remote_handler.start()
self.running = True
model = self.api_client.load_ai_model()
self.inference = Inference(model, self.on_annotation)
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)
self.inference.run_inference(command)
self.remote_handler.send(command.client_id, <bytes>'DONE'.encode('utf-8'))
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)
if command.command_type == CommandType.GET_USER:
self.get_user(command, self.api_client.get_user())
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())
elif command.command_type == CommandType.INFERENCE:
self.command_queue.put(command)
elif command.command_type == CommandType.STOP_INFERENCE:
self.inference.stop()
elif command.command_type == CommandType.EXIT:
self.stop()
else:
pass
except Exception as e:
@@ -64,28 +69,5 @@ cdef class CommandProcessor:
self.remote_handler.send(cmd.client_id, data)
def stop(self):
self.remote_handler.stop()
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()