File size: 1,376 Bytes
ec7bd39
 
 
 
1f9dab2
ec7bd39
 
 
1f9dab2
 
ec7bd39
1f9dab2
ec7bd39
 
1f9dab2
 
 
ec7bd39
 
 
1f9dab2
 
 
 
 
 
 
 
 
 
 
ec7bd39
 
1f9dab2
 
 
 
 
 
 
 
 
 
ec7bd39
 
1f9dab2
 
 
 
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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)