открытие сырой версии
This commit is contained in:
@@ -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
|
||||
@@ -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)
|
||||
@@ -0,0 +1,2 @@
|
||||
def loging(message, service, file_path, text):
|
||||
print(message)
|
||||
Reference in New Issue
Block a user