From 64cc2fd9d1effee00d796506af6affa237f35e4d Mon Sep 17 00:00:00 2001 From: Downupanddownup Date: Fri, 26 Apr 2024 16:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=89=93=E5=8D=B0=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E7=94=B1=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ref_Audio_Selector/common/common.py | 6 +-- Ref_Audio_Selector/config.ini | 14 ++++-- .../config_param/config_manager.py | 3 ++ .../config_param/config_params.py | 14 ++++-- Ref_Audio_Selector/config_param/log_config.py | 47 +++++++++++++------ .../ref_audio_selector_webui.py | 22 ++++----- .../tool/asr/funasr_asr_multi_level_dir.py | 7 +-- Ref_Audio_Selector/tool/audio_inference.py | 7 +-- Ref_Audio_Selector/tool/audio_similarity.py | 23 ++++----- .../tool/delete_inference_with_ref.py | 9 ++-- .../speaker_verification/voice_similarity.py | 6 ++- .../tool/text_comparison/asr_text_process.py | 5 +- .../tool/text_comparison/text_comparison.py | 4 +- 13 files changed, 105 insertions(+), 62 deletions(-) diff --git a/Ref_Audio_Selector/common/common.py b/Ref_Audio_Selector/common/common.py index b051b22..0a94a6d 100644 --- a/Ref_Audio_Selector/common/common.py +++ b/Ref_Audio_Selector/common/common.py @@ -1,5 +1,5 @@ from tools import my_utils -import glob +from Ref_Audio_Selector.config_param.log_config import logger import os @@ -102,9 +102,9 @@ def write_text_to_file(text, output_file_path): with open(output_file_path, 'w', encoding='utf-8') as file: file.write(text) except IOError as e: - print(f"Error occurred while writing to the file: {e}") + logger.info(f"Error occurred while writing to the file: {e}") else: - print(f"Text successfully written to file: {output_file_path}") + logger.info(f"Text successfully written to file: {output_file_path}") if __name__ == '__main__': diff --git a/Ref_Audio_Selector/config.ini b/Ref_Audio_Selector/config.ini index 26b47e9..7507853 100644 --- a/Ref_Audio_Selector/config.ini +++ b/Ref_Audio_Selector/config.ini @@ -1,15 +1,21 @@ # config.ini [Base] -# 函数时间消耗日志打印类型 file 打印到文件; close 关闭 -time_log_print_type = file -# 函数时间消耗日志保存目录路径 -time_log_print_dir = Ref_Audio_Selector/log/performance # 参考音频目录 reference_audio_dir = refer_audio # 临时文件目录 temp_dir = Ref_Audio_Selector/temp +[Log] +# 日志保存目录路径 +log_dir = Ref_Audio_Selector/log/general +# 日志级别 CRITICAL、FATAL、ERROR、WARNING、WARN、INFO、DEBUG、NOTSET、 +log_level = INFO +# 函数时间消耗日志打印类型 file 打印到文件; close 关闭 +time_log_print_type = file +# 函数时间消耗日志保存目录路径 +time_log_print_dir = Ref_Audio_Selector/log/performance + [AudioSample] # list转换待选参考音频目录 list_to_convert_reference_audio_dir = refer_audio_all diff --git a/Ref_Audio_Selector/config_param/config_manager.py b/Ref_Audio_Selector/config_param/config_manager.py index 4183f2a..ea7af4a 100644 --- a/Ref_Audio_Selector/config_param/config_manager.py +++ b/Ref_Audio_Selector/config_param/config_manager.py @@ -33,6 +33,9 @@ class ConfigManager: def get_base(self, key): return self.config.get('Base', key) + def get_log(self, key): + return self.config.get('Log', key) + def get_audio_sample(self, key): return self.config.get('AudioSample', key) diff --git a/Ref_Audio_Selector/config_param/config_params.py b/Ref_Audio_Selector/config_param/config_params.py index 9c585d4..de12d72 100644 --- a/Ref_Audio_Selector/config_param/config_params.py +++ b/Ref_Audio_Selector/config_param/config_params.py @@ -3,15 +3,21 @@ import Ref_Audio_Selector.config_param.config_manager as config_manager config = config_manager.get_config() # [Base] -# 函数时间消耗日志打印类型 file 打印到文件; close 关闭 -time_log_print_type = config.get_base('time_log_print_type') -# 函数时间消耗日志保存目录路径 -time_log_print_dir = config.get_base('time_log_print_dir') # 参考音频目录 reference_audio_dir = config.get_base('reference_audio_dir') # 临时文件目录 temp_dir = config.get_base('temp_dir') +# [Log] +# 日志保存目录路径 +log_dir = config.get_log('log_dir') +# 日志级别 CRITICAL、FATAL、ERROR、WARNING、WARN、INFO、DEBUG、NOTSET、 +log_level = config.get_log('log_level') +# 函数时间消耗日志打印类型 file 打印到文件; close 关闭 +time_log_print_type = config.get_log('time_log_print_type') +# 函数时间消耗日志保存目录路径 +time_log_print_dir = config.get_log('time_log_print_dir') + # [AudioSample] # list转换待选参考音频目录 list_to_convert_reference_audio_dir = config.get_audio_sample('list_to_convert_reference_audio_dir') diff --git a/Ref_Audio_Selector/config_param/log_config.py b/Ref_Audio_Selector/config_param/log_config.py index 249eba5..b10e5c9 100644 --- a/Ref_Audio_Selector/config_param/log_config.py +++ b/Ref_Audio_Selector/config_param/log_config.py @@ -3,37 +3,56 @@ import datetime import Ref_Audio_Selector.config_param.config_params as params -def setup_logging(): +def create_general_logger(): # 获取当前日期,用于文件名和日志内容 current_date = datetime.datetime.now().strftime('%Y-%m-%d') + # 创建一个用于控制台输出的处理器,并设置日志级别 + console_handler = logging.StreamHandler() + # console_handler.setLevel(logging.INFO) + # 可以设置控制台输出的格式 + console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + console_handler.setFormatter(console_formatter) + # 创建一个用于常规日志的处理器 - general_handler = logging.FileHandler('general.log', mode='a', encoding='utf-8') - general_handler.setLevel(logging.INFO) + general_handler = logging.FileHandler(f"{params.log_dir}/{current_date}.log", mode='a', encoding='utf-8') + # general_handler.setLevel(logging.INFO) general_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') general_handler.setFormatter(general_formatter) + # 配置一个常规的logger + general_logger = logging.getLogger('general') + level = logging.getLevelName(params.log_level) + general_logger.setLevel(level) + general_logger.addHandler(console_handler) + general_logger.addHandler(general_handler) + + # 配置根logger,以防万一 + logging.basicConfig(level=logging.WARNING, handlers=[general_handler]) + + return general_logger + + +def create_performance_logger(): + # 获取当前日期,用于文件名和日志内容 + current_date = datetime.datetime.now().strftime('%Y-%m-%d') # 创建一个专用于性能监控日志的处理器 performance_handler = logging.FileHandler( f"{params.time_log_print_dir}/{current_date}.log", mode='a', encoding='utf-8') - performance_handler.setLevel(logging.INFO) + # performance_handler.setLevel(logging.INFO) performance_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') performance_handler.setFormatter(performance_formatter) - # 配置一个常规的logger - general_logger = logging.getLogger('general') - general_logger.setLevel(logging.INFO) - general_logger.addHandler(general_handler) - # 配置一个专门用于性能监控的logger performance_logger = logging.getLogger('performance') performance_logger.setLevel(logging.INFO) performance_logger.addHandler(performance_handler) - # 配置根logger,以防万一 - logging.basicConfig(level=logging.WARNING, handlers=[general_handler]) - - return general_logger, performance_logger + return performance_logger -logger, p_logger = setup_logging() \ No newline at end of file +def setup_logging(): + return create_general_logger(), create_performance_logger() + + +logger, p_logger = setup_logging() diff --git a/Ref_Audio_Selector/ref_audio_selector_webui.py b/Ref_Audio_Selector/ref_audio_selector_webui.py index e3cead2..4569dd2 100644 --- a/Ref_Audio_Selector/ref_audio_selector_webui.py +++ b/Ref_Audio_Selector/ref_audio_selector_webui.py @@ -60,7 +60,7 @@ def convert_from_list(text_work_space_dir, text_role, text_list_input): # audio_similarity.convert_from_list(text_list_input, ref_audio_all) except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_convert_from_list_info = f"发生异常:{e}" text_sample_dir = '' return i18n(text_convert_from_list_info), text_sample_dir @@ -83,7 +83,7 @@ def start_similarity_analysis(work_space_dir, sample_dir, base_voice_path, need_ cmd += f' -c "{sample_dir}"' cmd += f' -o {similarity_file}' - print(cmd) + logger.info(cmd) p_similarity = Popen(cmd, shell=True) p_similarity.wait() @@ -136,7 +136,7 @@ def sample(text_work_space_dir, text_role, text_sample_dir, text_base_voice_path audio_similarity.sample(ref_audio_dir, similarity_list, int(text_subsection_num), int(text_sample_num)) except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_sample_info = f"发生异常:{e}" ref_audio_dir = '' text_model_inference_voice_dir = ref_audio_dir @@ -191,7 +191,7 @@ def model_inference(text_work_space_dir, text_role, text_model_inference_voice_d # audio_inference.generate_audio_files(url_composer, text_list, ref_audio_manager.get_ref_audio_list(), # inference_dir) except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_model_inference_info = f"发生异常:{e}" text_asr_audio_dir = '' return i18n(text_model_inference_info), text_asr_audio_dir, text_asr_audio_dir @@ -226,7 +226,7 @@ def asr(text_work_space_dir, text_role, text_asr_audio_dir, dropdown_asr_model, text_text_similarity_analysis_path = asr_file text_asr_info = f"耗时:{time_consuming:0.1f}秒;asr成功:生成文件{asr_file}" except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_asr_info = f"发生异常:{e}" text_text_similarity_analysis_path = '' return i18n(text_asr_info), text_text_similarity_analysis_path @@ -248,7 +248,7 @@ def open_asr(asr_inp_dir, asr_opt_dir, asr_model, asr_model_size, asr_lang): cmd += f' -l {asr_lang}' cmd += " -p %s" % ("float16" if is_half == True else "float32") - print(cmd) + logger.info(cmd) p_asr = Popen(cmd, shell=True) p_asr.wait() p_asr = None @@ -284,7 +284,7 @@ def text_similarity_analysis(text_work_space_dir, text_role, # open_text_similarity_analysis(text_text_similarity_analysis_path, similarity_dir) except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_text_similarity_analysis_info = f"发生异常:{e}" return i18n(text_text_similarity_analysis_info) @@ -297,7 +297,7 @@ def open_text_similarity_analysis(asr_file_path, output_dir, similarity_enlarge_ cmd += f' -o "{output_dir}"' cmd += f' -b {similarity_enlarge_boundary}' - print(cmd) + logger.info(cmd) p_text_similarity = Popen(cmd, shell=True) p_text_similarity.wait() p_text_similarity = None @@ -335,7 +335,7 @@ def similarity_audio_output(text_work_space_dir, text_role, text_base_audio_path text_similarity_audio_output_info = f'耗时:{time_consuming:0.1f}秒;相似度分析成功:生成目录{similarity_file_dir},文件{similarity_file}' except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_similarity_audio_output_info = f"发生异常:{e}" return i18n(text_similarity_audio_output_info) @@ -361,7 +361,7 @@ def sync_ref_audio(text_work_space_dir, text_role, text_sync_ref_audio_dir, text_sync_ref_audio_info = (f"耗时:{time_consuming:0.1f}秒;推理音频目录{text_sync_inference_audio_dir}下," f"text目录删除了{delete_text_wav_num}个参考音频,emotion目录下,删除了{delete_emotion_dir_num}个目录") except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_sync_ref_audio_info = f"发生异常:{e}" return i18n(text_sync_ref_audio_info) @@ -392,7 +392,7 @@ def create_config(text_work_space_dir, text_role, text_template, text_sync_ref_a text_create_config_info = f"耗时:{time_consuming:0.1f}秒;配置生成成功:生成文件{config_file}" except Exception as e: - traceback.print_exc() + logger.error("发生异常: \n%s", traceback.format_exc()) text_create_config_info = f"发生异常:{e}" return i18n(text_create_config_info) diff --git a/Ref_Audio_Selector/tool/asr/funasr_asr_multi_level_dir.py b/Ref_Audio_Selector/tool/asr/funasr_asr_multi_level_dir.py index 43ec04d..22fbfc1 100644 --- a/Ref_Audio_Selector/tool/asr/funasr_asr_multi_level_dir.py +++ b/Ref_Audio_Selector/tool/asr/funasr_asr_multi_level_dir.py @@ -4,6 +4,7 @@ import argparse import os import traceback import Ref_Audio_Selector.config_param.config_params as params +from Ref_Audio_Selector.config_param.log_config import logger from Ref_Audio_Selector.common.time_util import timeit_decorator from tqdm import tqdm from funasr import AutoModel @@ -31,7 +32,7 @@ def only_asr(input_file): text = model.generate(input=input_file)[0]["text"] except: text = '' - print(traceback.format_exc()) + logger.error(traceback.format_exc()) return text @@ -54,7 +55,7 @@ def execute_asr_multi_level_dir(input_folder, output_folder, model_size, languag output.append(f"{input_file_path}|{original_text}|{language.upper()}|{asr_text}") except: - print(traceback.format_exc()) + logger.error(traceback.format_exc()) # 创建或打开指定的输出目录 output_folder = output_folder or "output/asr_opt" @@ -67,7 +68,7 @@ def execute_asr_multi_level_dir(input_folder, output_folder, model_size, languag # 将输出写入文件 with open(output_file_path, "w", encoding="utf-8") as f: f.write("\n".join(output)) - print(f"ASR 任务完成->标注文件路径: {output_file_path}\n") + logger.info(f"ASR 任务完成->标注文件路径: {output_file_path}\n") return output_file_path diff --git a/Ref_Audio_Selector/tool/audio_inference.py b/Ref_Audio_Selector/tool/audio_inference.py index 39cc2c6..7f6e3cc 100644 --- a/Ref_Audio_Selector/tool/audio_inference.py +++ b/Ref_Audio_Selector/tool/audio_inference.py @@ -4,6 +4,7 @@ import itertools import Ref_Audio_Selector.config_param.config_params as params from Ref_Audio_Selector.common.time_util import timeit_decorator from urllib.parse import urlparse, parse_qs, urlencode, urlunparse, quote +from Ref_Audio_Selector.config_param.log_config import logger class URLComposer: @@ -69,7 +70,7 @@ def safe_encode_query_params(original_url): new_parsed_url = parsed_url._replace(query=new_query_string) encoded_url = urlunparse(new_parsed_url) - print(encoded_url) + logger.info(encoded_url) return encoded_url @@ -107,7 +108,7 @@ def generate_audio_files(url_composer, text_list, emotion_list, output_dir_path) # 检查是否已经存在对应的音频文件,如果存在则跳过 if os.path.exists(text_subdir_text_file_path) and os.path.exists(emotion_subdir_emotion_file_path): has_generated_count += 1 - print(f"进度: {has_generated_count}/{all_count}") + logger.info(f"进度: {has_generated_count}/{all_count}") continue if url_composer.is_emotion(): @@ -124,7 +125,7 @@ def generate_audio_files(url_composer, text_list, emotion_list, output_dir_path) f.write(audio_bytes) has_generated_count += 1 - print(f"进度: {has_generated_count}/{all_count}") + logger.info(f"进度: {has_generated_count}/{all_count}") def inference_audio_from_api(url): diff --git a/Ref_Audio_Selector/tool/audio_similarity.py b/Ref_Audio_Selector/tool/audio_similarity.py index 658251f..8517c64 100644 --- a/Ref_Audio_Selector/tool/audio_similarity.py +++ b/Ref_Audio_Selector/tool/audio_similarity.py @@ -2,6 +2,7 @@ import os import shutil import random import librosa +from Ref_Audio_Selector.config_param.log_config import logger def check_audio_duration(path, min_duration=3, max_duration=10): @@ -17,7 +18,7 @@ def check_audio_duration(path, min_duration=3, max_duration=10): return False except Exception as e: - print(f"无法打开或处理音频文件:{e}") + logger.error(f"无法打开或处理音频文件:{e}") return None @@ -33,7 +34,7 @@ def convert_from_list(list_file, output_dir): for line in lines: parts = line.strip().split('|') if len(parts) != 4: - print(f"Line format incorrect: {line}") + logger.error(f"Line format incorrect: {line}") continue audio_path, _, _, transcription = parts @@ -46,27 +47,27 @@ def convert_from_list(list_file, output_dir): # 如果目标文件已存在,不要覆盖 if os.path.exists(new_path): - print(f"File already exists: {new_path}") + logger.info(f"File already exists: {new_path}") continue try: # 检查音频文件是否存在 if not os.path.exists(audio_path): - print(f"Audio file does not exist: {audio_path}") + logger.info(f"Audio file does not exist: {audio_path}") continue if check_audio_duration(audio_path): # 复制音频文件到output目录并重命名 shutil.copy2(audio_path, new_path) - print(f"File copied and renamed to: {new_path}") + logger.info(f"File copied and renamed to: {new_path}") else: - print(f"File skipped due to duration: {audio_path}") + logger.info(f"File skipped due to duration: {audio_path}") except Exception as e: - print(f"An error occurred while processing: {audio_path}") - print(e) + logger.error(f"An error occurred while processing: {audio_path}") + logger.error(e) - print("Processing complete.") + logger.info("Processing complete.") def sample(output_audio_dir, similarity_list, subsection_num, sample_num): @@ -101,7 +102,7 @@ def sample(output_audio_dir, similarity_list, subsection_num, sample_num): dst_path = os.path.join(subdir_path, os.path.basename(src_path)) shutil.copyfile(src_path, dst_path) - print("Sampling completed.") + logger.info("Sampling completed.") def parse_similarity_file(file_path): @@ -150,7 +151,7 @@ def copy_and_move(output_audio_directory, similarity_scores): # 复制文件到新目录 shutil.copyfile(item['wav_path'], new_path) - print("已完成复制和重命名操作。") + logger.info("已完成复制和重命名操作。") if __name__ == '__main__': diff --git a/Ref_Audio_Selector/tool/delete_inference_with_ref.py b/Ref_Audio_Selector/tool/delete_inference_with_ref.py index 4d7f175..5231dde 100644 --- a/Ref_Audio_Selector/tool/delete_inference_with_ref.py +++ b/Ref_Audio_Selector/tool/delete_inference_with_ref.py @@ -2,6 +2,7 @@ import os import shutil import Ref_Audio_Selector.common.common as common import Ref_Audio_Selector.config_param.config_params as params +from Ref_Audio_Selector.config_param.log_config import logger def remove_matching_audio_files_in_text_dir(text_dir, emotions_list): @@ -12,12 +13,12 @@ def remove_matching_audio_files_in_text_dir(text_dir, emotions_list): wav_file_name = f"{emotion_tag}.wav" file_path = os.path.join(root, wav_file_name) if not os.path.exists(file_path): - print(f"Deleting file: {file_path}") + logger.info(f"Deleting file: {file_path}") try: os.remove(file_path) count += 1 except Exception as e: - print(f"Error deleting file {file_path}: {e}") + logger.error(f"Error deleting file {file_path}: {e}") return count @@ -39,13 +40,13 @@ def delete_emotion_subdirectories(emotion_dir, emotions_list): # 检查emotion子目录是否存在 if not os.path.isdir(folder_path): - print(f"Deleting directory: {folder_path}") + logger.info(f"Deleting directory: {folder_path}") try: # 使用shutil.rmtree删除整个子目录及其内容 shutil.rmtree(folder_path) count += 1 except Exception as e: - print(f"Error deleting directory {folder_path}: {e}") + logger.error(f"Error deleting directory {folder_path}: {e}") return count diff --git a/Ref_Audio_Selector/tool/speaker_verification/voice_similarity.py b/Ref_Audio_Selector/tool/speaker_verification/voice_similarity.py index 568c5e2..df9d7ee 100644 --- a/Ref_Audio_Selector/tool/speaker_verification/voice_similarity.py +++ b/Ref_Audio_Selector/tool/speaker_verification/voice_similarity.py @@ -5,6 +5,7 @@ import torchaudio.transforms as T import platform import Ref_Audio_Selector.config_param.config_params as params from Ref_Audio_Selector.common.time_util import timeit_decorator +from Ref_Audio_Selector.config_param.log_config import logger from modelscope.pipelines import pipeline @@ -32,6 +33,8 @@ def compare_audio_and_generate_report(reference_audio_path, comparison_dir_path, reference_audio_16k = reference_audio_path # Step 2: 用参考音频依次比较音频目录下的每个音频,获取相似度分数及对应路径 + all_count = len(comparison_audio_paths) + has_processed_count = 0 similarity_scores = [] for audio_path in comparison_audio_paths: score = sv_pipeline([reference_audio_16k, audio_path])['score'] @@ -39,7 +42,8 @@ def compare_audio_and_generate_report(reference_audio_path, comparison_dir_path, 'score': score, 'path': audio_path }) - print(f'similarity score: {score}, path: {audio_path}') + has_processed_count += 1 + logger.info(f'进度:{has_processed_count}/{all_count}') # Step 3: 根据相似度分数降序排列 similarity_scores.sort(key=lambda x: x['score'], reverse=True) diff --git a/Ref_Audio_Selector/tool/text_comparison/asr_text_process.py b/Ref_Audio_Selector/tool/text_comparison/asr_text_process.py index 585df90..96ce218 100644 --- a/Ref_Audio_Selector/tool/text_comparison/asr_text_process.py +++ b/Ref_Audio_Selector/tool/text_comparison/asr_text_process.py @@ -6,6 +6,7 @@ from Ref_Audio_Selector.common.time_util import timeit_decorator import Ref_Audio_Selector.tool.text_comparison.text_comparison as text_comparison import Ref_Audio_Selector.config_param.config_params as params import Ref_Audio_Selector.common.common as common +from Ref_Audio_Selector.config_param.log_config import logger def parse_asr_file(file_path): @@ -42,7 +43,7 @@ def calculate_similarity_and_append_to_list(input_list, boundary): item['similarity_score'] = similarity_score item['original_score'] = original_score has_been_processed_count += 1 - print(f'进度:{has_been_processed_count}/{all_count}') + logger.info(f'进度:{has_been_processed_count}/{all_count}') return input_list @@ -135,7 +136,7 @@ def process(asr_file_path, output_dir, similarity_enlarge_boundary): original_text_detail_file = os.path.join(output_dir, f'{params.text_similarity_by_text_detail_filename}.txt') format_list_to_emotion(original_text_detail_list, original_text_detail_file) - print('文本相似度分析完成。') + logger.info('文本相似度分析完成。') def parse_arguments(): diff --git a/Ref_Audio_Selector/tool/text_comparison/text_comparison.py b/Ref_Audio_Selector/tool/text_comparison/text_comparison.py index 156fa53..2c4a530 100644 --- a/Ref_Audio_Selector/tool/text_comparison/text_comparison.py +++ b/Ref_Audio_Selector/tool/text_comparison/text_comparison.py @@ -2,7 +2,7 @@ import os import torch from transformers import AutoTokenizer, AutoModel from scipy.spatial.distance import cosine -import math +from Ref_Audio_Selector.config_param.log_config import logger bert_path = os.environ.get( "bert_path", "GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large" @@ -11,7 +11,7 @@ bert_path = os.environ.get( # Set device to GPU if available, else CPU device = torch.device("cuda" if torch.cuda.is_available() else "cpu") -print(f'使用计算设备: {device}') +logger.info(f'使用计算设备: {device}') tokenizer = AutoTokenizer.from_pretrained(bert_path) model = AutoModel.from_pretrained(bert_path).to(device)