Passed
Push — test ( 6edc7d...e48527 )
by Nikita
02:42
created

build.handlers.users.process_message.zero_output()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
3
4
from gpt_3.preprocessing import encoding_text, decoding_text, get_text_gpt3
5
from utils.misc.throttling import rate_limit
6
from aiogram.dispatcher import FSMContext
7
from aiogram import types
8
from loader import dp
9
import asyncio
10
11
12
"""
13
14
15
    Created on 10.09.2021
16
    
17
    @author: Nikita
18
19
20
"""
21
22
23
async def zero_output(message: types.Message):
24
25
    await message.answer(text="Упс...")
26
27
    await asyncio.sleep(1)
28
29
    await message.answer(text="Что-то я не смог сформулировать ответ, "
30
                              "попробуйте перефразировать сообщение 🙁")
31
32
33
@rate_limit(3, 'message')
34
@dp.message_handler()
35
async def processing_message(message: types.Message, state: FSMContext):
36
37
    """
38
39
    The function is designed to receive messages in russian and generate a response.
40
41
    """
42
43
    await types.ChatActions.typing()
44
45
    data_storage = await state.get_data()
46
    text = message.text.lower()
47
48
    await state.update_data(history_text=text)
49
    await state.update_data(chat_id=message.chat.id)
50
    await state.update_data(first_name=message.from_user.first_name)
51
52
    input_text, check_question = encoding_text(text_encode=text)
53
54
    if 'history_text' in data_storage:
55
56
        if text == data_storage['history_text']:
57
58
            await message.answer(text="Ой... Где-то я уже это видел! 🥱")
59
60
        # input_text = torch.cat([context.chat_data['output'][-1], input_text[0]], dim=0)
61
        # input_text = input_text.unsqueeze(0)
62
63
        return
64
65
    text_gpt3 = get_text_gpt3(text_gpt=input_text, check_question=check_question)
66
    output_text = decoding_text(text_decode=text_gpt3)
67
68
    if len(output_text.split()) < 1:
69
70
        await zero_output(message)
71
72
        return
73
74
    await message.answer(text=output_text)
75
76
    if 'input' in data_storage:
77
78
        await state.update_data(input=input_text)
79
        await state.update_data(output=text_gpt3)
80
81
        return
82
83
    data_history = {'input': [input_text],
84
                    'output': [text_gpt3]}
85
86
    await state.update_data(data_history)
87