Spaces:
Running
on
Zero
Running
on
Zero
Delete whisper2.py
Browse files- whisper2.py +0 -114
whisper2.py
DELETED
@@ -1,114 +0,0 @@
|
|
1 |
-
from transformers import WhisperForConditionalGeneration, WhisperProcessor
|
2 |
-
import torchaudio
|
3 |
-
import torch
|
4 |
-
import librosa
|
5 |
-
import ffmpeg
|
6 |
-
|
7 |
-
MODEL_NAME = "openai/whisper-large-v3"
|
8 |
-
|
9 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
-
|
12 |
-
print("[ INFO ] Device: ", device)
|
13 |
-
#torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
14 |
-
torch_dtype = torch.float32
|
15 |
-
|
16 |
-
model = WhisperForConditionalGeneration.from_pretrained(MODEL_NAME, torch_dtype=torch_dtype).to(device)
|
17 |
-
processor = WhisperProcessor.from_pretrained(MODEL_NAME)
|
18 |
-
|
19 |
-
|
20 |
-
def convert_forced_to_tokens(forced_decoder_ids):
|
21 |
-
forced_decoder_tokens = []
|
22 |
-
for i, (idx, token) in enumerate(forced_decoder_ids):
|
23 |
-
if token is not None:
|
24 |
-
forced_decoder_tokens.append([idx, processor.tokenizer.decode(token)])
|
25 |
-
else:
|
26 |
-
forced_decoder_tokens.append([idx, token])
|
27 |
-
return forced_decoder_tokens
|
28 |
-
|
29 |
-
|
30 |
-
def change_formate(input_file):
|
31 |
-
ffmpeg.input(input_file).output("output.wav", **{'ar': '16000'}).run(overwrite_output=True) #loglevel='quiet'
|
32 |
-
return "output.wav"
|
33 |
-
|
34 |
-
|
35 |
-
def generate(audio):
|
36 |
-
# audio = change_formate(audio)
|
37 |
-
input_audio, sample_rate = torchaudio.load(audio)
|
38 |
-
input_audio = torchaudio.transforms.Resample(sample_rate, 16000)(input_audio)
|
39 |
-
#metadata = torchaudio.info(audio)
|
40 |
-
#length1 = math.ceil(metadata.num_frames / metadata.sample_rate)
|
41 |
-
length = librosa.get_duration(path=audio)
|
42 |
-
|
43 |
-
input_speech = input_audio[0]
|
44 |
-
|
45 |
-
|
46 |
-
if length <= 30:
|
47 |
-
input_features = processor(input_speech,
|
48 |
-
sampling_rate=16_000,
|
49 |
-
return_tensors="pt", torch_dtype=torch_dtype).input_features.to(device)
|
50 |
-
|
51 |
-
else:
|
52 |
-
input_features = processor(input_speech,
|
53 |
-
return_tensors="pt",
|
54 |
-
truncation=False,
|
55 |
-
padding="longest",
|
56 |
-
return_attention_mask=True,
|
57 |
-
sampling_rate=16_000).input_features.to(device)
|
58 |
-
forced_decoder_ids = []
|
59 |
-
forced_decoder_ids.append([1,50270]) #[1, '<|ca|>']
|
60 |
-
forced_decoder_ids.append([2,50262]) #[2, '<|es|>']
|
61 |
-
forced_decoder_ids.append([3,50360]) #[3, '<|transcribe|>']
|
62 |
-
|
63 |
-
forced_decoder_ids_modified = forced_decoder_ids
|
64 |
-
idx = processor.tokenizer.all_special_tokens.index("<|startofprev|>")
|
65 |
-
forced_bos_token_id = processor.tokenizer.all_special_ids[idx]
|
66 |
-
|
67 |
-
prompt = " transcribe an audio containing code-switching between es and ca"
|
68 |
-
prompt_tokens = processor.tokenizer(prompt, add_special_tokens=False).input_ids
|
69 |
-
|
70 |
-
# we need to force these tokens
|
71 |
-
forced_decoder_ids = []
|
72 |
-
for idx, token in enumerate(prompt_tokens):
|
73 |
-
# indexing starts from 1 for forced tokens (token at position 0 is the SOS token)
|
74 |
-
forced_decoder_ids.append([idx + 1, token])
|
75 |
-
|
76 |
-
# now we add the SOS token at the end
|
77 |
-
offset = len(forced_decoder_ids)
|
78 |
-
forced_decoder_ids.append([offset + 1, model.generation_config.decoder_start_token_id])
|
79 |
-
|
80 |
-
# now we need to append the rest of the prefix tokens (lang, task, timestamps)
|
81 |
-
offset = len(forced_decoder_ids)
|
82 |
-
for idx, token in forced_decoder_ids_modified:
|
83 |
-
forced_decoder_ids.append([idx + offset , token])
|
84 |
-
|
85 |
-
model.config.forced_decoder_ids = forced_decoder_ids
|
86 |
-
model.generation_config.forced_decoder_ids = forced_decoder_ids
|
87 |
-
|
88 |
-
|
89 |
-
if length <= 30:
|
90 |
-
pred_ids = model.generate(input_features,
|
91 |
-
return_timestamps=True,
|
92 |
-
decoder_start_token_id=forced_bos_token_id,
|
93 |
-
max_new_tokens=128)
|
94 |
-
#exclude prompt from output
|
95 |
-
forced_decoder_tokens = convert_forced_to_tokens(forced_decoder_ids)
|
96 |
-
output = processor.decode(pred_ids[0][len(forced_decoder_tokens) + 1:], skip_special_tokens=True)
|
97 |
-
|
98 |
-
else:
|
99 |
-
pred_ids = model.generate(input_features,
|
100 |
-
return_timestamps=True,
|
101 |
-
decoder_start_token_id=forced_bos_token_id,
|
102 |
-
logprob_threshold=-1.0,
|
103 |
-
compression_ratio_threshold=1.35,
|
104 |
-
temperature=(0.0, 0.2, 0.4),
|
105 |
-
no_speech_threshold=0.1,
|
106 |
-
)
|
107 |
-
output = processor.batch_decode(pred_ids, skip_special_tokens=True)
|
108 |
-
|
109 |
-
|
110 |
-
if length <= 30:
|
111 |
-
return output[1:]
|
112 |
-
else:
|
113 |
-
return output[0]
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|