liumaolin commited on
Commit
5c0e715
·
1 Parent(s): bfefeb3

Add system utilities and initialize core modules: implement `get_system_language` and `get_system_info`, update API startup with system defaults, and integrate ASR, LLM, and speech modules for enhanced functionality.

Browse files
src/VoiceDialogue/api/app.py CHANGED
@@ -5,6 +5,13 @@ from typing import Dict, Any
5
  from fastapi import FastAPI, HTTPException, APIRouter
6
  from fastapi.middleware.cors import CORSMiddleware
7
 
 
 
 
 
 
 
 
8
  from .middleware.logging import LoggingMiddleware
9
  from .middleware.rate_limit import RateLimitMiddleware
10
  from .routes import tts_routes, asr_routes
@@ -26,6 +33,8 @@ async def lifespan(app: FastAPI):
26
  # 启动时的初始化
27
  logger.info("正在启动VoiceDialogue API服务...")
28
 
 
 
29
  # 初始化TTS配置注册表
30
  try:
31
  from services.audio.audio_generator import tts_config_registry
@@ -35,6 +44,30 @@ async def lifespan(app: FastAPI):
35
  logger.error(f"TTS配置加载失败: {e}")
36
  app_state["tts_configs_loaded"] = False
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  app_state["system_running"] = True
39
  logger.info("VoiceDialogue API服务启动完成")
40
  yield
 
5
  from fastapi import FastAPI, HTTPException, APIRouter
6
  from fastapi.middleware.cors import CORSMiddleware
7
 
8
+ from services.audio.audio_player import AudioStreamPlayer
9
+ from services.core.constants import (
10
+ transcribed_text_queue, text_input_queue, audio_output_queue, audio_frames_queue, user_voice_queue
11
+ )
12
+ from services.speech import SpeechStateMonitor, ASRWorker
13
+ from services.text.text_generator import LLMResponseGenerator
14
+ from utils import get_system_language
15
  from .middleware.logging import LoggingMiddleware
16
  from .middleware.rate_limit import RateLimitMiddleware
17
  from .routes import tts_routes, asr_routes
 
33
  # 启动时的初始化
34
  logger.info("正在启动VoiceDialogue API服务...")
35
 
36
+ system_default_language = get_system_language()
37
+
38
  # 初始化TTS配置注册表
39
  try:
40
  from services.audio.audio_generator import tts_config_registry
 
44
  logger.error(f"TTS配置加载失败: {e}")
45
  app_state["tts_configs_loaded"] = False
46
 
47
+ speech_monitor = SpeechStateMonitor(
48
+ audio_frame_queue=audio_frames_queue,
49
+ user_voice_queue=user_voice_queue,
50
+ )
51
+ speech_monitor.start()
52
+ app_state['speech_monitor'] = speech_monitor
53
+
54
+ asr_worker = ASRWorker(
55
+ user_voice_queue=user_voice_queue, transcribed_text_queue=transcribed_text_queue,
56
+ language=system_default_language
57
+ )
58
+ asr_worker.start()
59
+ app_state['asr_worker'] = asr_worker
60
+
61
+ llm_generator = LLMResponseGenerator(
62
+ user_question_queue=transcribed_text_queue, generated_answer_queue=text_input_queue
63
+ )
64
+ llm_generator.start()
65
+ app_state['llm_generator'] = llm_generator
66
+
67
+ audio_player = AudioStreamPlayer(audio_playing_queue=audio_output_queue)
68
+ audio_player.start()
69
+ app_state['audio_player'] = audio_player
70
+
71
  app_state["system_running"] = True
72
  logger.info("VoiceDialogue API服务启动完成")
73
  yield
src/VoiceDialogue/services/speech/__init__.py CHANGED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from .asr_service import ASRWorker
2
+ from .speech_monitor import SpeechStateMonitor
3
+
4
+ __all__ = ['ASRWorker', 'SpeechStateMonitor']
src/VoiceDialogue/utils/__init__.py CHANGED
@@ -1,26 +1,26 @@
 
1
  from .download_utils import (
2
  download_model_from_huggingface, download_file_from_huggingface, check_file_exists_on_huggingface,
3
  download_lora_from_huggingface, download_civitai_file
4
  )
5
  from .strings import remove_emojis
6
- from .cache import LRUCacheDict
7
-
8
 
9
  # 导入HParams类,解决moyoyo_tts的序列化问题
10
  try:
11
  import sys
12
  from pathlib import Path
13
-
14
  # 添加third_party路径
15
  current_dir = Path(__file__).parent
16
  project_root = current_dir.parent.parent.parent
17
  third_party_path = project_root / "third_party"
18
-
19
  if str(third_party_path) not in sys.path:
20
  sys.path.insert(0, str(third_party_path))
21
-
22
  from moyoyo_tts.utils import HParams
23
-
24
  except ImportError:
25
  # 如果导入失败,创建一个简单的HParams类
26
  class HParams:
@@ -54,7 +54,6 @@ except ImportError:
54
  def __repr__(self):
55
  return self.__dict__.__repr__()
56
 
57
-
58
  __all__ = (
59
  'remove_emojis',
60
  'download_model_from_huggingface',
@@ -62,4 +61,7 @@ __all__ = (
62
  'check_file_exists_on_huggingface',
63
  'download_lora_from_huggingface',
64
  'download_civitai_file',
 
 
 
65
  )
 
1
+ from .cache import LRUCacheDict
2
  from .download_utils import (
3
  download_model_from_huggingface, download_file_from_huggingface, check_file_exists_on_huggingface,
4
  download_lora_from_huggingface, download_civitai_file
5
  )
6
  from .strings import remove_emojis
7
+ from .system import get_system_language, get_system_info
 
8
 
9
  # 导入HParams类,解决moyoyo_tts的序列化问题
10
  try:
11
  import sys
12
  from pathlib import Path
13
+
14
  # 添加third_party路径
15
  current_dir = Path(__file__).parent
16
  project_root = current_dir.parent.parent.parent
17
  third_party_path = project_root / "third_party"
18
+
19
  if str(third_party_path) not in sys.path:
20
  sys.path.insert(0, str(third_party_path))
21
+
22
  from moyoyo_tts.utils import HParams
23
+
24
  except ImportError:
25
  # 如果导入失败,创建一个简单的HParams类
26
  class HParams:
 
54
  def __repr__(self):
55
  return self.__dict__.__repr__()
56
 
 
57
  __all__ = (
58
  'remove_emojis',
59
  'download_model_from_huggingface',
 
61
  'check_file_exists_on_huggingface',
62
  'download_lora_from_huggingface',
63
  'download_civitai_file',
64
+ 'LRUCacheDict',
65
+ 'get_system_language',
66
+ 'get_system_info',
67
  )
src/VoiceDialogue/utils/system.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """系统相关工具函数"""
2
+
3
+ import locale
4
+ import os
5
+ import platform
6
+ from typing import Literal
7
+
8
+ __all__ = ('get_system_language', 'get_system_info')
9
+
10
+
11
+ def get_system_language() -> Literal['zh', 'en']:
12
+ """
13
+ 获取系统当前语言设置
14
+
15
+ Returns:
16
+ str: 返回 'zh' 或 'en',默认为 'zh'
17
+ """
18
+ try:
19
+ # 首先尝试从环境变量获取
20
+ lang = os.environ.get('LANG', '')
21
+ if not lang:
22
+ lang = os.environ.get('LANGUAGE', '')
23
+ if not lang:
24
+ lang = os.environ.get('LC_ALL', '')
25
+ if not lang:
26
+ lang = os.environ.get('LC_MESSAGES', '')
27
+
28
+ # 如果环境变量为空,尝试使用locale模块
29
+ if not lang:
30
+ try:
31
+ lang, _ = locale.getdefaultlocale()
32
+ if not lang:
33
+ lang = locale.getlocale()[0]
34
+ except (ValueError, AttributeError):
35
+ lang = None
36
+
37
+ # 解析语言代码
38
+ if lang:
39
+ lang = lang.lower()
40
+ if 'zh' in lang or 'chinese' in lang:
41
+ return 'zh'
42
+ elif 'en' in lang or 'english' in lang:
43
+ return 'en'
44
+
45
+ except Exception:
46
+ # 如果所有方法都失败,返回默认值
47
+ pass
48
+
49
+ # 默认返回中文
50
+ return 'zh'
51
+
52
+
53
+ def get_system_info() -> dict:
54
+ """
55
+ 获取系统信息
56
+
57
+ Returns:
58
+ dict: 包含系统信息的字典
59
+ """
60
+ return {
61
+ 'platform': platform.system(),
62
+ 'platform_version': platform.version(),
63
+ 'architecture': platform.architecture()[0],
64
+ 'machine': platform.machine(),
65
+ 'processor': platform.processor(),
66
+ 'language': get_system_language(),
67
+ 'python_version': platform.python_version(),
68
+ }