forked from iolkhovsky/BasilAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_server.py
More file actions
70 lines (54 loc) · 1.61 KB
/
bot_server.py
File metadata and controls
70 lines (54 loc) · 1.61 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
import os
import random
import telebot
from models import InferenceModel
from utils import read_yaml
GROUP_TYPE = 'group'
MY_NAME = '@NeuralGrayBot'
CONFIG_CMD = '/config'
class BotConfig:
group_response_prob = 0.1
@staticmethod
def parse(text):
pars = {}
msg_text = text.replace(MY_NAME, '').replace(CONFIG_CMD, '')
for item in msg_text.split(' '):
if len(item) >= 3:
key, value = item.split('=')
pars[key] = value
return pars
@staticmethod
def try_parse(text):
try:
pars = BotConfig.parse(text)
if 'p' in pars:
BotConfig.group_response_prob = float(pars['p'])
return f'New configuration: {BotConfig.group_response_prob}'
except Exception as e:
return f'Couldnt update configuration'
token = os.getenv('BOT_TOKEN', default = 'TOKEN')
bot = telebot.TeleBot(token)
config = read_yaml('config/eval.yaml')
model = InferenceModel(
model_config=config['model'],
tokenizer_config=config['tokenizer'],
)
config = BotConfig()
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Neural Gray Bot")
@bot.message_handler(func=lambda message: True)
def reply(message):
msg_text = message.text
if MY_NAME in msg_text and CONFIG_CMD in msg_text:
bot.reply_to(message, config.try_parse(msg_text))
else:
if message.chat.type == GROUP_TYPE:
if MY_NAME in message.text:
bot.reply_to(message, model(message.text))
elif random.uniform(0, 1) <= config.group_response_prob:
bot.send_message(message.chat.id, model(message.text))
else:
bot.send_message(message.chat.id, model(message.text))
if __name__ == '__main__':
bot.infinity_polling()