bomolopuu's picture
Upload 120 files
6f03d40 verified
import telebot
import tempfile
import time
import os
from pathlib import Path
import logging
import soundfile as sf
import librosa
from zeroshot import WORD_SCORE_DEFAULT_IF_NOLM
from app import process_wrapper
import gradio as gr
from dotenv import load_dotenv
# Настройка логирования
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
load_dotenv()
# Конфигурация
BOT_TOKEN = os.getenv('BOT_TOKEN')
if not BOT_TOKEN:
raise ValueError("No BOT_TOKEN found in environment variables")
WORDS_FILE_PATH = 'upload/english/ngen_lexicon_jan_2025.txt'
# Инициализация бота
bot = telebot.TeleBot(BOT_TOKEN)
def convert_audio(input_path):
"""Конвертирует аудио в формат WAV с частотой 16kHz"""
try:
# Загружаем аудио и ресемплируем до 16kHz
y, sr = librosa.load(input_path, sr=16000)
# Создаем временный WAV файл
output_path = input_path.replace('.ogg', '.wav')
sf.write(output_path, y, sr, format='WAV')
return output_path
except Exception as e:
logger.error(f"Error converting audio: {e}")
raise
@bot.message_handler(commands=['start'])
def send_welcome(message):
welcome_text = (
"👋 Привет! Я бот для автоматического распознавания речи.\n\n"
"🎤 Отправьте мне голосовое сообщение или аудиофайл на английском языке.\n\n"
"ℹ️ Поддерживаются файлы любого формата, которые Telegram может обработать как аудио."
)
bot.reply_to(message, welcome_text)
@bot.message_handler(content_types=['audio', 'voice'])
def handle_audio(message):
try:
# Отправляем сообщение о начале обработки
processing_msg = bot.reply_to(message, "🔄 Обрабатываю аудио...")
# Получаем информацию о файле
if message.voice:
file_info = bot.get_file(message.voice.file_id)
else:
file_info = bot.get_file(message.audio.file_id)
logger.info(f"Processing file: {file_info.file_path}")
# Скачиваем файл
downloaded_file = bot.download_file(file_info.file_path)
# Создаем временный файл
with tempfile.NamedTemporaryFile(delete=False, suffix='.ogg') as temp_audio:
temp_audio.write(downloaded_file)
temp_audio_path = temp_audio.name
# Конвертируем в WAV
wav_path = convert_audio(temp_audio_path)
logger.info(f"Converted to WAV: {wav_path}")
# Вызываем process_wrapper
transcription, logs = process_wrapper(
audio=wav_path,
words_file=WORDS_FILE_PATH,
wscore=WORD_SCORE_DEFAULT_IF_NOLM,
wscore_usedefault=True,
reference=None
)
logger.info(f"transcibe done!")
logger.info(f"tr:{transcription}, log:{logs}!")
# Удаляем временные файлы
os.unlink(temp_audio_path)
os.unlink(wav_path)
# Отправляем результат
if transcription:
bot.edit_message_text(
f"📝 Распознанный текст:\n\n{transcription}\n\n📊 Logs:\n{logs}",
chat_id=processing_msg.chat.id,
message_id=processing_msg.message_id
)
else:
raise ValueError("Empty transcription")
except Exception as e:
logger.error(f"Error processing audio: {e}")
error_message = (
"❌ Произошла ошибка при обработке аудио.\n\n"
"Убедитесь, что:\n"
"- Аудио содержит четкую английскую речь\n"
"- Длительность не превышает допустимую\n"
f"- Ошибка: {str(e)}"
)
# Если было сообщение о процессе - редактируем его
if 'processing_msg' in locals():
bot.edit_message_text(
error_message,
chat_id=processing_msg.chat.id,
message_id=processing_msg.message_id
)
else:
bot.reply_to(message, error_message)
def run_bot():
"""Запуск бота с обработкой ошибок и повторными попытками"""
while True:
try:
logger.info("Starting the bot...")
bot.polling(none_stop=True, timeout=60, long_polling_timeout=30)
logger.info("bot ended")
return
except (ConnectionError, ConnectionResetError, ConnectionAbortedError) as e:
logger.error(f"Connection error occurred: {e}")
logger.info("Waiting 15 seconds before reconnecting...")
time.sleep(15)
except Exception as e:
logger.error(f"Critical error: {e}")
logger.info("Waiting 15 seconds before restarting...")
time.sleep(15)
if __name__ == "__main__":
run_bot()