22import logging
33import torch
44from pathlib import Path
5+ import sys
56from torch import nn
7+ import os
68from torch .utils .data import DataLoader
79from ideeplc .model import MyNet
810from ideeplc .config import get_config
1517# Logging configuration
1618LOGGER = logging .getLogger (__name__ )
1719
20+ def setup_logging ():
21+ log_dir = Path ("logs" )
22+ log_dir .mkdir (exist_ok = True )
1823
24+ file_handler = logging .FileHandler (log_dir / "ideeplc.log" )
25+ console_handler = logging .StreamHandler ()
26+
27+ logging .basicConfig (
28+ level = logging .INFO ,
29+ format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' ,
30+ handlers = [file_handler , console_handler ]
31+ )
32+
33+ setup_logging ()
1934def get_model_save_path ():
2035 """
2136 Determines the correct directory and filename for saving the model.
@@ -26,10 +41,18 @@ def get_model_save_path():
2641 tuple: (model_save_path, model_dir)
2742 """
2843 timestamp = datetime .datetime .now ().strftime ("%m%d" )
29- model_dir = Path (f"ideeplc/models/{ timestamp } " )
30- pretrained_path = files ("ideeplc.models" ).joinpath ("pretrained_model.pth" )
44+ model_dir = Path ("ideeplc/models" ) / timestamp
3145 model_name = "pretrained_model.pth"
32- return model_dir / model_name , model_dir , pretrained_path
46+
47+ if getattr (sys , "frozen" , False ):
48+ # If frozen (PyInstaller)
49+ base_path = Path (sys ._MEIPASS )
50+ model_path = base_path / "ideeplc" / "models" / model_name
51+ else :
52+ # If normal Python environment
53+ model_path = files ("ideeplc.models" ).joinpath (model_name )
54+
55+ return model_path , model_dir , model_path
3356
3457
3558def main (args ):
0 commit comments