Skip to content

Commit

Permalink
refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiboRao committed Sep 21, 2024
1 parent c17b75e commit 5cea33c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 126 deletions.
6 changes: 2 additions & 4 deletions Source/JackFramework/Core/Graph/_user_dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
53 changes: 53 additions & 0 deletions Source/JackFramework/Core/Mode/_meta_interface_mode.py
Original file line number Diff line number Diff line change
@@ -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])
50 changes: 3 additions & 47 deletions Source/JackFramework/Core/Mode/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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:
Expand Down
51 changes: 2 additions & 49 deletions Source/JackFramework/Core/Mode/web_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
16 changes: 11 additions & 5 deletions Source/JackFramework/Web/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
37 changes: 16 additions & 21 deletions Source/JackFramework/Web/webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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})

0 comments on commit 5cea33c

Please sign in to comment.