-
Notifications
You must be signed in to change notification settings - Fork 1
/
openai-tts.py
46 lines (34 loc) · 1.05 KB
/
openai-tts.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
39
40
41
42
43
44
45
46
import json
import asyncio
from pathlib import Path
from openai import OpenAI
voices = {
'алесь': 'onyx',
'мара': 'shimmer',
'ron': 'onyx',
'hermiona': 'shimmer'
}
json_file_path = 'text-en.json'
async def synthesize_speech(text, voice, id):
id_str = str(id)
print("№" + id_str + " started")
client = OpenAI()
file_path = "speech" + id_str + ".mp3";
speech_file_path = Path(__file__).parent / file_path
response = client.audio.speech.create(
model="tts-1-hd",
voice=voice,
input=text
)
response.stream_to_file(speech_file_path)
print("№" + id_str + " done")
async def voice_dialogues(json_file, voices):
with open(json_file, 'r', encoding='utf-8') as file:
dialogues = json.load(file)
for dialogue in dialogues:
speaker = dialogue['name']
text = dialogue['text']
id = dialogue['id']
voice = voices.get(speaker, 'default')
await synthesize_speech(text, voice, id)
asyncio.run(voice_dialogues(json_file_path, voices))