Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import whisperx as wx
- from pydub import AudioSegment
- import os
- from dotenv import load_dotenv
- from config import AUDIO_FILE_DIR
- # Load .env file if present
- load_dotenv()
- device = "cuda"
- batch_size = 16
- compute_type = "int8"
- model_dir = "C:\\test"
- language = "en"
- model = wx.load_model("tiny", device, language=language, compute_type=compute_type, download_root=model_dir)
- def transcribe_audio(file_path):
- try:
- audio = AudioSegment.from_file(f"{AUDIO_FILE_DIR}/{file_path}")
- chunk_size = 10 * 60 * 1000
- num_chunks = len(audio) // chunk_size + (1 if len(audio) % chunk_size else 0)
- transcript = ""
- file_size = os.path.getsize(f"{AUDIO_FILE_DIR}/{file_path}")
- if file_size <= 24 * 1024 * 1024:
- result = model.transcribe(f"{AUDIO_FILE_DIR}/{file_path}", batch_size=batch_size)
- for segment in result['segments']:
- transcript += segment['text'] + " "
- else:
- for i in range(num_chunks):
- temp_chunk_path = f"{AUDIO_FILE_DIR}/temp_chunk.mp3"
- chunk = audio[i*chunk_size:(i+1)*chunk_size]
- with open(temp_chunk_path, 'wb') as f:
- chunk.export(f, format="mp3")
- try:
- result = model.transcribe(temp_chunk_path, batch_size=batch_size)
- for segment in result['segments']:
- transcript += segment['text'] + " "
- finally:
- os.remove(temp_chunk_path)
- os.remove(f"{AUDIO_FILE_DIR}/{file_path}")
- return transcript
- except FileNotFoundError as e:
- raise FileNotFoundError(f"The audio file {file_path} was not found.") from e
- except Exception as e:
- raise Exception(f"An error occurred during the transcription process: {e}") from e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement