-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
330 lines (293 loc) · 11.6 KB
/
main.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
os.environ["COQUI_TOS_AGREED"] = "1"
import re
import subprocess
import torch
import yaml
import numpy as np
from playwright.sync_api import sync_playwright
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
from scripts.login_screen import CONFIG
from scripts.utils import HiddenPrints
GAME_PATH = CONFIG["GAME_PATH"]
WEBUI_PATH = CONFIG["WEBUI_PATH"]
USE_TTS = CONFIG["USE_TTS"]
LAUNCH_YOURSELF = CONFIG["LAUNCH_YOURSELF"]
LAUNCH_YOURSELF_WEBUI = CONFIG["LAUNCH_YOURSELF_WEBUI"]
USE_ACTIONS = CONFIG["USE_ACTIONS"]
TTS_MODEL = CONFIG["TTS_MODEL"]
USE_SPEECH_RECOGNITION = CONFIG["USE_SPEECH_RECOGNITION"]
VOICE_SAMPLE_COQUI = CONFIG["VOICE_SAMPLE_COQUI"]
VOICE_SAMPLE_TORTOISE = CONFIG["VOICE_SAMPLE_TORTOISE"]
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Actions model
if USE_ACTIONS:
try:
from transformers import pipeline
except ModuleNotFoundError:
print("Please install transformers to use actions.")
USE_ACTIONS = False
with open("actions.yml", "r") as f:
ACTIONS = yaml.safe_load(f)
REVERT_ACTION_DICT = {}
for key in ACTIONS:
for action in ACTIONS[key]:
REVERT_ACTION_DICT[action] = key
ALL_ACTIONS = []
for key in ACTIONS:
ALL_ACTIONS += ACTIONS[key]
action_classifier = pipeline(
"zero-shot-classification",
model="sileod/deberta-v3-base-tasksource-nli")
# TTS model
with HiddenPrints():
if USE_TTS:
from scripts.play_tts import play_TTS, initialize_xtts
if TTS_MODEL == "Your TTS":
from scripts.tts_api import my_TTS
tts_model = my_TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
sampling_rate = 16000
voice_samples = None
conditioning_latents = None
elif TTS_MODEL == "XTTS":
tts_model = initialize_xtts()
sampling_rate = 24000
voice_samples = None
conditioning_latents = None
elif TTS_MODEL == "Tortoise TTS":
if device.type == "cuda":
from tortoise.api_fast import TextToSpeech, MODELS_DIR
else:
from tortoise.api import TextToSpeech, MODELS_DIR
from tortoise.utils.audio import load_voices
from voicefixer import VoiceFixer
tts_model = TextToSpeech(
models_dir=MODELS_DIR,
kv_cache=True,
)
voice_samples, conditioning_latents = load_voices([VOICE_SAMPLE_TORTOISE], ["tortoise_audios"])
vfixer = VoiceFixer()
sampling_rate = 24000
else:
print("No TTS model selected")
# Speech recognition model
if USE_SPEECH_RECOGNITION:
try:
import torch
except ModuleNotFoundError:
print("Please install torch to use speech recognition.")
USE_SPEECH_RECOGNITION = False
try:
import speech_recognition as sr
except ModuleNotFoundError:
print("Please install SpeechRecognition to use speech recognition.")
USE_SPEECH_RECOGNITION = False
try:
import whisper
except ModuleNotFoundError:
print("Please install whisper to use speech recognition.")
USE_SPEECH_RECOGNITION = False
try:
import pyaudio
except ModuleNotFoundError:
print("Please install pyaudio to use speech recognition.")
USE_SPEECH_RECOGNITION = False
english = True
def init_stt(model="base", english=True, energy=300, pause=0.8, dynamic_energy=False):
if model != "large" and english:
model = model + ".en"
audio_model = whisper.load_model(model)
r = sr.Recognizer()
r.energy_threshold = energy
r.pause_threshold = pause
r.dynamic_energy_threshold = dynamic_energy
return r, audio_model
r, audio_model = init_stt()
# Chatbot connection
WEBUI_PATH = WEBUI_PATH.replace("\\", "/")
if not LAUNCH_YOURSELF_WEBUI:
subprocess.Popen(WEBUI_PATH)
else:
print("Please launch text-generation_webui manually.")
print("Press enter to continue.")
input()
def launch(context):
print("Launching new browser page...")
page = context.new_page()
page.goto("http://127.0.0.1:7860")
# Wait for both network and DOM to be ready
page.wait_for_load_state("load")
page.wait_for_load_state("domcontentloaded")
print("Waiting for chat interface to be ready...")
# Wait for the chat interface to be visible
page.wait_for_selector("[class='svelte-1f354aw pretty_scrollbar']", timeout=60000)
print("Page loaded successfully")
context.storage_state(path="storage.json")
return page
def post_message(page, message):
if message == "QUIT":
page.fill("[class='svelte-1f354aw pretty_scrollbar']", "I'll be right back")
else:
page.fill("[class='svelte-1f354aw pretty_scrollbar']", message)
page.click('[id="Generate"]')
page.wait_for_selector('[id="stop"]')
# Main
GAME_PATH = GAME_PATH.replace("\\", "/")
clients = {}
addresses = {}
HOST = '127.0.0.1'
PORT = 12346
BUFSIZE = 1024
ADDRESS = (HOST, PORT)
SERVER = socket(AF_INET, SOCK_STREAM)
SERVER.bind(ADDRESS)
queued = False
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
uni_chr_re = re.compile(r'\\u[0-9a-fA-F]{4}')
# Launch the game
if not LAUNCH_YOURSELF:
subprocess.Popen(GAME_PATH+'/DDLC.exe')
def listen():
print("Waiting for connection...")
while True:
client, client_address = SERVER.accept()
print("%s:%s has connected." % client_address)
addresses[client] = client_address
Thread(target=call, args=(client,)).start()
def call(client):
thread = Thread(target=listenToClient, args=(client,), daemon=True)
thread.start()
def sendMessage(msg, name=""):
""" send message to all users present in
the chat room"""
for client in clients:
client.send(bytes(name, "utf8") + msg)
def send_answer(received_msg, msg):
if received_msg != "" and USE_ACTIONS:
sequence_to_classify = f"The player is speaking with Monika, his virtual \
girlfriend. Now he says: {received_msg}. What is the label of this sentence?"
action_to_take = action_classifier(sequence_to_classify, ALL_ACTIONS)
action_to_take = action_to_take["labels"][0]
print("Action: " + action_to_take)
action_to_take = REVERT_ACTION_DICT[action_to_take]
else:
action_to_take = "none"
action_to_take = action_to_take.encode("utf-8")
emotion = "".encode("utf-8")
msg = msg.encode("utf-8")
msg_to_send = msg + b"/g" + emotion + b"/g" + action_to_take
sendMessage(msg_to_send)
def listenToClient(client):
""" Get client username """
name = "User"
clients[client] = name
launched = False
play_obj = None
while True:
try:
received_msg = client.recv(BUFSIZE).decode("utf-8")
except:
print("Connection lost.")
os._exit(0)
received_msg = received_msg.split("/m")
rest_msg = received_msg[1]
received_msg = received_msg[0]
if received_msg == "chatbot":
if '/g' in rest_msg:
received_msg, step = rest_msg.split("/g")
else:
received_msg = client.recv(BUFSIZE).decode("utf-8") # Message containing the user input
received_msg, step = received_msg.split("/g")
step = int(step)
if received_msg == "begin_record":
if USE_SPEECH_RECOGNITION:
with sr.Microphone(sample_rate=16000) as source:
sendMessage("yes".encode("utf-8"))
audio = r.listen(source)
torch_audio = torch.from_numpy(
np.frombuffer(
audio.get_raw_data(),
np.int16
).flatten().astype(np.float32) / 32768.0)
audio_data = torch_audio
if english:
result = audio_model.transcribe(audio_data, language='english')
else:
result = audio_model.transcribe(audio_data)
received_msg = result['text']
else:
sendMessage("no".encode("utf-8"))
continue
print("User: "+received_msg)
if not launched:
pw = sync_playwright().start()
try:
browser = pw.firefox.launch(headless=False)
context = browser.new_context()
page = launch(context)
except:
print("Launch failed. Please check if text-generation_webui is running.")
_ = client.recv(BUFSIZE).decode("utf-8")
sendMessage("server_error".encode("utf-8"))
launched = False
pw.stop()
continue
launched = True
_ = client.recv(BUFSIZE).decode("utf-8")
sendMessage("server_ok".encode("utf-8"))
try:
post_message(page, received_msg)
except:
print("Error while sending message. Please check if text-generation_webui is running or if the model is loaded.")
_ = client.recv(BUFSIZE).decode("utf-8")
sendMessage("server_error".encode("utf-8"))
launched = False
pw.stop()
continue
while True:
try:
# Get all stop buttons and check their visibility
stop_buttons = page.locator('[id="stop"]').all()
is_generating = any(button.is_visible() for button in stop_buttons)
if not is_generating:
user = page.locator('[class="message-body"]').locator("nth=-1")
text = user.inner_html()
if len(text) > 0:
msg = text
msg = re.sub(r'<[^>]+>', '', msg)
msg = msg.replace('\n', '')
msg = os.linesep.join([s for s in msg.splitlines() if s])
msg = re.sub(' +', ' ', msg)
msg = re.sub(r'&[^;]+;', '', msg)
msg = msg.replace("END", "")
else:
continue
if received_msg != "QUIT":
if USE_TTS:
print("Using TTS")
play_obj = play_TTS(
step,
msg,
play_obj,
sampling_rate,
tts_model,
voice_samples,
conditioning_latents,
TTS_MODEL,
VOICE_SAMPLE_COQUI,
uni_chr_re)
print("Sent: " + msg)
send_answer(received_msg, msg)
break
except Exception as e:
print("Error checking generation status:", e)
launched = False
pw.stop()
break
if __name__ == "__main__":
SERVER.listen(5)
ACCEPT_THREAD = Thread(target=listen)
ACCEPT_THREAD.start()
ACCEPT_THREAD.join()
SERVER.close()