Spaces:
Running on Zero
Running on Zero
Download app.py from Thaimozhi/File-Store-Bot: direct link, hf CLI and curl.
- Browser
- Download file 3.6 kB
-
https://huggingface.co/spaces/Thaimozhi/File-Store-Bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Thaimozhi/File-Store-Bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Thaimozhi/File-Store-Bot/resolve/main/app.py
3.6 kB
| import asyncio | |
| import os | |
| import sys | |
| import psutil | |
| import shutil | |
| import threading | |
| import gradio as gr | |
| from bot import Bot | |
| from config import * | |
| from pyrogram import compose | |
| from pyrogram.errors import FloodWait | |
| try: | |
| import spaces | |
| except ImportError: | |
| spaces = None | |
| if spaces is not None: | |
| def dummy_gpu_loader(): | |
| return "ZeroGPU loaded" | |
| # Import the keep_alive_loop | |
| try: | |
| from plugins.anti_sleep import keep_alive_loop | |
| except ImportError: | |
| keep_alive_loop = None | |
| def get_system_stats(): | |
| try: | |
| cpu = psutil.cpu_percent() | |
| ram = psutil.virtual_memory().percent | |
| total, used, free = shutil.disk_usage("/") | |
| disk = round((used / total) * 100, 1) | |
| status_text = ( | |
| f"β‘ System Status: ACTIVE / ONLINE π’\n" | |
| f"π₯οΈ CPU Usage: {cpu}%\n" | |
| f"πΎ RAM Usage: {ram}%\n" | |
| f"π Disk Space: {disk}%\n" | |
| f"π Node/Space: {os.environ.get('SPACE_ID', 'Local Server')}" | |
| ) | |
| return status_text | |
| except Exception as e: | |
| return f"Status: Error retrieving system metrics ({str(e)})" | |
| async def start_bot(): | |
| bot = Bot( | |
| SESSION, | |
| WORKERS, | |
| DB_CHANNEL, | |
| FSUBS, | |
| TOKEN, | |
| ADMINS, | |
| MESSAGES, | |
| AUTO_DEL, | |
| DB_URI, | |
| DB_NAME, | |
| API_ID, | |
| API_HASH, | |
| PROTECT, | |
| DISABLE_BTN | |
| ) | |
| # Handle FloodWait on startup | |
| while True: | |
| try: | |
| await bot.start() | |
| break | |
| except FloodWait as e: | |
| wait = e.value if hasattr(e, 'value') else e.x | |
| print(f"FloodWait on startup! Waiting {wait} seconds before retrying...") | |
| await asyncio.sleep(wait + 10) | |
| except Exception as e: | |
| print(f"Startup error: {e}") | |
| return | |
| # Keep background thread event loop active without registering signal handlers | |
| while True: | |
| await asyncio.sleep(3600) | |
| def run_bot_in_thread(): | |
| loop = asyncio.new_event_loop() | |
| asyncio.set_event_loop(loop) | |
| # Start Keep Alive loop if found | |
| if keep_alive_loop: | |
| loop.create_task(keep_alive_loop()) | |
| loop.run_until_complete(start_bot()) | |
| # Start the Telegram Bot in a separate daemon thread | |
| threading.Thread(target=run_bot_in_thread, daemon=True).start() | |
| # Build Gradient theme Gradio Blocks UI | |
| theme = gr.themes.Default( | |
| primary_hue="indigo", | |
| secondary_hue="violet", | |
| neutral_hue="slate" | |
| ) | |
| with gr.Blocks(theme=theme, title="Yugen File Store Bot Dashboard") as demo: | |
| gr.Markdown( | |
| """ | |
| # π File Store Bot - Active Dashboard | |
| This Hugging Face Space runs the File Store Bot 24/7 under the Gradio Container. | |
| --- | |
| ### π’ Live Metrics & Status | |
| """ | |
| ) | |
| status_display = gr.Textbox( | |
| label="Environment Details & Health Metric", | |
| value=get_system_stats(), | |
| interactive=False, | |
| lines=5 | |
| ) | |
| # Refresh metrics button | |
| refresh_btn = gr.Button("π Refresh Stats", variant="primary") | |
| refresh_btn.click(fn=get_system_stats, outputs=status_display) | |
| gr.Markdown( | |
| """ | |
| --- | |
| ### π Resources & Navigation | |
| - π’ **Channel:** [Anime Pirates Tamil](https://t.me/AnimePiratesTamil) | |
| - π οΈ **Developer:** [@ProYato](https://t.me/ProYato) | |
| """ | |
| ) | |
| if __name__ == "__main__": | |
| # Launch Gradio server on HF default port 7860 | |
| demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False) | |