代码拉取完成,页面将自动刷新
同步操作将从 zhyqieqie/SenseVoice 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# Set the device with environment, default is cuda:0
# export SENSEVOICE_DEVICE=cuda:1
import os, re
from fastapi import FastAPI, File, Form
from fastapi.responses import HTMLResponse
from typing_extensions import Annotated
from typing import List
from enum import Enum
import torchaudio
from model import SenseVoiceSmall
from funasr.utils.postprocess_utils import rich_transcription_postprocess
from io import BytesIO
class Language(str, Enum):
auto = "auto"
zh = "zh"
en = "en"
yue = "yue"
ja = "ja"
ko = "ko"
nospeech = "nospeech"
model_dir = "iic/SenseVoiceSmall"
m, kwargs = SenseVoiceSmall.from_pretrained(model=model_dir, device=os.getenv("SENSEVOICE_DEVICE", "cuda:0"))
m.eval()
regex = r"<\|.*\|>"
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
async def root():
return """
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Api information</title>
</head>
<body>
<a href='./docs'>Documents of API</a>
</body>
</html>
"""
@app.post("/api/v1/asr")
async def turn_audio_to_text(files: Annotated[List[bytes], File(description="wav or mp3 audios in 16KHz")], keys: Annotated[str, Form(description="name of each audio joined with comma")], lang: Annotated[Language, Form(description="language of audio content")] = "auto"):
audios = []
audio_fs = 0
for file in files:
file_io = BytesIO(file)
data_or_path_or_list, audio_fs = torchaudio.load(file_io)
data_or_path_or_list = data_or_path_or_list.mean(0)
audios.append(data_or_path_or_list)
file_io.close()
if lang == "":
lang = "auto"
if keys == "":
key = ["wav_file_tmp_name"]
else:
key = keys.split(",")
res = m.inference(
data_in=audios,
language=lang, # "zh", "en", "yue", "ja", "ko", "nospeech"
use_itn=False,
ban_emo_unk=False,
key=key,
fs=audio_fs,
**kwargs,
)
if len(res) == 0:
return {"result": []}
for it in res[0]:
it["raw_text"] = it["text"]
it["clean_text"] = re.sub(regex, "", it["text"], 0, re.MULTILINE)
it["text"] = rich_transcription_postprocess(it["text"])
return {"result": res[0]}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。