открытие сырой версии

This commit is contained in:
2022-11-24 13:53:08 +07:00
parent 5199ee7d44
commit 949092707c
7 changed files with 106 additions and 0 deletions
+1
View File
@@ -24,6 +24,7 @@ wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.idea/
.installed.cfg
*.egg
MANIFEST
+24
View File
@@ -0,0 +1,24 @@
from loguru import logger
import telebot
from telebot import types
from functions import get_audio_messages_func
bot = telebot.TeleBot("5941118321:AAG0g0keLrlnuH_9U9X6ehpFFAdOX38qeXI")
logger.add("logs/logging_log.log", level="INFO")
@bot.message_handler(commands=['start'])
def start(message: types.Message):
name = message.chat.first_name if message.chat.first_name else 'No_name'
logger.info(f"Группа {name} (ID: {message.chat.id}) запустил бота")
bot.send_message(message.chat.id, 'Привет! Отправляй голосовое, я расшифрую!')
@bot.message_handler(content_types=['voice', 'video_note', 'video'])
def get_audio_messages(message: types.Message):
get_audio_messages_func.get_audio_messages_func(message, logger, bot)
def start_work_bot():
logger.info("Бот запустился")
bot.polling(none_stop=True, timeout=123)
+43
View File
@@ -0,0 +1,43 @@
import subprocess
import speech_recognition as sr
import os
from functions.loging import loging
class Converter:
def __init__(self, path_to_file: str, message, language: str = "ru-RU"):
self.language = language
self.path_to_file = path_to_file
self.service = None
self.message = message
def audio_to_text(self) -> str:
import mutagen
f = mutagen.File(self.path_to_file)
if float(f.info.length) >= 30.0:
self.service = "google"
return self.google()
else:
self.service = "yandex"
return self.yandex()
def google(self) -> str:
subprocess.run(['ffmpeg', '-v', 'quiet', '-i', self.path_to_file, self.path_to_file.replace(".ogg", ".wav")])
os.remove(self.path_to_file)
wav_file = self.path_to_file.replace(".ogg", ".wav")
r = sr.Recognizer()
with sr.AudioFile(wav_file) as source:
audio = r.record(source)
r.adjust_for_ambient_noise(source)
response = "google\n\n" + r.recognize_google(audio, language=self.language)
loging(self.message, "google", wav_file, response)
os.remove(wav_file)
return response
def yandex(self) -> str:
from speechkit import ShortAudioRecognition, Session
reg = ShortAudioRecognition(Session.from_api_key("AQVN3xNJamAFP4_FS6Gis0Uud0vONFk24umBSXvh"))
response = "yandex\n\n" + reg.recognize(open(str(self.path_to_file), str('rb')).read())
loging(self.message, "yandex", self.path_to_file, response)
os.remove(self.path_to_file)
return response
+26
View File
@@ -0,0 +1,26 @@
from functions.convert import Converter
from telebot import types
def get_audio_messages_func(message: types.Message, logger, bot):
# достаю file_id из разнообразных полей
if message.content_type in ['voice']:
file_id = message.voice.file_id
elif message.content_type in ['video']:
file_id = message.video.file_id
else:
file_id = message.video_note.file_id
# создаю file_name
file_name = "config/" + str(message.message_id) + '.ogg'
# достаю имя человека
name = message.chat.first_name if message.chat.first_name else 'No_name'
# записываю в файл
with open(file_name, 'wb') as new_file:
new_file.write(bot.download_file(bot.get_file(file_id).file_path))
# запуск конвертора
converter = Converter(file_name, message)
message_text = converter.audio_to_text()
# записываю всё в logger
logger.info(f"Чат {name} (ID: {message.chat.id}) обработал файл {file_name}, сервисом {converter.service}")
# отправляю сообщение
bot.send_message(message.chat.id, message_text, reply_to_message_id=message.message_id)
+2
View File
@@ -0,0 +1,2 @@
def loging(message, service, file_path, text):
print(message)
+9
View File
@@ -0,0 +1,9 @@
from Bot import start_work_bot
def main():
start_work_bot()
if __name__ == '__main__':
main()
+1
View File
@@ -0,0 +1 @@