forked from ClaudioPaonessa/whisper-lagoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
189 lines (145 loc) · 5.46 KB
/
Copy pathmain.py
File metadata and controls
189 lines (145 loc) · 5.46 KB
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
import os
import asyncio
import uuid
import requests
import tempfile
from functools import lru_cache
from pathlib import Path
from typing import Iterable
from fastapi import FastAPI, Form, HTTPException, status, Request
from faster_whisper import WhisperModel
from faster_whisper.transcribe import Segment
from azure.storage.blob import BlobClient
from download import download_model_if_not_cached
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
# url http://127.0.0.1:8000/v1/audio/transcriptions \
# -H "Authorization: Bearer $OPENAI_API_KEY" \
# -H "Content-Type: multipart/form-data" \
# -F model="whisper-ch" \
# -F file="@/path/to/file/openai.mp3"
# {
# "text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger..."
# }
MODEL_DATA_DIR = os.getenv("MODEL_DATA_DIR")
MEDIA_DIR = os.getenv("MEDIA_ROOT")
AZURE_BLOB_STORAGE_CONTAINER_NAME = os.getenv('AZURE_BLOB_STORAGE_CONTAINER_NAME')
AZURE_BLOB_STORAGE_CONNECTION_STRING = os.getenv('AZURE_BLOB_STORAGE_CONNECTION_STRING')
WHISPER_DEFAULT_SETTINGS = {
"whisper_model": os.getenv("MODEL"),
"quantization": os.getenv("QUANTIZATION"),
"task": "transcribe",
"language": "de",
"beam_size": 5,
}
@lru_cache(maxsize=1)
def get_whisper_model(whisper_model: str, quantization: str) -> WhisperModel:
"""Get a whisper model from the cache or download it if it doesn't exist"""
model_folder = download_model_if_not_cached(
model_data_dir=MODEL_DATA_DIR,
whisper_model_name=whisper_model,
quantization=quantization,
)
model = WhisperModel(str(model_folder), compute_type=quantization)
return model
def transcribe(
audio_path: str, whisper_model: str, quantization: str, **whisper_args
) -> Iterable[Segment]:
"""Transcribe the audio file using whisper"""
# Get whisper model
# NOTE: If mulitple models are selected, this may keep all of them in memory depending on the cache size
transcriber = get_whisper_model(whisper_model, quantization)
segments, _ = transcriber.transcribe(
audio=audio_path,
**whisper_args,
)
return segments
async def transcribe_post(postback_uri: str, audio_path: str):
try:
if not os.path.exists(audio_path):
print(f"File not found: {audio_path}")
r = requests.post(url=postback_uri, json=None)
r.raise_for_status()
return
print(f"{audio_path}: Starting transcription")
segments = transcribe(audio_path, **WHISPER_DEFAULT_SETTINGS)
segment_dicts = []
for segment in segments:
segment_dicts.append(
{
"transcript": segment.text,
"start": segment.start,
"end": segment.end,
}
)
data = {"content": segment_dicts}
print(f"{audio_path}: Posting transcription to {postback_uri}")
r = requests.post(url=postback_uri, json=data)
r.raise_for_status()
print(f"Deleting {audio_path}")
os.remove(audio_path)
except Exception as e:
print(e)
os.remove(audio_path)
@app.post("/v1/audio/transcriptions")
async def transcriptions(
request: Request,
model: str = Form(...),
file: str = Form(...)
):
print(f"Received request for {file}")
postback_uri = request.headers.get("LanguageServicePostbackUri")
assert model == "whisper-ch"
loop = asyncio.get_running_loop()
loop.create_task(transcribe_post(postback_uri, audio_path=str(file)))
@app.post("/v1/audio/transcriptions/azure-file")
async def transcriptions_azure_file(
request: Request,
model: str = Form(...),
content_file_url: str = Form(...),
):
print(f"Received request for azure file {content_file_url}")
postback_uri = request.headers.get("LanguageServicePostbackUri")
random_file_name = uuid.uuid4().hex
path = Path(f"{MEDIA_DIR}/{random_file_name}")
try:
blob_client = BlobClient.from_connection_string(
AZURE_BLOB_STORAGE_CONNECTION_STRING,
container_name=AZURE_BLOB_STORAGE_CONTAINER_NAME,
blob_name=content_file_url
)
with open(file=path, mode="wb") as fs:
download_stream = blob_client.download_blob()
fs.write(download_stream.readall())
assert model == "whisper-ch"
loop = asyncio.get_running_loop()
loop.create_task(transcribe_post(postback_uri, audio_path=str(path)))
except Exception as e:
print(e)
os.remove(path)
@app.post("/v1/audio/transcriptions/url")
async def transcriptions_url(
request: Request,
model: str = Form(...),
url: str = Form(...)
):
print(f"Received request for {url}")
postback_uri = request.headers.get("LanguageServicePostbackUri")
random_file_name = uuid.uuid4().hex
path = Path(f"{MEDIA_DIR}/{random_file_name}")
try:
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
assert model == "whisper-ch"
loop = asyncio.get_running_loop()
loop.create_task(transcribe_post(postback_uri, audio_path=str(path)))
except Exception as e:
print(e)
os.remove(path)
@app.get("/healthz", status_code=200)
async def health() -> str:
return "OK"