Celestat commited on
Commit
ab10969
1 Parent(s): 11a5aed

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +30 -82
run.py CHANGED
@@ -1,11 +1,10 @@
1
- import telebot
2
  import os
3
  from modules import chat_model, TTS, whisper_api, log_handler
4
  from requests.exceptions import ReadTimeout
5
  import sys
6
  import io
7
  import time
8
- from config import TELEBOT_API_KEY, HF_API_KEY, BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH, BARK_SMALL_MODELS, SPEAKER
9
 
10
  # TTS Init
11
  au_model = TTS('./assets/audio/', BARK_SMALL_MODELS)
@@ -14,106 +13,55 @@ print('TTS initialized successfully 👍')
14
  # chat_model Init
15
  chat = chat_model(HF_API_KEY)
16
 
17
- # Telegram Bot Init
18
- bot = telebot.TeleBot(TELEBOT_API_KEY)
19
-
20
  # Log declaration
21
  log = log_handler('./log/log.txt')
22
 
23
- @bot.message_handler(content_types=['text', 'document'])
 
 
 
 
 
24
  def handle_text(message):
25
  chat_id = message.chat.id
26
- if chat.number == 0:
27
- if message.text != '/start':
28
- response = chat.query_bot(f'{message.text}', BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH)
29
- bot.send_message(chat_id, response, timeout=None)
30
- # Add a new text message the './log/log.txt' file
31
- log.log_write(f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: {response}')
32
- else:
33
- bot.send_message(chat_id, 'Hello!', timeout=None)
34
- # Add a new text message the './log/log.txt' file
35
- log.log_write(f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: Hello!')
36
  else:
37
- if message.text == '/exit' or message.text == 'exit' or message.text == 'Exit':
38
- bot.send_message(chat_id, 'Bye!', timeout=None)
39
- log.log_write(f'Date:{message.date} User: {message.text} Bot: Closed correctly. ')
40
- log.fn_block()
41
- return
42
- elif message.text == '/reset' or message.text == 'reset' or message.text == 'Reset':
43
- bot.send_message(chat_id, 'Chat reset successfully.', timeout=None)
44
- log.log_write(f'Date:{message.date} User: {message.text} Bot: Chat reset successfully. ')
45
- chat.context = ''
46
- log.fn_block()
47
- return
48
- else:
49
- # Pass the ID of the last message
50
- response = chat.query_bot(f'{message.text}', BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH)
51
- bot.send_message(chat_id, response, timeout=None)
52
- # Add a new text message to the './log/log.txt' file
53
- log.log_write(f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: {response} ')
54
- return
55
 
56
  def send_audio(text, chat_id):
57
  try:
58
  au_model.to_audio(text, SPEAKER)
59
- bot.send_voice(chat_id, open('./assets/audio/tts.ogg', "rb"))
60
  os.remove("./assets/audio/tts.ogg")
61
  return
62
  except Exception as err:
63
  print('Error in send_audio function:\n'+str(err))
64
  log.log_write('Something went wrong with send_audio:\n'+str(err))
65
- bot.send_message(chat_id, text)
66
  return
67
 
68
- @bot.message_handler(content_types=['voice', 'audio'])
69
  def handle_voice(message):
70
  whisper = whisper_api(HF_API_KEY)
71
  try:
72
  chat_id = message.chat.id
73
- if chat.number == 0:
74
- if message.text != '/start':
75
- # Get information about the audio file
76
- file_info = bot.get_file(message.voice.file_id)
77
- file_path = file_info.file_path
78
- # Download the audio file
79
- downloaded_file = bot.download_file(file_path)
80
- # Save the audio file
81
- audio_file_path = './assets/audio/audio.ogg'
82
- with open(audio_file_path, 'wb') as f:
83
- f.write(downloaded_file)
84
 
85
- text = whisper.transcribe(audio_file_path)
86
- time.sleep(5)
87
- os.remove(audio_file_path)
88
- response = chat.query_bot(text, BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH)
89
- send_audio(response, message.chat.id)
90
- bot.send_message(chat_id, response, timeout=None)
91
- log.log_write(f'Date:{message.date} User: {message.text} Bot: {text}')
92
- return
93
- else:
94
- bot.send_message(chat_id, 'Hello!', timeout=None)
95
- # Add a new text message with its respective ID to the './log/log.txt' file
96
- write = f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: Hello!'
97
- log.log_write(write)
98
- else:
99
- # Get information about the audio file
100
- file_info = bot.get_file(message.voice.file_id)
101
- file_path = file_info.file_path
102
- # Download the audio file
103
- downloaded_file = bot.download_file(file_path)
104
- # Save the audio file
105
- audio_file_path = './assets/audio/audio.ogg'
106
- with open(audio_file_path, 'wb') as f:
107
- f.write(downloaded_file)
108
-
109
- text = whisper.transcribe(audio_file_path)
110
- time.sleep(5)
111
- os.remove(audio_file_path)
112
- response = chat.query_bot(text, BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH)
113
- send_audio(response, message.chat.id)
114
- bot.send_message(chat_id, response, timeout=None)
115
- log.log_write(f'Date:{message.date} User: {message.text} Bot: {text}')
116
- return
117
  except Exception as err:
118
  print('Error in handle_voice:\n'+str(err))
119
  log.fn_by_err(str(err))
@@ -123,9 +71,9 @@ if __name__ == "__main__":
123
  try:
124
  while True:
125
  try:
126
- bot.polling(timeout=30)
127
  except ReadTimeout:
128
  print("A timeout occurred. Trying again...")
129
  except Exception as err:
130
  print('Error in bot.polling:\n' + str(err))
131
- log.fn_by_err(str(err))
 
 
1
  import os
2
  from modules import chat_model, TTS, whisper_api, log_handler
3
  from requests.exceptions import ReadTimeout
4
  import sys
5
  import io
6
  import time
7
+ from config import HF_API_KEY, BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH, BARK_SMALL_MODELS, SPEAKER
8
 
9
  # TTS Init
10
  au_model = TTS('./assets/audio/', BARK_SMALL_MODELS)
 
13
  # chat_model Init
14
  chat = chat_model(HF_API_KEY)
15
 
 
 
 
16
  # Log declaration
17
  log = log_handler('./log/log.txt')
18
 
19
+ def send_message(chat_id, message):
20
+ print(message)
21
+
22
+ def send_voice(chat_id, voice_path):
23
+ print("Sending voice message...")
24
+
25
  def handle_text(message):
26
  chat_id = message.chat.id
27
+ if message.text != '/start':
28
+ response = chat.query_bot(f'{message.text}', BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH)
29
+ send_message(chat_id, response)
30
+ log.log_write(f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: {response}')
 
 
 
 
 
 
31
  else:
32
+ send_message(chat_id, 'Hello!')
33
+ log.log_write(f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: Hello!')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def send_audio(text, chat_id):
36
  try:
37
  au_model.to_audio(text, SPEAKER)
38
+ send_voice(chat_id, './assets/audio/tts.ogg')
39
  os.remove("./assets/audio/tts.ogg")
40
  return
41
  except Exception as err:
42
  print('Error in send_audio function:\n'+str(err))
43
  log.log_write('Something went wrong with send_audio:\n'+str(err))
44
+ send_message(chat_id, text)
45
  return
46
 
 
47
  def handle_voice(message):
48
  whisper = whisper_api(HF_API_KEY)
49
  try:
50
  chat_id = message.chat.id
51
+ file_info = bot.get_file(message.voice.file_id)
52
+ file_path = file_info.file_path
53
+ downloaded_file = bot.download_file(file_path)
54
+ audio_file_path = './assets/audio/audio.ogg'
55
+ with open(audio_file_path, 'wb') as f:
56
+ f.write(downloaded_file)
 
 
 
 
 
57
 
58
+ text = whisper.transcribe(audio_file_path)
59
+ time.sleep(5)
60
+ os.remove(audio_file_path)
61
+ response = chat.query_bot(text, BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH)
62
+ send_audio(response, message.chat.id)
63
+ send_message(chat_id, response)
64
+ log.log_write(f'Date:{message.date} User: {message.text} Bot: {text}')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  except Exception as err:
66
  print('Error in handle_voice:\n'+str(err))
67
  log.fn_by_err(str(err))
 
71
  try:
72
  while True:
73
  try:
74
+ polling()
75
  except ReadTimeout:
76
  print("A timeout occurred. Trying again...")
77
  except Exception as err:
78
  print('Error in bot.polling:\n' + str(err))
79
+ log.fn_by_err(str(err))