update:取消从音频文件转成文字,提高响应速度

This commit is contained in:
hrz
2025-03-31 21:24:48 +08:00
parent e2da300e09
commit 9178faef6d
2 changed files with 12 additions and 7 deletions
@@ -38,7 +38,7 @@ async def checkWakeupWords(conn, text):
opus_packets, duration = conn.tts.audio_to_opus_data(file) opus_packets, duration = conn.tts.audio_to_opus_data(file)
text_hello = WAKEUP_CONFIG["text"] text_hello = WAKEUP_CONFIG["text"]
if not text_hello: if not text_hello:
text_hello, _ = await conn.asr.speech_to_text(opus_packets, conn.session_id) text_hello = text
conn.audio_play_queue.put((opus_packets, text_hello, 0)) conn.audio_play_queue.put((opus_packets, text_hello, 0))
if time.time() - WAKEUP_CONFIG["create_time"] > WAKEUP_CONFIG["refresh_time"]: if time.time() - WAKEUP_CONFIG["create_time"] > WAKEUP_CONFIG["refresh_time"]:
asyncio.create_task(wakeupWordsResponse(conn)) asyncio.create_task(wakeupWordsResponse(conn))
@@ -74,6 +74,7 @@ async def wakeupWordsResponse(conn):
if old_file is not None: if old_file is not None:
os.remove(old_file) os.remove(old_file)
"""将文件挪到"wakeup_words.mp3""" """将文件挪到"wakeup_words.mp3"""
shutil.move(tts_file, WAKEUP_CONFIG["dir"] + "my_" + WAKEUP_CONFIG["file_name"] + "." + file_type) shutil.move(tts_file, WAKEUP_CONFIG["dir"] + "my_" +
WAKEUP_CONFIG["file_name"] + "." + file_type)
WAKEUP_CONFIG["create_time"] = time.time() WAKEUP_CONFIG["create_time"] = time.time()
WAKEUP_CONFIG["text"] = result WAKEUP_CONFIG["text"] = result
@@ -6,6 +6,7 @@ import asyncio
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
async def _get_device_status(conn, device_name, device_type, property_name): async def _get_device_status(conn, device_name, device_type, property_name):
"""获取设备状态""" """获取设备状态"""
status = await get_iot_status(conn, device_type, property_name) status = await get_iot_status(conn, device_type, property_name)
@@ -13,6 +14,7 @@ async def _get_device_status(conn, device_name, device_type, property_name):
raise Exception(f"你的设备不支持{device_name}控制") raise Exception(f"你的设备不支持{device_name}控制")
return status return status
async def _set_device_property(conn, device_name, device_type, method_name, property_name, new_value=None, action=None, step=10): async def _set_device_property(conn, device_name, device_type, method_name, property_name, new_value=None, action=None, step=10):
"""设置设备属性""" """设置设备属性"""
current_value = await _get_device_status(conn, device_name, device_type, property_name) current_value = await _get_device_status(conn, device_name, device_type, property_name)
@@ -32,9 +34,11 @@ async def _set_device_property(conn, device_name, device_type, method_name, prop
await send_iot_conn(conn, device_type, method_name, {property_name: current_value}) await send_iot_conn(conn, device_type, method_name, {property_name: current_value})
return current_value return current_value
def _handle_device_action(conn, func, success_message, error_message, *args, **kwargs): def _handle_device_action(conn, func, success_message, error_message, *args, **kwargs):
"""处理设备操作的通用函数""" """处理设备操作的通用函数"""
future = asyncio.run_coroutine_threadsafe(func(conn, *args, **kwargs), conn.loop) future = asyncio.run_coroutine_threadsafe(
func(conn, *args, **kwargs), conn.loop)
try: try:
result = future.result() result = future.result()
logger.bind(tag=TAG).info(f"{success_message}: {result}") logger.bind(tag=TAG).info(f"{success_message}: {result}")
@@ -45,9 +49,8 @@ def _handle_device_action(conn, func, success_message, error_message, *args, **k
response = f"{error_message}: {e}" response = f"{error_message}: {e}"
return ActionResponse(action=Action.RESPONSE, result=None, response=response) return ActionResponse(action=Action.RESPONSE, result=None, response=response)
# 设备控制 # 设备控制
# TODO: 和自动注册的iot方法重复了,也许可以删掉这里手动定义的实现?
# 另外观察到function_call准确率不够高,可能是llm的问题
handle_device_function_desc = { handle_device_function_desc = {
"type": "function", "type": "function",
"function": { "function": {
@@ -80,6 +83,7 @@ handle_device_function_desc = {
} }
} }
@register_function('handle_device', handle_device_function_desc, ToolType.IOT_CTL) @register_function('handle_device', handle_device_function_desc, ToolType.IOT_CTL)
def handle_device(conn, device_type: str, action: str, value: int = None): def handle_device(conn, device_type: str, action: str, value: int = None):
if device_type == "Speaker": if device_type == "Speaker":
@@ -88,10 +92,10 @@ def handle_device(conn, device_type: str, action: str, value: int = None):
method_name, property_name, device_name = "SetBrightness", "brightness", "亮度" method_name, property_name, device_name = "SetBrightness", "brightness", "亮度"
else: else:
raise Exception(f"未识别的设备类型: {device_type}") raise Exception(f"未识别的设备类型: {device_type}")
if action not in ["get", "set", "raise", "lower"]: if action not in ["get", "set", "raise", "lower"]:
raise Exception(f"未识别的动作名称: {action}") raise Exception(f"未识别的动作名称: {action}")
if action == "get": if action == "get":
# get # get
return _handle_device_action( return _handle_device_action(