forked from innightwolfsleep/llm_telegram_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramBotWrapper.py
More file actions
1398 lines (1326 loc) · 63.7 KB
/
Copy pathTelegramBotWrapper.py
File metadata and controls
1398 lines (1326 loc) · 63.7 KB
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
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 io
import os.path
from threading import Thread, Lock, Event
from pathlib import Path
import json
import time
from re import split, sub
from os import listdir
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, InputMediaAudio
from telegram.ext import CallbackContext, Filters, CommandHandler, MessageHandler, CallbackQueryHandler
from telegram.ext import Updater
from telegram.error import BadRequest
from telegram.constants import CHATACTION_TYPING
from typing import Dict, Tuple
from deep_translator import GoogleTranslator as Translator
try:
from extensions.telegram_bot.TelegramBotUser import TelegramBotUser as User
import extensions.telegram_bot.TelegramBotGenerator as Generator
from extensions.telegram_bot.TelegramBotSilero import Silero as Silero
from extensions.telegram_bot.TelegramBotSdApi import SdApi as SdApi
except ImportError:
from TelegramBotUser import TelegramBotUser as User
import TelegramBotGenerator as Generator
from TelegramBotSilero import Silero as Silero
from TelegramBotSdApi import SdApi as SdApi
class TelegramBotWrapper:
# Default error messages
GENERATOR_FAIL = "<GENERATION FAIL>"
GENERATOR_EMPTY_ANSWER = "<EMPTY ANSWER>"
UNKNOWN_TEMPLATE = "<UNKNOWN TEMPLATE>"
UNKNOWN_USER = "<UNKNOWN USER>"
# Various predefined data
MODE_ADMIN = "admin"
MODE_CHAT = "chat"
MODE_CHAT_R = "chat-restricted"
MODE_NOTEBOOK = "notebook"
MODE_PERSONA = "persona"
MODE_QUERY = "query"
BTN_CONTINUE = 'Continue'
BTN_NEXT = 'Next'
BTN_DEL_WORD = 'Delete_one_word'
BTN_REGEN = 'Regen'
BTN_CUTOFF = 'Cutoff'
BTN_DELETE = "Delete"
BTN_RESET = 'Reset'
BTN_DOWNLOAD = 'Download'
BTN_LORE = 'Context'
BTN_CHAR_LIST = 'Chars_list'
BTN_CHAR_LOAD = 'Chars_load'
BTN_MODEL_LIST = 'Model_list'
BTN_MODEL_LOAD = 'Model_load'
BTN_VOICE_LIST = 'Voice_list'
BTN_VOICE_LOAD = 'Voice_load'
BTN_PRESET_LIST = 'Presets_list'
BTN_PRESET_LOAD = 'Preset_load'
BTN_LANG_LIST = 'Language_list'
BTN_LANG_LOAD = 'Language_load'
BTN_OPTION = "options"
GET_MESSAGE = "message"
GENERATOR_MODE_NEXT = "/send_next_message"
GENERATOR_MODE_CONTINUE = "/continue_last_message"
# Supplementary structure
replace_prefixes = ["!", "-"] # Prefix to replace last message
impersonate_prefixes = ["#", "+"] # Prefix for "impersonate" message
# Prefix for persistence "impersonate" message
permanent_impersonate_prefixes = ["++"]
# Prefix for replace username "impersonate" message
permanent_user_prefixes = ["+="]
permanent_contex_add = ["+-"] # Prefix for adding string to context
# Prefix for sd api generation
sd_api_prefixes = ["📷", "📸", "📹", "🎥", "📽", ]
sd_api_prompt_of = "(Detailed description of appearance, outfit and surroundings of OBJECT:"
sd_api_prompt_self = "(Detailed description of appearance, surroundings and what doing right now:"
# Language list
language_dict = {
"en": "🇬🇧",
"ru": "🇷🇺",
"ja": "🇯🇵",
"fr": "🇫🇷",
"es": "🇪🇸",
"de": "🇩🇪",
"th": "🇹🇭",
"tr": "🇹🇷",
"it": "🇮🇹",
"hi": "🇮🇳",
"zh-CN": "🇨🇳",
"ar": "🇸🇾"}
# Set dummy obj for telegram updater
updater = None
# Define generator lock to prevent GPU overloading
generator_lock = Lock()
generation_timeout = 600
# Bot message open/close html tags. Set ["", ""] to disable.
html_tag = ["<pre>", "</pre>"]
translate_html_tag = ['<span class="tg-spoiler">', '</span>']
translation_as_hidden_text = "off"
# dict of User data dicts, here stored all users' session info.
users: Dict[int, User] = {}
# last message timestamp to avoid message flood.
last_msg_timestamp: Dict[int, int] = {}
# Delay between new messages to avoid flooding (sec)
flood_avoid_delay = 10.0
def __init__(self,
bot_mode="admin",
default_char="Example.yaml",
default_preset="LLaMA-Precise.txt",
model_lang="en",
user_lang="en",
characters_dir_path="characters",
presets_dir_path="presets",
history_dir_path="history",
token_file_path="configs/telegram_token.txt",
admins_file_path="configs/telegram_admins.txt",
users_file_path="configs/telegram_users.txt",
config_file_path="configs/telegram_config.json",
generator_params_file_path="configs/telegram_generator_params.json",
user_rules_file_path="configs/telegram_user_rules.json",
sd_api_url="http://127.0.0.1:7860",
sd_config_file_path="configs/telegram_sd_config.json",
):
"""Init telegram bot class. Use run_telegram_bot() to initiate bot.
Args
bot_mode: bot mode (chat, chat-restricted, notebook, persona). Default is "chat".
default_char: name of default character.json file. Default is "chat".
default_preset: name of default preset file.
model_lang: language of model
user_lang: language of conversation
characters_dir_path: place where stored characters .json files. Default is "chat".
presets_dir_path: path to presets generation presets.
history_dir_path: place where stored chat history. Default is "extensions/telegram_bot/history".
token_file_path: path to token file. Default is "extensions/telegram_bot/telegram_token.txt".
admins_file_path: path to admins file - user separated by "\n"
users_file_path: permitted users separated by "\n". If empty - no restriction (whitelist)
config_file_path: path to config file
generator_params_file_path: llm generation config path
user_rules_file_path: user rule matrix
sd_api_url: url if sd api uses for generating images
sd_config_file_path: config for sd api
"""
# Set paths to history, default token file, characters dir
self.history_dir_path = history_dir_path
self.characters_dir_path = characters_dir_path
self.presets_dir_path = presets_dir_path
self.token_file_path = token_file_path
self.admins_file_path = admins_file_path
self.users_file_path = users_file_path
self.config_file_path = config_file_path
self.generator_params_file_path = generator_params_file_path
self.user_rules_file_path = user_rules_file_path
self.sd_api_url = sd_api_url
self.sd_config_file_path = sd_config_file_path
# Set bot mode
self.bot_mode = bot_mode
self.generator_script = "" # mode loaded from config
self.model_path = ""
# Set default character json file
self.default_char = default_char
self.default_preset = default_preset
# Set translator
self.model_lang = model_lang
self.user_lang = user_lang
self.stopping_strings = []
self.eos_token = None
self.proxy_url = None
# Read config_file if existed, overwrite bot config
self.load_config_file(self.config_file_path)
# Load user generator parameters
if os.path.exists(self.generator_params_file_path):
with open(self.generator_params_file_path, "r") as params_file:
self.generation_params = json.loads(params_file.read())
else:
print("Cant find generator_params_file")
self.generation_params = {}
# Load preset
self.load_preset(self.default_preset)
# Load user rules
if os.path.exists(self.user_rules_file_path):
with open(self.user_rules_file_path, "r") as user_rules_file:
self.user_rules = json.loads(user_rules_file.read())
else:
print("Cant find user_rules_file_path: " + self.user_rules_file_path)
self.user_rules = {}
# Silero initiate
self.silero = Silero()
# SdApi initiate
self.SdApi = SdApi(self.sd_api_url, self.sd_config_file_path)
# generator initiate
print("Telegram bot generator script: ", self.generator_script)
Generator.init(self.generator_script, self.model_path)
def load_config_file(self, config_file_path: str):
if os.path.exists(config_file_path):
with open(config_file_path, "r") as config_file_path:
config = json.loads(config_file_path.read())
self.bot_mode = config.get("bot_mode", self.bot_mode)
self.generator_script = config.get("generator_script", self.generator_script)
self.model_path = config.get("model_path", self.model_path)
self.default_preset = config.get("default_preset", self.default_preset)
self.default_char = config.get("default_char", self.default_char)
self.model_lang = config.get("model_lang", self.model_lang)
self.user_lang = config.get("user_lang", self.user_lang)
self.characters_dir_path = config.get("characters_dir_path", self.characters_dir_path)
self.presets_dir_path = config.get("presets_dir_path", self.presets_dir_path)
self.history_dir_path = config.get("history_dir_path", self.history_dir_path)
self.token_file_path = config.get("token_file_path", self.token_file_path)
self.admins_file_path = config.get("admins_file_path", self.admins_file_path)
self.users_file_path = config.get("users_file_path", self.users_file_path)
self.generator_params_file_path = config.get("generator_params_file_path", self.generator_params_file_path)
self.user_rules_file_path = config.get("user_rules_file_path", self.user_rules_file_path)
self.sd_api_url = config.get("sd_api_url", self.sd_api_url)
self.sd_config_file_path = config.get("sd_config_file_path", self.sd_config_file_path)
self.translation_as_hidden_text = config.get("translation_as_hidden_text",
self.translation_as_hidden_text)
self.stopping_strings = config.get("stopping_strings", self.stopping_strings)
self.eos_token = config.get("eos_token", self.eos_token)
self.proxy_url = config.get("proxy_url", self.proxy_url)
else:
print("Cant find config_file " + config_file_path)
# =============================================================================
# Run bot with token! Initiate updater obj!
def run_telegram_bot(self, bot_token=None, token_file_name=None):
"""
Start the Telegram bot.
:param bot_token: (str) The Telegram bot token. If not provided, try to read it from `token_file_name`.
:param token_file_name: (str) The name of the file containing the bot token. Default is `None`.
:return: None
"""
if not bot_token:
token_file_name = token_file_name or self.token_file_path
with open(token_file_name, "r", encoding="utf-8") as f:
bot_token = f.read().strip()
# if using socks proxy, may require additional package : python-telegram-bot[socks]
# run this command to install "pip install "python-telegram-bot[socks]""
request_kwargs = {
'proxy_url': self.proxy_url,
}
self.updater = Updater(token=bot_token, use_context=True, request_kwargs=request_kwargs)
self.updater.dispatcher.add_handler(
CommandHandler("start", self.cb_start_command)),
self.updater.dispatcher.add_handler(
MessageHandler(Filters.text, self.cb_get_message))
self.updater.dispatcher.add_handler(
MessageHandler(
Filters.document.mime_type("application/json"),
self.cb_get_json_document))
self.updater.dispatcher.add_handler(
CallbackQueryHandler(self.cb_opt_button))
self.updater.start_polling()
Thread(target=self.no_sleep_callback).start()
print("Telegram bot started!", self.updater)
def no_sleep_callback(self):
while True:
try:
self.updater.bot.send_message(chat_id=99999999999, text='One message every minute')
except BadRequest:
pass
except Exception as error:
print(error)
time.sleep(60)
# =============================================================================
# Handlers
def cb_start_command(self, upd, context):
Thread(target=self.thread_welcome_message,
args=(upd, context)).start()
def cb_get_message(self, upd, context):
Thread(target=self.thread_get_message, args=(upd, context)).start()
def cb_opt_button(self, upd, context):
Thread(target=self.thread_push_button, args=(upd, context)).start()
def cb_get_json_document(self, upd, context):
Thread(
target=self.thread_get_json_document,
args=(
upd,
context)).start()
# =============================================================================
# Additional telegram actions
def thread_welcome_message(self, upd: Update, context: CallbackContext):
chat_id = upd.effective_chat.id
if not self.check_user_permission(chat_id):
return False
self.init_check_user(chat_id)
send_text = self.make_template_message("char_loaded", chat_id)
context.bot.send_message(
text=send_text, chat_id=chat_id,
reply_markup=self.get_options_keyboard(chat_id),
parse_mode="HTML")
def clean_last_message_markup(
self,
context: CallbackContext,
chat_id: int):
if (chat_id in self.users and
len(self.users[chat_id].msg_id) > 0):
last_msg = self.users[chat_id].msg_id[-1]
try:
context.bot.editMessageReplyMarkup(
chat_id=chat_id, message_id=last_msg, reply_markup=None)
except Exception as exception:
print("last_message_markup_clean", exception)
def make_template_message(
self,
request: str,
chat_id: int,
custom_string="") -> str:
# create a message using default_messages_template or return
# UNKNOWN_TEMPLATE
if chat_id in self.users:
user = self.users[chat_id]
if request in user.default_messages_template:
msg = user.default_messages_template[request]
msg = msg.replace("_CHAT_ID_", str(chat_id))
msg = msg.replace("_NAME1_", user.name1)
msg = msg.replace("_NAME2_", user.name2)
msg = msg.replace("_CONTEXT_", user.context)
msg = msg.replace("_GREETING_",
self.prepare_text(user.greeting, user.language, "to_user"))
msg = msg.replace("_CUSTOM_STRING_",
self.prepare_text(custom_string, user.language, "to_user"))
msg = msg.replace("_OPEN_TAG_", self.html_tag[0])
msg = msg.replace("_CLOSE_TAG_", self.html_tag[1])
return msg
else:
print(request, custom_string)
return self.UNKNOWN_TEMPLATE
else:
print(request, custom_string)
return self.UNKNOWN_USER
# =============================================================================
# Work with history! Init/load/save functions
def parse_characters_dir(self) -> list:
char_list = []
for f in listdir(self.characters_dir_path):
if f.endswith(('.json', '.yaml', '.yml')):
char_list.append(f)
return char_list
def parse_presets_dir(self) -> list:
preset_list = []
for f in listdir(self.presets_dir_path):
if f.endswith('.txt') or f.endswith('.yaml'):
preset_list.append(f)
return preset_list
def init_check_user(self, chat_id):
if chat_id not in self.users:
# Load default
self.users.update({chat_id: User()})
self.users[chat_id].load_character_file(
characters_dir_path=self.characters_dir_path,
char_file=self.default_char)
self.users[chat_id].load_user_history(
f'{self.history_dir_path}/{str(chat_id)}.json')
self.users[chat_id].find_and_load_user_char_history(
chat_id, self.history_dir_path)
def thread_get_json_document(self, upd: Update, context: CallbackContext):
chat_id = upd.message.chat.id
if not self.check_user_permission(chat_id):
return False
self.init_check_user(chat_id)
default_user_file_path = str(
Path(f'{self.history_dir_path}/{str(chat_id)}.json'))
with open(default_user_file_path, 'wb') as f:
context.bot.get_file(upd.message.document.file_id).download(out=f)
self.users[chat_id].load_user_history(default_user_file_path)
if len(self.users[chat_id].history) > 0:
last_message = self.users[chat_id].history[-1]
else:
last_message = "<no message in history>"
send_text = self.make_template_message(
"hist_loaded", chat_id, last_message)
context.bot.send_message(
chat_id=chat_id, text=send_text,
reply_markup=self.get_options_keyboard(chat_id),
parse_mode="HTML")
def typing_status_start(
self,
context: CallbackContext,
chat_id: int) -> Event:
typing_active = Event()
typing_active.set()
Thread(target=self.thread_typing_status, args=(
context, chat_id, typing_active)).start()
return typing_active
def thread_typing_status(
self,
context: CallbackContext,
chat_id: int,
typing_active: Event):
limit_counter = int(self.generation_timeout / 6)
while typing_active.is_set() and limit_counter > 0:
context.bot.send_chat_action(
chat_id=chat_id, action=CHATACTION_TYPING)
time.sleep(6)
limit_counter -= 1
def check_user_permission(self, chat_id):
# Read admins list
if os.path.exists(self.users_file_path):
with open(self.users_file_path, "r") as users_file:
users_list = users_file.read().split()
else:
users_list = []
# check
if str(chat_id) in users_list or len(users_list) == 0:
return True
else:
return False
def check_user_flood(self, chat_id):
if chat_id not in self.last_msg_timestamp:
self.last_msg_timestamp.update({chat_id: time.time()})
return True
if time.time() - \
self.flood_avoid_delay > self.last_msg_timestamp[chat_id]:
self.last_msg_timestamp.update({chat_id: time.time()})
return True
else:
return False
def check_user_rule(self, chat_id, option):
if os.path.exists(self.user_rules_file_path):
with open(self.user_rules_file_path, "r") as user_rules_file:
self.user_rules = json.loads(user_rules_file.read())
option = sub(r"[0123456789-]", "", option)
if option.endswith(self.BTN_OPTION):
option = self.BTN_OPTION
# Read admins list
if os.path.exists(self.admins_file_path):
with open(self.admins_file_path, "r") as admins_file:
admins_list = admins_file.read().split()
else:
admins_list = []
# check admin rules
if str(chat_id) in admins_list or self.bot_mode == self.MODE_ADMIN:
return bool(self.user_rules[option][self.MODE_ADMIN])
else:
return bool(self.user_rules[option][self.bot_mode])
# =============================================================================
# answer generator
def generate_answer(self, user_in, chat_id) -> Tuple[str, bool]:
answer = self.GENERATOR_FAIL
user = self.users[chat_id]
try:
# acquire generator lock if we can
self.generator_lock.acquire(timeout=self.generation_timeout)
# if generation will fail, return "fail" answer
# Preprocessing: add user_in to history in right order:
if user_in[:2] in self.permanent_impersonate_prefixes:
# If user_in starts with perm_prefix - just replace name2
user.name2 = user_in[2:]
self.generator_lock.release()
return "New name: " + user.name2, True
if self.bot_mode in [self.MODE_QUERY]:
user.history = []
if self.bot_mode in [self.MODE_NOTEBOOK]:
# If notebook mode - append to history only user_in, no
# additional preparing;
user.user_in.append(user_in)
user.history.append('')
user.history.append(user_in)
elif user_in == self.GENERATOR_MODE_NEXT:
# if user_in is "" - no user text, it is like continue generation
# adding "" history line to prevent bug in history sequence,
# add "name2:" prefix for generation
user.user_in.append(user_in)
user.history.append("")
user.history.append(user.name2 + ":")
elif user_in == self.GENERATOR_MODE_CONTINUE:
# if user_in is "" - no user text, it is like continue generation
# adding "" history line to prevent bug in history sequence,
# add "name2:" prefix for generation
pass
elif user_in[0] in self.sd_api_prefixes:
# If user_in starts with prefix - impersonate-like (if you try to get "impersonate view")
# adding "" line to prevent bug in history sequence, user_in is
# prefix for bot answer
if len(user_in) == 1:
user.user_in.append(user_in)
user.history.append("")
user.history.append(self.sd_api_prompt_self)
else:
user.user_in.append(user_in)
user.history.append("")
user.history.append(self.sd_api_prompt_of.replace("OBJECT", user_in[1:].strip()))
elif user_in[0] in self.impersonate_prefixes:
# If user_in starts with prefix - impersonate-like (if you try to get "impersonate view")
# adding "" line to prevent bug in history sequence, user_in is
# prefix for bot answer
user.user_in.append(user_in)
user.history.append("")
user.history.append(user_in[1:] + ":")
elif user_in[0] in self.replace_prefixes:
# If user_in starts with replace_prefix - fully replace last
# message
user.user_in.append(user_in)
user.history[-1] = user_in[1:]
self.generator_lock.release()
return user.history[-1], False
else:
# If not notebook/impersonate/continue mode then ordinary chat preparing
# add "name1&2:" to user and bot message (generation from name2
# point of view);
user.user_in.append(user_in)
user.history.append(user.name1 + ": " + user_in)
user.history.append(user.name2 + ":")
# Set eos_token and stopping_strings.
stopping_strings = self.stopping_strings.copy()
eos_token = self.eos_token
if self.bot_mode in [
self.MODE_CHAT,
self.MODE_CHAT_R,
self.MODE_ADMIN]:
stopping_strings += ["\n" + user.name1 + ":", "\n" + user.name2 + ":", ]
# adjust context/greeting/example
if user.context.strip().endswith("\n"):
context = f"{user.context.strip()}"
else:
context = f"{user.context.strip()}\n"
if len(user.example) > 0:
example = user.example + "\n<START>\n"
else:
example = ""
if len(user.greeting) > 0:
greeting = "\n" + user.name2 + ": " + user.greeting
else:
greeting = ""
# Make prompt: context + example + conversation history
prompt = ""
available_len = self.generation_params["truncation_length"]
context_len = Generator.tokens_count(context)
available_len -= context_len
if available_len < 0:
available_len = 0
print("telegram_bot: ", chat_id, " - CONTEXT IS TOO LONG!!!")
conversation = [example, greeting] + user.history
for s in reversed(conversation):
s = "\n" + s if len(s) > 0 else s
s_len = Generator.tokens_count(s)
if available_len >= s_len:
prompt = s + prompt
available_len -= s_len
else:
break
prompt = context + prompt.replace("\n\n", "\n")
# Generate!
answer = Generator.get_answer(
prompt=prompt,
generation_params=self.generation_params,
user=json.loads(user.to_json()),
eos_token=eos_token,
stopping_strings=stopping_strings,
default_answer=answer,
turn_template=user.turn_template)
# If generation result zero length - return "Empty answer."
if len(answer) < 1:
answer = self.GENERATOR_EMPTY_ANSWER
# Final return
if answer not in [
self.GENERATOR_EMPTY_ANSWER,
self.GENERATOR_FAIL]:
# if everything ok - add generated answer in history and return
# last
for end in stopping_strings:
if answer.endswith(end):
answer = answer[:-len(end)]
user.history[-1] = user.history[-1] + " " + answer
self.generator_lock.release()
return user.history[-1], False
except Exception as exception:
print("generate_answer", exception)
# anyway, release generator lock. Then return
self.generator_lock.release()
def prepare_text(
self,
original_text,
user_language="en",
direction="to_user"):
text = original_text
# translate
if self.model_lang != user_language:
try:
if direction == "to_model":
text = Translator(
source=user_language,
target=self.model_lang).translate(text)
elif direction == "to_user":
text = Translator(
source=self.model_lang,
target=user_language).translate(text)
except Exception as e:
text = "can't translate text:" + str(text)
print("translator_error", e)
# Add HTML tags and other...
if direction not in ["to_model", "no_html"]:
text = text.replace("#", "#").replace("<", "<").replace(">", ">")
original_text = original_text.replace("#", "#").replace("<", "<").replace(">", ">")
if self.model_lang != user_language and direction == "to_user" and self.translation_as_hidden_text == "on":
text = self.html_tag[0] + original_text + self.html_tag[1] + "\n" + \
self.translate_html_tag[0] + text + self.translate_html_tag[1]
else:
text = self.html_tag[0] + text + self.html_tag[1]
return text
def send_sd_image(self, upd: Update, context: CallbackContext, answer, user_text):
chat_id = upd.message.chat.id
file_list = self.SdApi.txt_to_image(answer)
answer = answer.replace(self.sd_api_prompt_of.replace("OBJECT", user_text[1:].strip()), "")
answer = answer.split("\n")[0]
if len(file_list) > 0:
for image_path in file_list:
if os.path.exists(image_path):
with open(image_path, "rb") as image_file:
context.bot.send_photo(caption=answer, chat_id=chat_id, photo=image_file)
os.remove(image_path)
def send(self, context: CallbackContext, chat_id: int, text: str):
user = self.users[chat_id]
text = self.prepare_text(text, self.users[chat_id].language, "to_user")
if user.silero_speaker == "None" or user.silero_model_id == "None":
message = context.bot.send_message(
text=text,
chat_id=chat_id,
parse_mode="HTML",
reply_markup=self.get_chat_keyboard())
return message
else:
if ":" in text:
audio_text = ":".join(text.split(":")[1:])
else:
audio_text = text
audio_path = self.silero.get_audio(
text=audio_text, user_id=chat_id, user=user)
if audio_path is not None:
with open(audio_path, "rb") as audio:
message = context.bot.send_audio(
chat_id=chat_id,
audio=audio,
caption=text,
filename=f"{user.name2}_to_{user.name1}.wav",
parse_mode="HTML",
reply_markup=self.get_chat_keyboard())
else:
message = context.bot.send_message(
text=text,
chat_id=chat_id,
parse_mode="HTML",
reply_markup=self.get_chat_keyboard())
return message
return message
def edit(
self,
context: CallbackContext,
upd: Update,
chat_id: int,
text: str,
message_id: int):
user = self.users[chat_id]
text = self.prepare_text(text, user.language, "to_user")
if upd.callback_query.message.text is not None:
context.bot.editMessageText(
text=text,
chat_id=chat_id,
parse_mode="HTML",
message_id=message_id,
reply_markup=self.get_chat_keyboard())
if upd.callback_query.message.audio is not None \
and user.silero_speaker != "None" \
and user.silero_model_id != "None":
if ":" in text:
audio_text = ":".join(text.split(":")[1:])
else:
audio_text = text
audio_path = self.silero.get_audio(
text=audio_text, user_id=chat_id, user=user)
if audio_path is not None:
with open(audio_path, "rb") as audio:
media = InputMediaAudio(
media=audio, filename=f"{user.name2}_to_{user.name1}.wav")
context.bot.edit_message_media(
chat_id=chat_id,
media=media,
message_id=message_id,
reply_markup=self.get_chat_keyboard())
if upd.callback_query.message.caption is not None:
context.bot.editMessageCaption(
chat_id=chat_id,
caption=text,
parse_mode="HTML",
message_id=message_id,
reply_markup=self.get_chat_keyboard())
# =============================================================================
# Message handler
def thread_get_message(self, upd: Update, context: CallbackContext):
# Extract user input and chat ID
user_text = upd.message.text
chat_id = upd.message.chat.id
if not self.check_user_permission(chat_id):
return False
if not self.check_user_flood(chat_id):
return False
# Send "typing" message
typing = self.typing_status_start(context, chat_id)
try:
if self.check_user_rule(
chat_id=chat_id,
option=self.GET_MESSAGE) is not True:
return False
self.init_check_user(chat_id)
user = self.users[chat_id]
# Generate answer and replace "typing" message with it
print("user_text", user_text)
user_text = self.prepare_text(
user_text, self.users[chat_id].language, "to_model")
print("user_text", user_text)
answer, system_message = self.generate_answer(
user_in=user_text, chat_id=chat_id)
if system_message:
context.bot.send_message(text=answer, chat_id=chat_id)
elif user_text[0] in self.sd_api_prefixes:
user.truncate_only_history()
self.send_sd_image(upd, context, answer, user_text)
else:
message = self.send(
text=answer, chat_id=chat_id, context=context)
# Clear buttons on last message (if they exist in current
# thread)
self.clean_last_message_markup(context, chat_id)
# Add message ID to message history
user.msg_id.append(message.message_id)
# Save user history
user.save_user_history(chat_id, self.history_dir_path)
except Exception as e:
print(e)
raise e
finally:
typing.clear()
# =============================================================================
# button
def thread_push_button(self, upd: Update, context: CallbackContext):
query = upd.callback_query
query.answer()
chat_id = query.message.chat.id
msg_id = query.message.message_id
option = query.data
if not self.check_user_permission(chat_id):
return False
# Send "typing" message
typing = self.typing_status_start(context, chat_id)
try:
if chat_id not in self.users:
self.init_check_user(chat_id)
if msg_id not in self.users[chat_id].msg_id and option in [
self.BTN_NEXT,
self.BTN_CONTINUE,
self.BTN_DEL_WORD,
self.BTN_REGEN,
self.BTN_CUTOFF]:
send_text = self.make_template_message("mem_lost", chat_id)
context.bot.editMessageText(
text=send_text, chat_id=chat_id, message_id=msg_id,
reply_markup=None, parse_mode="HTML")
else:
self.handle_button_option(option, chat_id, upd, context)
self.users[chat_id].save_user_history(
chat_id, self.history_dir_path)
except Exception as e:
print(e)
finally:
typing.clear()
def handle_button_option(self, option, chat_id, upd, context):
if option == self.BTN_RESET and self.check_user_rule(chat_id, option):
self.reset_history_button(upd=upd, context=context)
elif option == self.BTN_CONTINUE and self.check_user_rule(chat_id, option):
self.continue_message_button(upd=upd, context=context)
elif option == self.BTN_NEXT and self.check_user_rule(chat_id, option):
self.next_message_button(upd=upd, context=context)
elif option == self.BTN_DEL_WORD and self.check_user_rule(chat_id, option):
self.delete_word_button(upd=upd, context=context)
elif option == self.BTN_REGEN and self.check_user_rule(chat_id, option):
self.regenerate_message_button(upd=upd, context=context)
elif option == self.BTN_CUTOFF and self.check_user_rule(chat_id, option):
self.cutoff_message_button(upd=upd, context=context)
elif option == self.BTN_DOWNLOAD and self.check_user_rule(chat_id, option):
self.download_json_button(upd=upd, context=context)
elif option == self.BTN_OPTION and self.check_user_rule(chat_id, option):
self.show_options_button(upd=upd, context=context)
elif option == self.BTN_DELETE and self.check_user_rule(chat_id, option):
self.delete_pressed_button(upd=upd, context=context)
elif option.startswith(self.BTN_CHAR_LIST) and self.check_user_rule(chat_id, option):
self.keyboard_characters_button(
upd=upd, context=context, option=option)
elif option.startswith(self.BTN_CHAR_LOAD) and self.check_user_rule(chat_id, option):
self.load_character_button(upd=upd, context=context, option=option)
elif option.startswith(self.BTN_PRESET_LIST) and self.check_user_rule(chat_id, option):
self.keyboard_presets_button(
upd=upd, context=context, option=option)
elif option.startswith(self.BTN_PRESET_LOAD) and self.check_user_rule(chat_id, option):
self.load_presets_button(upd=upd, context=context, option=option)
elif option.startswith(self.BTN_MODEL_LIST) and self.check_user_rule(chat_id, option):
self.keyboard_models_button(
upd=upd, context=context, option=option)
elif option.startswith(self.BTN_MODEL_LOAD) and self.check_user_rule(chat_id, option):
self.load_model_button(upd=upd, context=context, option=option)
elif option.startswith(self.BTN_LANG_LIST) and self.check_user_rule(chat_id, option):
self.keyboard_language_button(
upd=upd, context=context, option=option)
elif option.startswith(self.BTN_LANG_LOAD) and self.check_user_rule(chat_id, option):
self.load_language_button(upd=upd, context=context, option=option)
elif option.startswith(self.BTN_VOICE_LIST) and self.check_user_rule(chat_id, option):
self.keyboard_voice_button(upd=upd, context=context, option=option)
elif option.startswith(self.BTN_VOICE_LOAD) and self.check_user_rule(chat_id, option):
self.load_voice_button(upd=upd, context=context, option=option)
def show_options_button(self, upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
user = self.users[chat_id]
history_tokens = -1
context_tokens = -1
greeting_tokens = -1
try:
history_tokens = Generator.tokens_count("\n".join(user.history))
context_tokens = Generator.tokens_count("\n".join(user.context))
greeting_tokens = Generator.tokens_count("\n".join(user.greeting))
except Exception as e:
print("options_button tokens_count", e)
send_text = f"""{user.name2} ({user.char_file}),
Conversation length: {str(len(user.history))} messages, ({history_tokens} tokens).
Context:{context_tokens}, greeting:{greeting_tokens} tokens.
Voice: {user.silero_speaker}
Language: {user.language}"""
context.bot.send_message(
text=send_text, chat_id=chat_id,
reply_markup=self.get_options_keyboard(chat_id),
parse_mode="HTML")
@staticmethod
def delete_pressed_button(upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
message_id = upd.callback_query.message.message_id
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
def next_message_button(self, upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
user = self.users[chat_id]
# send "typing"
self.clean_last_message_markup(context, chat_id)
answer, _ = self.generate_answer(
user_in=self.GENERATOR_MODE_NEXT, chat_id=chat_id)
message = self.send(text=answer, chat_id=chat_id, context=context)
self.users[chat_id].msg_id.append(message.message_id)
user.save_user_history(chat_id, self.history_dir_path)
def continue_message_button(self, upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
message = upd.callback_query.message
user = self.users[chat_id]
# get answer and replace message text!
answer, _ = self.generate_answer(
user_in=self.GENERATOR_MODE_CONTINUE, chat_id=chat_id)
self.edit(
text=answer,
chat_id=chat_id,
message_id=message.message_id,
context=context,
upd=upd)
self.users[chat_id].msg_id.append(message.message_id)
user.save_user_history(chat_id, self.history_dir_path)
def delete_word_button(self, upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
user = self.users[chat_id]
# get and change last message
last_message = user.history[-1]
last_word = split(r"\n+| +", last_message)[-1]
new_last_message = last_message[:-(len(last_word))]
new_last_message = new_last_message.strip()
user.history[-1] = new_last_message
# If there is previous message - add buttons to previous message
if user.msg_id:
self.edit(text=user.history[-1],
chat_id=chat_id,
message_id=user.msg_id[-1],
context=context,
upd=upd)
user.save_user_history(chat_id, self.history_dir_path)
def regenerate_message_button(self, upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
msg = upd.callback_query.message
user = self.users[chat_id]
# add pretty "retyping" to message text
# remove last bot answer, read and remove last user reply
user_in = user.truncate_only_history()
# get answer and replace message text!
answer, _ = self.generate_answer(user_in=user_in, chat_id=chat_id)
self.edit(
text=answer,
chat_id=chat_id,
message_id=msg.message_id,
context=context,
upd=upd)
user.save_user_history(chat_id, self.history_dir_path)
def cutoff_message_button(self, upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
user = self.users[chat_id]
# Edit or delete last message ID (strict lines)
last_msg_id = user.msg_id[-1]
context.bot.deleteMessage(chat_id=chat_id, message_id=last_msg_id)
# Remove last message and bot answer from history
user.truncate()
# If there is previous message - add buttons to previous message
if user.msg_id:
message_id = user.msg_id[-1]
context.bot.editMessageReplyMarkup(
chat_id=chat_id, message_id=message_id,
reply_markup=self.get_chat_keyboard())
user.save_user_history(chat_id, self.history_dir_path)
def download_json_button(self, upd: Update, context: CallbackContext):
chat_id = upd.callback_query.message.chat.id
if chat_id not in self.users:
return
user_file = io.StringIO(self.users[chat_id].to_json())
send_caption = self.make_template_message("hist_to_chat", chat_id)
context.bot.send_document(
chat_id=chat_id, caption=send_caption, document=user_file,
filename=self.users[chat_id].name2 + ".json")
def reset_history_button(self, upd: Update, context: CallbackContext):
# check if it is a callback_query or a command
if upd.callback_query:
chat_id = upd.callback_query.message.chat.id
else:
chat_id = upd.message.chat.id
if chat_id not in self.users:
return
user = self.users[chat_id]
if user.msg_id:
self.clean_last_message_markup(context, chat_id)
user.reset()
user.load_character_file(self.characters_dir_path, user.char_file)
send_text = self.make_template_message("mem_reset", chat_id)
context.bot.send_message(
chat_id=chat_id,
text=send_text,
reply_markup=self.get_options_keyboard(chat_id),
parse_mode="HTML")
# =============================================================================
# switching keyboard
def load_model_button(
self,
upd: Update,
context: CallbackContext,
option: str):
if Generator.get_model_list is not None:
model_list = Generator.get_model_list()
model_file = model_list[int(
option.replace(self.BTN_MODEL_LOAD, ""))]
chat_id = upd.effective_chat.id
send_text = "Loading " + model_file + ". 🪄"