Skip to content

Instantly share code, notes, and snippets.

@NN1985
Last active August 20, 2024 03:36
Show Gist options
  • Save NN1985/a0712821269259061177c6abb08e8e0a to your computer and use it in GitHub Desktop.
Save NN1985/a0712821269259061177c6abb08e8e0a to your computer and use it in GitHub Desktop.
ElevenLabs Text Input Streaming demo for LLMs
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