diff --git a/Source/JackFramework/Core/Graph/_user_dataloader.py b/Source/JackFramework/Core/Graph/_user_dataloader.py index 208acf0..d69d451 100644 --- a/Source/JackFramework/Core/Graph/_user_dataloader.py +++ b/Source/JackFramework/Core/Graph/_user_dataloader.py @@ -36,7 +36,5 @@ def user_load_test_data(self, cmd: str) -> tuple: return self.__jf_data_handler.load_test_data(cmd) def user_save_test_data(self, output_data: list, supplement: list, cmd: str) -> None: - res = [] - for idx, output_item in enumerate(output_data): - res.append(self.__jf_data_handler.save_test_data(output_item, supplement, cmd, idx)) - return res + return [self.__jf_data_handler.save_test_data( + output_item, supplement, cmd, idx) for idx, output_item in enumerate(output_data)] diff --git a/Source/JackFramework/Core/Mode/_meta_interface_mode.py b/Source/JackFramework/Core/Mode/_meta_interface_mode.py new file mode 100644 index 0000000..c28d956 --- /dev/null +++ b/Source/JackFramework/Core/Mode/_meta_interface_mode.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +from collections.abc import Callable + +from JackFramework.SysBasic.log_handler import LogHandler as log + +from .test_proc import TestProc + + +def error_handler(func: Callable) -> tuple: + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as e: + log.error(f"Error in {func.__name__}: {str(e)}") + return False + return wrapper + + +class InterfaceMode(TestProc): + ID_OUTPUTS_DATA, ID_SUPPLEMENT = 0, 1 + + def __init__(self, args: object, user_inference_func: object, + is_training: bool = False) -> None: + super().__init__(args, user_inference_func, is_training) + log.info('Create InterfaceMode') + + def __save_result(self, outputs_data: list, supplement: list, msg: str) -> None: + res = self._data_manager.user_save_test_data(outputs_data, supplement, msg) + log.info('jf server has saved the results') + return res + + @error_handler + def __try_load_data(self, msg: str) -> tuple: + return self._data_manager.user_load_test_data(msg) + + @error_handler + def __try_exec_testing_proc(self, batch_data: list) -> tuple: + return self._testing_data_proc(batch_data) + + @error_handler + def __try_save_result(self, msg: str, outputs_data: list, supplement: list) -> bool: + return self.__save_result(outputs_data, supplement, msg) + + def data_handler(self, msg: str) -> bool: + batch_data = self.__try_load_data(msg) + if batch_data is False: + return False + + res = self.__try_exec_testing_proc(batch_data) + if batch_data is False: + return False + + return self.__try_save_result(msg, res[self.ID_OUTPUTS_DATA], res[self.ID_SUPPLEMENT]) diff --git a/Source/JackFramework/Core/Mode/background.py b/Source/JackFramework/Core/Mode/background.py index a063cb3..095aaa8 100644 --- a/Source/JackFramework/Core/Mode/background.py +++ b/Source/JackFramework/Core/Mode/background.py @@ -2,10 +2,10 @@ from JackFramework.SysBasic.log_handler import LogHandler as log from JackFramework.Tools.process_comm import NamedPipe -from .test_proc import TestProc +from ._meta_interface_mode import InterfaceMode -class BackGround(TestProc): +class BackGround(InterfaceMode): __EXIT_COMMAND = 'jf stop' __RELY_MSG = 'the server has got message: %s' __RELY_FINISH = 'jf finish' @@ -26,50 +26,6 @@ def __init_setting(self) -> object: self.__named_pipe = NamedPipe('server') return self.__named_pipe - def __save_result(self, outputs_data: list, supplement: list, msg: str) -> None: - _, data_manager = self._get_graph_and_data_manager - data_manager.user_save_test_data(outputs_data, supplement, msg) - log.info('jf server has saved the results') - - def __try_load_data(self, msg: str) -> tuple: - res = True - try: - _, data_manager = self._get_graph_and_data_manager - batch_data = data_manager.user_load_test_data(msg) - except Exception: - log.error('Any error of load_test_data function or split in dataloader!') - res = False - batch_data = None - return res, batch_data - - def __try_exec_testing_proc(self, batch_data: list) -> tuple: - res, outputs_data, supplement = True, None, None - try: - outputs_data, supplement = self._testing_data_proc(batch_data) - except Exception: - log.error('Any error of inference function in model!') - res = False - return res, outputs_data, supplement - - def __try_save_result(self, msg: str, outputs_data: list, supplement: list) -> bool: - res = True - try: - self.__save_result(outputs_data, supplement, msg) - except Exception: - log.error('Any error of save_test_data function in dataloader!') - res = False - return res - - def __data_handler(self, msg: str) -> bool: - res, batch_data = self.__try_load_data(msg) - if not res: - return res - res, outputs_data, supplement = self.__try_exec_testing_proc(batch_data) - if not res: - return res - res = self.__try_save_result(msg, outputs_data, supplement) - return res - def __msg_handler(self, named_pipe: object) -> str: msg = named_pipe.receive() named_pipe.send(self.__RELY_MSG % msg) @@ -85,7 +41,7 @@ def __info_processing_loop(self, named_pipe: object) -> None: if (res := self.__exit_cmd(msg)): log.info(f'the result is {res}, background mode is exiting!') break - if (res := self.__data_handler(msg)): + if (res := self.data_handler(msg)): log.info(f'the result is {res}, the server is sending msg!') named_pipe.send(self.__RELY_FINISH) else: diff --git a/Source/JackFramework/Core/Mode/web_proc.py b/Source/JackFramework/Core/Mode/web_proc.py index cf9eb3c..9593599 100644 --- a/Source/JackFramework/Core/Mode/web_proc.py +++ b/Source/JackFramework/Core/Mode/web_proc.py @@ -2,10 +2,10 @@ from JackFramework.SysBasic.log_handler import LogHandler as log from JackFramework.Web.web_server import WebServer -from .test_proc import TestProc +from ._meta_interface_mode import InterfaceMode -class WebProc(TestProc): +class WebProc(InterfaceMode): """docstring for ClassName""" __WEB_HANDLER = None @@ -30,53 +30,6 @@ def __init_setting(self) -> object: graph.set_model_mode(False) graph.user_pretreatment(None) - def __save_result(self, outputs_data: list, supplement: list, msg: str) -> None: - _, data_manager = self._get_graph_and_data_manager - res = data_manager.user_save_test_data(outputs_data, supplement, msg) - log.info('jf server has saved the results') - return res - - def __try_load_data(self, msg: str) -> tuple: - res = True - try: - _, data_manager = self._get_graph_and_data_manager - batch_data = data_manager.user_load_test_data(msg) - except Exception: - log.error('Any error of load_test_data function or split in dataloader!') - res = False - batch_data = None - return res, batch_data - - def __try_exec_testing_proc(self, batch_data: list) -> tuple: - res, outputs_data, supplement = True, None, None - - try: - outputs_data, supplement = self._testing_data_proc(batch_data) - except Exception: - log.error('Any error of inference function in model!') - res = False - return res, outputs_data, supplement - - def __try_save_result(self, msg: str, outputs_data: list, supplement: list) -> bool: - res = True - res = self.__save_result(outputs_data, supplement, msg) - try: - res = self.__save_result(outputs_data, supplement, msg) - except Exception: - log.error('Any error of save_test_data function in dataloader!') - res = False - return res - - def data_handler(self, msg: str) -> bool: - res, batch_data = self.__try_load_data(msg) - if not res: - return res - res, outputs_data, supplement = self.__try_exec_testing_proc(batch_data) - if not res: - return res - res = self.__try_save_result(msg, outputs_data, supplement) - return res - def exec(self, rank: int = None) -> None: assert rank is None self._init_data_model_handler(rank) diff --git a/Source/JackFramework/Web/web_server.py b/Source/JackFramework/Web/web_server.py index b80a96f..d10fa43 100755 --- a/Source/JackFramework/Web/web_server.py +++ b/Source/JackFramework/Web/web_server.py @@ -29,13 +29,19 @@ def set_env(self) -> None: sys.path.append(str(Path(__file__).resolve().parent)) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jackframework_webserver.settings') - def start_web(self) -> None: - args = self.__args - django.setup() + def _run_web_server(self) -> None: call_command('makemigrations') call_command('migrate') - log.info(f'the web cmd: {args.web_cmd}') - execute_from_command_line(args.web_cmd.split(' ')) + log.info(f'the web cmd: {self.__args.web_cmd}') + execute_from_command_line(self.__args.web_cmd.split(' ')) + + def start_web(self) -> None: + try: + django.setup() + except Exception as e: + log.error(f"Failed to start web server: {e}") + raise + self._run_web_server() def exec(self) -> None: self.set_env() diff --git a/Source/JackFramework/Web/webapp/views.py b/Source/JackFramework/Web/webapp/views.py index 2ff4d39..8cd8f62 100644 --- a/Source/JackFramework/Web/webapp/views.py +++ b/Source/JackFramework/Web/webapp/views.py @@ -21,10 +21,8 @@ def __init__(self, args: object): @staticmethod def get_abs_path(root_folder: str, file_list: list) -> str: - res = [] - for file_name in file_list: - res.append(os.path.join(root_folder, os.path.basename(file_name))) - return res + return [os.path.join( + root_folder, os.path.basename(file_name)) for file_name in file_list] @staticmethod def create_folder() -> tuple: @@ -40,12 +38,9 @@ def create_folder() -> tuple: @staticmethod def receive_files(files: django.http.HttpRequest, uploads_folder: str) -> list: - files_path = [] - for f in files: - fs = FileSystemStorage(uploads_folder) - files_path.append(os.path.join( - settings.MEDIA_URL, Views.UPLOADS_FOLDER, fs.save(f.name, f))) - return files_path + fs = FileSystemStorage(uploads_folder) + return [os.path.join( + settings.MEDIA_URL, Views.UPLOADS_FOLDER, fs.save(f.name, f)) for f in files] @staticmethod def web_proc(files_path: list, uploads_folder: str, result_folder: str) -> list: @@ -57,23 +52,23 @@ def web_proc(files_path: list, uploads_folder: str, result_folder: str) -> list: @staticmethod def generate_results_path(res_files: list) -> list: - res_files_path = [] - for item in res_files: - res_files_path.append( - os.path.join(settings.MEDIA_URL, Views.RESULT_FOLDER, item)) - return res_files_path + return [os.path.join( + settings.MEDIA_URL, Views.RESULT_FOLDER, item) for item in res_files] @staticmethod def run(request: django.http.HttpRequest) -> django.shortcuts: files_path, res_files_path = None, None if request.method == 'POST': - files = request.FILES.getlist('images') - result_folder, uploads_folder = Views.create_folder() - files_path = Views.receive_files(files, uploads_folder) - res_files = Views.web_proc(files_path, uploads_folder, result_folder) + try: + files = request.FILES.getlist('images') + result_folder, uploads_folder = Views.create_folder() + files_path = Views.receive_files(files, uploads_folder) + res_files = Views.web_proc(files_path, uploads_folder, result_folder) - if res_files is not False: - res_files_path = Views.generate_results_path(res_files) + if res_files is not False: + res_files_path = Views.generate_results_path(res_files) + except Exception as e: + log.error(f"An error occurred during processing: {str(e)}") return render(request, 'webapp/index.html', {'images': files_path, 'res_images': res_files_path})