File size: 5,894 Bytes
be72cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import subprocess
import sys

# Install BrowserGym dependencies before running the main application
def install_browsergym():
    try:
        print("Installing BrowserGym dependencies...")
        subprocess.run("cd BrowserGym && make install", shell=True, check=True)
        print("BrowserGym installation completed successfully")
        
        # Add BrowserGym directory to sys.path directly
        current_dir = os.path.dirname(os.path.abspath(__file__))
        browsergym_path = os.path.join(current_dir, "BrowserGym")
        if browsergym_path not in sys.path:
            sys.path.insert(0, browsergym_path)
            print(f"Added BrowserGym to sys.path: {browsergym_path}")
        
        # Also set PYTHONPATH environment variable for child processes
        if "PYTHONPATH" in os.environ:
            os.environ["PYTHONPATH"] = f"{browsergym_path}:{os.environ['PYTHONPATH']}"
        else:
            os.environ["PYTHONPATH"] = browsergym_path
        print(f"Updated PYTHONPATH: {os.environ['PYTHONPATH']}")
        
        # Verify BrowserGym is importable
        try:
            import importlib.util

            # Try to import HighLevelActionSet
            spec = importlib.util.find_spec("browsergym.core.action.highlevel")
            if spec is None:
                print("Module browsergym.core.action.highlevel not found")
            else:
                module = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(module)
                HighLevelActionSet = getattr(module, "HighLevelActionSet")
            print('good!!')
            from browsergym.core.action.highlevel import HighLevelActionSet
            from browsergym.utils.obs import (
                flatten_axtree_to_str,
                flatten_dom_to_str,
                prune_html,
            )
            from browsergym.experiments import Agent
            print("BrowserGym successfully imported")
        except ImportError as e:
            print(f"BrowserGym import verification failed: {e}")
            print(f"Current sys.path: {sys.path}")
            raise
            
    except subprocess.CalledProcessError as e:
        print(f"Error installing BrowserGym: {e}")
        raise

# Install BrowserGym first
install_browsergym()

# Now import the rest of the modules
import logging
import gradio as gr
import openai
import multiprocessing

from process_run import process_run

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
    ]
)
logger = logging.getLogger(__name__)
logger.setLevel('INFO')

# Set your OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")


# Example instructions to display
EXAMPLES = [
    "When did the solar system form? Find on wikipedia.",
    "Find the rating of Monopoly (1935) on boardgamegeek.com",
]

URL_EXAMPLES = [
    "about:blank",
    "https://www.wikipedia.org",
    "https://www.boardgamegeek.com"
]

def main():
    logger.info("Starting BrowserGym web agent")
    
    with gr.Blocks(title="WebShephered Demo") as demo:
        # Add CSS for outlined groups
        gr.Markdown("# WebShephered Demo")
        with gr.Row():
            with gr.Column(scale=2):
                with gr.Column():
                    instruction = gr.Textbox(
                        label="Instruction",
                        placeholder="Enter your instruction here",
                        lines=2,
                    )
                    gr.Examples(
                        examples=[[e] for e in EXAMPLES],
                        inputs=instruction,
                        cache_examples=False,
                    )
                
                gr.Markdown("\n\n")
                
                with gr.Column():
                    start_url = gr.Textbox(
                        label="Starting URL",
                        placeholder="URL to start the browser at",
                        value="about:blank"
                    )
                    gr.Examples(
                        examples=URL_EXAMPLES,
                        inputs=start_url,
                        cache_examples=False,
                    )

                gr.Markdown("\n\n")

                model_name = gr.Dropdown(
                    label="Agent Model",
                    choices=["gpt-4o"],
                    value="gpt-4o"
                )
                run_btn = gr.Button("Run Demo")

                gr.Markdown("---")
                
                with gr.Column():
                    gr.Markdown("## Current State")
                    state_view = gr.Markdown()
                    browser_view = gr.Image(label="Browser View")

                    gr.Markdown("### Task Checklist from WebShephered")
                    checklist_view = gr.Markdown()
                    
                    gr.Markdown("### Action Selection in current step")
                    with gr.Row() as rm_row:
                        rm_cards_container = gr.HTML()
            with gr.Column(scale=2):
                gr.Markdown("## Trajectory")
                trajectory_container = gr.HTML() # Placeholder for our custom trajectory component

        
            
        run_btn.click(
            fn=process_run,
            inputs=[instruction, model_name, start_url],
            outputs=[state_view, browser_view, checklist_view, rm_cards_container, trajectory_container],
            api_name="run_agent",
            concurrency_limit=32,
            show_progress=True
        )

    logger.info("Launching Gradio interface")
    # Set max_threads to allow multiple concurrent requests
    demo.launch(share=True, max_threads=32)

if __name__ == "__main__":
    # Add support for multiprocessing on Windows
    multiprocessing.freeze_support()
    main()