-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatvox.py
54 lines (46 loc) · 1.38 KB
/
chatvox.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
47
48
49
50
51
52
53
54
#Import modules
import openai
import time
import subprocess
import pyaudio
# Set up the OpenAI API key and model ID
openai.api_key = ""
model_engine = "davinci"
# Set up the Voicevox path and voice ID
voicevox_path = "/path/to/voicevox"
voice_id = "YOUR_VOICE_ID"
# Keep track of the latest response ID
latest_response_id = ""
# Main loop
while True:
# Retrieve the latest response from ChatGPT
response = openai.Completion.create(
engine=model_engine,
prompt="Your prompt here",
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# Check if there's a new response to read out
if response["id"] != latest_response_id:
latest_response_id = response["id"]
text = response["choices"][0]["text"].strip()
# Generate the audio file using Voicevox
subprocess.run([voicevox_path, "--text", text, "--output", "output.wav", "--speaker", voice_id])
CHUNK = 1024
wf = wave.open("output.wav", "rb")
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(CHUNK)
while data:
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
# Wait for a bit before checking for new responses again
time.sleep(1)