-
Notifications
You must be signed in to change notification settings - Fork 1
/
horo_aiogram_bot.py
286 lines (255 loc) · 9.85 KB
/
horo_aiogram_bot.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
from aiogram import Bot, types
from aiogram import Dispatcher
from aiogram.filters import CommandStart
from aiogram.filters import Command
from aiogram import F
import asyncio
import logging
import json
import re
import random
from aiogram.utils.keyboard import ReplyKeyboardBuilder
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("horo_bot/models")
model = AutoModelForSeq2SeqLM.from_pretrained("horo_bot/models")
with open("horo_bot/config.json", "r") as f:
json_config = json.load(f)
TOKEN = json_config["token"]
logging.basicConfig(level=logging.INFO)
bot = Bot(TOKEN)
dp = Dispatcher()
lst = [
"♈Овен",
"♌Лев",
"♐Стрелец",
"♋Рак",
"♏Скорпион",
"♓Рыбы",
"♉Телец",
"♍Дева",
"♑Козерог",
"♊Близнецы",
"♎Весы",
"♒Водолей",
]
dialog = {
"in": [
"/hello",
"привет",
"hello",
"hi",
"privet",
"hey",
"добрый день",
"доброе утро",
"добрый вечер",
],
"out": ["Приветствую", "Здравствуйте", "Привет!", "Hi!", "Звездный привет!"],
}
# start message
@dp.message(CommandStart())
async def start(message: types.Message):
kb = [
[
types.KeyboardButton(text="🔥Знаки Огня"),
types.KeyboardButton(text="💧Знаки Воды"),
types.KeyboardButton(text="🌍Знаки Земли"),
types.KeyboardButton(text="🎈Знаки Воздуха"),
],
]
start_menu = types.ReplyKeyboardMarkup(
keyboard=kb, resize_keyboard=True, input_field_placeholder="Выбери свою стихию:"
)
await message.reply("Сперва выбери свою стихию:", reply_markup=start_menu)
# help message
@dp.message(Command("help"))
async def help(message: types.Message):
kb = [
[
types.KeyboardButton(text="🔥Знаки Огня"),
types.KeyboardButton(text="💧Знаки Воды"),
types.KeyboardButton(text="🌍Знаки Земли"),
types.KeyboardButton(text="🎈Знаки Воздуха"),
],
]
start_menu = types.ReplyKeyboardMarkup(
keyboard=kb, resize_keyboard=True, input_field_placeholder="Выбери свою стихию:"
)
await message.reply(
"""\
Привет, еще раз 😉!
Для начала работы выбери в меню свою стихию, а затем знак Зодиака. Напоминаю:\n\
- стихия Огня: ♈Овен, ♌Лев, ♐Стрелец\n\
- стихия Воды: ♋Рак, ♏Скорпион, ♓Рыбы\n\
- стихия Земли: ♉Телец, ♍Дева, ♑Козерог\n\
- стихия Воздуха: ♊Близнецы', ♎Весы, ♒Водолей""",
reply_markup=start_menu,
)
# any other text message
@dp.message(F.text)
async def handle_text(message: types.Message):
if message.text == "🔥Знаки Огня":
builder = ReplyKeyboardBuilder()
builder.row(
types.KeyboardButton(text="♈Овен"),
types.KeyboardButton(text="♌Лев"),
types.KeyboardButton(text="♐Стрелец"),
)
builder.row(types.KeyboardButton(text="Назад"))
await message.reply(
"Для какого знака Зодиака нужен гороскоп?",
reply_markup=builder.as_markup(resize_keyboard=True),
)
elif message.text == "💧Знаки Воды":
builder = ReplyKeyboardBuilder()
builder.row(
types.KeyboardButton(text="♋Рак"),
types.KeyboardButton(text="♏Скорпион"),
types.KeyboardButton(text="♓Рыбы"),
)
builder.row(types.KeyboardButton(text="Назад"))
await message.reply(
"Для какого знака Зодиака нужен гороскоп?",
reply_markup=builder.as_markup(resize_keyboard=True),
)
elif message.text == "🌍Знаки Земли":
builder = ReplyKeyboardBuilder()
builder.row(
types.KeyboardButton(text="♉Телец"),
types.KeyboardButton(text="♍Дева"),
types.KeyboardButton(text="♑Козерог"),
)
builder.row(types.KeyboardButton(text="Назад"))
await message.reply(
"Для какого знака Зодиака нужен гороскоп?",
reply_markup=builder.as_markup(resize_keyboard=True),
)
elif message.text == "🎈Знаки Воздуха":
builder = ReplyKeyboardBuilder()
builder.row(
types.KeyboardButton(text="♊Близнецы"),
types.KeyboardButton(text="♎Весы"),
types.KeyboardButton(text="♒Водолей"),
)
builder.row(types.KeyboardButton(text="Назад"))
await message.reply(
"Для какого знака Зодиака нужен гороскоп?",
reply_markup=builder.as_markup(resize_keyboard=True),
)
elif message.text == "Назад":
kb = [
[
types.KeyboardButton(text="🔥Знаки Огня"),
types.KeyboardButton(text="💧Знаки Воды"),
types.KeyboardButton(text="🌍Знаки Земли"),
types.KeyboardButton(text="🎈Знаки Воздуха"),
],
]
start_menu = types.ReplyKeyboardMarkup(
keyboard=kb,
resize_keyboard=True,
input_field_placeholder="Выбери свою стихию:",
)
await message.reply("Сперва выбери свою стихию:", reply_markup=start_menu)
elif any(x in message.text for x in lst):
await message.answer(
"{0.first_name}!\nСекунду, 👀смотрю на звезды...".format(message.from_user),
reply_markup=types.ReplyKeyboardRemove(),
)
input_ids = tokenizer.encode(message.text, return_tensors="pt")
sample_output = model.generate(
input_ids,
do_sample=True,
max_length=210,
top_p=0.7,
temperature=0.6,
top_k=0,
no_repeat_ngram_size=2,
eos_token_id=tokenizer.eos_token_id,
)
out = tokenizer.decode(sample_output[0][1:], skip_special_tokens=True)
out = re.sub(re.compile("[^а-яА-ЯЁё !.,:?;-]"), "", out)
symbols = [
"..",
",.",
"?.",
" , ",
" . ",
" : ",
".:",
":.",
"??",
"?!",
".?",
"? .",
"? .",
" .",
",,",
"::",
" ",
".и",
".или",
".!",
".,",
]
for symb in symbols:
out = out.replace(symb, "")
for symb in symbols:
out = out.replace(symb, "")
for symb in symbols:
out = out.replace(symb, "")
if "</s>" in out:
out = out[: out.find("</s>")].strip()
kb = [
[
types.KeyboardButton(text="🔥Знаки Огня"),
types.KeyboardButton(text="💧Знаки Воды"),
types.KeyboardButton(text="🌍Знаки Земли"),
types.KeyboardButton(text="🎈Знаки Воздуха"),
],
]
start_menu = types.ReplyKeyboardMarkup(
keyboard=kb,
resize_keyboard=True,
input_field_placeholder="Выбери свою стихию:",
)
await message.answer(
f"Гороскоп для знака {message.text}:\n\n{out}", reply_markup=start_menu
)
elif any(x in message.text.lower() for x in dialog["in"]):
kb = [
[
types.KeyboardButton(text="🔥Знаки Огня"),
types.KeyboardButton(text="💧Знаки Воды"),
types.KeyboardButton(text="🌍Знаки Земли"),
types.KeyboardButton(text="🎈Знаки Воздуха"),
],
]
start_menu = types.ReplyKeyboardMarkup(
keyboard=kb,
resize_keyboard=True,
input_field_placeholder="Выбери свою стихию:",
)
await message.answer(random.choice(dialog["out"]), reply_markup=start_menu)
else:
kb = [
[
types.KeyboardButton(text="🔥Знаки Огня"),
types.KeyboardButton(text="💧Знаки Воды"),
types.KeyboardButton(text="🌍Знаки Земли"),
types.KeyboardButton(text="🎈Знаки Воздуха"),
],
]
start_menu = types.ReplyKeyboardMarkup(
keyboard=kb,
resize_keyboard=True,
input_field_placeholder="Выбери свою стихию:",
)
await message.reply(
"Привет! Не могу понять твой запрос😅.Пожалуйста, воспользуйся меню для получения гороскопа😉!!!",
reply_markup=start_menu,
)
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())