File size: 2,105 Bytes
7174111 |
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 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import sys
import os
import warnings
# Suppress specific warnings
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
MODEL_NAME = "openai/whisper-large-v3"
BATCH_SIZE = 8
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = AutoModelForSpeechSeq2Seq.from_pretrained(
MODEL_NAME, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)
processor = AutoProcessor.from_pretrained(MODEL_NAME)
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
# max_new_tokens=448,
chunk_length_s=30,
batch_size=BATCH_SIZE,
return_timestamps=True,
torch_dtype=torch_dtype,
device=device,
)
def transcribe(audio_file_path, task="transcribe"):
if not os.path.exists(audio_file_path):
print(f"Error: The file '{audio_file_path}' does not exist.")
return
try:
with torch.no_grad():
result = pipe(audio_file_path, generate_kwargs={"task": task})
from pprint import pprint
pprint(result)
return result["text"]
except Exception as e:
print(f"Error during transcription: {str(e)}")
return None
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <audio_file_path> [task]")
print("task can be 'transcribe' or 'translate' (default is 'transcribe')")
sys.exit(1)
audio_file_path = sys.argv[1]
task = sys.argv[2] if len(sys.argv) > 2 else "transcribe"
if task not in ["transcribe", "translate"]:
print("Error: task must be either 'transcribe' or 'translate'")
sys.exit(1)
result = transcribe(audio_file_path, task)
if result:
print("Transcription result:")
print(result)
|