Cloud_VPS / app.py
mohammed978's picture
Update app.py
d6378ea verified
Raw History Blame Contribute Delete
3.61 kB
import gradio as gr
import subprocess
import os
import hashlib
import threading
import time
# ================= CONFIG =================
USERNAME = "wolf"
PASSWORD = "wolf123"
WORKSPACE = os.getcwd()
BOT_DIR = os.path.join(WORKSPACE, "bots")
LOG_DIR = os.path.join(WORKSPACE, "logs")
os.makedirs(BOT_DIR, exist_ok=True)
os.makedirs(LOG_DIR, exist_ok=True)
processes = {}
# ================= AUTH =================
def login(u, p):
if u == USERNAME and p == PASSWORD:
return True, "โœ… ุชู… ุงู„ุฏุฎูˆู„"
return False, "โŒ ุฎุทุฃ"
# ================= SAVE =================
def save_bot(name, code):
path = os.path.join(BOT_DIR, name)
with open(path, "w") as f:
f.write(code)
return "โœ… ุชู… ุงู„ุญูุธ"
# ================= RUN =================
def run_bot(name):
path = os.path.join(BOT_DIR, name)
if not os.path.exists(path):
return "โŒ ุงู„ู…ู„ู ุบูŠุฑ ู…ูˆุฌูˆุฏ"
if name in processes and processes[name].poll() is None:
return "โš ๏ธ ุงู„ุจูˆุช ูŠุนู…ู„"
log_file = open(os.path.join(LOG_DIR, f"{name}.log"), "w")
proc = subprocess.Popen(
["python3", path],
cwd=BOT_DIR,
stdout=log_file,
stderr=log_file
)
processes[name] = proc
return f"๐Ÿš€ ุชู… ุงู„ุชุดุบูŠู„ PID: {proc.pid}"
# ================= STOP =================
def stop_bot(name):
if name not in processes:
return "โŒ ู„ุง ูŠูˆุฌุฏ"
proc = processes[name]
if proc.poll() is None:
proc.terminate()
return "๐Ÿ›‘ ุชู… ุงู„ุฅูŠู‚ุงู"
return "โš ๏ธ ู…ุชูˆู‚ู"
# ================= STATUS =================
def status_bot(name):
if name not in processes:
return "๐Ÿ“ญ ู„ุง ูŠูˆุฌุฏ"
if processes[name].poll() is None:
return "๐ŸŸข ูŠุนู…ู„"
return "๐Ÿ”ด ู…ุชูˆู‚ู"
# ================= LOG =================
def read_log(name):
path = os.path.join(LOG_DIR, f"{name}.log")
if not os.path.exists(path):
return "๐Ÿ“ญ ู„ุง ูŠูˆุฌุฏ ู„ูˆู‚"
with open(path, "r") as f:
return f.read()[-4000:]
# ================= AUTO RESTART =================
def monitor():
while True:
for name, proc in list(processes.items()):
if proc.poll() is not None:
run_bot(name)
time.sleep(5)
threading.Thread(target=monitor, daemon=True).start()
# ================= UI =================
with gr.Blocks(title="Wolf VPS") as app:
login_state = gr.State(False)
with gr.Column(visible=True) as login_page:
gr.Markdown("## ๐Ÿ” ุฏุฎูˆู„")
u = gr.Textbox()
p = gr.Textbox(type="password")
btn = gr.Button("Login")
msg = gr.Textbox()
with gr.Column(visible=False) as panel:
gr.Markdown("## ๐Ÿบ Wolf Bot Panel")
name = gr.Textbox(value="bot.py")
code = gr.Code(language="python")
with gr.Row():
save = gr.Button("๐Ÿ’พ")
run = gr.Button("โ–ถ๏ธ")
stop = gr.Button("๐Ÿ›‘")
stat = gr.Button("๐Ÿ“Š")
out = gr.Textbox()
logs = gr.Textbox(lines=15)
refresh = gr.Button("๐Ÿ”„ Logs")
def do_login(x, y):
ok, m = login(x, y)
if ok:
return m, False, True
return m, True, False
btn.click(do_login, [u, p], [msg, login_page, panel])
save.click(save_bot, [name, code], [out])
run.click(run_bot, [name], [out])
stop.click(stop_bot, [name], [out])
stat.click(status_bot, [name], [out])
refresh.click(read_log, [name], [logs])
app.launch(server_name="0.0.0.0", server_port=7860)