From c5180df9ac1579c63f887f12f126831cd98ee119 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Fri, 14 Feb 2025 00:54:59 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 3 ++- .gitignore | 1 + config/settings.py | 9 +++++++-- core/providers/llm/base.py | 3 ++- core/providers/llm/dify/dify.py | 3 ++- core/providers/llm/openai/openai.py | 5 ++++- core/providers/tts/base.py | 14 ++++---------- core/providers/tts/doubao.py | 3 ++- core/providers/tts/edge.py | 3 ++- core/utils/tts.py | 12 +----------- core/utils/util.py | 12 +++++++----- 11 files changed, 34 insertions(+), 34 deletions(-) diff --git a/.dockerignore b/.dockerignore index 5873f95e..3fb61154 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,4 +7,5 @@ docs/ tmp/ LICENSE README.md -README_en.md \ No newline at end of file +README_en.md +.config.yaml \ No newline at end of file diff --git a/.gitignore b/.gitignore index fa607b72..9586fb9f 100644 --- a/.gitignore +++ b/.gitignore @@ -140,3 +140,4 @@ cython_debug/ model.pt tmp .DS_Store +.config.yaml diff --git a/config/settings.py b/config/settings.py index caa418dd..9bdab1fe 100644 --- a/config/settings.py +++ b/config/settings.py @@ -1,10 +1,15 @@ +import os import argparse -from core.utils.util import read_config +from core.utils.util import read_config, get_project_dir def load_config(): """加载配置文件""" parser = argparse.ArgumentParser(description="Server configuration") - parser.add_argument("--config_path", type=str, default="config.yaml") + default_config_file = "config.yaml" + # 判断是否存在私有的配置文件 + if os.path.exists(get_project_dir() + "." + default_config_file): + default_config_file = "." + default_config_file + parser.add_argument("--config_path", type=str, default=default_config_file) args = parser.parse_args() return read_config(args.config_path) diff --git a/core/providers/llm/base.py b/core/providers/llm/base.py index ac8a807c..94167967 100644 --- a/core/providers/llm/base.py +++ b/core/providers/llm/base.py @@ -1,7 +1,8 @@ from abc import ABC, abstractmethod + class LLMProviderBase(ABC): @abstractmethod def response(self, session_id, dialogue): """LLM response generator""" - pass \ No newline at end of file + pass diff --git a/core/providers/llm/dify/dify.py b/core/providers/llm/dify/dify.py index bd259095..48f069ee 100644 --- a/core/providers/llm/dify/dify.py +++ b/core/providers/llm/dify/dify.py @@ -5,6 +5,7 @@ from core.providers.llm.base import LLMProviderBase logger = logging.getLogger(__name__) + class LLMProvider(LLMProviderBase): def __init__(self, config): self.api_key = config["api_key"] @@ -35,4 +36,4 @@ class LLMProvider(LLMProviderBase): except Exception as e: logger.error(f"Error in response generation: {e}") - yield "【服务响应异常】" \ No newline at end of file + yield "【服务响应异常】" diff --git a/core/providers/llm/openai/openai.py b/core/providers/llm/openai/openai.py index ef2074b6..a0e0f33f 100644 --- a/core/providers/llm/openai/openai.py +++ b/core/providers/llm/openai/openai.py @@ -4,6 +4,7 @@ from core.providers.llm.base import LLMProviderBase logger = logging.getLogger(__name__) + class LLMProvider(LLMProviderBase): def __init__(self, config): self.model_name = config.get("model_name") @@ -12,6 +13,8 @@ class LLMProvider(LLMProviderBase): self.base_url = config.get("base_url") else: self.base_url = config.get("url") + if "你" in self.api_key: + logger.error("你还没配置LLM的密钥,请在配置文件中配置密钥,否则无法正常工作") self.client = openai.OpenAI(api_key=self.api_key, base_url=self.base_url) def response(self, session_id, dialogue): @@ -29,4 +32,4 @@ class LLMProvider(LLMProviderBase): if content: # 仅在content非空时生成 yield content except Exception as e: - logger.error(f"Error in response generation: {e}") \ No newline at end of file + logger.error(f"Error in response generation: {e}") diff --git a/core/providers/tts/base.py b/core/providers/tts/base.py index 62389f02..ba5cdbdb 100644 --- a/core/providers/tts/base.py +++ b/core/providers/tts/base.py @@ -1,20 +1,14 @@ import asyncio import logging import os -import json -import uuid -import base64 -# from datetime import datetime -# import edge_tts import numpy as np import opuslib -# import requests -# from core.utils.util import read_config, get_project_dir from pydub import AudioSegment from abc import ABC, abstractmethod logger = logging.getLogger(__name__) + class TTSProviderBase(ABC): def __init__(self, config, delete_audio_file): self.delete_audio_file = delete_audio_file @@ -34,8 +28,8 @@ class TTSProviderBase(ABC): max_repeat_time = max_repeat_time - 1 logger.error(f"语音生成失败: {text}:{tmp_file},再试{max_repeat_time}次") - if max_repeat_time>0: - logger.info(f"语音生成成功: {text}:{tmp_file},重试{5-max_repeat_time}次") + if max_repeat_time > 0: + logger.info(f"语音生成成功: {text}:{tmp_file},重试{5 - max_repeat_time}次") return tmp_file except Exception as e: @@ -86,4 +80,4 @@ class TTSProviderBase(ABC): opus_data = encoder.encode(np_frame.tobytes(), frame_size) opus_datas.append(opus_data) - return opus_datas, duration \ No newline at end of file + return opus_datas, duration diff --git a/core/providers/tts/doubao.py b/core/providers/tts/doubao.py index 073f2291..f6eca974 100644 --- a/core/providers/tts/doubao.py +++ b/core/providers/tts/doubao.py @@ -6,6 +6,7 @@ import requests from datetime import datetime from core.providers.tts.base import TTSProviderBase + class TTSProvider(TTSProviderBase): def __init__(self, config, delete_audio_file): super().__init__(config, delete_audio_file) @@ -52,4 +53,4 @@ class TTSProvider(TTSProviderBase): if "data" in resp.json(): data = resp.json()["data"] file_to_save = open(output_file, "wb") - file_to_save.write(base64.b64decode(data)) \ No newline at end of file + file_to_save.write(base64.b64decode(data)) diff --git a/core/providers/tts/edge.py b/core/providers/tts/edge.py index a0e28864..3c02597d 100644 --- a/core/providers/tts/edge.py +++ b/core/providers/tts/edge.py @@ -4,6 +4,7 @@ import edge_tts from datetime import datetime from core.providers.tts.base import TTSProviderBase + class TTSProvider(TTSProviderBase): def __init__(self, config, delete_audio_file): super().__init__(config, delete_audio_file) @@ -14,4 +15,4 @@ class TTSProvider(TTSProviderBase): async def text_to_speak(self, text, output_file): communicate = edge_tts.Communicate(text, voice=self.voice) # Use your preferred voice - await communicate.save(output_file) \ No newline at end of file + await communicate.save(output_file) diff --git a/core/utils/tts.py b/core/utils/tts.py index 25914cde..9803a4da 100644 --- a/core/utils/tts.py +++ b/core/utils/tts.py @@ -1,19 +1,9 @@ -import asyncio -import logging import os import sys -import json -import uuid -import base64 +import logging import importlib from datetime import datetime -import edge_tts -import numpy as np -import opuslib -import requests from core.utils.util import read_config, get_project_dir -from pydub import AudioSegment -from abc import ABC, abstractmethod logger = logging.getLogger(__name__) diff --git a/core/utils/util.py b/core/utils/util.py index 37e23c21..31ea5ca6 100644 --- a/core/utils/util.py +++ b/core/utils/util.py @@ -1,8 +1,7 @@ -import yaml -import unicodedata -import socket import os import json +import yaml +import socket def get_project_dir(): @@ -41,6 +40,7 @@ def is_segment(tokens): else: return False + def is_punctuation_or_emoji(char): """检查字符是否为空格、指定标点或表情符号""" # 定义需要去除的中英文标点(包括全角/半角) @@ -49,7 +49,7 @@ def is_punctuation_or_emoji(char): '。', '.', # 中文句号 + 英文句号 '!', '!', # 中文感叹号 + 英文感叹号 '-', '-', # 英文连字符 + 中文全角横线 - '、' # 中文顿号 + '、' # 中文顿号 } if char.isspace() or char in punctuation_set: return True @@ -63,6 +63,7 @@ def is_punctuation_or_emoji(char): ] return any(start <= code_point <= end for start, end in emoji_ranges) + def get_string_no_punctuation_or_emoji(s): """去除字符串首尾的空格、标点符号和表情符号""" chars = list(s) @@ -74,7 +75,8 @@ def get_string_no_punctuation_or_emoji(s): end = len(chars) - 1 while end >= start and is_punctuation_or_emoji(chars[end]): end -= 1 - return ''.join(chars[start:end+1]) + return ''.join(chars[start:end + 1]) + def remove_punctuation_and_length(text): # 全角符号和半角符号的Unicode范围