将打印信息,改由日志输出

This commit is contained in:
Downupanddownup 2024-04-26 16:18:40 +08:00
parent a291629438
commit 64cc2fd9d1
13 changed files with 105 additions and 62 deletions

View File

@ -1,5 +1,5 @@
from tools import my_utils from tools import my_utils
import glob from Ref_Audio_Selector.config_param.log_config import logger
import os 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: with open(output_file_path, 'w', encoding='utf-8') as file:
file.write(text) file.write(text)
except IOError as e: 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: 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__': if __name__ == '__main__':

View File

@ -1,15 +1,21 @@
# config.ini # config.ini
[Base] [Base]
# 函数时间消耗日志打印类型 file 打印到文件; close 关闭
time_log_print_type = file
# 函数时间消耗日志保存目录路径
time_log_print_dir = Ref_Audio_Selector/log/performance
# 参考音频目录 # 参考音频目录
reference_audio_dir = refer_audio reference_audio_dir = refer_audio
# 临时文件目录 # 临时文件目录
temp_dir = Ref_Audio_Selector/temp 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] [AudioSample]
# list转换待选参考音频目录 # list转换待选参考音频目录
list_to_convert_reference_audio_dir = refer_audio_all list_to_convert_reference_audio_dir = refer_audio_all

View File

@ -33,6 +33,9 @@ class ConfigManager:
def get_base(self, key): def get_base(self, key):
return self.config.get('Base', key) return self.config.get('Base', key)
def get_log(self, key):
return self.config.get('Log', key)
def get_audio_sample(self, key): def get_audio_sample(self, key):
return self.config.get('AudioSample', key) return self.config.get('AudioSample', key)

View File

@ -3,15 +3,21 @@ import Ref_Audio_Selector.config_param.config_manager as config_manager
config = config_manager.get_config() config = config_manager.get_config()
# [Base] # [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') reference_audio_dir = config.get_base('reference_audio_dir')
# 临时文件目录 # 临时文件目录
temp_dir = config.get_base('temp_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] # [AudioSample]
# list转换待选参考音频目录 # list转换待选参考音频目录
list_to_convert_reference_audio_dir = config.get_audio_sample('list_to_convert_reference_audio_dir') list_to_convert_reference_audio_dir = config.get_audio_sample('list_to_convert_reference_audio_dir')

View File

@ -3,37 +3,56 @@ import datetime
import Ref_Audio_Selector.config_param.config_params as params 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') 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 = logging.FileHandler(f"{params.log_dir}/{current_date}.log", mode='a', encoding='utf-8')
general_handler.setLevel(logging.INFO) # general_handler.setLevel(logging.INFO)
general_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') general_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
general_handler.setFormatter(general_formatter) 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( performance_handler = logging.FileHandler(
f"{params.time_log_print_dir}/{current_date}.log", mode='a', encoding='utf-8') 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_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
performance_handler.setFormatter(performance_formatter) performance_handler.setFormatter(performance_formatter)
# 配置一个常规的logger
general_logger = logging.getLogger('general')
general_logger.setLevel(logging.INFO)
general_logger.addHandler(general_handler)
# 配置一个专门用于性能监控的logger # 配置一个专门用于性能监控的logger
performance_logger = logging.getLogger('performance') performance_logger = logging.getLogger('performance')
performance_logger.setLevel(logging.INFO) performance_logger.setLevel(logging.INFO)
performance_logger.addHandler(performance_handler) performance_logger.addHandler(performance_handler)
# 配置根logger以防万一 return performance_logger
logging.basicConfig(level=logging.WARNING, handlers=[general_handler])
return general_logger, performance_logger
logger, p_logger = setup_logging() def setup_logging():
return create_general_logger(), create_performance_logger()
logger, p_logger = setup_logging()

View File

@ -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) # audio_similarity.convert_from_list(text_list_input, ref_audio_all)
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_convert_from_list_info = f"发生异常:{e}" text_convert_from_list_info = f"发生异常:{e}"
text_sample_dir = '' text_sample_dir = ''
return i18n(text_convert_from_list_info), 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' -c "{sample_dir}"'
cmd += f' -o {similarity_file}' cmd += f' -o {similarity_file}'
print(cmd) logger.info(cmd)
p_similarity = Popen(cmd, shell=True) p_similarity = Popen(cmd, shell=True)
p_similarity.wait() 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)) audio_similarity.sample(ref_audio_dir, similarity_list, int(text_subsection_num), int(text_sample_num))
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_sample_info = f"发生异常:{e}" text_sample_info = f"发生异常:{e}"
ref_audio_dir = '' ref_audio_dir = ''
text_model_inference_voice_dir = 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(), # audio_inference.generate_audio_files(url_composer, text_list, ref_audio_manager.get_ref_audio_list(),
# inference_dir) # inference_dir)
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_model_inference_info = f"发生异常:{e}" text_model_inference_info = f"发生异常:{e}"
text_asr_audio_dir = '' text_asr_audio_dir = ''
return i18n(text_model_inference_info), text_asr_audio_dir, 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_text_similarity_analysis_path = asr_file
text_asr_info = f"耗时:{time_consuming:0.1f}asr成功生成文件{asr_file}" text_asr_info = f"耗时:{time_consuming:0.1f}asr成功生成文件{asr_file}"
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_asr_info = f"发生异常:{e}" text_asr_info = f"发生异常:{e}"
text_text_similarity_analysis_path = '' text_text_similarity_analysis_path = ''
return i18n(text_asr_info), 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 += f' -l {asr_lang}'
cmd += " -p %s" % ("float16" if is_half == True else "float32") cmd += " -p %s" % ("float16" if is_half == True else "float32")
print(cmd) logger.info(cmd)
p_asr = Popen(cmd, shell=True) p_asr = Popen(cmd, shell=True)
p_asr.wait() p_asr.wait()
p_asr = None 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) # open_text_similarity_analysis(text_text_similarity_analysis_path, similarity_dir)
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_text_similarity_analysis_info = f"发生异常:{e}" text_text_similarity_analysis_info = f"发生异常:{e}"
return i18n(text_text_similarity_analysis_info) 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' -o "{output_dir}"'
cmd += f' -b {similarity_enlarge_boundary}' cmd += f' -b {similarity_enlarge_boundary}'
print(cmd) logger.info(cmd)
p_text_similarity = Popen(cmd, shell=True) p_text_similarity = Popen(cmd, shell=True)
p_text_similarity.wait() p_text_similarity.wait()
p_text_similarity = None 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}' text_similarity_audio_output_info = f'耗时:{time_consuming:0.1f}秒;相似度分析成功:生成目录{similarity_file_dir},文件{similarity_file}'
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_similarity_audio_output_info = f"发生异常:{e}" text_similarity_audio_output_info = f"发生异常:{e}"
return i18n(text_similarity_audio_output_info) 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}下," 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}个目录") f"text目录删除了{delete_text_wav_num}个参考音频emotion目录下删除了{delete_emotion_dir_num}个目录")
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_sync_ref_audio_info = f"发生异常:{e}" text_sync_ref_audio_info = f"发生异常:{e}"
return i18n(text_sync_ref_audio_info) 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}" text_create_config_info = f"耗时:{time_consuming:0.1f}秒;配置生成成功:生成文件{config_file}"
except Exception as e: except Exception as e:
traceback.print_exc() logger.error("发生异常: \n%s", traceback.format_exc())
text_create_config_info = f"发生异常:{e}" text_create_config_info = f"发生异常:{e}"
return i18n(text_create_config_info) return i18n(text_create_config_info)

View File

@ -4,6 +4,7 @@ import argparse
import os import os
import traceback import traceback
import Ref_Audio_Selector.config_param.config_params as params 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 Ref_Audio_Selector.common.time_util import timeit_decorator
from tqdm import tqdm from tqdm import tqdm
from funasr import AutoModel from funasr import AutoModel
@ -31,7 +32,7 @@ def only_asr(input_file):
text = model.generate(input=input_file)[0]["text"] text = model.generate(input=input_file)[0]["text"]
except: except:
text = '' text = ''
print(traceback.format_exc()) logger.error(traceback.format_exc())
return text 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}") output.append(f"{input_file_path}|{original_text}|{language.upper()}|{asr_text}")
except: except:
print(traceback.format_exc()) logger.error(traceback.format_exc())
# 创建或打开指定的输出目录 # 创建或打开指定的输出目录
output_folder = output_folder or "output/asr_opt" 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: with open(output_file_path, "w", encoding="utf-8") as f:
f.write("\n".join(output)) f.write("\n".join(output))
print(f"ASR 任务完成->标注文件路径: {output_file_path}\n") logger.info(f"ASR 任务完成->标注文件路径: {output_file_path}\n")
return output_file_path return output_file_path

View File

@ -4,6 +4,7 @@ import itertools
import Ref_Audio_Selector.config_param.config_params as params import Ref_Audio_Selector.config_param.config_params as params
from Ref_Audio_Selector.common.time_util import timeit_decorator from Ref_Audio_Selector.common.time_util import timeit_decorator
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse, quote from urllib.parse import urlparse, parse_qs, urlencode, urlunparse, quote
from Ref_Audio_Selector.config_param.log_config import logger
class URLComposer: class URLComposer:
@ -69,7 +70,7 @@ def safe_encode_query_params(original_url):
new_parsed_url = parsed_url._replace(query=new_query_string) new_parsed_url = parsed_url._replace(query=new_query_string)
encoded_url = urlunparse(new_parsed_url) encoded_url = urlunparse(new_parsed_url)
print(encoded_url) logger.info(encoded_url)
return 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): if os.path.exists(text_subdir_text_file_path) and os.path.exists(emotion_subdir_emotion_file_path):
has_generated_count += 1 has_generated_count += 1
print(f"进度: {has_generated_count}/{all_count}") logger.info(f"进度: {has_generated_count}/{all_count}")
continue continue
if url_composer.is_emotion(): 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) f.write(audio_bytes)
has_generated_count += 1 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): def inference_audio_from_api(url):

View File

@ -2,6 +2,7 @@ import os
import shutil import shutil
import random import random
import librosa import librosa
from Ref_Audio_Selector.config_param.log_config import logger
def check_audio_duration(path, min_duration=3, max_duration=10): 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 return False
except Exception as e: except Exception as e:
print(f"无法打开或处理音频文件:{e}") logger.error(f"无法打开或处理音频文件:{e}")
return None return None
@ -33,7 +34,7 @@ def convert_from_list(list_file, output_dir):
for line in lines: for line in lines:
parts = line.strip().split('|') parts = line.strip().split('|')
if len(parts) != 4: if len(parts) != 4:
print(f"Line format incorrect: {line}") logger.error(f"Line format incorrect: {line}")
continue continue
audio_path, _, _, transcription = parts audio_path, _, _, transcription = parts
@ -46,27 +47,27 @@ def convert_from_list(list_file, output_dir):
# 如果目标文件已存在,不要覆盖 # 如果目标文件已存在,不要覆盖
if os.path.exists(new_path): if os.path.exists(new_path):
print(f"File already exists: {new_path}") logger.info(f"File already exists: {new_path}")
continue continue
try: try:
# 检查音频文件是否存在 # 检查音频文件是否存在
if not os.path.exists(audio_path): 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 continue
if check_audio_duration(audio_path): if check_audio_duration(audio_path):
# 复制音频文件到output目录并重命名 # 复制音频文件到output目录并重命名
shutil.copy2(audio_path, new_path) 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: else:
print(f"File skipped due to duration: {audio_path}") logger.info(f"File skipped due to duration: {audio_path}")
except Exception as e: except Exception as e:
print(f"An error occurred while processing: {audio_path}") logger.error(f"An error occurred while processing: {audio_path}")
print(e) logger.error(e)
print("Processing complete.") logger.info("Processing complete.")
def sample(output_audio_dir, similarity_list, subsection_num, sample_num): 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)) dst_path = os.path.join(subdir_path, os.path.basename(src_path))
shutil.copyfile(src_path, dst_path) shutil.copyfile(src_path, dst_path)
print("Sampling completed.") logger.info("Sampling completed.")
def parse_similarity_file(file_path): 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) shutil.copyfile(item['wav_path'], new_path)
print("已完成复制和重命名操作。") logger.info("已完成复制和重命名操作。")
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -2,6 +2,7 @@ import os
import shutil import shutil
import Ref_Audio_Selector.common.common as common import Ref_Audio_Selector.common.common as common
import Ref_Audio_Selector.config_param.config_params as params 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): 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" wav_file_name = f"{emotion_tag}.wav"
file_path = os.path.join(root, wav_file_name) file_path = os.path.join(root, wav_file_name)
if not os.path.exists(file_path): if not os.path.exists(file_path):
print(f"Deleting file: {file_path}") logger.info(f"Deleting file: {file_path}")
try: try:
os.remove(file_path) os.remove(file_path)
count += 1 count += 1
except Exception as e: except Exception as e:
print(f"Error deleting file {file_path}: {e}") logger.error(f"Error deleting file {file_path}: {e}")
return count return count
@ -39,13 +40,13 @@ def delete_emotion_subdirectories(emotion_dir, emotions_list):
# 检查emotion子目录是否存在 # 检查emotion子目录是否存在
if not os.path.isdir(folder_path): if not os.path.isdir(folder_path):
print(f"Deleting directory: {folder_path}") logger.info(f"Deleting directory: {folder_path}")
try: try:
# 使用shutil.rmtree删除整个子目录及其内容 # 使用shutil.rmtree删除整个子目录及其内容
shutil.rmtree(folder_path) shutil.rmtree(folder_path)
count += 1 count += 1
except Exception as e: except Exception as e:
print(f"Error deleting directory {folder_path}: {e}") logger.error(f"Error deleting directory {folder_path}: {e}")
return count return count

View File

@ -5,6 +5,7 @@ import torchaudio.transforms as T
import platform import platform
import Ref_Audio_Selector.config_param.config_params as params import Ref_Audio_Selector.config_param.config_params as params
from Ref_Audio_Selector.common.time_util import timeit_decorator 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 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 reference_audio_16k = reference_audio_path
# Step 2: 用参考音频依次比较音频目录下的每个音频,获取相似度分数及对应路径 # Step 2: 用参考音频依次比较音频目录下的每个音频,获取相似度分数及对应路径
all_count = len(comparison_audio_paths)
has_processed_count = 0
similarity_scores = [] similarity_scores = []
for audio_path in comparison_audio_paths: for audio_path in comparison_audio_paths:
score = sv_pipeline([reference_audio_16k, audio_path])['score'] 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, 'score': score,
'path': audio_path '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: 根据相似度分数降序排列 # Step 3: 根据相似度分数降序排列
similarity_scores.sort(key=lambda x: x['score'], reverse=True) similarity_scores.sort(key=lambda x: x['score'], reverse=True)

View File

@ -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.tool.text_comparison.text_comparison as text_comparison
import Ref_Audio_Selector.config_param.config_params as params import Ref_Audio_Selector.config_param.config_params as params
import Ref_Audio_Selector.common.common as common import Ref_Audio_Selector.common.common as common
from Ref_Audio_Selector.config_param.log_config import logger
def parse_asr_file(file_path): 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['similarity_score'] = similarity_score
item['original_score'] = original_score item['original_score'] = original_score
has_been_processed_count += 1 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 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') 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) format_list_to_emotion(original_text_detail_list, original_text_detail_file)
print('文本相似度分析完成。') logger.info('文本相似度分析完成。')
def parse_arguments(): def parse_arguments():

View File

@ -2,7 +2,7 @@ import os
import torch import torch
from transformers import AutoTokenizer, AutoModel from transformers import AutoTokenizer, AutoModel
from scipy.spatial.distance import cosine 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 = os.environ.get(
"bert_path", "GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large" "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 # Set device to GPU if available, else CPU
device = torch.device("cuda" if torch.cuda.is_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) tokenizer = AutoTokenizer.from_pretrained(bert_path)
model = AutoModel.from_pretrained(bert_path).to(device) model = AutoModel.from_pretrained(bert_path).to(device)