from yandexttsfree import YandexFreeTTS import os tts = YandexFreeTTS() outputpath =…
- User
from yandex_tts_free import YandexFreeTTS import os
tts = YandexFreeTTS() output_path = 'output'
Создаем папку если её нет
if not os.path.exists(output_path): os.makedirs(output_path)
text = 'Это тестовый текст для проверки голосов.'
Генерация речи с голосом levitan и нейтральным настроением
tts.generate_speech_ya(output_path, 'test.mp3', text, 'levitan')
Генерация речи с голосом oksana и настроением good
tts.generate_speech_ya(output_path, 'test_good.mp3', text, 'oksana', 'good')
СДЕЛАЙ ОЗВУЧКУ РЕЦЕПТА БЛЮДА С ПОМОЩЬЮ БИБЛИОТЕКИ ВЫШЕ import telebot from gtts import gTTS import os
API_TOKEN = ‘YOUR_TELEGRAM_BOT_API_TOKEN’
bot = telebot.TeleBot(API_TOKEN)
recipes = { “борщ”: “Ингредиенты: \n- Свекла\n- Картофель\n- Морковь\n- Лук\n- Томатная паста\n- Капуста\n- Мясо\n\nПриготовление:\n1. Мясо сварить, бульон процедить.\n2. Свеклу, морковь, лук нарезать, обжарить с томатной пастой.\n3. В бульон добавить картофель, через 10 минут капусту.\n4. Через 10 минут добавить обжаренные овощи, варить до готовности.”, }
def generate_voice(text): tts = gTTS(text, lang=‘ru’, slow=False) # Ускоряем выдачу аудио audio_path = “response.mp3” tts.save(audio_path) # Сохранить сгенерированный голосовой файл return audio_path # Вернуть имя сохраненного файла
@bot.message_handler(content_types=[‘text’]) def handle_text(message): user_input = message.text.lower().strip() commands = [‘скажи рецепт’, ‘рецепт’, ‘как приготовить’, ‘как готовить’]
dish = None for command in commands: if user_input.startswith(command): dish = user_input[len(command):].strip() break if dish: recipe = recipes.get(dish, “Извините, рецепт этого блюда не найден.”) bot.send_message(message.chat.id, recipe) # Generate and send voice message response_audio_file = generate_voice(recipe) with open(response_audio_file, “rb”) as voice: bot.send_voice(message.chat.id, voice) # Delete temporary file if os.path.exists(response_audio_file): os.remove(response_audio_file) else: bot.send_message(message.chat.id, “Пожалуйста, уточните команду и название блюда.”)if name == “main”: bot.polling(none_stop=True)
- Assistant