-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpredict.py
217 lines (191 loc) · 8.42 KB
/
predict.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
from typing import Optional, Any
import os
import time
import subprocess
import torch
import numpy as np
from cog import BasePredictor, Input, Path, BaseModel
from whisper.model import Whisper, ModelDimensions
from whisper.tokenizer import LANGUAGES, TO_LANGUAGE_CODE
from whisper.utils import format_timestamp
MODEL_CACHE = "weights"
BASE_URL = f"https://fly.storage.tigris.dev/weights/models/whisper/"
class ModelOutput(BaseModel):
detected_language: str
transcription: str
segments: Any
translation: Optional[str]
txt_file: Optional[Path]
srt_file: Optional[Path]
def download_weights(url: str, dest: str) -> None:
start = time.time()
print("[!] Initiating download from URL: ", url)
print("[~] Destination path: ", dest)
if ".tar" in dest:
dest = os.path.dirname(dest)
command = ["pget", "-vf" + ("x" if ".tar" in url else ""), url, dest]
try:
print(f"[~] Running command: {' '.join(command)}")
subprocess.check_call(command, close_fds=False)
except subprocess.CalledProcessError as e:
print(
f"[ERROR] Failed to download weights. Command '{' '.join(e.cmd)}' returned non-zero exit status {e.returncode}."
)
raise
print("[+] Download completed in: ", time.time() - start, "seconds")
class Predictor(BasePredictor):
def setup(self):
"""Load the large-v3 model"""
self.model_cache = MODEL_CACHE
self.models = {}
self.current_model = "large-v3"
self.load_model("large-v3")
def load_model(self, model_name):
if model_name not in self.models:
if not os.path.exists(self.model_cache):
os.makedirs(self.model_cache)
model_file = f"{model_name}.pt"
url = BASE_URL + model_file
dest_path = os.path.join(self.model_cache, model_file)
if not os.path.exists(dest_path):
download_weights(url, dest_path)
with open(dest_path, "rb") as fp:
checkpoint = torch.load(fp, map_location="cpu")
dims = ModelDimensions(**checkpoint["dims"])
model = Whisper(dims)
model.load_state_dict(checkpoint["model_state_dict"])
model.to("cuda")
self.models[model_name] = model
self.current_model = model_name
return self.models[model_name]
def predict(
self,
audio: Path = Input(description="Audio file"),
# Note: We only serve the large-v3 model to reduce switching costs and because it meets most users' needs.
# Other model sizes (base, small, tiny) are commented out as they're not currently offered.
model: str = Input(
choices=[
"large-v3",
# "base",
# "small",
# "tiny",
],
default="large-v3",
description="Whisper model size (currently only large-v3 is supported).",
),
transcription: str = Input(
choices=["plain text", "srt", "vtt"],
default="plain text",
description="Choose the format for the transcription",
),
translate: bool = Input(
default=False,
description="Translate the text to English when set to True",
),
language: str = Input(
choices=["auto"]
+ sorted(LANGUAGES.keys())
+ sorted([k.title() for k in TO_LANGUAGE_CODE.keys()]),
default="auto",
description="Language spoken in the audio, specify 'auto' for automatic language detection",
),
temperature: float = Input(
default=0,
description="temperature to use for sampling",
),
patience: float = Input(
default=None,
description="optional patience value to use in beam decoding, as in https://arxiv.org/abs/2204.05424, the default (1.0) is equivalent to conventional beam search",
),
suppress_tokens: str = Input(
default="-1",
description="comma-separated list of token ids to suppress during sampling; '-1' will suppress most special characters except common punctuations",
),
initial_prompt: str = Input(
default=None,
description="optional text to provide as a prompt for the first window.",
),
condition_on_previous_text: bool = Input(
default=True,
description="if True, provide the previous output of the model as a prompt for the next window; disabling may make the text inconsistent across windows, but the model becomes less prone to getting stuck in a failure loop",
),
temperature_increment_on_fallback: float = Input(
default=0.2,
description="temperature to increase when falling back when the decoding fails to meet either of the thresholds below",
),
compression_ratio_threshold: float = Input(
default=2.4,
description="if the gzip compression ratio is higher than this value, treat the decoding as failed",
),
logprob_threshold: float = Input(
default=-1.0,
description="if the average log probability is lower than this value, treat the decoding as failed",
),
no_speech_threshold: float = Input(
default=0.6,
description="if the probability of the <|nospeech|> token is higher than this value AND the decoding has failed due to `logprob_threshold`, consider the segment as silence",
),
) -> ModelOutput:
"""Transcribes and optionally translates a single audio file"""
print(f"Transcribe with {model} model.")
if model != self.current_model:
self.model = self.load_model(model)
else:
self.model = self.models[self.current_model]
if temperature_increment_on_fallback is not None:
temperature = tuple(
np.arange(temperature, 1.0 + 1e-6, temperature_increment_on_fallback)
)
else:
temperature = [temperature]
normalized_language = language.lower() if language.lower() != "auto" else None
if normalized_language and normalized_language not in LANGUAGES:
normalized_language = TO_LANGUAGE_CODE.get(normalized_language, normalized_language)
args = {
"language": normalized_language,
"patience": patience,
"suppress_tokens": suppress_tokens,
"initial_prompt": initial_prompt,
"condition_on_previous_text": condition_on_previous_text,
"compression_ratio_threshold": compression_ratio_threshold,
"logprob_threshold": logprob_threshold,
"no_speech_threshold": no_speech_threshold,
"fp16": True,
"verbose": False,
}
with torch.inference_mode():
result = self.model.transcribe(str(audio), temperature=temperature, **args)
if transcription == "plain text":
transcription = result["text"]
elif transcription == "srt":
transcription = write_srt(result["segments"])
else:
transcription = write_vtt(result["segments"])
if translate:
translation = self.model.transcribe(
str(audio), task="translate", temperature=temperature, **args
)
detected_language_code = result["language"]
detected_language_name = LANGUAGES.get(detected_language_code, detected_language_code)
return ModelOutput(
segments=result["segments"],
detected_language=detected_language_name,
transcription=transcription,
translation=translation["text"] if translate else None,
)
def write_vtt(transcript):
result = ""
for segment in transcript:
result += f"{format_timestamp(segment['start'])} --> {format_timestamp(segment['end'])}\n"
result += f"{segment['text'].strip().replace('-->', '->')}\n"
result += "\n"
return result
def write_srt(transcript):
result = ""
for i, segment in enumerate(transcript, start=1):
result += f"{i}\n"
result += f"{format_timestamp(segment['start'], always_include_hours=True, decimal_marker=',')} --> "
result += f"{format_timestamp(segment['end'], always_include_hours=True, decimal_marker=',')}\n"
result += f"{segment['text'].strip().replace('-->', '->')}\n"
result += "\n"
return result