File size: 5,660 Bytes
6f03d40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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()