Spaces:
Running on Zero
Running on Zero
Download app.py from codebydante/twisted_audio: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/spaces/codebydante/twisted_audio/resolve/main/app.py
- Command line
-
hf download hf://spaces/codebydante/twisted_audio/app.py
-
curl -L -o app.py https://huggingface.co/spaces/codebydante/twisted_audio/resolve/main/app.py
2.48 kB
| import asyncio | |
| import threading | |
| import logging | |
| import gradio as gr | |
| from main import app, start_web_server | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Compatibilidad ZeroGPU / Spaces | |
| try: | |
| import spaces | |
| def gpu_health_check(x): | |
| return x | |
| except Exception: | |
| def gpu_health_check(x): | |
| return x | |
| _BOT_STARTED = False | |
| _BOT_LOCK = threading.Lock() | |
| def _start_bot_in_background(): | |
| global _BOT_STARTED | |
| with _BOT_LOCK: | |
| if _BOT_STARTED: | |
| logger.info("El bot ya está en ejecución en otro hilo, omitiendo duplicación.") | |
| return | |
| _BOT_STARTED = True | |
| loop = asyncio.new_event_loop() | |
| asyncio.set_event_loop(loop) | |
| async def _async_bot_runner(): | |
| try: | |
| logger.info("Asignando loop y arrancando Pyrogram en memoria...") | |
| app.loop = loop | |
| await start_web_server() | |
| await app.start() | |
| logger.info("🟢 Bot de Telegram 'Twisted Audio' activo y respondiendo mensajes.") | |
| try: | |
| from pyrogram.types import BotCommand | |
| await app.set_bot_commands([ | |
| BotCommand("start", "⚙️ Panel Principal"), | |
| BotCommand("help", "❓ Ayuda e Instrucciones") | |
| ]) | |
| except Exception as e: | |
| logger.warning(f"No se pudieron registrar comandos: {e}") | |
| while True: | |
| await asyncio.sleep(3600) | |
| except Exception as e: | |
| logger.error(f"Error al ejecutar el bot en segundo plano: {e}", exc_info=True) | |
| loop.run_until_complete(_async_bot_runner()) | |
| # Iniciar el bot inmediatamente pero asegurando instancia única | |
| bot_thread = threading.Thread(target=_start_bot_in_background, daemon=True) | |
| bot_thread.start() | |
| def check_status(user_input): | |
| gpu_health_check(user_input) | |
| return f"🟢 Bot 'Twisted Audio' activo 24/7 en Telegram. Recibido: {user_input}" | |
| demo = gr.Interface( | |
| fn=check_status, | |
| inputs=gr.Textbox(label="Estado del Servidor", placeholder="Escribe algo para verificar..."), | |
| outputs=gr.Textbox(label="Respuesta de confirmación"), | |
| title="🎧 Twisted Audio", | |
| 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." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |