No success using async client from ElevenLabs python library

Hi, because of the timeouts, I wanted to use the async call for ElevenLabs.

However, while I get a “success”, no exports, no logs.

import os
import asyncio
import elevenlabs
from elevenlabs.client import AsyncElevenLabs
import tempfile

client = AsyncElevenLabs(
    api_key=os.environ["ELEVENLABS_API_KEY"],
)

async def generate_speech(text, voice_id):
    audio = await client.generate(
        text=text,
        voice=voice_id,
        model="eleven_multilingual_v2"
    )

    # Get the request ID from the response headers
    request_id = response.headers.get("x-request-id", "N/A")
        
    # Log the request ID
    print(f"Request ID: {request_id}")
    
    # Consume the async generator and convert to bytes
    audio_bytes = b''.join([chunk async for chunk in audio])
    
    # Create a temporary file to store the audio
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
        temp_file.write(audio_bytes)
        temp_file_path = temp_file.name
    
    print(f"Generated audio file: {temp_file_path}")

    return temp_file_path

async def handler(pd: "pipedream"):
    # Reference data from previous steps
    text_array = pd.steps["code"]["$return_value"]["audioParts"]
    voice_id = pd.steps["firebase_admin_sdk_get_compaction_doc"]["$return_value"]["voice_id"]
    
    print(f"Voice ID: {voice_id}")  # Log the voice_id

    # Generate speech for each string in the array concurrently
    audio_file_tasks = [generate_speech(text, voice_id) for text in text_array]
    audio_files = await asyncio.gather(*audio_file_tasks)
    
    console.log(f"Generated audio files: {audio_files}")  # Log the audio_files list

    # Return data for use in future steps
    return {"audio_files": audio_files}

@pi

Hi @Timfong888,

I’m sorry, but it seems like your question got cut off. Could you please provide more details or clarify your question regarding Pipedream? I’m here to help!

I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double-check my answers and code! Learn how to build your own.