Last active
August 20, 2024 03:36
-
-
Save NN1985/a0712821269259061177c6abb08e8e0a to your computer and use it in GitHub Desktop.
ElevenLabs Text Input Streaming demo for LLMs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
import elevenlabs | |
# Uncomment the following lines to set the API keys | |
openai.api_key = "key_here" | |
elevenlabs.set_api_key("key_here") | |
def write(prompt: str): | |
for chunk in openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-0301", | |
messages=[{"role": "user", "content": prompt}], | |
stream=True, | |
): | |
# Extract the content from the chunk if available | |
if (text_chunk := chunk["choices"][0]["delta"].get("content")) is not None: | |
yield text_chunk | |
# Generate a text stream | |
text_stream = write("A five sentence story about The Fonz and his cool adventures.") | |
# Convert the text stream to an audio stream | |
audio_stream = elevenlabs.generate( | |
text=text_stream, | |
voice="Thomas", | |
stream=True, | |
latency=4, | |
) | |
# Stream the audio | |
output = elevenlabs.stream(audio_stream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment