binary1ne/web-ui
0
1import os2import socket3import subprocess4import argparse5import gradio as gr6from src.webui.interface import theme_map, create_ui7 8from dotenv import load_dotenv9load_dotenv()10 11html="""12<div id="vnc-container" style="width:100%;height:800px;border:2px solid #444;border-radius:8px;"></div>13<script type="module">14 import RFB from "https://unpkg.com/@novnc/novnc/core/rfb.js";15 16 // Connect directly to your WebSocket proxy17 const url = "ws://localhost:6080/websockify";18 const target = document.getElementById('vnc-container');19 20 try {21 const rfb = new RFB(target, url);22 rfb.viewOnly = false; // true = disable keyboard/mouse input23 rfb.scaleViewport = true;24 rfb.resizeSession = true;25 rfb.background = "black";26 27 console.log("โ
Connected to VNC via WebSocket:", url);28 } catch (err) {29 target.innerHTML = "<p style='color:red'> โ Failed to connect to VNC server.<br>" + err + "</p>";30 }31</script>32"""33def get_vnc_iframe():34 # get hostname + IP35 hostname = socket.gethostname()36 ip = socket.gethostbyname(hostname)37 hostname = subprocess.check_output("hostname", shell=True).decode().strip()38 # build iframe URL39 url_old = f"http://{hostname}:6080/vnc.html?host={hostname}&port=6080"40 url=f"/usr/share/novnc/vnc.html?host={hostname}&port=6080"41 print("############## URL :",url)42 # return HTML iframe43 return f"""44 <iframe src="{url}" width="100%" height="800" style="border:1px solid red;"></iframe>45 """46 47# # Example theme map for the argument parser (add more themes if you have them)48# theme_map = {49# "Ocean": gr.themes.Ocean(),50# "Default": gr.themes.Default(),51# # Add other themes as needed52# }53 54# # Function to run VNC setup (generates .vnc/passwd)55# def setup_vnc(password):56# vnc_dir = os.path.expanduser("~/.vnc")57# os.makedirs(vnc_dir, exist_ok=True)58# passwd_file = os.path.join(vnc_dir, "passwd")59# proc = subprocess.Popen(60# f"echo '{password}' | vncpasswd -f > '{passwd_file}'",61# shell=True,62# stdout=subprocess.PIPE,63# stderr=subprocess.PIPE,64# )65# stdout, stderr = proc.communicate()66# os.chmod(passwd_file, 0o600)67# return f"VNC password set to: {password}\n{stdout.decode()}\n{stderr.decode()}"68 69# # Function to run the webui.py application70# def start_webui(ip="0.0.0.0", port=7860):71# # This will run in a thread72# def run():73# subprocess.run(["python", "webui.py", "--ip", ip, "--port", str(port)], cwd="/app")74# thread = threading.Thread(target=run, daemon=True)75# thread.start()76# return f"webui.py started at http://{ip}:{port}"77 78# # Gradio interface for VNC setup79# def vnc_setup(password):80# result = setup_vnc(password)81# return result82 83# # Main app UI (tab content)84# def create_ui(theme_name="Ocean"):85# with gr.Blocks(theme=theme_map.get(theme_name, gr.themes.Ocean())) as base_ui:86# gr.Markdown("## WebUI and VNC Setup")87 88# with gr.Tab("Start WebUI"):89# with gr.Row():90# ip = gr.Textbox(label="IP", value="0.0.0.0")91# port = gr.Number(label="Port", value=7788)92# webui_btn = gr.Button("Start WebUI")93# webui_out = gr.Textbox(label="WebUI Output")94# webui_btn.click(start_webui, inputs=[ip, port], outputs=webui_out)95 96# with gr.Tab("VNC Setup"):97# vnc_password = gr.Textbox(label="VNC Password", type="password")98# vnc_btn = gr.Button("Setup VNC Password")99# vnc_out = gr.Textbox(label="VNC Setup Output")100# vnc_btn.click(vnc_setup, inputs=[vnc_password], outputs=vnc_out)101# return base_ui102 103def main():104 parser = argparse.ArgumentParser(description="Gradio WebUI for Browser Agent")105 parser.add_argument("--ip", type=str, default=os.getenv("HOST", "0.0.0.0"), help="IP address to bind to")106 parser.add_argument("--port", type=int, default=int(os.getenv("PORT", "7860")), help="Port to listen on")107 parser.add_argument("--theme", type=str, default=os.getenv("THEME", "Ocean"), choices=theme_map.keys(), help="Theme to use for the UI")108 args = parser.parse_args()109 110 # Existing UI111 base_ui = create_ui(theme_name=args.theme)112 113 # Combine into single Gradio app114 with gr.Blocks(theme=theme_map[args.theme]) as demo:115 gr.Markdown("# ๐ Browser Agent Web-UI")116 117 with gr.Tab("Main App"):118 base_ui.render()119 120 with gr.Tab("Web VNC"):121 # The iframe must point to the actual noVNC server (default: localhost:6080/vnc.html)122 # gr.HTML(f'<iframe src="http://localhost:6080/vnc.html" width="100%" height="800" style="border:1px solid red;"></iframe>')123 # gr.HTML(html)124 gr.HTML(get_vnc_iframe())125 demo.queue().launch(server_name=args.ip, server_port=args.port)126 127if __name__ == "__main__":128 main()