Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,53 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import AutoTokenizer, VitsModel
|
| 3 |
import torch
|
| 4 |
import scipy.io.wavfile
|
| 5 |
import threading
|
| 6 |
-
import
|
| 7 |
-
import os
|
| 8 |
-
from telegram import Update
|
| 9 |
-
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters
|
| 10 |
|
| 11 |
-
# 1.
|
| 12 |
model_name = "facebook/mms-tts-che"
|
| 13 |
model = VitsModel.from_pretrained(model_name)
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
inputs = tokenizer(text.strip().lower(), return_tensors="pt")
|
| 19 |
with torch.no_grad():
|
| 20 |
output = model(**inputs).waveform
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
file_path = "voice.wav"
|
| 25 |
-
scipy.io.wavfile.write(file_path, rate=sampling_rate, data=audio_data)
|
| 26 |
-
return sampling_rate, audio_data, file_path
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
print(f"📩 Текст для бота: {update.message.text}")
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
with open(
|
| 40 |
-
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"
|
| 43 |
-
|
| 44 |
-
def run_bot_in_background():
|
| 45 |
-
try:
|
| 46 |
-
# Настройка асинхронности для контейнера
|
| 47 |
-
loop = asyncio.new_event_loop()
|
| 48 |
-
asyncio.set_event_loop(loop)
|
| 49 |
-
|
| 50 |
-
app = ApplicationBuilder().token(TG_TOKEN).build()
|
| 51 |
-
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_tg_message))
|
| 52 |
-
|
| 53 |
-
print("🚀 Бот запущен и слушает Telegram!")
|
| 54 |
-
app.run_polling(drop_pending_updates=True, close_loop=False)
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"🔥 Критическая ошибка: {e}")
|
| 57 |
|
| 58 |
# Запуск бота в отдельном потоке
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import telebot
|
| 3 |
from transformers import AutoTokenizer, VitsModel
|
| 4 |
import torch
|
| 5 |
import scipy.io.wavfile
|
| 6 |
import threading
|
| 7 |
+
import time
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# 1. Настройка модели
|
| 10 |
model_name = "facebook/mms-tts-che"
|
| 11 |
model = VitsModel.from_pretrained(model_name)
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
|
| 14 |
+
def tts_logic(text):
|
| 15 |
+
inputs = tokenizer(text.lower(), return_tensors="pt")
|
|
|
|
| 16 |
with torch.no_grad():
|
| 17 |
output = model(**inputs).waveform
|
| 18 |
+
file_path = "audio.wav"
|
| 19 |
+
scipy.io.wavfile.write(file_path, rate=model.config.sampling_rate, data=output.squeeze().numpy())
|
| 20 |
+
return file_path
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# 2. Настройка бота (теперь на pyTelegramBotAPI)
|
| 23 |
+
TOKEN = "8287698372:AAEwoOWgQWHbeyDmJdN0B-OpKpy7-TARHMg"
|
| 24 |
+
bot = telebot.TeleBot(TOKEN)
|
| 25 |
|
| 26 |
+
@bot.message_handler(func=lambda message: True)
|
| 27 |
+
def echo_all(message):
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
+
path = tts_logic(message.text)
|
| 30 |
+
with open(path, 'rb') as audio:
|
| 31 |
+
bot.send_voice(message.chat.id, audio)
|
| 32 |
except Exception as e:
|
| 33 |
+
print(f"Ошибка: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Запуск бота в отдельном потоке
|
| 36 |
+
def run_bot():
|
| 37 |
+
while True:
|
| 38 |
+
try:
|
| 39 |
+
print("🚀 Бот запущен...")
|
| 40 |
+
bot.polling(none_stop=True, interval=0)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Перезапуск бота через 5 сек... {e}")
|
| 43 |
+
time.sleep(5)
|
| 44 |
+
|
| 45 |
+
threading.Thread(target=run_bot, daemon=True).start()
|
| 46 |
+
|
| 47 |
+
# 3. Интерфейс сайта (чтобы HF не выключал сервер)
|
| 48 |
+
def web_interface(text):
|
| 49 |
+
path = tts_logic(text)
|
| 50 |
+
return path
|
| 51 |
+
|
| 52 |
+
demo = gr.Interface(fn=web_interface, inputs="text", outputs="audio")
|
| 53 |
+
demo.launch()
|