How to fix azure.core.exceptions.ServiceResponseError: ('Connection aborted.', TimeoutError('The write operation timed out'))

Kokou sitsope SEKPONA 0 Reputation points Student Ambassador
2024-03-13T02:15:40.55+00:00

I'm using this code for text to speech with python sdk

But i from yesterday, i get 'Connection aborted.', TimeoutError'. I'm in django and i'm using azure as backend storage .

This is my code:

def ssml_to_audio( ssml_text, username): 
  audio , created=Myvideo.objects.get_or_create(user=username)
  if created:
    audio.save()
  print("=="*5,"ssml_to_audio", "=="*5)
  audio.audio.name= f"audio/{username}.mp3"
  audio.save()
  #final_audio=AudioSegment.silent(duration=round(100))
  speech_config = speechsdk.SpeechConfig(subscription="c5e22d45782b4f41af9265e7ee868df0", region="eastus")
  audio_config = speechsdk.audio.PullAudioOutputStream()

  speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
  for ssml in ssml_text:
        #print("iteration: ",i, ssml)
        speech_synthesis_result = speech_synthesizer.speak_ssml_async(ssml).get()# speak_text_async(text).get()
        if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
            #final_audio+=AudioSegment.from_file(BytesIO(speech_synthesis_result.audio_data))
             audio.audio.save(name =f"audio/{username}.mp3", content=ContentFile(speech_synthesis_result.audio_data), save=False)
        elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = speech_synthesis_result.cancellation_details
            print("Speech synthesis canceled: {}".format(cancellation_details.reason))
            if cancellation_details.reason == speechsdk.CancellationReason.Error:
                if cancellation_details.error_details:
                    print("Error details: {}".format(cancellation_details.error_details))
                    print("Did you set the speech resource key and region values?")
  audio.save()
  print("Model  upload pret")
  #with audio.audio.open(mode="wb") as audio_file:
    #final_audio.export(audio_file, "mp3")
  outurl = f"https://youtubeconvstorage.blob.core.windows.net/media/audio/{username}.mp3"
  print(f"Audio file saved as {outurl}")
  return outurl

Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,369 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
914 questions
{count} votes

1 answer

Sort by: Most helpful
  1. santoshkc 3,770 Reputation points Microsoft Vendor
    2024-03-13T09:21:37.33+00:00

    Hi @Kokou sitsope SEKPONA,

    Thank you for reaching out to Microsoft Q&A forum!

    Regarding your issues 'Connection aborted' and 'TimeoutError', I suggest you to check your Network connection, Azure credentials and also check Azure storage account is running and accessible. I repro the below code for text to speech with python sdk without any errors:

    import azure.cognitiveservices.speech as speechsdk
    from azure.storage.blob import BlobServiceClient, ContentSettings
    
    # Azure Speech SDK Configuration
    speech_config = speechsdk.SpeechConfig(subscription='KEY', region='REGION')
    speech_config.speech_synthesis_voice_name = 'en-US-JennyNeural'
    
    # Azure Storage configuaration
    connection_string = 'your_storage_account_connection_string' 
    container_name = 'your_container_name'
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    blob_container_client = blob_service_client.get_container_client(container_name)
    
    # Text to be synthesized
    text = "I'm excited to try text to speech"
    
    # Speech Synthesis
    speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
    speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
    if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
        print("Speech synthesized for text [{}]".format(text))
    
        # Upload audio to Azure Storage
        audio_data = speech_synthesis_result.audio_data
        blob_name = f"audio/{text.replace(' ', '_')}.wav"
        blob_container_client.upload_blob(name=blob_name, data=audio_data, content_settings=ContentSettings(content_type='audio/wav'))
        print(f"Audio content saved to Azure Storage: {blob_name}")
    
    elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = speech_synthesis_result.cancellation_details
        print("Speech synthesis canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            if cancellation_details.error_details:
                print("Error details: {}".format(cancellation_details.error_details))
                print("Did you set the speech resource key and region values?")
    
    

    For more info see: Text-to-speech-overview.

    I hope you understand! Thank you.

    0 comments No comments