File size: 3,094 Bytes
8f0298e
 
 
 
 
 
 
 
 
1620ca9
 
 
 
 
 
 
8f0298e
1620ca9
8f0298e
 
 
 
 
 
 
28326a1
 
8f0298e
1620ca9
8f0298e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1620ca9
8f0298e
1620ca9
8f0298e
 
1620ca9
8f0298e
 
 
 
1620ca9
 
8f0298e
 
 
1620ca9
8f0298e
 
 
 
cb73afd
8f0298e
 
 
28326a1
8f0298e
 
 
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
import os
import argparse
import logging
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import gradio as gr

def install_chrome():
    os.system("apt update && apt install -y curl")
    os.system("curl -fsSL https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-chrome-keyring.gpg")
    os.system("echo 'deb [signed-by=/usr/share/keyrings/google-chrome-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list")
    os.system("apt update && apt install -y google-chrome-stable")

install_chrome()

logger = logging.getLogger(__name__)
_global_browser = None

def setup_browser(headless=True, window_size=(1280, 720)):
    global _global_browser
    chrome_options = Options()
    if headless:
        chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument(f"--window-size={window_size[0]},{window_size[1]}")
    chrome_options.binary_location = "/usr/bin/google-chrome-stable"

    service = Service(ChromeDriverManager().install())
    _global_browser = webdriver.Chrome(service=service, options=chrome_options)

    return _global_browser

def close_browser():
    global _global_browser
    if _global_browser:
        _global_browser.quit()
        _global_browser = None

async def run_agent(task_description, headless=True, window_size=(1280, 720)):
    global _global_browser
    if not _global_browser:
        setup_browser(headless=headless, window_size=window_size)
    try:
        logger.info(f"Executing task: {task_description}")
        _global_browser.get("https://example.com")
        result = f"Task executed successfully on {task_description}"
        return result, None
    except Exception as e:
        logger.error(f"Error while running agent: {e}")
        return None, str(e)

def create_ui():
    with gr.Blocks() as demo:
        gr.Markdown("# Selenium-based Browser Automation")
        task_input = gr.Textbox(label="Task Description", placeholder="Describe the task you want to execute...")
        headless_mode = gr.Checkbox(label="Run in Headless Mode", value=True, interactive=True)
        run_button = gr.Button("Run Task")
        output_result = gr.Textbox(label="Result")
        output_error = gr.Textbox(label="Error")
        run_button.click(run_agent, inputs=[task_input, headless_mode], outputs=[output_result, output_error])
    return demo

def main():
    parser = argparse.ArgumentParser(description="Gradio UI for Selenium Automation")
    parser.add_argument("--ip", type=str, default="0.0.0.0", help="IP address to bind to")
    parser.add_argument("--port", type=int, default=7860, help="Port to listen on")
    args = parser.parse_args()
    demo = create_ui()
    demo.launch(server_name="0.0.0.0", server_port=args.port)

if __name__ == "__main__":
    main()