Merge branch 'xinnan-tech:main' into main

This commit is contained in:
Albert Wang 2025-02-13 15:57:01 +08:00 committed by GitHub
commit fa78287a71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 92 additions and 80 deletions

View file

@ -12,7 +12,13 @@
本项目需要配合esp32硬件设备使用,如果童鞋们已经购买了esp32相关硬件,且成功对接虾哥部署的后端,并且想自己独立搭建 本项目需要配合esp32硬件设备使用,如果童鞋们已经购买了esp32相关硬件,且成功对接虾哥部署的后端,并且想自己独立搭建
`xiaozhi-esp32`后端服务,可学习本项目。 `xiaozhi-esp32`后端服务,可学习本项目。
想看使用效果,请猛戳这个视频[小智esp32连接自己的后台模型](https://www.bilibili.com/video/BV1FMFyejExX) 想看使用效果,请猛戳这个视频
<a href="https://www.bilibili.com/video/BV1FMFyejExX">
<picture>
<img alt="小智esp32连接自己的后台模型" src="docs/images/demo.png" />
</picture>
</a>
要想完整体验本项目,需要以下总体步骤: 要想完整体验本项目,需要以下总体步骤:
@ -22,6 +28,10 @@
- 下载`xiaozhi-esp32`项目,把`接口地址`修改成本项目地址,然后编译,把新固件烧录到硬件设备上。 - 下载`xiaozhi-esp32`项目,把`接口地址`修改成本项目地址,然后编译,把新固件烧录到硬件设备上。
- 启动设备,查看电脑或服务器的控制台,如果看到日志,说明成功连到本项目的接口了。 - 启动设备,查看电脑或服务器的控制台,如果看到日志,说明成功连到本项目的接口了。
## 警告
本项目成立时间较短,还未通过网络安全测评,请勿在生产环境中使用。
## 功能清单 ## 功能清单
## 已实现 ## 已实现
@ -57,7 +67,7 @@
## 方式一:docker快速部署 ## 方式一:docker快速部署
如果你的电脑是`arm`架构的电脑,请暂时使用`方式二:本地源码运行`。`arm`架构需要再等几天,因为目前还没有适配arm架构的镜像。 docker镜像已支持x86架构、arm64架构的CPU,支持在国产操作系统上运行。
### 1. 安装docker ### 1. 安装docker
@ -166,7 +176,7 @@ docker rmi ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest
### 1.安装基础环境 ### 1.安装基础环境
本项目使用`conda`管理依赖环境,安装好后开始执行以下命令。 本项目使用`conda`管理依赖环境,安装好后,开始执行以下命令。
``` ```
conda remove -n xiaozhi-esp32-server --all -y conda remove -n xiaozhi-esp32-server --all -y
@ -174,7 +184,7 @@ conda create -n xiaozhi-esp32-server python=3.10 -y
conda activate xiaozhi-esp32-server conda activate xiaozhi-esp32-server
``` ```
如果你的电脑是windows,执行: 执行以上命令后, 如果你的电脑是Windows或Mac,执行下面的语句:
``` ```
conda activate xiaozhi-esp32-server conda activate xiaozhi-esp32-server
@ -182,7 +192,7 @@ conda install conda-forge::libopus
conda install conda-forge::ffmpeg conda install conda-forge::ffmpeg
``` ```
如果你的电脑是ubuntu,执行: 如果你的电脑是ubuntu,执行下面的语句:
``` ```
apt-get install libopus0 ffmpeg apt-get install libopus0 ffmpeg
@ -336,7 +346,7 @@ https://espressif.github.io/esp-launchpad/
## 1、TTS 经常失败,经常超时 ## 1、TTS 经常失败,经常超时
建议:如果`EdgeTTS`经常失败,先检查一下是否用了梯子。 建议:如果`EdgeTTS`经常失败,先检查一下是否用了梯子,如果用了梯子,请把梯子关了试试。
如果用的是`火山引擎的豆包TTS`经常失败,最好使用付费版本,因为他们的测试版本只有2个并发。 如果用的是`火山引擎的豆包TTS`经常失败,最好使用付费版本,因为他们的测试版本只有2个并发。
## 2、我想通过小智控制电灯、空调、远程开关机等操作。 ## 2、我想通过小智控制电灯、空调、远程开关机等操作。

View file

@ -14,8 +14,13 @@ This project requires compatible esp32 hardware devices. If you have purchased e
Xiage's deployed backend, and wish to independently set up the `xiaozhi-esp32` backend service, this project is for Xiage's deployed backend, and wish to independently set up the `xiaozhi-esp32` backend service, this project is for
you. you.
To see a demo, watch this To see a demo, watch this video:
video: [Xiaozhi ESP32 Connecting to Custom Backend Model](https://www.bilibili.com/video/BV1FMFyejExX)
<a href="https://www.bilibili.com/video/BV1FMFyejExX">
<picture>
<img alt="小智esp32连接自己的后台模型" src="docs/images/demo.png" />
</picture>
</a>
To fully experience this project, follow these steps: To fully experience this project, follow these steps:
@ -27,6 +32,11 @@ To fully experience this project, follow these steps:
firmware to your device. firmware to your device.
- Start the device and check your server console logs to verify successful connection. - Start the device and check your server console logs to verify successful connection.
## Warning
This project has been established for a short time and has not passed the network security assessment, so please do not
use it in the production environment.
## Feature List ## Feature List
## Implemented ## Implemented
@ -64,8 +74,8 @@ is recommended to run the local source code.
## Method 1: Quick deployment of docker ## Method 1: Quick deployment of docker
If your computer is an `arm` architecture computer, please use `Method 2: Run local source code`. The `arm` architecture The docker image has supported the CPU of x86 architecture and arm64 architecture, and supports running on Chinese
needs to wait for a few more days, because there is currently no mirror that is suitable for the arm architecture. operating systems.
1. Install docker 1. Install docker
@ -175,8 +185,7 @@ is very useful, and it is required to `compile esp32 firmware` later.
### 1.Install Prerequisites ### 1.Install Prerequisites
This project uses `conda` to manage the dependent environment, and after installation, it starts executing the following This project uses 'conda' to manage dependencies, and after installation, start executing the following commands:
commands.
``` ```
conda remove -n xiaozhi-esp32-server --all -y conda remove -n xiaozhi-esp32-server --all -y
@ -184,7 +193,7 @@ conda create -n xiaozhi-esp32-server python=3.10 -y
conda activate xiaozhi-esp32-server conda activate xiaozhi-esp32-server
``` ```
If your computer is Windows, execute: After executing the above command, if your computer is Windows or Mac, execute the following statement:
``` ```
conda activate xiaozhi-esp32-server conda activate xiaozhi-esp32-server
@ -192,7 +201,7 @@ conda install conda-forge::libopus
conda install conda-forge::ffmpeg conda install conda-forge::ffmpeg
``` ```
If your computer is ubuntu, execute: If your computer is ubuntu, execute the following statement:
``` ```
apt-get install libopus0 ffmpeg apt-get install libopus0 ffmpeg

View file

@ -153,12 +153,17 @@ class ConnectionHandler:
text = None text = None
try: try:
future = self.tts_queue.get() future = self.tts_queue.get()
if future is None:
continue
text = None text = None
try: try:
self.logger.debug("正在处理TTS任务...")
tts_file, text = future.result(timeout=10) tts_file, text = future.result(timeout=10)
self.logger.debug(f"TTS文件生成完毕,文件路径: {tts_file}")
if os.path.exists(tts_file): if os.path.exists(tts_file):
opus_datas, duration = self.tts.wav_to_opus_data(tts_file) opus_datas, duration = self.tts.wav_to_opus_data(tts_file)
else: else:
self.logger.error(f"TTS文件不存在: {tts_file}")
opus_datas = [] opus_datas = []
duration = 0 duration = 0
except TimeoutError: except TimeoutError:
@ -175,6 +180,7 @@ class ConnectionHandler:
if self.tts.delete_audio_file and os.path.exists(tts_file): if self.tts.delete_audio_file and os.path.exists(tts_file):
os.remove(tts_file) os.remove(tts_file)
except Exception as e: except Exception as e:
self.logger.error(f"TTS任务处理错误: {e}")
self.clearSpeakStatus() self.clearSpeakStatus()
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})), self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})),
@ -190,7 +196,7 @@ class ConnectionHandler:
if tts_file is None: if tts_file is None:
self.logger.error(f"tts转换失败,{text}") self.logger.error(f"tts转换失败,{text}")
return None return None
self.logger.debug(f"TTS 文件生成完毕") self.logger.debug(f"TTS 文件生成完毕: {tts_file}")
return tts_file, text return tts_file, text
def clearSpeakStatus(self): def clearSpeakStatus(self):

View file

@ -36,6 +36,57 @@ async def handleAudioMessage(conn, audio):
conn.reset_vad_states() conn.reset_vad_states()
async def startToChat(conn, text): async def startToChat(conn, text):
# 异步发送 stt 信息
asyncio.create_task(
schedule_with_interrupt(0, send_stt_message(conn, text))
)
conn.executor.submit(conn.chat, text)
async def sendAudioMessage(conn, audios, duration, text):
base_delay = conn.tts_duration
# 发送 tts.start
if text == conn.tts_first_text:
logger.info(f"发送第一段语音: {text}")
conn.tts_start_speak_time = time.time()
# 发送 sentence_start(每个音频文件之前发送一次)
await send_tts_message(conn, "sentence_start", text)
conn.tts_duration += duration
# 发送音频数据
for idx, opus_packet in enumerate(audios):
await conn.websocket.send(opus_packet)
# 每个音频文件发送结束时,发送 sentence_end
if idx == len(audios) - 1:
await send_tts_message(conn, "sentence_end", text)
if conn.llm_finish_task and text == conn.tts_last_text:
stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time)
stop_task = asyncio.create_task(
schedule_with_interrupt(stop_duration, send_tts_message(conn, 'stop'))
)
conn.scheduled_tasks.append(stop_task)
async def send_tts_message(conn, state, text=None):
"""发送 TTS 状态消息"""
message = {
"type": "tts",
"state": state,
"session_id": conn.session_id
}
if text is not None:
message["text"] = text
await conn.websocket.send(json.dumps(message))
if state == "stop":
conn.clearSpeakStatus()
async def send_stt_message(conn, text):
"""发送 STT 状态消息"""
stt_text = get_string_no_punctuation_or_emoji(text) stt_text = get_string_no_punctuation_or_emoji(text)
await conn.websocket.send(json.dumps({ await conn.websocket.send(json.dumps({
"type": "stt", "type": "stt",
@ -49,66 +100,7 @@ async def startToChat(conn, text):
"emotion": "happy", "emotion": "happy",
"session_id": conn.session_id} "session_id": conn.session_id}
)) ))
conn.executor.submit(conn.chat, text) await send_tts_message(conn, "start")
async def sendAudioMessage(conn, audios, duration, text):
base_delay = conn.tts_duration
if text == conn.tts_first_text:
logger.info(f"发送第一段语音: {text}")
conn.tts_start_speak_time = time.time()
await conn.websocket.send(json.dumps({
"type": "tts",
"state": "start",
"session_id": conn.session_id
}))
# 调度文字显示任务
text_task = asyncio.create_task(
schedule_with_interrupt(
base_delay - 0.5,
send_sentence_start(conn, text)
)
)
conn.scheduled_tasks.append(text_task)
conn.tts_duration = conn.tts_duration + duration
# 发送音频数据
for opus_packet in audios:
await conn.websocket.send(opus_packet)
if conn.llm_finish_task and text == conn.tts_last_text:
stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time)
stop_task = asyncio.create_task(
schedule_with_interrupt(stop_duration, send_tts_stop(conn, text))
)
conn.scheduled_tasks.append(stop_task)
async def send_sentence_start(conn, text):
await conn.websocket.send(json.dumps({
"type": "tts",
"state": "sentence_start",
"text": text,
"session_id": conn.session_id
}))
async def send_tts_stop(conn, text):
await conn.websocket.send(json.dumps({
"type": "tts",
"state": "sentence_end",
"text": text,
"session_id": conn.session_id
}))
await conn.websocket.send(json.dumps({
"type": "tts",
"state": "stop",
"session_id": conn.session_id
}))
conn.clearSpeakStatus()
async def schedule_with_interrupt(delay, coro): async def schedule_with_interrupt(delay, coro):

View file

@ -30,11 +30,6 @@ docker push ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-amd64
docker tag xiaozhi-esp32-server:local ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-arm64 docker tag xiaozhi-esp32-server:local ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-arm64
docker push ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-arm64 docker push ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-arm64
# 合并版本号
docker manifest create ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:1.0.0 ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-amd64 ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-arm64 --amend
docker manifest inspect ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:1.0.0
docker manifest push ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:1.0.0
# 推送最新版本 # 推送最新版本
docker manifest rm ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest docker manifest rm ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest
docker manifest create ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-amd64 ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-arm64 --amend docker manifest create ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-amd64 ccr.ccs.tencentyun.com/xinnan/xiaozhi-esp32-server:latest-arm64 --amend

BIN
docs/images/demo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 142 KiB

After

Width:  |  Height:  |  Size: 481 KiB

BIN
img.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB