diff --git a/config/config.yaml b/config/config.yaml index 13da64e..cd0444a 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -18,7 +18,8 @@ replace_model: true # 是否替换现有模型 #---检测和推理配置---# # 检测和推理使用模型路径 -model_path: model/psychology.pth +model_path: /data/model/psychology.pth +default_model_path: model/psychology.pth #---样本特征---# # 标签名称 diff --git a/psy_api.py b/psy_api.py index dd1a636..32d7ee2 100644 --- a/psy_api.py +++ b/psy_api.py @@ -245,6 +245,14 @@ async def evaluate_model(request: Request, features_list: List[Features]): abs_model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), config["model_path"])) config["model_path"] = abs_model_path + # 检查模型文件是否存在,如果不存在则复制模型文件 + if not os.path.exists(config["model_path"]): + if not os.path.isabs(config['default_model_path']): + config['default_model_path'] = os.path.join(os.path.dirname(os.path.abspath(__file__)), config['default_model_path']) + os.makedirs(os.path.dirname(config["model_path"]), exist_ok=True) + shutil.copyfile(config['default_model_path'], config["model_path"]) + log_print(f"Model file not found. Copied default model from {config['default_model_path']} to {config['model_path']}") + # 配置日志 log_path = os.path.abspath(os.path.join(static_dir, f"evaluate_log_{now}.log")) update_log_handler(log_path) @@ -324,6 +332,14 @@ async def inference_model(request: Request, features_list: List[Features]): abs_model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), config["model_path"])) config["model_path"] = abs_model_path + # 检查模型文件是否存在,如果不存在则复制模型文件 + if not os.path.exists(config["model_path"]): + if not os.path.isabs(config['default_model_path']): + config['default_model_path'] = os.path.join(os.path.dirname(os.path.abspath(__file__)), config['default_model_path']) + os.makedirs(os.path.dirname(config["model_path"]), exist_ok=True) + shutil.copyfile(config['default_model_path'], config["model_path"]) + log_print(f"Model file not found. Copied default model from {config['default_model_path']} to {config['model_path']}") + # 特征和标签 X = feature_label_weighted[config['feature_names']].values