lugia19 / elevenlabslib

Full python wrapper for the elevenlabs API.
MIT License
150 stars 27 forks source link

ERROR:root:We're not at the end, yet we recieved less data than expected. THIS SHOULD NOT HAPPEN. #24

Closed deama closed 8 months ago

deama commented 8 months ago

I'm having the issue with:

ERROR:root:We're not at the end, yet we recieved less data than expected. THIS SHOULD NOT HAPPEN.

popping up after I try and stream it. Could this be fixed? I'm thinking it needs maybe more time in the buffer before it starts playing it?

Here's my code:

from elevenlabslib import *
import sys
import time

# Initialize the ElevenLabs API
api_key = "[api-key]"
user = ElevenLabsUser(api_key)
voice = user.get_voices_by_name("Adam")[0]

# Audio generation options
playbackOptions = PlaybackOptions(runInBackground=True)
generationOptions = GenerationOptions(model_id="eleven_english_v2", stability=0.5, similarity_boost=1.0, style=0.2, use_speaker_boost=True)

# Generate and play the audio
try:
    audioStreamFuture = voice.generate_stream_audio_v2(sys.argv[1], playbackOptions, generationOptions)[1]
    audioStream = audioStreamFuture.result()

    # Check if the audio stream is ready and active
    while not audioStream.active:
        time.sleep(0.1)  # Wait for the audio stream to become active

    # This loop ensures the audio plays to completion
    while audioStream.active:
        time.sleep(0.1)

except Exception as e:
    print(f"An error occurred: {e}")
lugia19 commented 8 months ago

The error suggests you're on an old version of the library - be sure to update to the latest one.

Additionally, here's how you can wait on the audio to finish playing in a better way:

import threading
from elevenlabslib import *
import sys

# Initialize the ElevenLabs API
api_key = "[api-key]"
user = ElevenLabsUser(api_key)
voice = user.get_voices_by_name("Adam")[0]

# Audio generation options
playback_end_event = threading.Event()
playbackOptions = PlaybackOptions(runInBackground=True, onPlaybackEnd=playback_end_event.set)
generationOptions = GenerationOptions(model_id="eleven_english_v2", stability=0.5, similarity_boost=1.0, style=0.2, use_speaker_boost=True)

# Generate and play the audio
try:
    voice.generate_stream_audio_v2(sys.argv[1], playbackOptions, generationOptions)

    #Wait for the audio to play to completion:
    playback_end_event.wait()

except Exception as e:
    print(f"An error occurred: {e}")
deama commented 8 months ago

Ah thanks, that seemed to do the trick, I had to force update it with

pip install --upgrade elevenlabslib

deama commented 8 months ago

Hey sorry to reopen, but now the issue seems to be that it's not closing properly after playing the audio, the python script in task manager remains open until I force close it, here's the full code so far:

import threading
from elevenlabslib import *
import sys

# Initialize the ElevenLabs API
api_key = "[API]"
user = ElevenLabsUser(api_key)
voice = user.get_voices_by_name("Adam")[0]

# Audio generation options
playback_end_event = threading.Event()
playbackOptions = PlaybackOptions(runInBackground=True, onPlaybackEnd=playback_end_event.set)
generationOptions = GenerationOptions(model_id="eleven_english_v2", stability=0.5, similarity_boost=1.0, style=0.2, use_speaker_boost=True)

# Generate and play the audio
try:
    voice.generate_stream_audio_v2(sys.argv[1], playbackOptions, generationOptions)

    #Wait for the audio to play to completion:
    playback_end_event.wait()

except Exception as e:
    print(f"An error occurred: {e}")

Possibly it's due to the library update? It was working fine on the old version 0.10 except for that bug I mentioned earlier that I had before, but on 0.14 it doesn't close?

I don't think it's a threading issue per sey, I tried my old method as well and it gave the same issue.

EDIT: After playing around with it, it appears to get stuck after giving it text that is longer than a specific length, e.g. the following text keeps getting it stuck:

I'm happy for the Iron Tower and all, congratulations on another release. But as a player, to me it's an extremely verbose game. I'll give it another try. But I'll likely drop it. And then wait for the game to become moddable + for ChatGPT to be able to shorten text precisely as instructed, then mod it and play as it should be played, with 90% of the text thrown out.

When it gets stuck, I can also see the memory keep increasing indefinitely.

lugia19 commented 8 months ago

That's... certainly interesting. Let me take a look at what's going on.

lugia19 commented 8 months ago

I actually can't reproduce this at all - it stops running correctly. Can you set logging level to debug and send over the log?

deama commented 8 months ago

How do set the logging level to debug?

lugia19 commented 8 months ago

Add the line logging.basicConfig(level=logging.DEBUG) to the start of the script (you'll need to import logging)

lugia19 commented 8 months ago

Okay, nevermind, I have managed to reproduce it - it just took a much longer text. I'll look into it tomorrow and post any updates.

deama commented 8 months ago

It gave this monstrosity:

output.zip

DEBUG:root:Putting 8192 bytes in queue.
DEBUG:root:The following is some logging for a new and fun soundfile bug, which I (poorly) worked around. Lovely.
DEBUG:root:Before the bug: frame 1140480 (byte 413779)
DEBUG:root:After the bug: frame 1140480 (byte 413779)
DEBUG:root:Fun bug happened AFTER the download was over. Continue.
DEBUG:root:Done recreating, now at 1140480 (byte 412525).
DEBUG:root:Read bytes: 8192

What the hell is that?

lugia19 commented 8 months ago

Should be fixed in 0.14.2 - give it a shot and let me know.

deama commented 8 months ago

Nice, works, thanks!