-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
main.py
711 lines (595 loc) · 24.9 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import os
import sys
import re
import random
import shutil
import atexit
import threading
import queue
import uuid
from typing import Callable, Iterator, Optional
from loguru import logger
import numpy as np
import yaml
import chardet
import __init__
from asr.asr_factory import ASRFactory
from asr.asr_interface import ASRInterface
from live2d_model import Live2dModel
from llm.llm_factory import LLMFactory
from llm.llm_interface import LLMInterface
from prompts import prompt_loader
from tts.tts_factory import TTSFactory
from tts.tts_interface import TTSInterface
from translate.translate_interface import TranslateInterface
from translate.translate_factory import TranslateFactory
from utils.audio_preprocessor import audio_filter
class OpenLLMVTuberMain:
"""
The main class for the OpenLLM VTuber.
It initializes the Live2D controller, ASR, TTS, and LLM based on the provided configuration.
Run `conversation_chain` to start one conversation (user_input -> llm -> speak).
Attributes:
- config (dict): The configuration dictionary.
- llm (LLMInterface): The LLM instance.
- asr (ASRInterface): The ASR instance.
- tts (TTSInterface): The TTS instance.
"""
EXEC_FLAG_CHECK_TIMEOUT = 8 # seconds
def __init__(
self,
configs: dict,
custom_asr: ASRInterface | None = None,
custom_tts: TTSInterface | None = None,
) -> None:
logger.info(f"t41372/Open-LLM-VTuber, version {__init__.__version__}")
self.config: dict = configs
self.verbose = self.config.get("VERBOSE", False)
self.live2d: Live2dModel | None = self.init_live2d()
self._continue_exec_flag = threading.Event()
self._continue_exec_flag.set() # Set the flag to continue execution
self.session_id: str = str(uuid.uuid4().hex)
self.heard_sentence: str = ""
# Init ASR if voice input is on.
self.asr: ASRInterface | None
if self.config.get("VOICE_INPUT_ON", False):
# if custom_asr is provided, don't init asr and use it instead.
if custom_asr is None:
self.asr = self.init_asr()
else:
print("Using custom ASR")
self.asr = custom_asr
else:
self.asr = None
# Init TTS if TTS is on.
self.tts: TTSInterface
if self.config.get("TTS_ON", False):
# if custom_tts is provided, don't init tts and use it instead.
if custom_tts is None:
self.tts = self.init_tts()
else:
print("Using custom TTS")
self.tts = custom_tts
else:
self.tts = None
# Init Translator if enabled
self.translator: TranslateInterface | None
if self.config.get("TRANSLATE_AUDIO", False):
try:
translate_provider = self.config.get("TRANSLATE_PROVIDER", "DeepLX")
self.translator = TranslateFactory.get_translator(
translate_provider=translate_provider,
**self.config.get(translate_provider, {}),
)
except Exception as e:
print(f"Error initializing Translator: {e}")
print("Proceed without Translator.")
self.translator = None
else:
self.translator = None
self.llm: LLMInterface = self.init_llm()
# Initialization methods
def init_live2d(self) -> Live2dModel | None:
if not self.config.get("LIVE2D", False):
return None
try:
live2d_model_name = self.config.get("LIVE2D_MODEL")
live2d_controller = Live2dModel(live2d_model_name)
except Exception as e:
print(f"Error initializing Live2D: {e}")
print("Proceed without Live2D.")
return None
return live2d_controller
def init_llm(self) -> LLMInterface:
llm_provider = self.config.get("LLM_PROVIDER")
llm_config = self.config.get(llm_provider, {})
system_prompt = self.get_system_prompt()
llm = LLMFactory.create_llm(
llm_provider=llm_provider, SYSTEM_PROMPT=system_prompt, **llm_config
)
return llm
def init_asr(self) -> ASRInterface:
asr_model = self.config.get("ASR_MODEL")
asr_config = self.config.get(asr_model, {})
asr = ASRFactory.get_asr_system(asr_model, **asr_config)
return asr
def init_tts(self) -> TTSInterface:
tts_model = self.config.get("TTS_MODEL", "pyttsx3TTS")
tts_config = self.config.get(tts_model, {})
return TTSFactory.get_tts_engine(tts_model, **tts_config)
def set_audio_output_func(
self, audio_output_func: Callable[[Optional[str], Optional[str]], None]
) -> None:
"""
Set the audio output function to be used for playing audio files.
The function should accept two arguments: sentence (str) and filepath (str).
sentence: str | None
- The sentence to be displayed on the frontend.
- If None, empty sentence will be displayed.
filepath: str | None
- The path to the audio file to be played.
- If None, no audio will be played.
Here is an example of the function:
~~~python
def _play_audio_file(sentence: str | None, filepath: str | None) -> None:
if filepath is None:
print("No audio to be streamed. Response is empty.")
return
if sentence is None:
sentence = ""
print(f">> Playing {filepath}...")
playsound(filepath)
~~~
"""
self._play_audio_file = audio_output_func
# def _play_audio_file(self, sentence: str, filepath: str | None) -> None:
def get_system_prompt(self) -> str:
"""
Construct and return the system prompt based on the configuration file.
"""
if self.config.get("PERSONA_CHOICE"):
system_prompt = prompt_loader.load_persona(
self.config.get("PERSONA_CHOICE")
)
else:
system_prompt = self.config.get("DEFAULT_PERSONA_PROMPT_IN_YAML")
if self.live2d is not None:
system_prompt += prompt_loader.load_util(
self.config.get("LIVE2D_Expression_Prompt")
).replace("[<insert_emomap_keys>]", self.live2d.emo_str)
if self.verbose:
print("\n === System Prompt ===")
print(system_prompt)
return system_prompt
# Main conversation methods
def conversation_chain(self, user_input: str | np.ndarray | None = None) -> str:
"""
One iteration of the main conversation.
1. Get user input (text or audio) if not provided as an argument
2. Call the LLM with the user input
3. Speak (or not)
Parameters:
- user_input (str, numpy array, or None): The user input to be used in the conversation. If it's string, it will be considered as user input. If it's a numpy array, it will be transcribed. If it's None, we'll request input from the user.
Returns:
- str: The full response from the LLM
"""
if not self._continue_exec_flag.wait(
timeout=self.EXEC_FLAG_CHECK_TIMEOUT
): # Wait for the flag to be set
print(
">> Execution flag not set. In interruption state for too long. Resetting the flag and exiting the conversation chain."
)
self._continue_exec_flag.set()
raise InterruptedError(
"Conversation chain interrupted. Wait flag timeout reached."
)
# Generate a random number between 0 and 3
color_code = random.randint(0, 3)
c = [None] * 4
# Define the color codes for red, blue, green, and white
c[0] = "\033[91m"
c[1] = "\033[94m"
c[2] = "\033[92m"
c[3] = "\033[0m"
# Apply the color to the console output
print(f"{c[color_code]}New Conversation Chain started!")
# if user_input is not string, make it string
if user_input is None:
user_input = self.get_user_input()
elif isinstance(user_input, np.ndarray):
print("transcribing...")
user_input = self.asr.transcribe_np(user_input)
if user_input.strip().lower() == self.config.get("EXIT_PHRASE", "exit").lower():
print("Exiting...")
exit()
print(f"User input: {user_input}")
chat_completion: Iterator[str] = self.llm.chat_iter(user_input)
if not self.config.get("TTS_ON", False):
full_response = ""
for char in chat_completion:
if not self._continue_exec_flag.is_set():
self._interrupt_post_processing()
print("\nInterrupted!")
return None
full_response += char
print(char, end="")
return full_response
full_response = self.speak(chat_completion)
if self.verbose:
print(f"\nComplete response: [\n{full_response}\n]")
print(f"{c[color_code]}Conversation completed.")
return full_response
def get_user_input(self) -> str:
"""
Get user input using the method specified in the configuration file.
It can be from the console, local microphone, or the browser microphone.
Returns:
- str: The user input
"""
# for live2d with browser, their input are now injected by the server class
# and they no longer use this method
if self.config.get("VOICE_INPUT_ON", False):
# get audio from the local microphone
print("Listening from the microphone...")
return self.asr.transcribe_with_local_vad()
else:
return input("\n>> ")
def speak(self, chat_completion: Iterator[str]) -> str:
"""
Speak the chat completion using the TTS engine.
Parameters:
- chat_completion (Iterator[str]): The chat completion to speak
Returns:
- str: The full response from the LLM
"""
full_response = ""
if self.config.get("SAY_SENTENCE_SEPARATELY", True):
full_response = self.speak_by_sentence_chain(chat_completion)
else: # say the full response at once? how stupid
full_response = ""
for char in chat_completion:
if not self._continue_exec_flag.is_set():
print("\nInterrupted!")
self._interrupt_post_processing()
return None
print(char, end="")
full_response += char
print("\n")
filename = self._generate_audio_file(full_response, "temp")
if self._continue_exec_flag.is_set():
self._play_audio_file(
sentence=full_response,
filepath=filename,
)
else:
self._interrupt_post_processing()
return full_response
def _generate_audio_file(self, sentence: str, file_name_no_ext: str) -> str | None:
"""
Generate an audio file from the given sentence using the TTS engine.
Parameters:
- sentence (str): The sentence to generate audio from
- file_name_no_ext (str): The name of the audio file without the extension
Returns:
- str or None: The path to the generated audio file or None if the sentence is empty
"""
if self.verbose:
print(f">> generating {file_name_no_ext}...")
if not self.tts:
return None
if self.live2d:
sentence = self.live2d.remove_emotion_keywords(sentence)
if sentence.strip() == "":
return None
return self.tts.generate_audio(sentence, file_name_no_ext=file_name_no_ext)
def _play_audio_file(self, sentence: str | None, filepath: str | None) -> None:
"""
Play the audio file either locally or remotely using the Live2D controller if available.
Parameters:
- sentence (str): The sentence to display
- filepath (str): The path to the audio file. If None, no audio will be streamed.
"""
if filepath is None:
print("No audio to be streamed. Response is empty.")
return
if sentence is None:
sentence = ""
try:
if self.verbose:
print(f">> Playing {filepath}...")
self.tts.play_audio_file_local(filepath)
self.tts.remove_file(filepath, verbose=self.verbose)
except ValueError as e:
if str(e) == "Audio is empty or all zero.":
print("No audio to be streamed. Response is empty.")
else:
raise e
except Exception as e:
print(f"Error playing the audio file {filepath}: {e}")
def speak_by_sentence_chain(self, chat_completion: Iterator[str]) -> str:
"""
Generate and play the chat completion sentences one by one using the TTS engine.
Now properly handles interrupts in a multi-threaded environment using the existing _continue_exec_flag.
"""
task_queue = queue.Queue()
full_response = [""] # Use a list to store the full response
interrupted_error_event = threading.Event()
def producer_worker():
try:
index = 0
sentence_buffer = ""
for char in chat_completion:
if not self._continue_exec_flag.is_set():
raise InterruptedError("Producer interrupted")
if char:
print(char, end="", flush=True)
sentence_buffer += char
full_response[0] += char
if self.is_complete_sentence(sentence_buffer):
if self.verbose:
print("\n")
if not self._continue_exec_flag.is_set():
raise InterruptedError("Producer interrupted")
tts_target_sentence = sentence_buffer
tts_target_sentence = audio_filter(
tts_target_sentence,
translator=(
self.translator
if self.config.get("TRANSLATE_AUDIO", False)
else None
),
remove_special_char=self.config.get(
"REMOVE_SPECIAL_CHAR", True
),
)
audio_filepath = self._generate_audio_file(
tts_target_sentence, file_name_no_ext=uuid.uuid4()
)
if not self._continue_exec_flag.is_set():
raise InterruptedError("Producer interrupted")
audio_info = {
"sentence": sentence_buffer,
"audio_filepath": audio_filepath,
}
task_queue.put(audio_info)
index += 1
sentence_buffer = ""
# Handle any remaining text in the buffer
if sentence_buffer:
if not self._continue_exec_flag.is_set():
raise InterruptedError("Producer interrupted")
print("\n")
audio_filepath = self._generate_audio_file(
sentence_buffer, file_name_no_ext=uuid.uuid4()
)
audio_info = {
"sentence": sentence_buffer,
"audio_filepath": audio_filepath,
}
task_queue.put(audio_info)
except InterruptedError:
print("\nProducer interrupted")
interrupted_error_event.set()
return # Exit the function
except Exception as e:
print(
f"Producer error: Error generating audio for sentence: '{sentence_buffer}'.\n{e}",
"Producer stopped\n",
)
return
finally:
task_queue.put(None) # Signal end of production
def consumer_worker():
self.heard_sentence = ""
while True:
try:
if not self._continue_exec_flag.is_set():
raise InterruptedError("😱Consumer interrupted")
audio_info = task_queue.get(
timeout=0.1
) # Short timeout to check for interrupts
if audio_info is None:
break # End of production
if audio_info:
self.heard_sentence += audio_info["sentence"]
self._play_audio_file(
sentence=audio_info["sentence"],
filepath=audio_info["audio_filepath"],
)
task_queue.task_done()
except queue.Empty:
continue # No item available, continue checking for interrupts
except InterruptedError as e:
print(f"\n{str(e)}, stopping worker threads")
interrupted_error_event.set()
return # Exit the function
except Exception as e:
print(
f"Consumer error: Error playing sentence '{audio_info['sentence']}'.\n {e}"
)
continue
producer_thread = threading.Thread(target=producer_worker)
consumer_thread = threading.Thread(target=consumer_worker)
producer_thread.start()
consumer_thread.start()
producer_thread.join()
consumer_thread.join()
if interrupted_error_event.is_set():
self._interrupt_post_processing()
raise InterruptedError(
"Conversation chain interrupted: consumer model interrupted"
)
print("\n\n --- Audio generation and playback completed ---")
return full_response[0]
def interrupt(self, heard_sentence: str = "") -> None:
"""Set the interrupt flag to stop the conversation chain.
Preferably provide the sentences that were already shown or heard by the user before the interrupt so that the LLM can handle the memory properly.
Parameters:
- heard_sentence (str): The sentence that was already shown or heard by the user before the interrupt.
(because apparently the user won't know the rest of the response.)
"""
self._continue_exec_flag.clear()
self.llm.handle_interrupt(heard_sentence)
def _interrupt_post_processing(self) -> None:
"""Perform post-processing tasks (like resetting the continue flag to allow next conversation chain to start) after an interrupt."""
self._continue_exec_flag.set() # Reset the interrupt flag
def _check_interrupt(self):
"""Check if we are in an interrupt state and raise an exception if we are."""
if not self._continue_exec_flag.is_set():
raise InterruptedError("Conversation chain interrupted: checked")
def is_complete_sentence(self, text: str):
"""
Check if the text is a complete sentence.
text: str
the text to check
"""
white_list = [
"...",
"Dr.",
"Mr.",
"Ms.",
"Mrs.",
"Jr.",
"Sr.",
"St.",
"Ave.",
"Rd.",
"Blvd.",
"Dept.",
"Univ.",
"Prof.",
"Ph.D.",
"M.D.",
"U.S.",
"U.K.",
"U.N.",
"E.U.",
"U.S.A.",
"U.K.",
"U.S.S.R.",
"U.A.E.",
]
for item in white_list:
if text.strip().endswith(item):
return False
punctuation_blacklist = [
".",
"?",
"!",
"。",
";",
"?",
"!",
"…",
"〰",
"〜",
"~",
"!",
]
return any(text.strip().endswith(punct) for punct in punctuation_blacklist)
def clean_cache(self):
cache_dir = "./cache"
if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)
os.makedirs(cache_dir)
def load_and_apply_config(self, config_file: str) -> None:
"""
Load and apply the selected configuration settings from the alternative configuration file.
Parameters:
- config_file (str): The path to the alternative configuration file.
"""
with open(config_file, "r", encoding="utf-8") as file:
new_config = yaml.safe_load(file)
# Update the current configuration with the new settings
self.config.update(new_config)
# Reinitialize components with the new configuration
self.live2d = self.init_live2d()
self.asr = self.init_asr()
self.tts = self.init_tts()
self.translator = self.init_translator()
self.llm = self.init_llm()
def init_translator(self) -> TranslateInterface | None:
"""
Initialize the translator based on the configuration.
Returns:
- TranslateInterface or None: The initialized translator or None if not enabled.
"""
if self.config.get("TRANSLATE_AUDIO", False):
try:
translate_provider = self.config.get("TRANSLATE_PROVIDER", "DeepLX")
translator = TranslateFactory.get_translator(
translate_provider=translate_provider,
**self.config.get(translate_provider, {}),
)
return translator
except Exception as e:
print(f"Error initializing Translator: {e}")
print("Proceed without Translator.")
return None
else:
return None
def load_config_with_env(path) -> dict:
"""
Load the configuration file with environment variables.
Parameters:
- path (str): The path to the configuration file.
Returns:
- dict: The configuration dictionary.
Raises:
- FileNotFoundError if the configuration file is not found.
- yaml.YAMLError if the configuration file is not a valid YAML file.
"""
if not os.path.exists(path):
raise FileNotFoundError(f"Config file not found: {path}")
# Try common encodings first
encodings = ["utf-8", "utf-8-sig", "gbk", "gb2312", "ascii"]
content = None
for encoding in encodings:
try:
with open(path, "r", encoding=encoding) as file:
content = file.read()
break
except UnicodeDecodeError:
continue
if content is None:
# Try detecting encoding as last resort
try:
with open(path, "rb") as file:
raw_data = file.read()
detected = chardet.detect(raw_data)
if detected["encoding"]:
content = raw_data.decode(detected["encoding"])
except Exception as e:
logger.error(f"Error detecting encoding for config file {path}: {e}")
raise UnicodeError(f"Failed to decode config file {path} with any encoding")
# Match ${VAR_NAME}
pattern = re.compile(r"\$\{(\w+)\}")
# replace ${VAR_NAME} with os.getenv('VAR_NAME')
def replacer(match):
env_var = match.group(1)
return os.getenv(env_var, match.group(0))
content = pattern.sub(replacer, content)
try:
return yaml.safe_load(content)
except yaml.YAMLError as e:
logger.error(f"Error parsing YAML from {path}: {e}")
raise
if __name__ == "__main__":
logger.add(sys.stderr, level="DEBUG")
config = load_config_with_env("conf.yaml")
vtuber_main = OpenLLMVTuberMain(config)
atexit.register(vtuber_main.clean_cache)
def _interrupt_on_i():
while input(">>> say i and press enter to interrupt: ") == "i":
print("\n\n!!!!!!!!!! interrupt !!!!!!!!!!!!...\n")
print("Heard sentence: ", vtuber_main.heard_sentence)
vtuber_main.interrupt(vtuber_main.heard_sentence)
if config.get("VOICE_INPUT_ON", False):
threading.Thread(target=_interrupt_on_i).start()
print("tts on: ", vtuber_main.config.get("TTS_ON", False))
while True:
try:
vtuber_main.conversation_chain()
except InterruptedError as e:
print(f"😢Conversation was interrupted. {e}")
continue