mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
* 添加 长期记忆功能 * fix bugs * add memory parm for config.yaml * fix is_segment for tts * 推送主分支测试 * 🎈 perf: 美化log * 🐞 fix: bug 改用opus_next,增加文档 * 合并main到develop (#75) * fix: 多种docker build 编译方式 * fix:push * 优化打包速度 (#65) * 🐳 chore: 优化打包速度 使用国内镜像 * 🎈 perf: 优化打包速度 注释日志的时间信息 * 合并多个更新 (#68) * 🐳 chore: 优化打包速度 使用国内镜像 * 🎈 perf: 优化打包速度 注释日志的时间信息 * 🐳 chore: 改用docker hub * 🐎 ci(ci): 自动打包docker * 🐳 chore: 更新文档 * 🎈 perf: 改用opuslib_next * 🌈 style: 统一log * minimax语音tts和阿里云智能语音tts适配 * 本地模型不重复创建,共用一个 (#51) * 本地模型不重复创建,共用一个 * 更新web端页面发布版本 * 🎈 perf: 优化开发体验 (#70) 添加内置模型下载,yarn --------- Co-authored-by: TOM88812 <ttangxs0808@163.com> Co-authored-by: HonestQiao <honestqiao@163.com> Co-authored-by: 玄凤科技 <128943504+Eric0308@users.noreply.github.com> Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com> Co-authored-by: Huang <32005838+openrz@users.noreply.github.com> * feat: support doubao online ASR * feat: support doubao online ASR --------- Co-authored-by: fofo <zhongpei@vip.qq.com> Co-authored-by: hrz <1710360675@qq.com> Co-authored-by: 玄凤科技 <128943504+Eric0308@users.noreply.github.com> Co-authored-by: kalicyh <34980061+kalicyh@users.noreply.github.com> Co-authored-by: TOM88812 <ttangxs0808@163.com> Co-authored-by: HonestQiao <honestqiao@163.com> Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com> Co-authored-by: Huang <32005838+openrz@users.noreply.github.com> Co-authored-by: 胡垚 <qsct9501@163.com>
125 lines
No EOL
3.8 KiB
Python
125 lines
No EOL
3.8 KiB
Python
import os
|
||
import re
|
||
import json
|
||
import yaml
|
||
import socket
|
||
|
||
|
||
def get_project_dir():
|
||
"""获取项目根目录"""
|
||
return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + '/'
|
||
|
||
|
||
def get_local_ip():
|
||
try:
|
||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||
# Connect to Google's DNS servers
|
||
s.connect(("8.8.8.8", 80))
|
||
local_ip = s.getsockname()[0]
|
||
s.close()
|
||
return local_ip
|
||
except Exception as e:
|
||
return "127.0.0.1"
|
||
|
||
|
||
def read_config(config_path):
|
||
with open(config_path, "r", encoding="utf-8") as file:
|
||
config = yaml.safe_load(file)
|
||
return config
|
||
|
||
|
||
def write_json_file(file_path, data):
|
||
"""将数据写入 JSON 文件"""
|
||
with open(file_path, 'w', encoding='utf-8') as file:
|
||
json.dump(data, file, ensure_ascii=False, indent=4)
|
||
|
||
|
||
def is_segment(tokens):
|
||
if tokens[-1] in (",", ".", "?", ",", "。", "?", "!", "!", ";", ";", ":", ":") and len(tokens) > 5:
|
||
return True
|
||
elif tokens[-1] in ( ".", "?", "。", "?", "!", "!", ";", ";", ):
|
||
return True
|
||
else:
|
||
return False
|
||
|
||
|
||
def is_punctuation_or_emoji(char):
|
||
"""检查字符是否为空格、指定标点或表情符号"""
|
||
# 定义需要去除的中英文标点(包括全角/半角)
|
||
punctuation_set = {
|
||
',', ',', # 中文逗号 + 英文逗号
|
||
'。', '.', # 中文句号 + 英文句号
|
||
'!', '!', # 中文感叹号 + 英文感叹号
|
||
'-', '-', # 英文连字符 + 中文全角横线
|
||
'、' # 中文顿号
|
||
}
|
||
if char.isspace() or char in punctuation_set:
|
||
return True
|
||
# 检查表情符号(保留原有逻辑)
|
||
code_point = ord(char)
|
||
emoji_ranges = [
|
||
(0x1F600, 0x1F64F), (0x1F300, 0x1F5FF),
|
||
(0x1F680, 0x1F6FF), (0x1F900, 0x1F9FF),
|
||
(0x1FA70, 0x1FAFF), (0x2600, 0x26FF),
|
||
(0x2700, 0x27BF)
|
||
]
|
||
return any(start <= code_point <= end for start, end in emoji_ranges)
|
||
|
||
|
||
def get_string_no_punctuation_or_emoji(s):
|
||
"""去除字符串首尾的空格、标点符号和表情符号"""
|
||
chars = list(s)
|
||
# 处理开头的字符
|
||
start = 0
|
||
while start < len(chars) and is_punctuation_or_emoji(chars[start]):
|
||
start += 1
|
||
# 处理结尾的字符
|
||
end = len(chars) - 1
|
||
while end >= start and is_punctuation_or_emoji(chars[end]):
|
||
end -= 1
|
||
return ''.join(chars[start:end + 1])
|
||
|
||
|
||
def remove_punctuation_and_length(text):
|
||
# 全角符号和半角符号的Unicode范围
|
||
full_width_punctuations = '!"#$%&'()*+,-。/:;<=>?@[\]^_`{|}~'
|
||
half_width_punctuations = '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
|
||
space = ' ' # 半角空格
|
||
full_width_space = ' ' # 全角空格
|
||
|
||
# 去除全角和半角符号以及空格
|
||
result = ''.join([char for char in text if
|
||
char not in full_width_punctuations and char not in half_width_punctuations and char not in space and char not in full_width_space])
|
||
|
||
if result == "Yeah":
|
||
return 0
|
||
return len(result), result
|
||
|
||
|
||
def check_password(password):
|
||
"""
|
||
检查密码是否满足以下条件:
|
||
1. 密码长度大于八位。
|
||
2. 密码包含英文和数字。
|
||
3. 密码不能包含“xiaozhi”字符。
|
||
|
||
:param password: 要检查的密码
|
||
:return: 如果密码满足条件,则返回True;否则返回False。
|
||
"""
|
||
# 检查密码长度
|
||
if len(password) < 8:
|
||
return False
|
||
|
||
# 检查是否包含英文字符和数字
|
||
if not re.search(r'[A-Za-z]', password) or not re.search(r'[0-9]', password):
|
||
return False
|
||
|
||
# 检查是否包含“xiaozhi”字符
|
||
if "xiaozhi" in password:
|
||
return False
|
||
|
||
if "1234" in password:
|
||
return False
|
||
|
||
# 如果满足所有条件,则返回True
|
||
return True |