Spaces:
Running on Zero
Running on Zero
Upload 40 files
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
import logging
|
| 3 |
import gradio as gr
|
| 4 |
from main import app, start_web_server
|
|
@@ -6,7 +7,7 @@ from main import app, start_web_server
|
|
| 6 |
logging.basicConfig(level=logging.INFO)
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
-
# Compatibilidad
|
| 10 |
try:
|
| 11 |
import spaces
|
| 12 |
@spaces.GPU
|
|
@@ -16,11 +17,44 @@ except Exception:
|
|
| 16 |
def gpu_health_check(x):
|
| 17 |
return x
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def check_status(user_input):
|
| 20 |
gpu_health_check(user_input)
|
| 21 |
return f"🟢 Bot 'Twisted Audio' activo 24/7 en Telegram. Recibido: {user_input}"
|
| 22 |
|
| 23 |
-
# Interfaz Gradio
|
| 24 |
demo = gr.Interface(
|
| 25 |
fn=check_status,
|
| 26 |
inputs=gr.Textbox(label="Estado del Servidor", placeholder="Escribe algo para verificar..."),
|
|
@@ -29,28 +63,5 @@ demo = gr.Interface(
|
|
| 29 |
description="### Bot de Telegram Activo 24/7 (Gradio Free SDK)\nEnvía tus videos al bot de Telegram para extraer su audio en formato MP3."
|
| 30 |
)
|
| 31 |
|
| 32 |
-
async def run_all():
|
| 33 |
-
# 1. Iniciar Gradio en modo no bloqueante
|
| 34 |
-
logger.info("Iniciando interfaz Gradio...")
|
| 35 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, prevent_thread_lock=True)
|
| 36 |
-
|
| 37 |
-
# 2. Iniciar servidor web secundario y cliente Pyrogram en el event loop principal
|
| 38 |
-
logger.info("Iniciando servidor interno y cliente Pyrogram...")
|
| 39 |
-
await start_web_server()
|
| 40 |
-
await app.start()
|
| 41 |
-
logger.info("🟢 Bot de Telegram 'Twisted Audio' iniciado con éxito.")
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
from pyrogram.types import BotCommand
|
| 45 |
-
await app.set_bot_commands([
|
| 46 |
-
BotCommand("start", "⚙️ Panel Principal"),
|
| 47 |
-
BotCommand("help", "❓ Ayuda e Instrucciones")
|
| 48 |
-
])
|
| 49 |
-
except Exception as e:
|
| 50 |
-
logger.warning(f"No se pudieron configurar los comandos del bot: {e}")
|
| 51 |
-
|
| 52 |
-
from pyrogram import idle
|
| 53 |
-
await idle()
|
| 54 |
-
|
| 55 |
if __name__ == "__main__":
|
| 56 |
-
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
import threading
|
| 3 |
import logging
|
| 4 |
import gradio as gr
|
| 5 |
from main import app, start_web_server
|
|
|
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
+
# Compatibilidad ZeroGPU / Spaces
|
| 11 |
try:
|
| 12 |
import spaces
|
| 13 |
@spaces.GPU
|
|
|
|
| 17 |
def gpu_health_check(x):
|
| 18 |
return x
|
| 19 |
|
| 20 |
+
def _start_bot_in_background():
|
| 21 |
+
loop = asyncio.new_event_loop()
|
| 22 |
+
asyncio.set_event_loop(loop)
|
| 23 |
+
|
| 24 |
+
async def _async_bot_runner():
|
| 25 |
+
try:
|
| 26 |
+
logger.info("Asignando loop y arrancando Pyrogram...")
|
| 27 |
+
# Vincular el cliente Pyrogram al event loop actual del hilo
|
| 28 |
+
app.loop = loop
|
| 29 |
+
await start_web_server()
|
| 30 |
+
await app.start()
|
| 31 |
+
logger.info("🟢 Bot de Telegram 'Twisted Audio' activo y escuchando mensajes.")
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
from pyrogram.types import BotCommand
|
| 35 |
+
await app.set_bot_commands([
|
| 36 |
+
BotCommand("start", "⚙️ Panel Principal"),
|
| 37 |
+
BotCommand("help", "❓ Ayuda e Instrucciones")
|
| 38 |
+
])
|
| 39 |
+
except Exception as e:
|
| 40 |
+
logger.warning(f"No se pudieron registrar comandos: {e}")
|
| 41 |
+
|
| 42 |
+
# Mantener el bucle corriendo indefinidamente sin usar idle()
|
| 43 |
+
while True:
|
| 44 |
+
await asyncio.sleep(3600)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
logger.error(f"Error al ejecutar el bot en segundo plano: {e}", exc_info=True)
|
| 47 |
+
|
| 48 |
+
loop.run_until_complete(_async_bot_runner())
|
| 49 |
+
|
| 50 |
+
# Iniciar el bot inmediatamente al importar el módulo en Hugging Face
|
| 51 |
+
bot_thread = threading.Thread(target=_start_bot_in_background, daemon=True)
|
| 52 |
+
bot_thread.start()
|
| 53 |
+
|
| 54 |
def check_status(user_input):
|
| 55 |
gpu_health_check(user_input)
|
| 56 |
return f"🟢 Bot 'Twisted Audio' activo 24/7 en Telegram. Recibido: {user_input}"
|
| 57 |
|
|
|
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=check_status,
|
| 60 |
inputs=gr.Textbox(label="Estado del Servidor", placeholder="Escribe algo para verificar..."),
|
|
|
|
| 63 |
description="### Bot de Telegram Activo 24/7 (Gradio Free SDK)\nEnvía tus videos al bot de Telegram para extraer su audio en formato MP3."
|
| 64 |
)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|