File-Store-Bot / app.py
File Store Bot
Fix Pyrogram main-thread signal restriction using direct client.start()
8ee4a30
Raw History Blame Contribute Delete
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:
@spaces.GPU
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)