NoxchiynAz / app.py
ReconMadeIt's picture
Update app.py
4f6ec7c verified
import gradio as gr
import telebot
from transformers import AutoTokenizer, VitsModel
import torch
import scipy.io.wavfile
import threading
import time
# 1. Настройка модели
model_name = "facebook/mms-tts-che"
model = VitsModel.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
def tts_logic(text):
inputs = tokenizer(text.lower(), return_tensors="pt")
with torch.no_grad():
output = model(**inputs).waveform
file_path = "audio.wav"
scipy.io.wavfile.write(file_path, rate=model.config.sampling_rate, data=output.squeeze().numpy())
return file_path
# 2. Настройка бота (теперь на pyTelegramBotAPI)
TOKEN = "8287698372:AAEwoOWgQWHbeyDmJdN0B-OpKpy7-TARHMg"
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(func=lambda message: True)
def echo_all(message):
try:
path = tts_logic(message.text)
with open(path, 'rb') as audio:
bot.send_voice(message.chat.id, audio)
except Exception as e:
print(f"Ошибка: {e}")
# Запуск бота в отдельном потоке
def run_bot():
while True:
try:
print("🚀 Бот запущен...")
bot.polling(none_stop=True, interval=0)
except Exception as e:
print(f"Перезапуск бота через 5 сек... {e}")
time.sleep(5)
threading.Thread(target=run_bot, daemon=True).start()
# 3. Интерфейс сайта (чтобы HF не выключал сервер)
def web_interface(text):
path = tts_logic(text)
return path
demo = gr.Interface(fn=web_interface, inputs="text", outputs="audio")
demo.launch()