-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from TomTom101/TomTom101/openai_tts
Added OpenAI TTS (#196)
- Loading branch information
Showing
2 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import asyncio | ||
import unittest | ||
|
||
import openai | ||
import pyaudio | ||
from dotenv import load_dotenv | ||
|
||
from pipecat.frames.frames import AudioRawFrame, ErrorFrame | ||
from pipecat.services.openai import OpenAITTSService | ||
|
||
load_dotenv() | ||
|
||
|
||
class TestWhisperOpenAIService(unittest.IsolatedAsyncioTestCase): | ||
async def test_whisper_tts(self): | ||
pa = pyaudio.PyAudio() | ||
stream = pa.open(format=pyaudio.paInt16, | ||
channels=1, | ||
rate=24_000, | ||
output=True) | ||
|
||
tts = OpenAITTSService(voice="nova") | ||
|
||
async for frame in tts.run_tts("Hello, there. Nice to meet you, seems to work well"): | ||
self.assertIsInstance(frame, AudioRawFrame) | ||
stream.write(frame.audio) | ||
|
||
await asyncio.sleep(.5) | ||
stream.stop_stream() | ||
pa.terminate() | ||
|
||
tts = OpenAITTSService(voice="invalid_voice") | ||
with self.assertRaises(openai.BadRequestError): | ||
async for frame in tts.run_tts("wont work"): | ||
self.assertIsInstance(frame, ErrorFrame) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |