FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
openai-python/examples/audio.py at main · yaohwang/openai-python · GitHub
yaohwang
openai-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
openai-python
/
examples
/
audio.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
64 lines (47 loc) · 1.78 KB
Breadcrumbs
openai-python
/
examples
/
audio.py
Copy path
File metadata and controls
executable file
·
64 lines (47 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env rye run python
import
time
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
:
stream_to_speakers
()
# 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
)
def
stream_to_speakers
()
->
None
:
import
pyaudio
player_stream
=
pyaudio
.
PyAudio
().
open
(
format
=
pyaudio
.
paInt16
,
channels
=
1
,
rate
=
24000
,
output
=
True
)
start_time
=
time
.
time
()
with
openai
.
audio
.
speech
.
with_streaming_response
.
create
(
model
=
"tts-1"
,
voice
=
"alloy"
,
response_format
=
"pcm"
,
# similar to WAV, but without a header chunk at the start.
input
=
"""I see skies of blue and clouds of white
The bright blessed days, the dark sacred nights
And I think to myself
What a wonderful world"""
,
)
as
response
:
print
(
f"Time to first byte:
{
int
((
time
.
time
()
-
start_time
)
*
1000
)
}
ms"
)
for
chunk
in
response
.
iter_bytes
(
chunk_size
=
1024
):
player_stream
.
write
(
chunk
)
print
(
f"Done in
{
int
((
time
.
time
()
-
start_time
)
*
1000
)
}
ms."
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL