|
|
|
|
|
|
|
import torch |
|
from transformers import pipeline |
|
import sys |
|
import os |
|
|
|
MODEL_NAME = "openai/whisper-large-v3-turbo" |
|
BATCH_SIZE = 8 |
|
|
|
device = 0 if torch.cuda.is_available() else "cpu" |
|
|
|
pipe = pipeline( |
|
task="automatic-speech-recognition", |
|
model=MODEL_NAME, |
|
chunk_length_s=30, |
|
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: |
|
text = pipe(audio_file_path, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"] |
|
return 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) |
|
|