This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
asrapp.py
executable file
·242 lines (202 loc) · 7.51 KB
/
asrapp.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# adopted from amazing https://github.com/matatonic/openedai-whisper/tree/main
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import PlainTextResponse
import os
import torch
from transformers import pipeline
from typing import Optional, List
from fastapi import UploadFile, Form
from fastapi.responses import PlainTextResponse, JSONResponse
import uvicorn
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
class OpenAIStub(FastAPI):
def __init__(self) -> None:
super().__init__()
self.models = {}
self.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
pipe = None
MODEL_DICT = {}
app = OpenAIStub()
device = "cuda" if torch.cuda.is_available() else "cpu"
if torch.cuda.is_available():
dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
else:
dtype = torch.float32
def get_model(model_id) -> tuple[AutoModelForSpeechSeq2Seq, AutoProcessor]:
global MODEL_DICT
if model_id in MODEL_DICT:
return MODEL_DICT[model_id]
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id,
torch_dtype=dtype,
low_cpu_mem_usage=True,
use_safetensors=True,
attn_implementation="sdpa",
)
model.to(device)
if torch.cuda.is_available():
model.forward = torch.compile(
model.forward, mode="max-autotune", fullgraph=True
)
model.generation_config.cache_implementation = "static"
processor = AutoProcessor.from_pretrained(model_id)
MODEL_DICT[model_id] = (model, processor)
return model, processor
async def whisper(model_id, file, response_format: str, **kwargs):
model, processor = get_model(model_id)
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
device=device,
chunk_length_s=30,
torch_dtype=dtype,
)
result = pipe(await file.read(), batch_size=4, **kwargs)
filename_noext, ext = os.path.splitext(file.filename)
if response_format == "text":
return PlainTextResponse(
result["text"].strip(),
headers={
"Content-Disposition": f"attachment; filename={filename_noext}.txt"
},
)
elif response_format == "json":
return JSONResponse(
content={"text": result["text"].strip()},
media_type="application/json",
headers={
"Content-Disposition": f"attachment; filename={filename_noext}.json"
},
)
elif response_format == "verbose_json":
chunks = result["chunks"]
response = {
"task": kwargs["generate_kwargs"]["task"],
# "language": "english",
"duration": chunks[-1]["timestamp"][1],
"text": result["text"].strip(),
}
if kwargs["return_timestamps"] == "word":
response["words"] = [
{
"word": chunk["text"].strip(),
"start": chunk["timestamp"][0],
"end": chunk["timestamp"][1],
}
for chunk in chunks
]
else:
response["segments"] = [
{
"id": i,
# "seek": 0,
"start": chunk["timestamp"][0],
"end": chunk["timestamp"][1],
"text": chunk["text"].strip(),
# "tokens": [ ],
# "temperature": 0.0,
# "avg_logprob": -0.2860786020755768,
# "compression_ratio": 1.2363636493682861,
# "no_speech_prob": 0.00985979475080967
}
for i, chunk in enumerate(chunks)
]
return JSONResponse(
content=response,
media_type="application/json",
headers={
"Content-Disposition": f"attachment; filename={filename_noext}_verbose.json"
},
)
elif response_format == "srt":
def srt_time(t):
return "{:02d}:{:02d}:{:06.3f}".format(
int(t // 3600), int(t // 60) % 60, t % 60
).replace(".", ",")
return PlainTextResponse(
"\n".join(
[
f"{i}\n{srt_time(chunk['timestamp'][0])} --> {srt_time(chunk['timestamp'][1])}\n{chunk['text'].strip()}\n"
for i, chunk in enumerate(result["chunks"], 1)
]
),
media_type="text/srt; charset=utf-8",
headers={
"Content-Disposition": f"attachment; filename={filename_noext}.srt"
},
)
elif response_format == "vtt":
def vtt_time(t):
return "{:02d}:{:06.3f}".format(int(t // 60), t % 60)
return PlainTextResponse(
"\n".join(
["WEBVTT\n"]
+ [
f"{vtt_time(chunk['timestamp'][0])} --> {vtt_time(chunk['timestamp'][1])}\n{chunk['text'].strip()}\n"
for chunk in result["chunks"]
]
),
media_type="text/vtt; charset=utf-8",
headers={
"Content-Disposition": f"attachment; filename={filename_noext}.vtt"
},
)
@app.post("/v1/audio/transcriptions")
async def transcriptions(
file: UploadFile,
model: str = Form(...),
language: Optional[str] = Form(None),
prompt: Optional[str] = Form(None),
response_format: Optional[str] = Form("json"),
temperature: Optional[float] = Form(None),
timestamp_granularities: List[str] = Form(["segment"]),
):
global pipe
kwargs = {"generate_kwargs": {"task": "transcribe"}}
if language:
kwargs["generate_kwargs"]["language"] = language.lower()
# May work soon, https://github.com/huggingface/transformers/issues/27317
# if prompt:
# kwargs["initial_prompt"] = prompt
if temperature:
kwargs["generate_kwargs"]["temperature"] = temperature
kwargs["generate_kwargs"]["do_sample"] = False
kwargs["generate_kwargs"]["num_beams"] = 1
if response_format == "verbose_json" and "word" in timestamp_granularities:
kwargs["return_timestamps"] = "word"
else:
kwargs["return_timestamps"] = response_format in ["verbose_json", "srt", "vtt"]
return await whisper(model, file, response_format, **kwargs)
@app.post("/v1/audio/translations")
async def translations(
file: UploadFile,
model: str = Form(...),
prompt: Optional[str] = Form(None),
response_format: Optional[str] = Form("json"),
temperature: Optional[float] = Form(None),
):
global pipe
kwargs = {"generate_kwargs": {"task": "translate"}}
# May work soon, https://github.com/huggingface/transformers/issues/27317
# if prompt:
# kwargs["initial_prompt"] = prompt
if temperature:
kwargs["generate_kwargs"]["temperature"] = temperature
kwargs["generate_kwargs"]["do_sample"] = False
kwargs["generate_kwargs"]["num_beams"] = 1
kwargs["return_timestamps"] = response_format in ["verbose_json", "srt", "vtt"]
return await whisper(model, file, response_format, **kwargs)
@app.get("/v1")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7862)