Spaces:
Sleeping
Sleeping
File size: 1,692 Bytes
a9652a8 4f6ec7c a9652a8 83af10e 4f6ec7c a9652a8 4f6ec7c 83af10e a9652a8 4f6ec7c a9652a8 4f6ec7c a9652a8 4f6ec7c 83af10e 4f6ec7c 83af10e 4f6ec7c 83af10e 4f6ec7c 83af10e ebc03a3 4f6ec7c | 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 | 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() |