-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.py
42 lines (32 loc) · 876 Bytes
/
voice.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
import os
import sys
from datetime import datetime
from dotenv import load_dotenv
import openai
from openai import OpenAI
from pathlib import Path
load_dotenv()
# Fail if no commandline argument is provided
if len(sys.argv) < 2:
print("Please provide an text")
exit(1)
text_prompt = sys.argv[1]
client = OpenAI()
speech_file_path = ""
# Stream audio and save it to file in the voice folder
if not os.path.isdir("voice"):
os.mkdir("voice")
cur_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
try:
speech_file_path = f"voice/{cur_time}.mp3"
response = client.audio.speech.create(
model="tts-1",
voice=os.getenv("SPEECH_VOICE") or "alloy",
input=text_prompt
)
response.stream_to_file(speech_file_path)
except openai.OpenAIError as e:
print(e.http_status)
print(e.error)
exit(1)
print(speech_file_path)