I have my Nodejs backend that’s making a call to whisper API. I use the openai.audio.transcriptions.create call.
However, I repeatedly get the same error stating “Openai API Error: connection error”. All my api calls have failed even though they previously worked. I have even switched internet providers but still encountered the exact same error.
I have even gone to an extent of intentionally giving the wrong API credentials yet this time the error rightly stated that I had used wrong credentials. So I’m wondering why the connection error I every consistent.
I encountered the same problem. Really annoying. First I thought it was the filesize. The api says no files larger than 25 mb. So I cut them into pieces. Each piece is 4.58 mb and the last one is 1.82 mb.
Some chunks get transcribed, and some are not getting transcribed because of error “Connection error”. When I run again, sometimes other chunks are transcribed. Sometimes no chunks are transcribed at all. Really confusing.
Here is my code:
import os
from dotenv import load_dotenv
from pydub import AudioSegment
from openai import OpenAI
# Load environment variables
load_dotenv()
# Create an API client
client = OpenAI()
MAX_FILE_SIZE_MB = 25 # Whisper's file size limit in MB
def transcribe_chunk(audio_chunk, chunk_index):
# Export the chunk to a temporary file
temp_file = f"temp_chunk_{chunk_index}.mp3"
print(f"🔄 Exporting chunk {chunk_index} to {temp_file}...")
audio_chunk.export(temp_file, format="mp3")
# Check the file size
file_size_mb = os.path.getsize(temp_file) / (1024 * 1024) # Convert bytes to MB
print(f"ℹ️ Chunk {chunk_index} file size: {file_size_mb:.2f} MB")
if file_size_mb > MAX_FILE_SIZE_MB:
print(f"⚠️ Chunk {chunk_index} is too large ({file_size_mb:.2f} MB). Skipping transcription.")
return None
try:
with open(temp_file, "rb") as audio_file:
print(f"🚀 Sending chunk {chunk_index} to Whisper for transcription...")
transcription = client.audio.transcriptions.create(
model="whisper-1",
language="en",
file=audio_file,
response_format="text"
)
print(f"✅ Received transcription for chunk {chunk_index}")
return transcription
except Exception as e:
print(f"❌ An error occurred while transcribing chunk {chunk_index}: {str(e)}")
return None
finally:
# Clean up the temporary file
if os.path.exists(temp_file):
os.remove(temp_file)
print(f"🗑️ Temporary file {temp_file} deleted.")
def transcribe_audio_sequentially(audio_file_path, chunk_duration_minutes=10):
print(f"🔄 Loading audio file: {audio_file_path}...")
audio = AudioSegment.from_mp3(audio_file_path)
print(f"✅ Audio file loaded successfully.")
# Split the audio into chunks
print(f"🔄 Splitting audio into {chunk_duration_minutes}-minute chunks...")
chunk_duration_ms = chunk_duration_minutes * 60 * 1000
chunks = [audio[i:i + chunk_duration_ms] for i in range(0, len(audio), chunk_duration_ms)]
print(f"✅ Audio split into {len(chunks)} chunks.")
# Prepare for transcription
transcriptions = []
transcribed_chunks = []
skipped_chunks = []
# Transcribe chunks sequentially
for chunk_index, chunk in enumerate(chunks):
print(f"🔄 Processing chunk {chunk_index} of {len(chunks) - 1}...")
transcription = transcribe_chunk(chunk, chunk_index)
transcriptions.append(transcription)
if transcription:
transcribed_chunks.append(chunk_index)
else:
skipped_chunks.append(chunk_index)
# Concatenate transcriptions in the correct order
print("🔄 Concatenating transcriptions...")
full_transcription = "".join(filter(None, transcriptions)) # filter out None values
print("✅ Transcriptions concatenated successfully.")
# Save the transcription
base_name = os.path.splitext(os.path.basename(audio_file_path))[0]
output_file = f"transcriptions/{base_name}.txt"
print(f"💾 Saving transcription to {output_file}...")
os.makedirs("transcriptions", exist_ok=True)
with open(output_file, "w", encoding="utf-8") as text_file:
text_file.write(full_transcription)
print(f"✅ Full transcription saved to {output_file}.")
print("🎉 Transcription process completed successfully.")
# Final summary of transcribed and skipped chunks
print("\n📊 Summary:")
print(f"✅ Chunks transcribed: {transcribed_chunks}")
print(f"⚠️ Chunks skipped: {skipped_chunks}")
if skipped_chunks:
print("⚠️ Some chunks were not transcribed. Please check the log for details.")
if __name__ == "__main__":
# Example usage
audio_file_path = "audio_files/soup/filename.mp3"
transcribe_audio_sequentially(audio_file_path)
Can you post some logs of this script running? can you also add timestamps to the log so we can see how fast this is all operating at, wondering if you are trying to send many requests at once.