import os import pexpect import gradio as gr import time import subprocess # Log file ka path LOG_FILE = "/tmp/bitping_debug.log" def run_command(command): with open(LOG_FILE, "a") as f: f.write(f"\n--- Running: {command} ---\n") return subprocess.getoutput(command) def init_bitping(): email = os.environ.get("EMAIL") password = os.environ.get("PASSWORD") with open(LOG_FILE, "w") as f: f.write("Starting Bitping Initialization...\n") if not email or not password: with open(LOG_FILE, "a") as f: f.write("❌ ERROR: EMAIL or PASSWORD secrets are missing in HF Settings!\n") return try: # Check if bitpingd exists path_check = run_command("which bitpingd || find / -name bitpingd 2>/dev/null") bitping_path = path_check.strip().split('\n')[0] if not bitping_path: bitping_path = "/root/.bitping/bin/bitpingd" # Login Process using pexpect child = pexpect.spawn(f'{bitping_path} login', encoding='utf-8', timeout=20) index = child.expect(['(?i)email', pexpect.EOF, pexpect.TIMEOUT]) if index == 0: child.sendline(email) child.expect('(?i)password') child.sendline(password) time.sleep(5) # Start the node os.system(f"nohup {bitping_path} > {LOG_FILE} 2>&1 &") with open(LOG_FILE, "a") as f: f.write("✅ Attempted login and started node in background.\n") else: with open(LOG_FILE, "a") as f: f.write(f"❌ Failed to find Email prompt. Output: {child.before}\n") except Exception as e: with open(LOG_FILE, "a") as f: f.write(f"❌ Python Error: {str(e)}\n") # Initialization call init_bitping() # Gradio UI Functions def chat_response(message, history): return "Support Bot: I am active. How can I help?" def get_logs(): if os.path.exists(LOG_FILE): with open(LOG_FILE, "r") as f: return f.read() return "No logs found yet." # Create UI with gr.Blocks() as demo: gr.Markdown("# Customer Support Portal") with gr.Tab("Chat"): gr.ChatInterface(chat_response) with gr.Tab("System Status (Debug)"): log_output = gr.Textbox(label="Background Logs", lines=15) refresh_btn = gr.Button("Refresh Logs") refresh_btn.click(get_logs, outputs=log_output) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)