本地部署 xiaozhi-esp32-server

安装 Anaconda

下载安装

清华大学开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

启动安装程序时,以管理员身份运行

/images/documents/本地部署xiaozhi-esp32-server/1.png
(图1)
/images/documents/本地部署xiaozhi-esp32-server/2.png
(图2)

安装的过程中会非常的慢

环境变量

path 环境变量添加以下内容:

  • D:\Anaconda
  • D:\Anaconda\Scripts
  • D:\Anaconda\Library\bin
  • D:\Anaconda\Library\mingw-w64\bin
  • D:\Anaconda\Library\usr\bin
  • D:\Anaconda\pkgs

测试是否配置成功 conda --version

部署 xiaozhi-sep32-server

安装基础环境

/images/documents/本地部署xiaozhi-esp32-server/3.png
(图3)

运行之后,如果你能看到命令行窗口前面有一个( base )字样,说明你成功进入了 conda 环境。那么你就可以执行以下命令了。

/images/documents/本地部署xiaozhi-esp32-server/4.png
(图4)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
conda remove -n xiaozhi-esp32-server --all -y
conda create -n xiaozhi-esp32-server python=3.10 -y
conda activate xiaozhi-esp32-server

# 添加清华源通道
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge

conda install libopus -y
conda install ffmpeg -y

安装本项目依赖

xiaozhi-esp32-server 地址,点击 Download ZIP 按钮下载项目压缩包。

此时它的名字可能叫 xiaozhi-esp32-server-main,你需要把它重命名成 xiaozhi-esp32-server,在这个文件里,进入到 main 文件夹,再进入到 xiaozhi-server,好了请记住这个目录 xiaozhi-server

1
2
3
4
5
6
# 继续使用conda环境
conda activate xiaozhi-esp32-server
# 进入到你的项目根目录,再进入main/xiaozhi-server
cd main/xiaozhi-server
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip install -r requirements.txt

下载语音识别模型文件

到项目作者提供的连接处下载,下载后把 model.pt 文件放在 models/SenseVoiceSmall 目录下。

配置项目文件

如果你的 xiaozhi-server 目录没有 data,你需要创建 data 目录。 如果你的 data 下面没有 .config.yaml 文件,你可以把源码目录下的 config.yaml 文件复制一份,重命名为 .config.yaml

运行项目

1
2
3
# 确保在xiaozhi-server目录下执行
conda activate xiaozhi-esp32-server
python app.py

报错处理

错误一

Exception: Could not find Opus library. Make sure it is installed.

/images/documents/本地部署xiaozhi-esp32-server/5.png
(图5)

1.找到报错文件 {python下目录}\lib\site-packages\opuslib_next\api\__init__.py 并打开 __init__.py

我的位置是在:E:\software\anaconda3\envs\xiaozhi-esp32-server\Lib\site-packages\opuslib_next\api\__init__.py

/images/documents/本地部署xiaozhi-esp32-server/6.png
(图6)

2.下载 opus:https://github.com/ShiftMediaProject/opus/releases

/images/documents/本地部署xiaozhi-esp32-server/7.png
(图7)

3.解压找到 x64 文件下的 opus.dll,并复制路径

/images/documents/本地部署xiaozhi-esp32-server/8.png
(图8)

4.添加代码至 步骤1 的红框位置处保存文件

1
2
if lib_location is None:
    lib_location = r'D:\ProgramData\Anaconda3\envs\xiaozhi-esp32-server\Lib\site-packages\libopus_v1.4_msvc17\bin\x64\opus.dll'

错误二

提示 ffmpeg is not installed 没有安装,重新进行 conda install conda-forge::ffmpeg -y 依然无果

1.打开 Dpwnload FFmpeg 官网 https://ffmpeg.org/download.html,选择安装包 Windows builds from gyan.dev

/images/documents/本地部署xiaozhi-esp32-server/9.png
(图9)

2.下滑找到 release bulids 部分,选择 ffmpeg-7.0.2-essentials_build.zip

/images/documents/本地部署xiaozhi-esp32-server/10.png
(图10)

3.下载完成后,解压缩得到 FFmpeg 文件夹。

解压后的文件夹中应包含以下目录:

  • bin:FFmpeg 可执行文件所在的文件夹,运行 FFmpeg 的所有命令都需通过此目录下的文件。
  • doc:文档资料。
  • presets:预设的格式和编码方案。

进入 bin 目录,可以看到 FFmpeg 的三个核心可执行文件:

/images/documents/本地部署xiaozhi-esp32-server/11.png
(图11)

4.将上述三个 exe 文件复制到 conda xiaozhi-esp32-server 环境下,例:

C:\Users\用户名\.conda\envs\xiaozhi-esp32-server\Library\bin

/images/documents/本地部署xiaozhi-esp32-server/12.png
(图12)

5.重新进入到你的项目目录,执行以下命令

1
2
3
4
5
conda activate xiaozhi-esp32-server

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

pip install -r requirements.txt

PyCharm 调试 xiaozhi-esp32-server

Anaconda 配置好虚拟环境后,需要将环境添加进 PyCharm 中。

/images/documents/本地部署xiaozhi-esp32-server/13.png
(图13)

点击文件夹图标

/images/documents/本地部署xiaozhi-esp32-server/14.png
(图14)
  • 因为是 Conda executable,所以我们要选择 Conda.exe 不能选择 Python 解释器,因此我们要选择那个在 Anaconda 根目录下的 _Conda.exe (注意:文件名中有下划线)。如果找不到 _conda.exe,可以选择 base 环境下的 conda.exe(即 Anaconda 根目录下的 Conda.exe ),在新版本的 anaconda 中好像已经无法找到 _conda.exe 了,选择 conda.exe 即可,然后点击右侧的 Load Environments。如果在 base 环境中找不到 conda.exe,进 Scripts 选择 conda.exe 即可。
  • 根目录下的 _conda.exe 或者 conda.exe
  • 如果根目录没有,选择 Scripts 目录下的 conda.exe

选择完成后,最后点击右侧的 Load Environments

/images/documents/本地部署xiaozhi-esp32-server/15.png
(图15)

接下来,因为我们已经在 Anaconda 中创建了虚拟环境,因此点击 Use existing environment, 选择已安装的 Anaconda 中的虚拟环境即可,点击右下角的 OK,即可

/images/documents/本地部署xiaozhi-esp32-server/16.png
(图16)

根据需求选择已经存在的环境即可

/images/documents/本地部署xiaozhi-esp32-server/17.png
(图17)

返回创建项目页面,点击 Create 即可

/images/documents/本地部署xiaozhi-esp32-server/18.png
(图18)

成功进入环境

/images/documents/本地部署xiaozhi-esp32-server/19.png
(图19)

Previously configured interpreter 中无法识别
看看所要添加的虚拟环境中是否 installpython,环境中没有安装 python 是识别不到的。 也就说仅创建一个虚拟环境,pycharm 应该是无法识别到的。
提示
环境变量 Path 里添加了 AnacondaScripts 的话就可以用 Scripts 里面的 conda.exe

补充

项目在 PyCharm 中突然无法启动

/images/documents/本地部署xiaozhi-esp32-server/21.png
(图21)

但是我在 Anaconda Prompt 中运行是正常的

/images/documents/本地部署xiaozhi-esp32-server/20.png
(图20)

经过测试,ffmpeg 确实安装了,没有遗漏,我在 app.py 中将 check_ffmpeg_installed() 注释,可以正常启动使用,就是控制台会打印提示信息

/images/documents/本地部署xiaozhi-esp32-server/22.png
(图22)

构建 Docker 镜像

将本地项目复制到服务器上,注意是整个项目,而不是只复制一个 xiaozhi-server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 依赖中 torch 太大了,下载会超时,所以先单独下载依赖,构建镜像时,复制过去就行了
# 注意:需要的是 python 为 3.10 的依赖,如果你的服务器的 python 版本不是 3.10,那么可以通过 conda 来下载 3.10 的依赖

# 创建 python 为 3.10 的虚拟环境
conda create --name xiaozhi-esp32-server python=3.10

# 下载依赖,根据 /xiaozhi-esp32-server/main/xiaozhi-server/requirements.txt 下载依赖到 /xiaozhi-esp32-server/dependencies
pip download -r /xiaozhi-esp32-server/main/xiaozhi-server/requirements.txt -d /xiaozhi-esp32-server/dependencies 

# 退出并删除虚拟环境
conda deactivate
conda remove --name xiaozhi-esp32-server --all

由于自身需求我在 requirements.txt 中添加了一个新的依赖 psycopg2,因此我需要修改 Dockerfile-server 文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 第一阶段:构建Python依赖
FROM python:3.10-slim AS builder

WORKDIR /app

# 安装编译工具链和 PostgreSQL 开发库
RUN apt-get update && \
    apt-get install -y --no-install-recommends build-essential libpq-dev && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 复制本地下载的依赖
COPY dependencies /app/dependencies

# 复制 requirements.txt
COPY main/xiaozhi-server/requirements.txt .

# 离线安装依赖
RUN pip install --no-index --find-links=/app/dependencies -r requirements.txt

# 第二阶段:生产镜像
FROM python:3.10-slim

WORKDIR /opt/xiaozhi-esp32-server

# 安装系统依赖(包括 libpq 运行时库)
RUN apt-get update && \
    apt-get install -y --no-install-recommends libopus0 ffmpeg libpq5 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 从构建阶段复制Python包和前端构建产物
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages

# 复制应用代码
COPY main/xiaozhi-server .

# 启动应用
CMD ["python", "app.py"]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 启动镜像看是否报错
docker run -it --rm xiaozhi-esp32-server:server_latest python app.py

# 提示:配置文件太旧了
# 复制配置文件到容器内
docker cp /home/agi/xiaozhi-esp32-server/main/xiaozhi-server/data/.config.yaml 容器名称/id:/opt/xiaozhi-esp32-server/data/.config.yaml

# 再次测试
# 启动容器
docker start 容器id

# 进入容器
docker exec -it 容器id /bin/bash

# 启动
python app.py

Conda 相关命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 检查当前 Conda 配置中的通道设置
conda config --show channels

# 添加镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

# 移除镜像
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

0%