Modify the following python code so as not to process chunks serially. Indeed, I believe…
- User
Modify the following python code so as not to process chunks serially. Indeed, I believe that batch is useless because chunks are processed serially. It takes a very, very long time to turn and it doesn't stop. Python code: `import torch from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline from pydub import AudioSegment
wav_file_path = "/content/Deleuze.wav"
device = "cuda:0" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "openai/whisper-large-v3"
model = AutoModelForSpeechSeq2Seq.from_pretrained( model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True ) model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline( "automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, chunk_length_s=1000, # Keep this larger to reduce word splitting batch_size=10, # Process one chunk at a time torch_dtype=torch_dtype, device=device, )
Define chunk duration and overlap in milliseconds
chunk_duration_ms = 800 * 1000 # 120 seconds overlap_duration_ms = 5 * 1000 # 5 seconds overlap audio_length_ms = len(audio) start_time = 0
List to store transcriptions
transcriptions = []
Process each chunk of the audio
while start_time < audio_length_ms: end_time = min(start_time + chunk_duration_ms, audio_length_ms) chunk = audio[start_time:end_time]
# Export the chunk to a temporary file chunk_file_path = "/content/sample_data/chunk.wav" chunk.export(chunk_file_path, format="wav") # Use the pipeline to transcribe the chunk result = pipe(chunk_file_path) # Append the result for the current chunk to the list transcriptions.append(result["text"]) # Update start time for the next chunk, with overlap start_time = end_time - overlap_duration_msWrite all transcriptions to output.txt
with open("/content/sample_data/output.txt", "w") as output_file: for transcription in transcriptions: output_file.write(transcription + "\n") # Add newline for separation
print("Transcription completed and saved to output.txt")`
- Assistant