Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion funasr/datasets/audio_datasets/jsonl2scp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import os
import json
import torch
Expand Down Expand Up @@ -58,7 +59,8 @@ def main_hydra(cfg: DictConfig):
("/Users/zhifu/funasr1.0/test_local/wav.scp", "/Users/zhifu/funasr1.0/test_local/text.txt"),
)
if isinstance(scp_file_list, str):
scp_file_list = eval(scp_file_list)
# 只允许字面量(路径列表/元组),禁止 eval 执行任意代码
scp_file_list = ast.literal_eval(scp_file_list)
data_type_list = kwargs.get("data_type_list", ("source", "target"))
jsonl_file = kwargs.get(
"jsonl_file_in", "/Users/zhifu/funasr1.0/test_local/audio_datasets.jsonl"
Expand Down
4 changes: 3 additions & 1 deletion funasr/datasets/audio_datasets/scp2jsonl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import os
import json
import torch
Expand Down Expand Up @@ -127,7 +128,8 @@ def main_hydra(cfg: DictConfig):
("/Users/zhifu/funasr1.0/test_local/wav.scp", "/Users/zhifu/funasr1.0/test_local/text.txt"),
)
if isinstance(scp_file_list, str):
scp_file_list = eval(scp_file_list)
# 只允许字面量(路径列表/元组),禁止 eval 执行任意代码
scp_file_list = ast.literal_eval(scp_file_list)
data_type_list = kwargs.get("data_type_list", ("source", "target"))
jsonl_file_out = kwargs.get(
"jsonl_file_out", "/Users/zhifu/funasr1.0/test_local/audio_datasets.jsonl"
Expand Down
4 changes: 3 additions & 1 deletion funasr/datasets/audio_datasets/sensevoice2jsonl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import os
import json
import torch
Expand Down Expand Up @@ -193,7 +194,8 @@ def main_hydra(cfg: DictConfig):
("/Users/zhifu/funasr1.0/test_local/wav.scp", "/Users/zhifu/funasr1.0/test_local/text.txt"),
)
if isinstance(scp_file_list, str):
scp_file_list = eval(scp_file_list)
# 只允许字面量(路径列表/元组),禁止 eval 执行任意代码
scp_file_list = ast.literal_eval(scp_file_list)
data_type_list = kwargs.get("data_type_list", ("source", "target"))
jsonl_file_out = kwargs.get(
"jsonl_file_out", "/Users/zhifu/funasr1.0/test_local/audio_datasets.jsonl"
Expand Down
15 changes: 9 additions & 6 deletions funasr/download/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def download_from_url(url):
storage = HTTPStorage()
# bytes
data = storage.read(url)
work_dir = tempfile.TemporaryDirectory().name
if not os.path.exists(work_dir):
os.makedirs(work_dir)
file_path = os.path.join(work_dir, os.path.basename(url))
# mkdtemp 返回真实存在且不会被 GC 回收的目录;
# 原 TemporaryDirectory().name 未持有引用,目录随即被析构删除
work_dir = tempfile.mkdtemp(prefix="funasr_download_")
# 用 URL path 的 basename,避免查询串(?a=b)进入文件名(Windows 非法字符)
file_name = os.path.basename(result.path) or "downloaded_file"
file_path = os.path.join(work_dir, file_name)
with open(file_path, "wb") as fb:
fb.write(data)
assert file_path is not None, f"failed to download: {url}"
Expand Down Expand Up @@ -166,7 +168,8 @@ def read(self, url):
Args:
url: TODO.
"""
r = requests.get(url)
# 超时:防止恶意/故障服务器让下载线程永久挂起
r = requests.get(url, timeout=(10, 60))
r.raise_for_status()
return r.content

Expand All @@ -176,7 +179,7 @@ def read_text(self, url):
Args:
url: TODO.
"""
r = requests.get(url)
r = requests.get(url, timeout=(10, 60))
r.raise_for_status()
return r.text

Expand Down
47 changes: 45 additions & 2 deletions funasr/models/data2vec/data2vec_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import ast
import logging
import math

Expand All @@ -21,6 +22,48 @@
from funasr.models.transformer.utils.nets_utils import make_pad_mask


def _parse_conv_feature_layers(expr):
"""安全解析卷积层配置字符串,替代 eval()。

仅允许整数字面量、list/tuple 以及它们之间的 + / * 运算,
例如 "[(512,10,5)] + [(512,3,2)] * 4"。
配置可能来自下载的模型 config.yaml,eval() 会执行其中任意代码。

Args:
expr: 卷积层配置表达式字符串。

Returns:
解析后的层配置列表。
"""
if not isinstance(expr, str):
return expr

def _eval_node(node):
if isinstance(node, ast.Expression):
return _eval_node(node.body)
if isinstance(node, ast.Constant) and type(node.value) is int:
return node.value
if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
operand = _eval_node(node.operand)
if type(operand) is int:
return -operand
if isinstance(node, ast.List):
return [_eval_node(e) for e in node.elts]
if isinstance(node, ast.Tuple):
return tuple(_eval_node(e) for e in node.elts)
if isinstance(node, ast.BinOp):
left, right = _eval_node(node.left), _eval_node(node.right)
if isinstance(node.op, ast.Add):
return left + right
if isinstance(node.op, ast.Mult):
return left * right
raise ValueError(
f"Unsupported conv_feature_layers expression: {expr!r}"
)

return _eval_node(ast.parse(expr, mode="eval"))


def get_annealed_rate(start, end, curr_step, total_steps):
"""Get annealed rate.

Expand Down Expand Up @@ -164,7 +207,7 @@ def __init__(

# ConvFeatureExtractionModel
self.conv_feature_layers = conv_feature_layers
feature_enc_layers = eval(conv_feature_layers)
feature_enc_layers = _parse_conv_feature_layers(conv_feature_layers)
self.extractor_embed = feature_enc_layers[-1][0]
self.feature_extractor = ConvFeatureExtractionModel(
conv_layers=feature_enc_layers,
Expand Down Expand Up @@ -401,7 +444,7 @@ def _conv_out_length(input_length, kernel_size, stride):
"""
return torch.floor((input_length - kernel_size).to(torch.float32) / stride + 1)

conv_cfg_list = eval(self.conv_feature_layers)
conv_cfg_list = _parse_conv_feature_layers(self.conv_feature_layers)

for i in range(len(conv_cfg_list)):
input_lengths = _conv_out_length(
Expand Down
6 changes: 5 additions & 1 deletion funasr/models/llm_asr/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import logging
from typing import Union, Dict, List, Tuple, Optional

Expand Down Expand Up @@ -1340,7 +1341,10 @@ def data_load_speech(self, contents: dict, tokenizer, frontend, meta_data={}, **
if sub_str.startswith("!"):
sub_str = sub_str[1:]
if sub_str.startswith("!"): # !!bytes
sub_str = eval(sub_str[1:])
# 安全解析字节字面量(如 b'...')。
# 原 eval() 会执行任意代码:训练数据/用户输入中的
# <|startofspeech|>!!...<|endofspeech|> 片段可注入恶意表达式。
sub_str = ast.literal_eval(sub_str[1:])
try:
time1 = time.perf_counter()
data_src = load_audio_text_image_video(sub_str, fs=frontend.fs)
Expand Down
2 changes: 1 addition & 1 deletion funasr/models/qwen_audio/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_T_after_cnn(L_in, dilation=1):
L_in: TODO.
dilation: TODO.
"""
for padding, kernel_size, stride in eval("[(1,3,1)] + [(1,3,2)] "):
for padding, kernel_size, stride in [(1, 3, 1), (1, 3, 2)]:
L_out = L_in + 2 * padding - dilation * (kernel_size - 1) - 1
L_out = 1 + L_out // stride
L_in = L_out
Expand Down
83 changes: 60 additions & 23 deletions runtime/python/http/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import argparse
import asyncio
import functools
import logging
import os
import uuid
Expand All @@ -7,6 +9,7 @@
import ffmpeg
import uvicorn
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from modelscope.utils.logger import get_logger

from funasr import AutoModel
Expand Down Expand Up @@ -52,6 +55,13 @@
parser.add_argument("--certfile", type=str, default=None, required=False, help="certfile for ssl")
parser.add_argument("--keyfile", type=str, default=None, required=False, help="keyfile for ssl")
parser.add_argument("--temp_dir", type=str, default="temp_dir/", required=False, help="temp dir")
parser.add_argument(
"--max_upload_mb",
type=int,
default=100,
required=False,
help="max upload audio size in MB (reject larger uploads to avoid memory/disk exhaustion)",
)
args = parser.parse_args()
logger.info("----------- Configuration Arguments -----------")
for arg, value in vars(args).items():
Expand Down Expand Up @@ -91,40 +101,67 @@

@app.post("/recognition")
async def api_recognition(audio: UploadFile = File(..., description="audio file")):
suffix = audio.filename.split(".")[-1]
audio_path = f"{args.temp_dir}/{str(uuid.uuid1())}.{suffix}"
async with aiofiles.open(audio_path, "wb") as out_file:
content = await audio.read()
await out_file.write(content)
# 扩展名只保留字母数字,防止客户端用带 ../ 的文件名做路径穿越
raw_suffix = (audio.filename or "").rsplit(".", 1)[-1]
suffix = "".join(c for c in raw_suffix if c.isalnum())[:10] or "audio"
audio_path = os.path.join(args.temp_dir, f"{uuid.uuid1()}.{suffix}")
max_bytes = args.max_upload_mb * 1024 * 1024
received = 0
try:
audio_bytes, _ = (
ffmpeg.input(audio_path, threads=0)
.output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=16000)
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
# 分块落盘并限制总大小,避免超大上传耗尽内存/磁盘
async with aiofiles.open(audio_path, "wb") as out_file:
while True:
chunk = await audio.read(1024 * 1024)
if not chunk:
break
received += len(chunk)
if received > max_bytes:
return JSONResponse(
status_code=413,
content={
"msg": f"音频文件过大(上限 {args.max_upload_mb}MB)",
"code": 1,
},
)
await out_file.write(chunk)
try:
audio_bytes, _ = (
ffmpeg.input(audio_path, threads=0)
.output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=16000)
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
)
except Exception as e:
logger.error(f"读取音频文件发生错误,错误信息:{e}")
return {"msg": "读取音频文件发生错误", "code": 1}
# 推理是阻塞操作:放线程池执行,避免卡死事件循环导致所有客户端挂起
loop = asyncio.get_running_loop()
rec_results = await loop.run_in_executor(
None,
functools.partial(model.generate, input=audio_bytes, is_final=True, **param_dict),
)
except Exception as e:
logger.error(f"读取音频文件发生错误,错误信息:{e}")
return {"msg": "读取音频文件发生错误", "code": 1}
rec_results = model.generate(input=audio_bytes, is_final=True, **param_dict)
# 结果为空
if len(rec_results[0]["text"] ) == 0:
return {"text": "", "sentences": [], "code": 0}
elif len(rec_results[0]["text"] ) > 0:
# 解析识别结果
# 结果为空
if not rec_results:
return {"text": "", "sentences": [], "code": 0}
rec_result = rec_results[0]
text = rec_result["text"]
text = rec_result.get("text", "")
if len(text) == 0:
return {"text": "", "sentences": [], "code": 0}
# 解析识别结果
sentences = []
for sentence in rec_result["sentence_info"]:
for sentence in rec_result.get("sentence_info", []):
# 每句话的时间戳
sentences.append(
{"text": sentence["text"], "start": sentence["start"], "end": sentence["end"]}
)
ret = {"text": text, "sentences": sentences, "code": 0}
logger.info(f"识别结果:{ret}")
return ret
else:
logger.info(f"识别结果:{rec_results}")
return {"msg": "未知错误", "code": -1}
finally:
# 无论成功失败都清理临时文件,防止磁盘被上传文件占满
try:
os.remove(audio_path)
except OSError:
pass


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion runtime/python/libtorch/funasr_torch/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def read_yaml(yaml_path: Union[str, Path]) -> Dict:
raise FileExistsError(f"The {yaml_path} does not exist.")

with open(str(yaml_path), "rb") as f:
data = yaml.load(f, Loader=yaml.Loader)
# safe_load:模型配置可能来自远端下载,yaml.Loader 会执行任意 Python 标签
data = yaml.safe_load(f)
return data


Expand Down
3 changes: 2 additions & 1 deletion runtime/python/onnxruntime/funasr_onnx/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ def read_yaml(yaml_path: Union[str, Path]) -> Dict:
raise FileExistsError(f"The {yaml_path} does not exist.")

with open(str(yaml_path), "rb") as f:
data = yaml.load(f, Loader=yaml.Loader)
# safe_load:模型配置可能来自远端下载,yaml.Loader 会执行任意 Python 标签
data = yaml.safe_load(f)
return data


Expand Down
Loading