File size: 3,596 Bytes
60e9648
 
 
 
 
 
 
 
 
 
 
 
1662765
 
 
 
 
 
 
 
 
 
60e9648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ee4a30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e9648
 
 
 
8ee4a30
60e9648
 
 
 
 
 
 
8ee4a30
 
 
 
 
60e9648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce2e472
60e9648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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)