-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathaudio.py
executable file
·38 lines (28 loc) · 933 Bytes
/
audio.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
38
#!/usr/bin/env rye run python
from pathlib import Path
from openai import OpenAI
# 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()