-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
56 lines (42 loc) · 1.76 KB
/
bot.py
File metadata and controls
56 lines (42 loc) · 1.76 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
from config import BOT_CONFIG
from ml_model import classify_intent
from utils import clear_phrase
from dialogue_base import generate_answer
import random
import datetime
from gtts import gTTS
def get_answer_by_intent(intent):
return random.choice(BOT_CONFIG['intents'][intent]['responses'])
def get_failure_phrase():
return random.choice(BOT_CONFIG['failure_phrases'])
def log_dialog(user_text, bot_response):
with open("log.txt", "a", encoding="utf-8") as log:
timestamp = datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
log.write(f"{timestamp} USER: {user_text}\n")
log.write(f"{timestamp} BOT : {bot_response}\n\n")
# Функция для логирования диалога
def log_dialog(user_text, bot_response):
with open("log.txt", "a", encoding="utf-8") as log:
timestamp = datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
log.write(f"{timestamp} USER: {user_text}\n")
log.write(f"{timestamp} BOT : {bot_response}\n\n")
# Функция для озвучивания ответа
def speak(text, lang='ru'):
tts = gTTS(text=text, lang=lang, slow=False)
file_name = "response.mp3"
tts.save(file_name)
return file_name
# Основная логика бота
def bot(replica):
cleared = clear_phrase(replica)
intent = classify_intent(cleared)
if intent in BOT_CONFIG['intents']:
answer = get_answer_by_intent(intent)
else:
answer = generate_answer(cleared)
if not answer:
answer = get_failure_phrase()
log_dialog(replica, answer)
# Генерируем голосовое сообщение
audio_file = speak(answer)
return answer, audio_file # Возвращаем ответ и путь к аудиофайлу