brain4 / app.py
Elliotasdasdasfasas
Debug Brain4: Defensive Import + System Check Tab
3279a97
import gradio as gr
import threading
import os
import time
import sys
import subprocess
# Defensive Import
try:
from azure_bridge import AzureSFTPBridge
IMPORT_ERROR = None
except Exception as e:
AzureSFTPBridge = None
IMPORT_ERROR = str(e)
# Global Bridge Instance
bridge = None
def get_system_info():
"""Returns debug info about the environment."""
info = f"Python: {sys.version}\n"
info += f"User: {os.getuid()}\n"
info += f"CWD: {os.getcwd()}\n"
info += f"ls -la: \n{subprocess.getoutput('ls -la')}\n"
info += f"Cloudflared: {subprocess.getoutput('cloudflared --version')}\n"
info += f"Pip Freeze: \n{subprocess.getoutput('pip freeze')}\n"
if IMPORT_ERROR:
info += f"\n❌ CRITICAL: Azure Bridge Import Failed: {IMPORT_ERROR}"
else:
info += "\nβœ… Azure Bridge Import Successful"
return info
def start_bridge():
global bridge
if IMPORT_ERROR:
return f"❌ Cannot start: Import failed ({IMPORT_ERROR})"
if not os.environ.get("SSH_KEY"):
return "❌ Error: SSH_KEY not found in Secrets."
try:
bridge = AzureSFTPBridge()
# Run tunnel in background to not block UI
t = threading.Thread(target=bridge.start_tunnel, daemon=True)
t.start()
return "⏳ Bridge initializing... Wait 10s then Test Connection."
except Exception as e:
return f"❌ Bridge Init Failed: {e}"
def test_connection():
global bridge
if not bridge:
return "❌ Bridge not started. Click 'Start Bridge' first."
try:
# Try to list files in the mount
files = bridge.list_files("/mnt/lightrag")
return f"βœ… SUCCESS! Files in Azure:\n{files}"
except Exception as e:
return f"❌ Connection Failed: {e}"
with gr.Blocks(title="Brain4: SFTP Bridge (Debug Mode)") as demo:
gr.Markdown("# 🧠 Brain4: Azure SFTP Bridge (Debug Mode)")
with gr.Tabs():
with gr.Tab("Bridge Control"):
gr.Markdown("Status: **Running as Root** (Pure TCP Tunnel)")
with gr.Row():
btn_start = gr.Button("β‘  Start Bridge", variant="primary")
btn_test = gr.Button("β‘‘ Test Connection")
output_log = gr.Textbox(label="Bridge Log", lines=5)
btn_start.click(start_bridge, inputs=None, outputs=output_log)
btn_test.click(test_connection, inputs=None, outputs=output_log)
with gr.Tab("System Debug"):
btn_debug = gr.Button("πŸ” Scan System")
debug_log = gr.Textbox(label="System Info", lines=20)
btn_debug.click(get_system_info, inputs=None, outputs=debug_log)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)