-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
1582 lines (1359 loc) · 76.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
863
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import json
import torch
import pygame
import asyncio
import aiohttp
import aiofiles
import translators as ts
import speech_recognition as sr
from functools import partial
from elevenlabs import VoiceSettings
from elevenlabs.client import ElevenLabs
from qasync import QEventLoop, asyncSlot
from silero_tts.silero_tts import SileroTTS
from characterai import aiocai, authUser, sendCode
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import QTranslator, QUrl
from PyQt6.QtGui import QDesktopServices
from PyQt6.QtMultimedia import QMediaDevices
from PyQt6.QtWidgets import (QApplication, QInputDialog, QLabel, QMainWindow,
QMessageBox, QPushButton, QWidget, QHBoxLayout,
QListWidgetItem)
from resources.call_interface import Call_MainWindow
from resources.main_interface import Ui_MainWindow
class MainWindow(QMainWindow):
"""Этот класс отвечает за работу графического интерфейса программы"""
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Soul of Waifu")
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self._translate = QtCore.QCoreApplication.translate
self.configuration = Configuration()
self.conversation = Conversation()
self.get_home_page()
self.first_message_sent = {}
self.character_chats = {}
self.setup_side_menu_actions()
self.setup_social_media_actions()
self.setup_top_menu_actions()
self.setup_settings_tab()
self.load_characters_to_list()
self.inoutput_devices()
self.retranslateUi()
def setup_side_menu_actions(self):
"""Подключение действий для кнопок бокового меню"""
self.ui.pushButton_CreateCharacter.clicked.connect(self.on_pushButton_CreateCharacter_clicked)
self.ui.pushButton_ListOfCharacters.clicked.connect(self.on_pushButton_ListOfCharacters_clicked)
self.ui.pushButton_Options.clicked.connect(self.on_pushButton_Options_clicked)
self.ui.pushButton_CreateCharacter2.clicked.connect(self.on_pushButton_CreateCharacter_clicked)
self.ui.pushButton_AddCharacter.clicked.connect(self.add_character)
def setup_social_media_actions(self):
"""Подключение действий для кнопок социальных сетей"""
self.ui.pushButton_Youtube.clicked.connect(self.open_youtube)
self.ui.pushButton_Discord.clicked.connect(self.open_discord)
self.ui.pushButton_Github.clicked.connect(self.open_github)
def setup_top_menu_actions(self):
"""Подключение действий для кнопок верхнего меню"""
self.ui.actionAbout_program.triggered.connect(self.about_program)
self.ui.actionGet_Token_from_Character_AI.triggered.connect(self.get_token_from_character_ai)
self.ui.actionCreate_new_character.triggered.connect(self.on_pushButton_CreateCharacter_clicked)
self.ui.actionSettings.triggered.connect(self.on_pushButton_Options_clicked)
self.ui.actionGet_support_from_GitHub.triggered.connect(self.open_github)
self.ui.actionGet_support_from_Discord.triggered.connect(self.open_discord)
def setup_settings_tab(self):
"""Настройка настроек"""
self.setup_api_token()
self.setup_elevenlabs_settings()
self.setup_combobox_connections()
def setup_api_token(self):
"""Настройка API токена"""
api_token = self.configuration.get_value("api_token")
if api_token:
self.ui.lineEdit_apiToken.setText(api_token)
self.ui.lineEdit_apiToken.textChanged.connect(self.load_token)
def setup_elevenlabs_settings(self):
"""Настройка токена и голосового ID Eleven Labs"""
eleven_token = self.configuration.get_value("eleven_api_token")
if eleven_token:
self.ui.lineEdit_ElevenToken.setText(eleven_token)
self.ui.lineEdit_ElevenToken.textChanged.connect(self.load_eleven_token)
eleven_voice_id = self.configuration.get_value("eleven_voice_id")
if eleven_voice_id:
self.ui.lineEdit_ElevenToken_voiceID.setText(eleven_voice_id)
self.ui.lineEdit_ElevenToken_voiceID.textChanged.connect(self.load_eleven_voice_id)
def setup_combobox_connections(self):
"""Подключение сигналов для комбобоксов"""
self.load_selected_tts()
self.ui.comboBox_TTS.currentIndexChanged.connect(self.on_comboBox_TTS_changed)
self.ui.comboBox_TTS_Lang.currentIndexChanged.connect(self.on_comboBox_TTS_Lang_changed)
self.ui.comboBox_TTS_Voice.currentIndexChanged.connect(self.on_comboBox_TTS_Voice_changed)
self.ui.comboBox_TTS_Device.currentIndexChanged.connect(self.on_comboBox_TTS_Device_changed)
self.ui.comboBox_Language.currentIndexChanged.connect(self.on_comboBox_Language_changed)
self.ui.comboBox_SpeechToText.currentIndexChanged.connect(self.on_comboBox_SpeechToText_changed)
def reset_button_styles(self):
"""Сброс стилей кнопок бокового меню на значения по умолчанию"""
buttons = [
self.ui.pushButton_Home,
self.ui.pushButton_CreateCharacter,
self.ui.pushButton_ListOfCharacters,
self.ui.pushButton_Options
]
default_style = "QPushButton {background-color: none; color: rgb(255, 255, 255);}"
for btn in buttons:
btn.setStyleSheet(default_style)
btn.setChecked(False)
def set_active_button_style(self, button):
"""Установка активного стиля для кнопки"""
active_style = "QPushButton {background-color: rgb(63, 92, 186); color: black; border-radius: 10px;}"
button.setStyleSheet(active_style)
button.setChecked(True)
def on_pushButton_Home_clicked(self):
"""Обработка клика по кнопке Home"""
self.reset_button_styles()
self.set_active_button_style(self.ui.pushButton_Home)
character_list = self.configuration.configuration_data.get("character_list")
self.ui.stackedWidget.setCurrentIndex(1 if character_list else 0)
def on_pushButton_CreateCharacter_clicked(self):
"""Обработка клика по кнопке Create Character"""
self.reset_button_styles()
self.set_active_button_style(self.ui.pushButton_CreateCharacter)
self.ui.stackedWidget.setCurrentIndex(2)
def on_pushButton_ListOfCharacters_clicked(self):
"""Обработка клика по кнопке List of Characters"""
self.reset_button_styles()
self.set_active_button_style(self.ui.pushButton_ListOfCharacters)
self.ui.stackedWidget.setCurrentIndex(3)
def on_pushButton_Options_clicked(self):
"""Обработка клика по кнопке Options"""
self.reset_button_styles()
self.set_active_button_style(self.ui.pushButton_Options)
self.ui.stackedWidget.setCurrentIndex(4)
def open_youtube(self):
"""Открытие YouTube канала"""
QDesktopServices.openUrl(QUrl("https://www.youtube.com/@jofizcd"))
def open_discord(self):
"""Открытие Discord-сервера"""
QDesktopServices.openUrl(QUrl("https://discord.com/invite/HC89b3CvZH"))
def open_github(self):
"""Открытие GitHub репозитория"""
QDesktopServices.openUrl(QUrl("https://github.com/jofizcd/Soul-of-Waifu"))
def about_program(self):
"""Показать окно О программе"""
description = self.tr("Soul of Waifu is a program that will allow you to interact with your waifu. The program uses the API from the Character AI website, so you will be able to bring your characters from there. The main feature of Soul of Waifu is the ability to have a voice conversation with your character. In the future, I will add the ability to upload your LLMs. This way, you will be able to switch between a character with Character AI and a local character.")
sub_description = self.tr("If you have any suggestions and ideas for the development of Soul of Waifu or if you have any problems with the program, I am waiting for you in my Discord server.")
github_site = "https://github.com/jofizcd"
align = "justify"
mbox_about = QMessageBox()
mbox_about.setWindowIcon(QtGui.QIcon("./resources/icons/Logotype.ico"))
mbox_about.setWindowTitle(self.tr("Soul of Waifu - About"))
mbox_about.setText(f"<p><b>Soul of Waifu v1.0.0</b></p>"
f"<p align={align}>{description}</p>"
f"<p align={align}>{sub_description}</p>"
f"<p><i>Developed by <a href={github_site}>jofizcd</a></i></p>")
mbox_about.exec()
def get_token_from_character_ai(self):
"""Получение токена от Character AI"""
email_address, agreement = QInputDialog.getText(self, self.tr('Get Token'), self.tr('Enter your email:'))
if agreement and email_address:
sendCode(email_address)
link, agreement_link = QInputDialog.getText(self, self.tr('Get Token'), self.tr('Enter the link from the email:'))
if agreement_link and link:
user_token = authUser(link, email_address)
email_message = self.tr(f"Thank you! Your email is: {email_address}")
api_token_message = self.tr(f"Your API token: {user_token}")
combined_message = f"{email_message}<br>{api_token_message}"
configuration.configuration_process("api_token", user_token)
self.show_message_box("Soul of Waifu - Get API Token", combined_message)
def show_message_box(self, title, text):
"""Показать сообщение"""
mbox = QMessageBox()
mbox.setWindowIcon(QtGui.QIcon("./resources/icons/Logotype.ico"))
mbox.setWindowTitle(self.tr(title))
mbox.setText(text)
mbox.exec()
def inoutput_devices(self):
"""Загрузка доступных устройств ввода и вывода аудио"""
self.populate_devices(QMediaDevices.audioInputs(), self.ui.comboBox_InputDevice)
self.populate_devices(QMediaDevices.audioOutputs(), self.ui.comboBox_OutputDevice)
def populate_devices(self, devices, combobox):
"""Заполнение выпадающего списка устройствами"""
combobox.clear()
for device in devices:
combobox.addItem(device.description(), device)
def load_token(self):
"""Загрузка и сохранение API токена"""
self.configuration.configuration_process("api_token", self.ui.lineEdit_apiToken.text())
def load_eleven_token(self):
"""Загрузка и сохранение токена ElevenLabs"""
self.configuration.configuration_process("eleven_api_token", self.ui.lineEdit_ElevenToken.text())
def load_eleven_voice_id(self):
"""Загрузка и сохранение ID голоса ElevenLabs"""
self.configuration.configuration_process("eleven_voice_id", self.ui.lineEdit_ElevenToken_voiceID.text())
def load_selected_tts(self):
"""Загрузка выбранных настроек Text-to-Speech"""
self.load_combobox_value("selected_tts", self.ui.comboBox_TTS, self.selector_tts)
self.load_combobox_value("selected_tts_language", self.ui.comboBox_TTS_Lang, self.selector_tts_language)
self.load_combobox_value("selected_tts_voice", self.ui.comboBox_TTS_Voice)
self.load_combobox_value("selected_tts_device", self.ui.comboBox_TTS_Device)
def load_combobox_value(self, key, combobox, selector=None):
"""Загрузка значения для выпадающего списка"""
value = self.configuration.get_value(key)
if value is not None:
combobox.setCurrentIndex(value)
if selector:
selector(value)
else:
self.configuration.configuration_process(key, 0)
def on_comboBox_TTS_changed(self, index):
"""Обработка изменения выбора Text-To-Speech"""
self.configuration.configuration_process("selected_tts", index)
self.selector_tts(index)
def selector_tts(self, selected_tts):
"""Настройка видимости элементов интерфейса в зависимости от выбранного Text-To-Speech"""
components = [
self.ui.comboBox_TTS_Lang,
self.ui.comboBox_TTS_Voice,
self.ui.comboBox_TTS_Device,
self.ui.lineEdit_ElevenToken,
self.ui.lineEdit_ElevenToken_voiceID,
self.ui.label_12, # Choose language
self.ui.label_13, # Choose device
self.ui.label_14, # Write your token
self.ui.label_15, # Choose voice
self.ui.label_elevenlabs_voice_id
]
for component in components:
component.hide()
if selected_tts == 2:
component.hide()
self.ui.comboBox_TTS_Lang.show()
self.ui.comboBox_TTS_Voice.show()
self.ui.comboBox_TTS_Device.show()
self.ui.label_12.show()
self.ui.label_13.show()
self.ui.label_15.show()
elif selected_tts == 3:
component.hide()
self.ui.lineEdit_ElevenToken.show()
self.ui.label_14.show()
if self.configuration.get_value("eleven_api_token"):
self.ui.lineEdit_ElevenToken_voiceID.show()
self.ui.label_elevenlabs_voice_id.show()
def show_components(self, components):
"""Показать компоненты"""
for component in components:
component.show()
def on_comboBox_TTS_Lang_changed(self, index):
"""Обработка изменения выбора языка Text-To-Speech"""
self.configuration.configuration_process("selected_tts_language", index)
self.selector_tts_language(index)
def selector_tts_language(self, selected_tts_language):
"""Настройка доступных голосов в зависимости от выбранного языка Text-To-Speech"""
self.ui.comboBox_TTS_Voice.clear()
voices = {
0: ["en_0", "en_1", "en_2", "en_3", "en_4", "en_5", "en_6", "en_7", "en_8", "en_9", "en_10", "en_11", "en_12"],
1: ["aidar", "xenia", "kseniya", "baya", "eugene"]
}
self.ui.comboBox_TTS_Voice.addItems(voices.get(selected_tts_language, []))
def on_comboBox_TTS_Voice_changed(self, index):
"""Обработка изменения выбора голоса Text-To-Speech"""
self.configuration.configuration_process("selected_tts_voice", index)
def on_comboBox_TTS_Device_changed(self, index):
"""Обработка изменения выбора устройства Text-To-Speech"""
self.configuration.configuration_process("selected_tts_device", index)
def on_comboBox_Language_changed(self, index):
"""Обработка изменения выбора языка интерфейса"""
self.configuration.configuration_process("selected_language", index)
def on_comboBox_SpeechToText_changed(self, index):
"""Обработка изменения выбора технологии Speech-To-Text"""
self.configuration.configuration_process("selected_stt", index)
@asyncSlot()
async def add_character(self):
"""Добавляет нового персонажа в список"""
character_id = self.ui.character_id_lineEdit.text()
conversation = await Conversation().init_async()
character_name, character_title, character_first_message = await conversation.get_character(character_id)
character_avatar = await conversation.get_character_avatar(character_id)
if await self.is_character_already_added(character_id, character_name):
return
self.configuration.configuration_character_list_process(
character_name, character_id, character_avatar, character_title, character_first_message)
self.ui.character_id_lineEdit.clear()
if character_name is None or character_avatar is None:
raise ValueError("Character name or avatar cannot be None")
await self.add_character_to_characters_list(character_name, character_avatar)
self.show_message_box("Soul of Waifu - Information",
self.tr(f"Character {character_name} has been added to the character list."))
async def is_character_already_added(self, character_id, character_name):
"""Проверка, добавлен ли персонаж ранее"""
character_data = self.configuration.load_configuration()
if 'character_list' in character_data:
for character in character_data['character_list'].values():
if character['character_id'] == character_id:
self.show_message_box("Soul of Waifu - Character Error",
self.tr(f"Character {character_name} has already been added previously."))
return True
return False
def show_message_box(self, title, message):
"""Показ сообщения"""
QtWidgets.QMessageBox.information(self, self.tr(title), message)
@asyncSlot()
async def add_character_to_characters_list(self, character_name, character_avatar):
"""Добавляет персонажа в виджет списка персонажей"""
item = QListWidgetItem()
item_widget = QWidget()
item_layout = QHBoxLayout()
name_label = QLabel(character_name)
avatar_label = QLabel()
avatar_pixmap = self.load_avatar_pixmap(character_avatar)
avatar_label.setPixmap(avatar_pixmap)
avatar_label.setFixedSize(70, 70)
delete_button = QPushButton(self.tr("Delete"))
delete_button.clicked.connect(lambda: self.delete_character(character_name, item))
configuration_data = self.configuration.load_configuration()
character_data = configuration_data['character_list']
character_description = character_data[character_name]['character_description']
character_first_message = character_data[character_name]['character_first_message']
chat_button = QPushButton(self.tr("Chat"))
chat_button.clicked.connect(lambda: self.start_chat(character_name, character_avatar, character_description, character_first_message))
set_voice_button = QPushButton(self.tr("Set voice"))
self.setup_voice_button(set_voice_button, character_name)
item_layout.addWidget(avatar_label)
item_layout.addWidget(name_label)
item_layout.addWidget(chat_button)
item_layout.addWidget(delete_button)
item_layout.addWidget(set_voice_button)
item_widget.setLayout(item_layout)
item.setSizeHint(item_widget.sizeHint())
self.ui.characterlistWidget.addItem(item)
self.ui.characterlistWidget.setItemWidget(item, item_widget)
self.ui.characterlistWidget.setCurrentItem(item)
self.style_widgets_character_list(name_label, avatar_label, delete_button, set_voice_button, chat_button)
def style_widgets_character_list(self, name_label, avatar_label, delete_button, set_voice_button, chat_button):
"""Стилизация виджетов"""
self.ui.characterlistWidget.setStyleSheet("""
QListWidget {
background-color: rgb(44, 44, 44);
border: 1px solid rgb(40, 40, 40);
padding: 10px;
border-radius: 1px;
}
QListWidget::item {
border-bottom: 1px solid rgb(37, 37, 37);
}
QListWidget::item:hover {
background-color: rgb(33, 33, 33);
}
""")
name_label.setStyleSheet("""
QLabel {
font-size: 16px;
color: white;
padding: 10px;
font-family: 'Muli SemiBold', sans-serif;
}
""")
avatar_label.setStyleSheet("""
QLabel {
border: 1px solid #ccc;
border-radius: 35px;
padding: 2px;
}
""")
delete_button.setStyleSheet("""
QPushButton {
font-size: 16px;
background-color: #ff4d4d;
color: white;
border: none;
border-radius: 5px;
padding: 5px 10px;
font-family: 'Muli', sans-serif;
}
QPushButton:hover {
background-color: #ff3333;
}
""")
delete_button.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed)
chat_button.setStyleSheet("""
QPushButton {
font-size: 16px;
background-color: rgb(36, 64, 94);
color: white;
border: none;
border-radius: 5px;
padding: 5px 10px;
font-family: 'Muli', sans-serif;
}
QPushButton:hover {
background-color: #45a049;
}
""")
chat_button.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed)
set_voice_button.setStyleSheet("""
QPushButton {
font-size: 16px;
background-color: rgb(85, 85, 85);
color: white;
border: none;
border-radius: 5px;
padding: 5px 10px;
font-family: 'Muli', sans-serif;
}
QPushButton:hover {
background-color: #45a049;
}
""")
set_voice_button.setSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed)
def load_avatar_pixmap(self, avatar_filename):
"""Загружает и обрабатывает изображение аватара"""
pixmap = QtGui.QPixmap(f"./resources/avatars/{avatar_filename}")
pixmap = pixmap.scaled(70, 70, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.SmoothTransformation)
mask = QtGui.QPixmap(63, 63)
mask.fill(QtCore.Qt.GlobalColor.transparent)
painter = QtGui.QPainter(mask)
painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
painter.setBrush(QtGui.QBrush(QtCore.Qt.GlobalColor.black))
painter.setCompositionMode(QtGui.QPainter.CompositionMode.CompositionMode_Source)
painter.drawEllipse(0, 0, 63, 63)
painter.end()
painter.begin(mask)
painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
painter.setCompositionMode(QtGui.QPainter.CompositionMode.CompositionMode_SourceIn)
painter.drawPixmap(0, 0, pixmap)
painter.end()
return mask
def setup_voice_button(self, button, character_name):
"""Настраивает кнопку установки голоса для персонажа"""
configuration = self.configuration.load_configuration()
selected_tts = configuration.get("selected_tts", 0)
if selected_tts == 1:
button.clicked.connect(lambda: self.open_voice_window(character_name))
button.setVisible(True)
else:
button.clicked.connect(lambda: None)
button.setVisible(False)
def open_voice_window(self, character_name):
"""Открывает окно настройки голоса для выбранного персонажа"""
self.current_character_name = character_name
self.voice_window = VoiceWindow(character_name)
self.voice_window.show()
@asyncSlot()
async def load_characters_to_list(self):
"""Загружает персонажей из конфигурационного файла в интерфейс"""
character_list = self.configuration.load_configuration()
characters = character_list.get('character_list', {})
for character_name, data in characters.items():
character_avatar = data.get('character_avatar')
await self.add_character_to_characters_list(character_name, character_avatar)
def delete_character(self, character_name, item):
"""Удаляет персонажа из списка и конфигурации"""
reply = QMessageBox.question(
self,
self.tr("Soul of Waifu - Вопрос"),
self.tr("Вы уверены, что хотите удалить персонажа?"),
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.Yes:
self._remove_character_from_list(character_name, item)
def _remove_character_from_list(self, character_name, item):
"""Удаляет персонажа из конфигурации и списка в UI"""
character_list = self.configuration.configuration_data.get("character_list", {})
if character_name in character_list:
del character_list[character_name]
self.ui.characterlistWidget.takeItem(self.ui.characterlistWidget.row(item))
self.configuration.save_configuration()
@asyncSlot()
async def get_home_page(self):
"""Загружает домашнюю страницу с данными пользователя"""
user_data = self.configuration.load_configuration()
if 'user_data' not in user_data or not user_data['user_data']:
conversation = await Conversation().init_async()
user_name, user_avatar = await conversation.get_user()
self.configuration.configuration_user_process(user_name, user_avatar)
user_data = self.configuration.load_configuration()
user_name = list(user_data['user_data'].keys())[0]
user_avatar = user_data['user_data'][user_name]['user_avatar']
character_list = self.configuration.configuration_data.get("character_list")
if character_list:
self.ui.stackedWidget.setCurrentIndex(1)
self.ui.pushButton_Home.setChecked(True)
welcome_message = self.tr("Nice to see you again, ")
self.ui.HomeUser_Label.setText(welcome_message user_name)
user_pixmap = QtGui.QPixmap(f"./resources/avatars/{user_avatar}")
user_pixmap = user_pixmap.scaled(100, 100)
self.ui.user_avatar_label.setPixmap(user_pixmap)
else:
self.ui.stackedWidget.setCurrentIndex(0)
self.ui.pushButton_Home.setChecked(True)
async def chat_with_character(self, character_name, character_avatar, character_description, character_first_message):
"""Инициализирует чат с выбранным персонажем"""
if character_name not in self.character_chats:
self.character_chats[character_name] = []
selected_tts = self.configuration.load_configuration()["selected_tts"]
self.ui.stackedWidget.setCurrentIndex(5)
self.ui.character_image.setPixmap(QtGui.QPixmap(f"./resources/avatars/{character_avatar}").scaled(45, 45, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.SmoothTransformation))
self.ui.character_name.setText(character_name)
self.ui.character_description.setText(character_description)
self.set_cursor(selected_tts)
self.display_chat(character_name)
if not self.first_message_sent.get(character_name, False):
self.add_message(character_first_message, character_name, character_name)
self.first_message_sent[character_name] = True
self.disconnect_signals()
self.connect_signals(selected_tts, character_name, character_avatar)
def set_cursor(self, selected_tts):
"""Устанавливает курсор в зависимости от выбранного TTS"""
if selected_tts == 0:
self.ui.btn_call.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.ForbiddenCursor))
else:
self.ui.btn_call.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.PointingHandCursor))
def disconnect_signals(self):
"""Отключает сигналы, если они уже подключены"""
try:
self.ui.line_edit_message.returnPressed.disconnect()
self.ui.btn_send_message.clicked.disconnect()
self.ui.btn_call.clicked.disconnect()
except TypeError:
pass
def connect_signals(self, selected_tts, character_name, character_avatar):
"""Подключает сигналы в зависимости от выбранного TTS"""
match selected_tts:
case 0: # None
self.ui.line_edit_message.returnPressed.connect(lambda: asyncio.create_task(self.send_message_None(character_name)))
self.ui.btn_send_message.clicked.connect(lambda: asyncio.create_task(self.send_message_None(character_name)))
case 1: # Character AI
self.ui.line_edit_message.returnPressed.connect(lambda: asyncio.create_task(self.send_message_Character_AI_TTS_text(character_name)))
self.ui.btn_send_message.clicked.connect(lambda: asyncio.create_task(self.send_message_Character_AI_TTS_text(character_name)))
self.ui.btn_call.clicked.connect(lambda: self.open_call_window(selected_tts, character_avatar, character_name))
case 2: # Silero TTS
self.ui.line_edit_message.returnPressed.connect(lambda: asyncio.create_task(self.send_message_SileroTTS_text(character_name)))
self.ui.btn_send_message.clicked.connect(lambda: asyncio.create_task(self.send_message_SileroTTS_text(character_name)))
self.ui.btn_call.clicked.connect(lambda: self.open_call_window(selected_tts, character_avatar, character_name))
case 3: # Eleven Labs
self.ui.line_edit_message.returnPressed.connect(lambda: asyncio.create_task(self.send_message_ElevenLabs_text(character_name)))
self.ui.btn_send_message.clicked.connect(lambda: asyncio.create_task(self.send_message_ElevenLabs_text(character_name)))
self.ui.btn_call.clicked.connect(lambda: self.open_call_window(selected_tts, character_avatar, character_name))
def start_chat(self, character_name, character_avatar, character_description, character_first_message):
"""Запускает чат с персонажем"""
asyncio.create_task(self.chat_with_character(character_name, character_avatar, character_description, character_first_message))
def save_user_message(self):
"""Сохраняет сообщение пользователя и очищает поле ввода"""
user_message = self.ui.line_edit_message.text()
self.ui.line_edit_message.clear()
return user_message
def display_chat(self, character_name):
"""Отображает чат с выбранным персонажем"""
self.clear_chat()
self.load_chat_messages(character_name)
def clear_chat(self):
"""Очищает виджет чата"""
for i in reversed(range(self.ui.scroll_layout.count())):
widget = self.ui.scroll_layout.itemAt(i).widget()
if widget is not None:
widget.deleteLater()
def load_chat_messages(self, character_name):
"""Загружает сообщения чата для выбранного персонажа"""
for message, sender in self.character_chats[character_name]:
self.create_message_widget(message, sender)
def create_message_widget(self, message, sender):
"""Создает виджет сообщения и добавляет его в чат"""
message_widget = QWidget()
message_layout = QHBoxLayout()
message_widget.setLayout(message_layout)
message_label = QLabel(message)
message_label.setWordWrap(True)
message_label.setStyleSheet(self.get_message_style(sender))
if sender == "user":
message_layout.addStretch()
message_layout.addWidget(message_label)
else:
message_layout.addWidget(message_label)
message_layout.addStretch()
self.ui.scroll_layout.addWidget(message_widget)
asyncio.create_task(self.scroll_to_bottom())
async def scroll_to_bottom(self):
"""Асинхронно прокручивает scrollArea вниз"""
await asyncio.sleep(0.05)
self.ui.scrollArea_chat.verticalScrollBar().setValue(self.ui.scrollArea_chat.verticalScrollBar().maximum())
def add_message(self, message, sender, character_name):
"""Добавляет сообщение в историю чата и отображает его, если нужный персонаж активен"""
self.character_chats.setdefault(character_name, []).append((message, sender))
if self.ui.character_name.text() == character_name:
self.display_chat(character_name)
def get_message_style(self, sender):
"""Возвращает стиль для сообщения в зависимости от отправителя"""
if sender == "user":
return """
QLabel {
color: #E1E1E1;
background-color: #2E2E2E;
border-radius: 10px;
padding: 10px;
margin: 5px;
font-size: 14px;
}
"""
else:
return """
QLabel {
color: #E1E1E1;
background-color: #3E3E3E;
border-radius: 10px;
padding: 10px;
margin: 5px;
font-size: 14px;
}
"""
def open_call_window(self, selected_tts, character_avatar, character_name):
"""Открывает окно вызова для выбранного персонажа"""
self.call_window = CallWindow(selected_tts, character_avatar, character_name)
self.call_window.show()
async def send_message(self, character_name, tts_method, user_message=None,
selected_language=None, selected_tts_language=None, selected_tts_voice=None,
selected_tts_device=None, eleven_labs_token=None, eleven_labs_voice_id=None,
candidate_id=None, chat_id=None, turn_id=None):
"""Отправляет сообщение, используя указанный метод TTS"""
if user_message is None:
user_message = self.save_user_message()
if not user_message:
return
self.add_message(user_message, "user", character_name)
conversation = await Conversation().init_async()
configuration_data = self.configuration.load_configuration()
character_data = configuration_data['character_list']
character_id = character_data[character_name]['character_id']
selected_language = configuration_data['selected_language']
selected_tts_language = configuration_data['selected_tts_language']
selected_tts_voice = configuration_data['selected_tts_voice']
selected_tts_device = configuration_data['selected_tts_device']
eleven_labs_token = configuration_data['eleven_api_token']
eleven_labs_voice_id = configuration_data['eleven_voice_id']
if tts_method == "None":
character_message = await conversation.character_conversation_send_message_None(character_id, user_message)
self.handle_response_language(character_message, character_name, selected_language)
else:
if 'voice_id' not in character_data[character_name]:
reply = QMessageBox.information(
self,
self.tr("Soul of Waifu - Voice Error"),
self.tr("You have not assigned a voice to your character"),
QMessageBox.StandardButton.Ok
)
if reply == QMessageBox.StandardButton.Ok:
self.open_voice_window(character_name)
return
voice_id = character_data[character_name]['voice_id']
if tts_method in ["CharacterAI_TTS_text", "CharacterAI_TTS_call"]:
character_message, turn_id, candidate_id, chat_id = await conversation.character_conversation_send_message_CharacterAI_TTS(character_id, user_message)
elif tts_method in ["SileroTTS_text", "SileroTTS_call"]:
character_message = await conversation.character_conversation_send_message_SileroTTS(character_id, user_message)
elif tts_method in ["ElevenLabs_text", "ElevenLabs_call"]:
character_message = await conversation.character_conversation_send_message_ElevenLabs(character_id, user_message)
await self.handle_tts_response(character_message, character_name, selected_language, tts_method, voice_id, candidate_id, chat_id, turn_id, selected_tts_language, selected_tts_voice, selected_tts_device, eleven_labs_token, eleven_labs_voice_id)
def handle_response_language(self, character_message, character_name, selected_language):
"""Обрабатывает сообщение персонажа в зависимости от выбранного языка"""
if selected_language == 0: # English
self.add_message(character_message, character_name, character_name)
elif selected_language == 1: # Русский
character_message_translated = ts.translate_text(character_message, "yandex", "en", "ru")
self.add_message(character_message_translated, character_name, character_name)
async def handle_tts_response(self, character_message, character_name, selected_language, tts_method, voice_id, candidate_id, chat_id, turn_id, selected_tts_language, selected_tts_voice, selected_tts_device, eleven_labs_token, eleven_labs_voice_id):
"""Обрабатывает ответ персонажа с использованием TTS"""
conversation = await Conversation().init_async()
if tts_method in ["CharacterAI_TTS_text", "CharacterAI_TTS_call"]:
self.handle_response_language(character_message, character_name, selected_language)
await conversation.character_conversation_reply_CharacterAI_TTS(candidate_id, chat_id, turn_id, voice_id)
elif tts_method in ["SileroTTS_text", "SileroTTS_call"]:
self.handle_response_language(character_message, character_name, selected_language)
character_message = character_message.replace("*", "")
await asyncio.sleep(0.5)
await conversation.character_conversation_reply_SileroTTS(selected_tts_language, selected_tts_voice, selected_tts_device, character_message)
elif tts_method in ["ElevenLabs_text", "ElevenLabs_call"]:
self.handle_response_language(character_message, character_name, selected_language)
character_message = character_message.replace("*", "")
await asyncio.sleep(0.5)
await conversation.character_conversation_reply_ElevenLabs(character_message, eleven_labs_token, eleven_labs_voice_id)
async def send_message_None(self, character_name):
await self.send_message(character_name, "None")
async def send_message_Character_AI_TTS_text(self, character_name):
await self.send_message(character_name, "CharacterAI_TTS_text")
async def send_message_Character_AI_TTS_call(self, character_name, user_message):
await self.send_message(character_name, "CharacterAI_TTS_call", user_message)
async def send_message_SileroTTS_text(self, character_name):
await self.send_message(character_name, "SileroTTS_text")
async def send_message_SileroTTS_call(self, character_name, user_message):
await self.send_message(character_name, "SileroTTS_call", user_message)
async def send_message_ElevenLabs_text(self, character_name):
await self.send_message(character_name, "ElevenLabs_text")
async def send_message_ElevenLabs_call(self, character_name, user_message):
await self.send_message(character_name, "ElevenLabs_call", user_message)
def retranslateUi(self):
self.ui.pushButton_Home.setText(self.tr(" Main Navigation"))
self.ui.pushButton_CreateCharacter.setText(self.tr(" Create Character"))
self.ui.pushButton_ListOfCharacters.setText(self.tr(" List of Characters"))
self.ui.pushButton_Options.setText(self.tr(" Options"))
self.ui.pushButton_Youtube.setText(self.tr(" YouTube"))
self.ui.pushButton_Discord.setText(self.tr(" Discord"))
self.ui.pushButton_Github.setText(self.tr(" GitHub"))
self.ui.creditsLabel.setText(self.tr("Created by Jofi"))
self.ui.version.setText(self.tr("v1.0.0"))
self.ui.NoneCharacter_Label.setText(self.tr("You do not have any characters added. Click the button to add more characters."))
self.ui.pushButton_CreateCharacter2.setText(self.tr(" Create Character"))
self.ui.MainNavigation_Label.setText(self.tr("MAIN NAVIGATION"))
self.ui.creditsLabel_2.setText(self.tr("Created by Jofi"))
self.ui.version_2.setText(self.tr("v1.0.0"))
self.ui.MainNavigation2_Label.setText(self.tr("MAIN NAVIGATION"))
self.ui.HomeUser_Label.setText(self.tr("Nice to see you again,"))
self.ui.user_avatar_label.setText(self.tr(""))
self.ui.creditsLabel_5.setText(self.tr("Created by Jofi"))
self.ui.version_5.setText(self.tr("v1.0.0"))
self.ui.label_4.setText(self.tr("Write your character\'s ID"))
self.ui.label_7.setText(self.tr("Character ID:"))
self.ui.pushButton_AddCharacter.setText(self.tr("Add"))
self.ui.MainNavigation2_Label_2.setText(self.tr("CREATE CHARACTER"))
self.ui.creditsLabel_6.setText(self.tr("Created by Jofi"))
self.ui.version_6.setText(self.tr("v1.0.0"))
self.ui.ListOfCharacters_Label.setText(self.tr("LIST OF CHARACTERS"))
self.ui.creditsLabel_7.setText(self.tr("Created by Jofi"))
self.ui.version_7.setText(self.tr("v1.0.0"))
self.ui.Options_Label.setText(self.tr("OPTIONS"))
self.ui.label_8.setText(self.tr("Character Ai API Token"))
self.ui.label_9.setText(self.tr("Write your token:"))
self.ui.label_10.setText(self.tr("Generating a character\'s voice"))
self.ui.label_11.setText(self.tr("Choose TTS:"))
self.ui.comboBox_TTS.setItemText(1, self.tr("Character AI"))
self.ui.comboBox_TTS.setItemText(2, self.tr("Silero TTS"))
self.ui.comboBox_TTS.setItemText(3, self.tr("Eleven Labs"))
self.ui.label_12.setText(self.tr("Choose language:"))
self.ui.comboBox_TTS_Lang.setItemText(0, self.tr("English"))
self.ui.comboBox_TTS_Lang.setItemText(1, self.tr("Russian"))
self.ui.label_13.setText(self.tr("Choose device:"))
self.ui.comboBox_TTS_Device.setItemText(0, self.tr("CPU"))
self.ui.comboBox_TTS_Device.setItemText(1, self.tr("GPU"))
self.ui.label_14.setText(self.tr("Write your token:"))
self.ui.label_15.setText(self.tr("Choose voice:"))
self.ui.tabWidget.setTabText(self.ui.tabWidget.indexOf(self.ui.config_tab), self.tr("Configuration"))
self.ui.label_16.setText(self.tr("Language"))
self.ui.comboBox_Language.setItemText(0, self.tr("English"))
self.ui.comboBox_Language.setItemText(1, self.tr("Русский"))
self.ui.label_17.setText(self.tr("Devices"))
self.ui.label_18.setText(self.tr("Input device:"))
self.ui.label_19.setText(self.tr("Output device:"))
self.ui.tabWidget.setTabText(self.ui.tabWidget.indexOf(self.ui.system_tab), self.tr("System"))
self.ui.line_edit_message.setPlaceholderText(self.tr("Write your message to character"))
self.ui.character_name.setText(self.tr("Character name"))
self.ui.character_description.setText(self.tr("Character description"))
self.ui.label_mainWords.setText(self.tr("Soul of Waifu is the place where your dream becomes real"))
self.ui.menuSoW.setTitle(self.tr("Soul of Waifu"))
self.ui.menuHelp.setTitle(self.tr("Help"))
self.ui.actionAbout_program.setText(self.tr("About Soul of Waifu"))
self.ui.actionGet_Token_from_Character_AI.setText(self.tr("Get Token from Character AI"))
self.ui.actionCreate_new_character.setText(self.tr("Create new character"))
self.ui.actionSettings.setText(self.tr("Settings"))
self.ui.actionGet_support_from_Discord.setText(self.tr("Get support from Discord"))
self.ui.actionGet_support_from_GitHub.setText(self.tr("Get support from GitHub"))
self.ui.label_elevenlabs_voice_id.setText(self.tr("Write Voice ID: "))
class Conversation:
"""В этом классе прописывается возможность взаимодействия пользователя и больших языковых моделей для общения в программе"""
def __init__(self):
self.configuration = Configuration()
async def init_async(self):
self.characterAI_token = self.configuration.get_value("api_token")
self.client = aiocai.Client(self.characterAI_token)
return self
async def get_characterai_tts_audio(self, candidateID, roomID, turnID, voiceID):
"""Получаем содержимое всего json-файла с ссылки Character AI TTS"""
url = "https://neo.character.ai/multimodal/api/v1/memo/replay"
headers = {
"Content-Type": "application/json",
'Authorization': f'Token {self.characterAI_token}'
}
data = {
"candidateId": candidateID,
"roomId": roomID,
"turnId": turnID,
"voiceId": voiceID
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as response:
if response.status == 200:
return await response.json()
else:
raise Exception(f"Ошибка запроса: {response.status}, {await response.text()}")
async def get_character(self, character_id):
"""Функция для получения информации о персонаже"""
if len(character_id) == 43:
get_character = await self.client.get_char(character_id)
return get_character.name, get_character.title, get_character.greeting
else:
self.show_error_message(
"Soul of Waifu - Character Error",
"Your text is not character ID",
"Enter your ID correctly"
)
return None, None, None
async def get_character_avatar(self, character_id):
"""Функция для получения файла с изображением персонажа"""
get_character = await self.client.get_char(character_id)
avatar_file_name = get_character.avatar_file_name.split("/")[-1]
await get_character.avatar.download(f"./resources/avatars/{avatar_file_name}")
return avatar_file_name
async def get_user(self):
"""Функция для получения информации о пользователе"""
get_user = await self.client.get_me()
avatar_file_name = get_user.avatar_file_name.split("/")[-1]
await get_user.avatar.download(f"./resources/avatars/{avatar_file_name}")
return get_user.name, avatar_file_name
def show_error_message(self, title, text_1, text_2):
"""Функция для отображения сообщения об ошибке"""
mbox_problem = QMessageBox()
mbox_problem.setWindowIcon(QtGui.QIcon("./resources/icons/Logotype.ico"))
mbox_problem.setWindowTitle(title)
align = "justify"
mbox_problem.setText(f"<p><b>Error</b></p><p align={align}>{text_1}</p><p align={align}>{text_2}</p>")
mbox_problem.exec()
async def send_message(self, character_id, user_message):
"""Общий метод для отправки сообщения персонажу и получения содержимого ответа"""
if self.client is None:
await self.init_async()
get_chat = await self.client.get_chat(character_id)
chat_id = get_chat.chat_id
async with await self.client.connect() as chat:
message = await chat.send_message(character_id, chat_id, user_message)
return message
async def character_conversation_send_message_None(self, character_id, user_message):
"""Метод для отправки персонажу сообщения и получения содержимого ответа. Mode: None"""
message = await self.send_message(character_id, user_message)
return message.text
async def character_conversation_send_message_CharacterAI_TTS(self, character_id, user_message):
"""Метод для отправки персонажу сообщения и получения содержимого ответа. Mode: Character AI TTS"""
message = await self.send_message(character_id, user_message)
return message.text, message.turn_key.turn_id, message.primary_candidate_id, message.turn_key.chat_id
async def character_conversation_send_message_SileroTTS(self, character_id, user_message):
"""Метод для отправки персонажу сообщения и получения содержимого ответа. Mode: Silero TTS"""
message = await self.send_message(character_id, user_message)
return message.text
async def character_conversation_send_message_ElevenLabs(self, character_id, user_message):
"""Метод для отправки персонажу сообщения и получения содержимого ответа. Mode: Eleven Labs"""
message = await self.send_message(character_id, user_message)
return message.text
async def _fetch_audio(self, url, filename):
"""Скачивание аудио с url-ссылки и его воспроизведение"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
async with aiofiles.open(filename, 'wb') as f:
async for chunk in response.content.iter_any():
if chunk:
await f.write(chunk)
else:
print(f"Failed to download file. Status code: {response.status}")
def _play_and_remove_file(self, filename):
"""Проигрывает аудио с файла и удаляет его"""