How to convert telegram voice message to text by using Whisper

137 Views Asked by At

I'm trying to make a telegram bot that can convert telegram voice messages to text but in Internet I could't find anything useful, I also asked this question to ChatGPT but I could't get the result. So I decided to asked you about the solution to this problem maybe somebody have the solution or got the same problem. Sorry for my English I'm studing it.

import telebot
import openai
import whisper
import numpy as np

bot = telebot.TeleBot('#')
openai.api_key = '#'


@bot.message_handler(commands=['start'])
def handle_start_help(message):
    user_first_name = str(message.chat.first_name)
    bot.reply_to(message,
                 f"Привет, {user_first_name}! Добро пожаловать в мир PyProject-Assistant, "
                 f"просто задай вопрос и я найду на него ответ\nProject was made by #")


@bot.message_handler(content_types=['text'])
def get_text_messages(message):
    print(message)
    completion = openai.ChatCompletion.create(
        model='#',
        messages=[
            {'role': 'user', 'content': message.text.capitalize()}
        ]
    )
    to_say = completion["choices"][0]["message"]["content"]

    bot.send_message(message.from_user.id, to_say)


@bot.message_handler(content_types=['voice'])
def voice_processing(message):
    bot.send_message(message.from_user.id, 'Пока что я не умею обрабатывать voice messages, но вскоре научусь')
    file_info = bot.get_file(message.voice.file_id)
    downloaded_file = bot.download_file(file_info.file_path)
    aud_array = np.frombuffer(downloaded_file, np.int8).flatten().astype(np.float32) / 32768.0
    speech_model = whisper.load_model('small')
    result = speech_model.transcribe(aud_array)
    print(result['text'])


bot.polling(none_stop=True, interval=0)

0

There are 0 best solutions below