-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_speech.py
37 lines (30 loc) · 991 Bytes
/
text_to_speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
from pathlib import Path
from openai import OpenAI
from dotenv import load_dotenv,find_dotenv
load_dotenv(find_dotenv())
# gets OPENAI_API_KEY from your environment variables
openai = OpenAI()
speech_file_path = Path(__file__).parent / "speech.mp3"
def main() -> None:
# Create text-to-speech audio file
with openai.audio.speech.with_streaming_response.create(
model="tts-1",
voice="alloy",
input="the quick brown fox jumped over the lazy dogs",
) as response:
response.stream_to_file(speech_file_path)
# Create transcription from audio file
transcription = openai.audio.transcriptions.create(
model="whisper-1",
file=speech_file_path,
)
print(transcription.text)
# Create translation from audio file
translation = openai.audio.translations.create(
model="whisper-1",
file=speech_file_path,
)
print(translation.text)
if __name__ == "__main__":
main()