Skip to content

Instantly share code, notes, and snippets.

@hamishdickson
Created February 6, 2024 12:45
Show Gist options
  • Save hamishdickson/74c4868dc9932355721120fce25d027a to your computer and use it in GitHub Desktop.
Save hamishdickson/74c4868dc9932355721120fce25d027a to your computer and use it in GitHub Desktop.
async def chat_completion():
"""Retrieve text from OpenAI and pass it to the text-to-speech function."""
# Existing code ...
print(f"time to generate response: {time.time() - start}")
# Select a filler word randomly and send it for synthesis
filler_word = random.choice(["um", "er", "hmm"])
await send_filler_to_speech(filler_word, CONFIG.voice_id)
# Existing code continues...
async def send_filler_to_speech(filler_text, voice_id):
"""Send a filler text to ElevenLabs API for immediate speech synthesis."""
uri = f"wss://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream-input?model_id={CONFIG.voice_model}&output_format={CONFIG.format}"
async with websockets.connect(uri) as websocket:
await websocket.send(
json.dumps(
{
"text": filler_text,
"voice_settings": {
"stability": CONFIG.stability,
"similarity_boost": CONFIG.similarity_boost,
"use_speaker_boost": CONFIG.use_speaker_boost,
"style": 0,
},
"xi_api_key": os.getenv("ELEVEN_API_KEY"),
}
)
)
# Listen for a brief moment for the audio data and then close the connection
try:
message = await websocket.recv()
data = json.loads(message)
if data.get("audio"):
audio_bytes = base64.b64decode(data["audio"])
sio_client = st.session_state.sio
sio_client.emit("audioData", {"audioData": base64.b64encode(audio_bytes).decode("utf-8")})
await stream([audio_bytes]) # Use the existing stream function to play audio
except websockets.exceptions.ConnectionClosed:
print("Connection closed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment