Created
October 17, 2024 22:24
-
-
Save altryne/cb6937931886e3fcd76dc02f3ec0e15c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 base64 | |
import os | |
from openai import OpenAI | |
import pyaudio | |
import wave | |
import weave | |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | |
def get_audio_stream(): | |
# Initialize PyAudio | |
p = pyaudio.PyAudio() | |
# Open an audio stream | |
stream = p.open(format=pyaudio.paInt16, | |
channels=1, | |
rate=22050, # Adjust if needed | |
output=True, | |
frames_per_buffer=4096) | |
# Open a wave file for writing | |
with wave.open("output.wav", "wb") as wav_file: | |
wav_file.setnchannels(1) # Mono | |
wav_file.setsampwidth(2) # 16-bit | |
wav_file.setframerate(22050) # Sample rate (adjust if needed) | |
completion = client.chat.completions.create( | |
model="gpt-4o-audio-preview", | |
modalities=["text", "audio"], | |
audio={"voice": "fable", "format": "pcm16"}, | |
stream=True, | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": "Trick or treat."} | |
] | |
) | |
for chunk in completion: | |
if hasattr(chunk, 'choices') and len(chunk.choices) > 0: | |
if hasattr(chunk.choices[0].delta, 'audio') and chunk.choices[0].delta.audio.get('data') is not None: | |
audio_data = base64.b64decode(chunk.choices[0].delta.audio.get('data')) | |
# Write to audio stream | |
stream.write(audio_data) | |
# Write to wave file | |
wav_file.writeframes(audio_data) | |
# Close and terminate PyAudio | |
stream.stop_stream() | |
stream.close() | |
p.terminate() | |
return stream.read(4096) | |
# Call the function | |
get_audio_stream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment