whisper2
Browse filesSigned-off-by: Balazs Horvath <acsipont@gmail.com>
whisper2
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
6 |
+
import sys
|
7 |
+
import os
|
8 |
+
import warnings
|
9 |
+
|
10 |
+
# Suppress specific warnings
|
11 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
12 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
13 |
+
|
14 |
+
MODEL_NAME = "openai/whisper-large-v3"
|
15 |
+
BATCH_SIZE = 8
|
16 |
+
|
17 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
18 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
19 |
+
|
20 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
21 |
+
MODEL_NAME, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
22 |
+
)
|
23 |
+
model.to(device)
|
24 |
+
|
25 |
+
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
26 |
+
|
27 |
+
pipe = pipeline(
|
28 |
+
"automatic-speech-recognition",
|
29 |
+
model=model,
|
30 |
+
tokenizer=processor.tokenizer,
|
31 |
+
feature_extractor=processor.feature_extractor,
|
32 |
+
# max_new_tokens=448,
|
33 |
+
chunk_length_s=30,
|
34 |
+
batch_size=BATCH_SIZE,
|
35 |
+
return_timestamps=True,
|
36 |
+
torch_dtype=torch_dtype,
|
37 |
+
device=device,
|
38 |
+
)
|
39 |
+
|
40 |
+
def transcribe(audio_file_path, task="transcribe"):
|
41 |
+
if not os.path.exists(audio_file_path):
|
42 |
+
print(f"Error: The file '{audio_file_path}' does not exist.")
|
43 |
+
return
|
44 |
+
|
45 |
+
try:
|
46 |
+
with torch.no_grad():
|
47 |
+
result = pipe(audio_file_path, generate_kwargs={"task": task})
|
48 |
+
from pprint import pprint
|
49 |
+
pprint(result)
|
50 |
+
return result["text"]
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error during transcription: {str(e)}")
|
53 |
+
return None
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
if len(sys.argv) < 2:
|
57 |
+
print("Usage: python script.py <audio_file_path> [task]")
|
58 |
+
print("task can be 'transcribe' or 'translate' (default is 'transcribe')")
|
59 |
+
sys.exit(1)
|
60 |
+
|
61 |
+
audio_file_path = sys.argv[1]
|
62 |
+
task = sys.argv[2] if len(sys.argv) > 2 else "transcribe"
|
63 |
+
|
64 |
+
if task not in ["transcribe", "translate"]:
|
65 |
+
print("Error: task must be either 'transcribe' or 'translate'")
|
66 |
+
sys.exit(1)
|
67 |
+
|
68 |
+
result = transcribe(audio_file_path, task)
|
69 |
+
if result:
|
70 |
+
print("Transcription result:")
|
71 |
+
print(result)
|
72 |
+
|