forked from ardha27/AI-Waifu-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
219 lines (190 loc) · 6.97 KB
/
run.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import openai
import winsound
import sys
import pytchat
import time
import re
import pyaudio
import keyboard
import wave
import threading
import json
from config import *
from utils.translate import *
from utils.TTS import *
from utils.subtitle import *
from utils.promptMaker import *
# to help the CLI write unicode characters to the terminal
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
# use your own API Key, you can get it from https://openai.com/. I place my API Key in a separate file called config.py
openai.api_key = api_key
conversation = []
# Create a dictionary to hold the message data
history = {"history": conversation}
mode = 0
total_characters = 0
chat = ""
chat_now = ""
chat_prev = ""
is_Speaking = False
owner_name = "Ardha"
# function to get the user's input audio
def record_audio():
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
WAVE_OUTPUT_FILENAME = "input.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
frames = []
print("Recording...")
while keyboard.is_pressed('RIGHT_SHIFT'):
data = stream.read(CHUNK)
frames.append(data)
print("Stopped recording.")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
transcribe_audio("input.wav")
# function to transcribe the user's audio
def transcribe_audio(file):
global chat_now
try:
audio_file= open(file, "rb")
# Translating the audio to English
# transcript = openai.Audio.translate("whisper-1", audio_file)
# Transcribe the audio to detected language
transcript = openai.Audio.transcribe("whisper-1", audio_file)
chat_now = transcript.text
print ("Question: " + chat_now)
except:
print("Error transcribing audio")
return
result = owner_name + " said " + chat_now
conversation.append({'role': 'user', 'content': result})
openai_answer()
# function to get an answer from OpenAI
def openai_answer():
global total_characters, conversation
for item in conversation:
if isinstance(item, dict) and "content" in item:
content = item["content"]
total_characters += len(content)
while total_characters > 4000:
try:
# print(total_characters)
# print(len(conversation))
conversation.pop(2)
total_characters -= len(conversation[2]["content"])
except:
print("Error: Prompt too long!")
with open("conversation.json", "w", encoding="utf-8") as f:
# Write the message data to the file in JSON format
json.dump(history, f, indent=4)
prompt = getPrompt()
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=prompt,
max_tokens=128,
temperature=1,
top_p=0.9
)
message = response['choices'][0]['message']['content']
conversation.append({'role': 'assistant', 'content': message})
translate_text(message)
# function to capture livechat from youtube
def get_livechat(video_id):
try:
global chat
live = pytchat.create(video_id=video_id)
# while live.is_alive():
while live.is_alive():
for c in live.get().sync_items():
# Ignore chat from the streamer and Nightbot, change this if you want to include the streamer's chat
if c.author.name == 'Nightbot':
continue
# if not c.message.startswith("!") and c.message.startswith('#'):
if not c.message.startswith("!"):
# Remove emojis from the chat
chat_raw = re.sub(r':[^\s]+:', '', c.message)
# chat_author makes the chat look like this: "Nightbot: Hello". So the assistant can respond to the user's name
chat = c.author.name + ' berkata ' + chat_raw
print(chat)
time.sleep(1)
except KeyboardInterrupt:
print("Program stopped by user")
# translating is optional
def translate_text(text):
global is_Speaking
# subtitle will act as subtitle for the viewer
# subtitle = translate_google(text, "ID")
# tts will be the string to be converted to audio
detect = detect_google(text)
tts = translate_deeplx(text, f"{detect}", "JA")
# tts_en = translate_google(text, f"{detect}", "EN")
try:
# print("ID Answer: " + subtitle)
print("JP Answer: " + tts)
# print("EN Answer: " + tts_en)
except:
print("Error translating text")
return
# Choose between the available TTS engines
# Japanese TTS
voicevox_tts(tts)
# Silero TTS, Silero TTS can generate English, Russian, French, Hindi, Spanish, German, etc. Uncomment the line below. Make sure the input is in that language
# silero_tts(tts_en, "en", "v3_en", "en_21")
# Generate Subtitle
generate_subtitle(chat_now, text)
time.sleep(1)
# is_Speaking is used to prevent the assistant speaking more than one audio at a time
is_Speaking = True
winsound.PlaySound("test.wav", winsound.SND_FILENAME)
is_Speaking = False
# Clear the text files after the assistant has finished speaking
time.sleep(1)
with open ("output.txt", "w") as f:
f.truncate(0)
with open ("chat.txt", "w") as f:
f.truncate(0)
def preparation():
global conversation, chat_now, chat, chat_prev
while True:
# If the assistant is not speaking, and the chat is not empty, and the chat is not the same as the previous chat
# then the assistant will answer the chat
chat_now = chat
if is_Speaking == False and chat_now != chat_prev:
# Saving chat history
conversation.append({'role': 'user', 'content': chat_now})
chat_prev = chat_now
openai_answer()
time.sleep(1)
if __name__ == "__main__":
try:
# You can change the mode to 1 if you want to record audio from your microphone
# or change the mode to 2 if you want to capture livechat from youtube
mode = input("Mode (1-Mic, 2-Youtube Live): ")
if mode == "1":
print("Press and Hold Right Shift to record audio")
while True:
if keyboard.is_pressed('RIGHT_SHIFT'):
record_audio()
elif mode == "2":
live_id = input("Livestream ID: ")
# Threading is used to capture livechat and answer the chat at the same time
t = threading.Thread(target=preparation)
t.start()
get_livechat(live_id)
except KeyboardInterrupt:
print("Stopped")