-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
702 lines (467 loc) · 19.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
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
import time
import colorama
import humanize, os, threading
import emoji
import utils.audio
import utils.hotkeys
import utils.transcriber_translate
import utils.voice
import utils.vtube_studio
import utils.alarm
import utils.volume_listener
import utils.minecraft
import utils.log_conversion
import utils.cane_lib
import API.Oogabooga_Api_Support
import utils.lorebook
import utils.camera
import utils.z_waif_discord
import utils.web_ui
import utils.settings
import utils.retrospect
import utils.based_rag
import utils.tag_task_controller
import utils.gaming_control
from dotenv import load_dotenv
load_dotenv()
TT_CHOICE = os.environ.get("WHISPER_CHOICE")
char_name = os.environ.get("CHAR_NAME")
stored_transcript = "Issue with message cycling!"
undo_allowed = False
is_live_pipe = False
# noinspection PyBroadException
def main():
while True:
print("You" + colorama.Fore.GREEN + colorama.Style.BRIGHT + " (mic) " + colorama.Fore.RESET + ">", end="", flush=True)
# Stative control depending on what mode we are (gaming, streaming, normal, ect.)
if utils.settings.is_gaming_loop:
command = utils.gaming_control.gaming_step()
else:
command = utils.hotkeys.chat_input_await()
# Flag us as running a command now
global is_live_pipe
is_live_pipe = True
if command == "CHAT":
main_converse()
elif command == "RATE":
main_rate()
elif command == "NEXT":
main_next()
elif command == "REDO":
main_undo()
elif command == "SOFT_RESET":
main_soft_reset()
elif command == "ALARM":
main_alarm_message()
elif command == "VIEW":
main_view_image()
elif command == "BLANK":
main_send_blank()
# Flag us as no longer running a command
is_live_pipe = False
def main_converse():
print(
"\rYou" + colorama.Fore.GREEN + colorama.Style.BRIGHT + " (mic " + colorama.Fore.YELLOW + "[Recording]" + colorama.Fore.GREEN + ") " + colorama.Fore.RESET + ">",
end="", flush=True)
# Actual recording and waiting bit
audio_buffer = utils.audio.record()
try:
tanscribing_log = "\rYou" + colorama.Fore.GREEN + colorama.Style.BRIGHT + " (mic " + colorama.Fore.BLUE + "[Transcribing (" + str(
humanize.naturalsize(
os.path.getsize(audio_buffer))) + ")]" + colorama.Fore.GREEN + ") " + colorama.Fore.RESET + "> "
print(tanscribing_log, end="", flush=True)
# My own edit- To remove possible transcribing errors
transcript = "Whoops! The code is having some issues, chill for a second."
transcript = utils.transcriber_translate.to_transcribe_original_language(audio_buffer)
except Exception as e:
print(colorama.Fore.RED + colorama.Style.BRIGHT + "Error: " + str(e))
return
# Fix the transcript, to stop any accidental repeats (whisper glitch)
transcript = utils.cane_lib.remove_repeats(transcript)
# Print the transcript
print('\r' + ' ' * len(tanscribing_log), end="")
print('\r' + colorama.Fore.RED + colorama.Style.BRIGHT + "--" + colorama.Fore.RESET
+ "----Me----"
+ colorama.Fore.RED + colorama.Style.BRIGHT + "--\n" + colorama.Fore.RESET)
print(f"{transcript.strip()}")
print("\n")
# Store the message, for cycling purposes
global stored_transcript
stored_transcript = transcript
# Actual sending of the message, waits for reply automatically
API.Oogabooga_Api_Support.send_via_oogabooga(transcript)
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function
main_message_speak()
# After use, delete the recording.
try:
os.remove(audio_buffer)
except:
pass
def main_message_speak():
#
# Message is received Here
#
message = API.Oogabooga_Api_Support.receive_via_oogabooga()
#
# Speak the message now!
#
s_message = emoji.replace_emoji(message, replace='')
utils.voice.set_speaking(True)
voice_speaker = threading.Thread(target=utils.voice.speak_line(s_message))
voice_speaker.daemon = True
voice_speaker.start()
# Minirest for frame-piercing (race condition as most people call it) for the speaking
time.sleep(0.01)
while utils.voice.check_if_speaking():
time.sleep(0.01)
def message_checks(message):
#
# Runs message checks for plugins, such as VTube Studio and Minecraft
#
# Log our message
print(colorama.Fore.MAGENTA + colorama.Style.BRIGHT + "--" + colorama.Fore.RESET
+ "----" + char_name + "----"
+ colorama.Fore.MAGENTA + colorama.Style.BRIGHT + "--\n" + colorama.Fore.RESET)
print(f"{message}")
print("\n")
#
# Vtube Studio Emoting
#
if utils.settings.vtube_enabled:
# Feeds the message to our VTube Studio script
utils.vtube_studio.set_emote_string(message)
# Check for any emotes on it's end
vtube_studio_thread = threading.Thread(target=utils.vtube_studio.check_emote_string)
vtube_studio_thread.daemon = True
vtube_studio_thread.start()
#
# Minecraft API
#
if utils.settings.minecraft_enabled:
utils.minecraft.check_for_command(message)
#
# Gaming
#
if utils.settings.gaming_enabled:
utils.gaming_control.message_inputs(message)
# We can now undo the previous message
global undo_allowed
undo_allowed = True
def main_rate():
# function to rate character messages from main
# NOTE: NO LONGER VALID
return
def main_next():
API.Oogabooga_Api_Support.next_message_oogabooga()
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function
main_message_speak()
def main_minecraft_chat(message):
# Limit the amount of tokens allowed to send (minecraft chat limits)
API.Oogabooga_Api_Support.force_tokens_count(47)
# Actual sending of the message, waits for reply automatically
API.Oogabooga_Api_Support.send_via_oogabooga(message)
# Reply in the craft
utils.minecraft.minecraft_chat()
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function, if we are set to speak them
if utils.settings.speak_shadowchats:
main_message_speak()
def main_discord_chat(message):
# Actual sending of the message, waits for reply automatically
API.Oogabooga_Api_Support.send_via_oogabooga(message)
#
# CHATS WILL BE GRABBED AFTER THIS RUNS!
#
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function, if we are set to speak them (NOTE: THIS WILL SLOW THE REPLY PROCESS, AS SHE WILL HAVE TO SPEAK THE WHOLE REPLY FIRST)
if utils.settings.speak_shadowchats:
main_message_speak()
def main_web_ui_chat(message):
# Actual sending of the message, waits for reply automatically
API.Oogabooga_Api_Support.send_via_oogabooga(message)
#
# CHATS WILL BE GRABBED AFTER THIS RUNS!
#
# Cut voice if needed
utils.voice.force_cut_voice()
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function, if we are set to speak them (NOTE: THIS WILL SLOW THE REPLY PROCESS, AS SHE WILL HAVE TO SPEAK THE WHOLE REPLY FIRST)
if utils.settings.speak_shadowchats:
main_message_speak()
def main_web_ui_next():
# Cut voice if needed
utils.voice.force_cut_voice()
API.Oogabooga_Api_Support.next_message_oogabooga()
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function, if we are set to speak them (NOTE: THIS WILL SLOW THE REPLY PROCESS, AS SHE WILL HAVE TO SPEAK THE WHOLE REPLY FIRST)
if utils.settings.speak_shadowchats:
main_message_speak()
def main_discord_next():
API.Oogabooga_Api_Support.next_message_oogabooga()
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function, if we are set to speak them (NOTE: THIS WILL SLOW THE REPLY PROCESS, AS SHE WILL HAVE TO SPEAK THE WHOLE REPLY FIRST)
if utils.settings.speak_shadowchats:
main_message_speak()
def main_undo():
global undo_allowed
if undo_allowed:
undo_allowed = False
# Cut voice if needed
utils.voice.force_cut_voice()
API.Oogabooga_Api_Support.undo_message()
print("\nUndoing the previous message!\n")
time.sleep(0.1)
def main_soft_reset():
API.Oogabooga_Api_Support.soft_reset()
# We can noT undo
global undo_allowed
undo_allowed = False
time.sleep(0.1)
def main_alarm_message():
# Check for the daily memory
if utils.alarm.random_memories and len(utils.based_rag.history_database) > 100:
main_memory_proc()
# Send it!
API.Oogabooga_Api_Support.send_via_oogabooga(utils.alarm.get_alarm_message())
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
main_message_speak()
# Clear the alarm
utils.alarm.clear_alarm()
def main_memory_proc():
if len(utils.based_rag.history_database) < 100:
print("Not enough conversation history for memories!")
return
# Retrospect and get a random memory
utils.retrospect.retrospect_random_mem_summary()
#
# CHATS WILL BE GRABBED AFTER THIS RUNS!
#
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function, if we are set to speak them (NOTE: THIS WILL SLOW THE REPLY PROCESS, AS SHE WILL HAVE TO SPEAK THE WHOLE REPLY FIRST)
if utils.settings.speak_shadowchats:
main_message_speak()
def main_view_image():
# Disabled for now! Loading in the multi-modal pipeline puts us overburdened!
# NOTE: No longer disabled, but please add a toggle for this on/off depending on cam!
# return
# Give us some feedback
print("\n\nViewing the camera! Please wait...\n")
# Clear the camera inputs
utils.hotkeys.clear_camera_inputs()
#
# Get the image first before any processing.
#
# Use the image feed
if utils.settings.cam_use_image_feed:
utils.camera.use_image_feed()
#
# Screenshot capture
elif utils.settings.cam_use_screenshot:
utils.camera.capture_screenshot()
#
# Normal camera capture
else:
# If we do not want to preview, simply take the image. Else, do the confirmation loop
if not utils.settings.cam_image_preview:
utils.camera.capture_pic()
else:
break_cam_loop = False
# Loop to check for if the image is what we want or not
# NOTE: If image preview is on, then the loop will not even go! Neat!
while not break_cam_loop:
utils.hotkeys.clear_camera_inputs()
utils.camera.capture_pic()
while not (utils.hotkeys.VIEW_IMAGE_PRESSED or utils.hotkeys.CANCEL_IMAGE_PRESSED):
time.sleep(0.05)
if utils.hotkeys.VIEW_IMAGE_PRESSED:
break_cam_loop = True
utils.hotkeys.clear_camera_inputs()
print("Image processing...\n")
# Check if we want to run the MIC or not for direct talk
direct_talk_transcript = ""
if utils.settings.cam_direct_talk:
direct_talk_transcript = view_image_prompt_get()
# View and process the image, storing the result
transcript = API.Oogabooga_Api_Support.view_image(direct_talk_transcript)
# Fix up our transcript & show us
print("\n" + transcript + "\n")
# We can now undo the previous message
global undo_allowed
undo_allowed = True
# Check if we need to reply after the image
if utils.settings.cam_reply_after:
view_image_after_chat("So, what did you think of the image, " + char_name + "?")
def view_image_prompt_get():
print(
"\rYou" + colorama.Fore.GREEN + colorama.Style.BRIGHT + " (mic " + colorama.Fore.YELLOW + "[Recording]" + colorama.Fore.GREEN + ") " + colorama.Fore.RESET + ">",
end="", flush=True)
# Manually toggle on the recording of the mic (it is disabled by toggling off. also works with autochat too BTW)
utils.hotkeys.speak_input_on_from_cam_direct_talk()
# Actual recording and waiting bit
audio_buffer = utils.audio.record()
try:
tanscribing_log = "\rYou" + colorama.Fore.GREEN + colorama.Style.BRIGHT + " (mic " + colorama.Fore.BLUE + "[Transcribing (" + str(
humanize.naturalsize(
os.path.getsize(audio_buffer))) + ")]" + colorama.Fore.GREEN + ") " + colorama.Fore.RESET + "> "
print(tanscribing_log, end="", flush=True)
# My own edit- To remove possible transcribing errors
transcript = "Whoops! The code is having some issues, chill for a second."
transcript = utils.transcriber_translate.to_transcribe_original_language(audio_buffer)
except Exception as e:
print(colorama.Fore.RED + colorama.Style.BRIGHT + "Error: " + str(e))
return
# Print the transcript
print('\r' + ' ' * len(tanscribing_log), end="")
print('\r' + colorama.Fore.RED + colorama.Style.BRIGHT + "--" + colorama.Fore.RESET
+ "----Me----"
+ colorama.Fore.RED + colorama.Style.BRIGHT + "--\n" + colorama.Fore.RESET)
print(f"{transcript.strip()}")
print("\n")
# Return our transcipt of what we have just said
return transcript
def view_image_after_chat(message):
# Actual sending of the message, waits for reply automatically
API.Oogabooga_Api_Support.send_via_oogabooga(message)
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function
main_message_speak()
def main_send_blank():
# Give us some feedback
print("\nSending blank message...\n")
transcript = ""
# Store the message, for cycling purposes
global stored_transcript
stored_transcript = transcript
# Actual sending of the message, waits for reply automatically
API.Oogabooga_Api_Support.send_via_oogabooga(transcript)
# Run our message checks
reply_message = API.Oogabooga_Api_Support.receive_via_oogabooga()
message_checks(reply_message)
# Pipe us to the reply function
main_message_speak()
def run_program():
# Announce that the program is running
print("Welcome back! Loading chat interface...\n\n", end="", flush=True)
# Load hotkey ON/OFF on boot
utils.hotkeys.load_hotkey_bootstate()
# Load the defaults for modules being on/off in the settings
minecraft_enabled_string = os.environ.get("MODULE_MINECRAFT")
if minecraft_enabled_string == "ON":
utils.settings.minecraft_enabled = True
else:
utils.settings.minecraft_enabled = False
alarm_enabled_string = os.environ.get("MODULE_ALARM")
if alarm_enabled_string == "ON":
utils.settings.alarm_enabled = True
else:
utils.settings.alarm_enabled = False
vtube_enabled_string = os.environ.get("MODULE_VTUBE")
if vtube_enabled_string == "ON":
utils.settings.vtube_enabled = True
else:
utils.settings.vtube_enabled = False
discord_enabled_string = os.environ.get("MODULE_DISCORD")
if discord_enabled_string == "ON":
utils.settings.discord_enabled = True
else:
utils.settings.discord_enabled = False
rag_enabled_string = os.environ.get("MODULE_RAG")
if rag_enabled_string == "ON":
utils.settings.rag_enabled = True
else:
utils.settings.rag_enabled = False
vision_enabled_string = os.environ.get("MODULE_VISUAL")
if vision_enabled_string == "ON":
utils.settings.vision_enabled = True
else:
utils.settings.vision_enabled = False
gaming_enabled_string = os.environ.get("MODULE_GAMING")
if gaming_enabled_string == "ON":
utils.settings.gaming_enabled = True
else:
utils.settings.gaming_enabled = False
utils.settings.eyes_follow = os.environ.get("EYES_FOLLOW")
# Load in our char name
utils.settings.char_name = char_name
# Load tags and tasks
utils.tag_task_controller.load_tags_tasks()
# Run any needed log conversions
utils.log_conversion.run_conversion()
# Load the previous chat history
API.Oogabooga_Api_Support.check_load_past_chat()
# Start the VTube Studio interaction in a separate thread, we ALWAYS do this FYI
if utils.settings.vtube_enabled:
vtube_studio_thread = threading.Thread(target=utils.vtube_studio.run_vtube_studio_connection)
vtube_studio_thread.daemon = True
vtube_studio_thread.start()
# Start another thread for the alarm interaction
if utils.settings.alarm_enabled:
alarm_thread = threading.Thread(target=utils.alarm.alarm_loop)
alarm_thread.daemon = True
alarm_thread.start()
# Start another thread for the volume levels, and another for the full auto toggle
volume_listener = threading.Thread(target=utils.volume_listener.run_volume_listener)
volume_listener.daemon = True
volume_listener.start()
volume_listener_toggle = threading.Thread(target=utils.hotkeys.listener_timer)
volume_listener_toggle.daemon = True
volume_listener_toggle.start()
# Start another thread for the Minecraft watchdog
if utils.settings.minecraft_enabled:
minecraft_thread = threading.Thread(target=utils.minecraft.chat_check_loop)
minecraft_thread.daemon = True
minecraft_thread.start()
# Start another thread for Discord
if utils.settings.discord_enabled:
discord_thread = threading.Thread(target=utils.z_waif_discord.run_z_waif_discord)
discord_thread.daemon = True
discord_thread.start()
# Start another thread for camera facial track, if we want that
if utils.settings.eyes_follow == "Faces":
face_follow_thread = threading.Thread(target=utils.camera.loop_follow_look)
face_follow_thread.daemon = True
face_follow_thread.start()
elif utils.settings.eyes_follow == "Random":
face_follow_thread = threading.Thread(target=utils.camera.loop_random_look)
face_follow_thread.daemon = True
face_follow_thread.start()
# Start another thread for Gradio
gradio_thread = threading.Thread(target=utils.web_ui.launch_demo)
gradio_thread.daemon = True
gradio_thread.start()
# Run the primary loop
main()
if __name__ == "__main__":
current_directory = os.path.dirname(os.path.abspath(__file__))
# Create the resource directory path based on the current directory
resource_directory = os.path.join(current_directory, "utils","resource")
os.makedirs(resource_directory, exist_ok=True)
# Create the voice_in and voice_out directory paths
voice_in_directory = os.path.join(resource_directory, "voice_in")
voice_out_directory = os.path.join(resource_directory, "voice_out")
# Create the voice_in and voice_out directories if they don't exist
os.makedirs(voice_in_directory, exist_ok=True)
os.makedirs(voice_out_directory, exist_ok=True)
run_program()