Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, VitsModel
|
| 3 |
import torch
|
| 4 |
import scipy.io.wavfile
|
| 5 |
-
import os
|
| 6 |
import threading
|
| 7 |
import asyncio
|
| 8 |
from telegram import Update
|
|
@@ -20,10 +19,9 @@ def tts_chechen_logic(text):
|
|
| 20 |
|
| 21 |
sampling_rate = model.config.sampling_rate
|
| 22 |
audio_data = output.squeeze().numpy()
|
| 23 |
-
file_path = "voice.
|
| 24 |
-
scipy.io.wavfile.write(
|
| 25 |
-
|
| 26 |
-
return sampling_rate, audio_data, "temp.wav"
|
| 27 |
|
| 28 |
# --- БЛОК TELEGRAM БОТА ---
|
| 29 |
TG_TOKEN = "8287698372:AAHjHaJVy_DajLqQhs08rNHn_22H--AUopQ"
|
|
@@ -31,45 +29,37 @@ TG_TOKEN = "8287698372:AAHjHaJVy_DajLqQhs08rNHn_22H--AUopQ"
|
|
| 31 |
async def handle_tg_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 32 |
if not update.message or not update.message.text:
|
| 33 |
return
|
| 34 |
-
|
| 35 |
-
user_text = update.message.text
|
| 36 |
-
print(f"Обработка для ТГ: {user_text}")
|
| 37 |
try:
|
| 38 |
-
_, _, file_path = tts_chechen_logic(
|
| 39 |
with open(file_path, 'rb') as audio:
|
| 40 |
await context.bot.send_voice(chat_id=update.effective_chat.id, voice=audio)
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"Ошибка
|
| 43 |
|
| 44 |
-
def
|
| 45 |
-
# Создаем
|
| 46 |
-
loop = asyncio.new_event_loop()
|
| 47 |
-
asyncio.set_event_loop(loop)
|
| 48 |
-
|
| 49 |
-
print("Запуск инициализации бота...")
|
| 50 |
app = ApplicationBuilder().token(TG_TOKEN).build()
|
| 51 |
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_tg_message))
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
app.run_polling(drop_pending_updates=True)
|
| 56 |
|
| 57 |
# Запускаем поток
|
| 58 |
-
|
| 59 |
-
t.start()
|
| 60 |
# --------------------------
|
| 61 |
|
| 62 |
-
# Интерфейс Gradio
|
| 63 |
def gradio_interface(text):
|
| 64 |
sr, audio, _ = tts_chechen_logic(text)
|
| 65 |
return (sr, audio)
|
| 66 |
|
| 67 |
-
|
| 68 |
fn=gradio_interface,
|
| 69 |
-
inputs=gr.Textbox(label="
|
| 70 |
-
outputs=gr.Audio(label="
|
| 71 |
-
title="NoxchiynAz"
|
| 72 |
)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 2 |
from transformers import AutoTokenizer, VitsModel
|
| 3 |
import torch
|
| 4 |
import scipy.io.wavfile
|
|
|
|
| 5 |
import threading
|
| 6 |
import asyncio
|
| 7 |
from telegram import Update
|
|
|
|
| 19 |
|
| 20 |
sampling_rate = model.config.sampling_rate
|
| 21 |
audio_data = output.squeeze().numpy()
|
| 22 |
+
file_path = "voice.wav"
|
| 23 |
+
scipy.io.wavfile.write(file_path, rate=sampling_rate, data=audio_data)
|
| 24 |
+
return sampling_rate, audio_data, file_path
|
|
|
|
| 25 |
|
| 26 |
# --- БЛОК TELEGRAM БОТА ---
|
| 27 |
TG_TOKEN = "8287698372:AAHjHaJVy_DajLqQhs08rNHn_22H--AUopQ"
|
|
|
|
| 29 |
async def handle_tg_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 30 |
if not update.message or not update.message.text:
|
| 31 |
return
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
+
_, _, file_path = tts_chechen_logic(update.message.text)
|
| 34 |
with open(file_path, 'rb') as audio:
|
| 35 |
await context.bot.send_voice(chat_id=update.effective_chat.id, voice=audio)
|
| 36 |
except Exception as e:
|
| 37 |
+
print(f"Ошибка бота: {e}")
|
| 38 |
|
| 39 |
+
def start_bot():
|
| 40 |
+
# Создаем приложение внутри потока
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
app = ApplicationBuilder().token(TG_TOKEN).build()
|
| 42 |
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_tg_message))
|
| 43 |
|
| 44 |
+
print("--- ЗАПУСК БОТА В ПОТОКЕ ---")
|
| 45 |
+
# Используем этот метод для запуска в контейнерах
|
| 46 |
+
app.run_polling(close_loop=False, drop_pending_updates=True)
|
| 47 |
|
| 48 |
# Запускаем поток
|
| 49 |
+
threading.Thread(target=start_bot, daemon=True).start()
|
|
|
|
| 50 |
# --------------------------
|
| 51 |
|
| 52 |
+
# Интерфейс Gradio
|
| 53 |
def gradio_interface(text):
|
| 54 |
sr, audio, _ = tts_chechen_logic(text)
|
| 55 |
return (sr, audio)
|
| 56 |
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
fn=gradio_interface,
|
| 59 |
+
inputs=gr.Textbox(label="Текст на чеченском"),
|
| 60 |
+
outputs=gr.Audio(label="Голос"),
|
| 61 |
+
title="NoxchiynAz TTS"
|
| 62 |
)
|
| 63 |
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|