Skip to content

Commit 666b655

Browse files
committed
add openai api to the tts tutorial
1 parent 45b9613 commit 666b655

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

machine-learning/text-to-speech/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
- `pip3 install -r requirements.txt`
33
- To convert text to speech online using Google API, use `tts_google.py`
44
- To use offline engines in your platform, consider using `tts_pyttsx3.py`
5+
- To use the OpenAI API, use `tts_openai.py`
56
- To use transformers, use `tts_transformers.py`

machine-learning/text-to-speech/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ playsound
44
soundfile
55
transformers
66
datasets
7-
sentencepiece
7+
sentencepiece
8+
openai
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from openai import OpenAI
2+
3+
# initialize the OpenAI API client
4+
api_key = "YOUR_OPENAI_API_KEY"
5+
client = OpenAI(api_key=api_key)
6+
7+
# sample text to generate speech from
8+
text = """In his miracle year, he published four groundbreaking papers.
9+
These outlined the theory of the photoelectric effect, explained Brownian motion,
10+
introduced special relativity, and demonstrated mass-energy equivalence."""
11+
12+
# generate speech from the text
13+
response = client.audio.speech.create(
14+
model="tts-1", # the model to use, there is tts-1 and tts-1-hd
15+
voice="nova", # the voice to use, there is alloy, echo, fable, onyx, nova, and shimmer
16+
input=text, # the text to generate speech from
17+
speed=1.0, # the speed of the generated speech, ranging from 0.25 to 4.0
18+
)
19+
# save the generated speech to a file
20+
response.stream_to_file("openai-output.mp3")

0 commit comments

Comments
 (0)