Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from funasr import AutoModel
|
2 |
+
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
3 |
+
from modelscope import snapshot_download
|
4 |
+
|
5 |
+
import io
|
6 |
+
import os
|
7 |
+
import tempfile
|
8 |
+
import json
|
9 |
+
from typing import Optional
|
10 |
+
|
11 |
+
import torch
|
12 |
+
|
13 |
+
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
|
14 |
+
from fastapi.responses import StreamingResponse, Response
|
15 |
+
|
16 |
+
from config import model_config
|
17 |
+
|
18 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
19 |
+
model_dir = snapshot_download(model_config['model_dir'])
|
20 |
+
|
21 |
+
class SynthesizeResponse(Response):
|
22 |
+
media_type = 'text/plain'
|
23 |
+
|
24 |
+
app = FastAPI()
|
25 |
+
|
26 |
+
@app.post('/asr', response_class=SynthesizeResponse)
|
27 |
+
async def generate(
|
28 |
+
file: UploadFile = File(...),
|
29 |
+
vad_model: str = Form("fsmn-vad"),
|
30 |
+
vad_kwargs: str = Form('{"max_single_segment_time": 30000}'),
|
31 |
+
ncpu: int = Form(4),
|
32 |
+
batch_size: int = Form(1),
|
33 |
+
language: str = Form("auto"),
|
34 |
+
use_itn: bool = Form(True),
|
35 |
+
batch_size_s: int = Form(60),
|
36 |
+
merge_vad: bool = Form(True),
|
37 |
+
merge_length_s: int = Form(15),
|
38 |
+
batch_size_threshold_s: int = Form(50),
|
39 |
+
hotword: Optional[str] = Form(" "),
|
40 |
+
spk_model: str = Form("cam++"),
|
41 |
+
ban_emo_unk: bool = Form(False),
|
42 |
+
) -> StreamingResponse:
|
43 |
+
try:
|
44 |
+
# 将字符串转换为字典
|
45 |
+
vad_kwargs = json.loads(vad_kwargs)
|
46 |
+
|
47 |
+
# 创建临时文件并保存上传的音频文件
|
48 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
49 |
+
temp_file_path = temp_file.name
|
50 |
+
input_wav_bytes = await file.read()
|
51 |
+
temp_file.write(input_wav_bytes)
|
52 |
+
|
53 |
+
try:
|
54 |
+
# 初始化模型
|
55 |
+
model = AutoModel(
|
56 |
+
model=model_dir,
|
57 |
+
trust_remote_code=False,
|
58 |
+
remote_code="./model.py",
|
59 |
+
vad_model=vad_model,
|
60 |
+
vad_kwargs=vad_kwargs,
|
61 |
+
ncpu=ncpu,
|
62 |
+
batch_size=batch_size,
|
63 |
+
hub="ms",
|
64 |
+
device=device,
|
65 |
+
)
|
66 |
+
|
67 |
+
# 生成结果
|
68 |
+
res = model.generate(
|
69 |
+
input=temp_file_path, # 使用临时文件路径作为输入
|
70 |
+
cache={},
|
71 |
+
language=language,
|
72 |
+
use_itn=use_itn,
|
73 |
+
batch_size_s=batch_size_s,
|
74 |
+
merge_vad=merge_vad,
|
75 |
+
merge_length_s=merge_length_s,
|
76 |
+
batch_size_threshold_s=batch_size_threshold_s,
|
77 |
+
hotword=hotword,
|
78 |
+
spk_model=spk_model,
|
79 |
+
ban_emo_unk=ban_emo_unk
|
80 |
+
)
|
81 |
+
|
82 |
+
# 处理结果
|
83 |
+
text = rich_transcription_postprocess(res[0]["text"])
|
84 |
+
|
85 |
+
# 返回结果
|
86 |
+
return StreamingResponse(io.BytesIO(text.encode('utf-8')), media_type="text/plain")
|
87 |
+
|
88 |
+
finally:
|
89 |
+
# 确保在处理完毕后删除临时文件
|
90 |
+
if os.path.exists(temp_file_path):
|
91 |
+
os.remove(temp_file_path)
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
raise HTTPException(status_code=500, detail=str(e))
|
config.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
model_config = {
|
2 |
+
'model_dir': 'iic/SenseVoiceSmall'
|
3 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
2 |
+
torch
|
3 |
+
torchaudio
|
4 |
+
funasr
|
5 |
+
modelscope
|
6 |
+
huggingface
|
7 |
+
huggingface_hub
|
8 |
+
uvicorn
|
9 |
+
fastapi
|
10 |
+
python-dotenv
|
11 |
+
numpy
|
12 |
+
gradio
|
13 |
+
rotary_embedding_torch
|
14 |
+
|
run.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uvicorn
|
2 |
+
import os
|
3 |
+
|
4 |
+
from app import app
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
port = int(os.getenv('PORT', 3151))
|
9 |
+
|
10 |
+
if __name__ == '__main__':
|
11 |
+
uvicorn.run(app, host='0.0.0.0', port=port)
|