-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathsrt-server.py
223 lines (183 loc) · 6.73 KB
/
srt-server.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import soundfile as sf
import sys
import tempfile
import torch
from abc import ABC, abstractmethod
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
from loguru import logger
from pydantic import BaseModel
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from transformers.utils import is_flash_attn_2_available
from typing import Union
from urllib.parse import unquote
app = FastAPI()
class TranscriptionEngine(ABC):
@abstractmethod
def transcribe(self, file, audio_content, **kwargs):
pass
class TransformersEngine(TranscriptionEngine):
def __init__(self):
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
if torch.cuda.is_available():
device = "cuda"
torch_dtype = torch.float16
elif torch.backends.mps.is_available():
device = "mps"
torch_dtype = torch.float16
else:
device = "cpu"
torch_dtype = torch.float32
# 400ms
model_id = "openai/whisper-large-v2"
# 220ms
model_id = "distil-whisper/large-v2"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id,
torch_dtype=torch_dtype,
low_cpu_mem_usage=True,
use_safetensors=True,
attn_implementation="flash_attention_2" if is_flash_attn_2_available() else "sdpa",
).to(device)
processor = AutoProcessor.from_pretrained(model_id)
self.pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
max_new_tokens=128,
chunk_length_s=30,
batch_size=16,
return_timestamps=True,
torch_dtype=torch_dtype,
device=device,
model_kwargs={"attn_implementation": "flash_attention_2"} if is_flash_attn_2_available() else {"attn_implementation": "sdpa"},
)
def transcribe(self, file, audio_content, **kwargs):
result = self.pipe(audio_content, **kwargs)
return result["text"], result.get("chunks", [])
class FasterWhisperEngine(TranscriptionEngine):
def __init__(self):
from faster_whisper import WhisperModel
# 350ms
model_id = "large-v2"
# 300ms
model_id = "distil-large-v2"
# 280ms
model_id = "distil-medium.en"
# 300ms
model_id = "distil-large-v3"
self.model = WhisperModel(model_id, device="cuda", compute_type="float16")
def transcribe(self, file, audio_content, **kwargs):
segments, _ = self.model.transcribe(unquote(file.filename), beam_size=5)
full_text = "".join(segment.text for segment in segments)
logger.info(full_text)
return full_text, [{"start": s.start, "end": s.end, "text": s.text} for s in segments]
'''
WIP - ffmpeg fails
'''
class SenseVoiceEngine(TranscriptionEngine):
def __init__(self):
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess
device = "cuda" if torch.cuda.is_available() else "cpu"
# git clone https://huggingface.co/FunAudioLLM/SenseVoiceSmall
model_id = "FunAudioLLM/SenseVoiceSmall"
self.model = AutoModel(
model=model_id,
vad_kwargs={"max_single_segment_time": 30000},
device=device,
hub="hf",
)
def transcribe(self, file, audio_content, **kwargs):
res = self.model.generate(
input=unquote(file.filename),
cache={},
language="auto", # "zh", "en", "yue", "ja", "ko", "nospeech"
use_itn=True,
batch_size_s=60,
merge_length_s=15,
)
from funasr.utils.postprocess_utils import rich_transcription_postprocess
text = rich_transcription_postprocess(res[0]["text"])
logger.info(text)
return text, []
# For shorter sentences, the regular transformers pipeline seems to be faster than faster-whisper?
'''
try:
engine = FasterWhisperEngine()
logger.info("Using FasterWhisperEngine")
except ImportError:
engine = TransformersEngine()
logger.info("Using TransformersEngine")
'''
engine = TransformersEngine()
logger.info("Using TransformersEngine")
class TranscriptionResponse(BaseModel):
text: str
@app.post("/v1/audio/transcriptions", response_model=TranscriptionResponse)
async def create_transcription(
file: UploadFile = File(...),
model: str = Form(...),
language: str = Form(None),
prompt: str = Form(None),
response_format: str = Form("json"),
temperature: float = Form(0.0)
):
audio_content = await file.read()
text, _ = engine.transcribe(audio_content, generate_kwargs={"language": language, "task": "transcribe"})
response = {"text": text}
return JSONResponse(content=response, media_type="application/json")
@app.post("/v1/audio/translations", response_model=TranscriptionResponse)
async def create_translation(
file: UploadFile = File(...),
model: str = Form(...),
prompt: str = Form(None),
response_format: str = Form("json"),
temperature: float = Form(0.0)
):
# Read the audio file
audio_content = await file.read()
text, _ = engine.transcribe(audio_content, generate_kwargs={"task": "translate"})
response = {"text": text}
return JSONResponse(content=response, media_type="application/json")
@app.post("/inference")
async def inference(
file: UploadFile = File(...),
temperature: float = Form(0.0),
temperature_inc: float = Form(0.0),
response_format: str = Form("json")
):
# Read the audio file
audio_content = await file.read()
temperature += temperature_inc
text, segments = engine.transcribe(
file,
audio_content,
generate_kwargs={
"temperature": temperature,
"do_sample": True
} if isinstance(engine, TransformersEngine) else {
"beam_size": 5,
"temperature": temperature
}
)
# Prepare the response based on the requested format
if response_format == "json":
response = {
"text": text,
# "segments": [
# {
# "start": segment["timestamp"][0],
# "end": segment["timestamp"][1],
# "text": segment["text"]
# }
# for segment in result["chunks"]
# ]
}
else:
response = {"text": text}
return JSONResponse(content=response, media_type="application/json")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8001)