iruno commited on
Commit
a26b167
·
verified ·
1 Parent(s): 6a7ac36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -157
app.py CHANGED
@@ -2,163 +2,18 @@ import os
2
  import subprocess
3
  import sys
4
 
5
- # Install BrowserGym dependencies before running the main application
6
- def install_browsergym():
7
- try:
8
- print("Installing BrowserGym dependencies...")
9
- subprocess.run("cd BrowserGym && make install", shell=True, check=True)
10
- print("BrowserGym installation completed successfully")
11
-
12
- # Add BrowserGym directory to sys.path directly
13
- current_dir = os.path.dirname(os.path.abspath(__file__))
14
- browsergym_path = os.path.join(current_dir, "BrowserGym")
15
- if browsergym_path not in sys.path:
16
- sys.path.insert(0, browsergym_path)
17
- print(f"Added BrowserGym to sys.path: {browsergym_path}")
18
-
19
- # Also set PYTHONPATH environment variable for child processes
20
- if "PYTHONPATH" in os.environ:
21
- os.environ["PYTHONPATH"] = f"{browsergym_path}:{os.environ['PYTHONPATH']}"
22
- else:
23
- os.environ["PYTHONPATH"] = browsergym_path
24
- print(f"Updated PYTHONPATH: {os.environ['PYTHONPATH']}")
25
-
26
- # Verify BrowserGym is importable
27
- try:
28
- import importlib.util
29
 
30
- # Try to import HighLevelActionSet
31
- spec = importlib.util.find_spec("browsergym.core.action.highlevel")
32
- if spec is None:
33
- print("Module browsergym.core.action.highlevel not found")
34
- else:
35
- module = importlib.util.module_from_spec(spec)
36
- spec.loader.exec_module(module)
37
- HighLevelActionSet = getattr(module, "HighLevelActionSet")
38
- print("BrowserGym successfully imported")
39
- except ImportError as e:
40
- print(f"BrowserGym import verification failed: {e}")
41
- print(f"Current sys.path: {sys.path}")
42
- raise
43
-
44
- except subprocess.CalledProcessError as e:
45
- print(f"Error installing BrowserGym: {e}")
46
- raise
47
 
48
- # Install BrowserGym first
49
- install_browsergym()
 
50
 
51
- # Now import the rest of the modules
52
- import logging
53
- import gradio as gr
54
- import openai
55
- import multiprocessing
56
-
57
- from process_run import process_run
58
-
59
- # Configure logging
60
- logging.basicConfig(
61
- level=logging.INFO,
62
- format='%(asctime)s - %(levelname)s - %(message)s',
63
- handlers=[
64
- logging.StreamHandler(),
65
- ]
66
- )
67
- logger = logging.getLogger(__name__)
68
- logger.setLevel('INFO')
69
-
70
- # Set your OpenAI API key
71
- openai.api_key = os.getenv("OPENAI_API_KEY")
72
-
73
-
74
- # Example instructions to display
75
- EXAMPLES = [
76
- "When did the solar system form? Find on wikipedia.",
77
- "Find the rating of Monopoly (1935) on boardgamegeek.com",
78
- ]
79
-
80
- URL_EXAMPLES = [
81
- "about:blank",
82
- "https://www.wikipedia.org",
83
- "https://www.boardgamegeek.com"
84
- ]
85
-
86
- def main():
87
- logger.info("Starting BrowserGym web agent")
88
-
89
- with gr.Blocks(title="WebShephered Demo") as demo:
90
- # Add CSS for outlined groups
91
- gr.Markdown("# WebShephered Demo")
92
- with gr.Row():
93
- with gr.Column(scale=2):
94
- with gr.Column():
95
- instruction = gr.Textbox(
96
- label="Instruction",
97
- placeholder="Enter your instruction here",
98
- lines=2,
99
- )
100
- gr.Examples(
101
- examples=[[e] for e in EXAMPLES],
102
- inputs=instruction,
103
- cache_examples=False,
104
- )
105
-
106
- gr.Markdown("\n\n")
107
-
108
- with gr.Column():
109
- start_url = gr.Textbox(
110
- label="Starting URL",
111
- placeholder="URL to start the browser at",
112
- value="about:blank"
113
- )
114
- gr.Examples(
115
- examples=URL_EXAMPLES,
116
- inputs=start_url,
117
- cache_examples=False,
118
- )
119
-
120
- gr.Markdown("\n\n")
121
-
122
- model_name = gr.Dropdown(
123
- label="Agent Model",
124
- choices=["gpt-4o"],
125
- value="gpt-4o"
126
- )
127
- run_btn = gr.Button("Run Demo")
128
-
129
- gr.Markdown("---")
130
-
131
- with gr.Column():
132
- gr.Markdown("## Current State")
133
- state_view = gr.Markdown()
134
- browser_view = gr.Image(label="Browser View")
135
-
136
- gr.Markdown("### Task Checklist from WebShephered")
137
- checklist_view = gr.Markdown()
138
-
139
- gr.Markdown("### Action Selection in current step")
140
- with gr.Row() as rm_row:
141
- rm_cards_container = gr.HTML()
142
- with gr.Column(scale=2):
143
- gr.Markdown("## Trajectory")
144
- trajectory_container = gr.HTML() # Placeholder for our custom trajectory component
145
-
146
-
147
-
148
- run_btn.click(
149
- fn=process_run,
150
- inputs=[instruction, model_name, start_url],
151
- outputs=[state_view, browser_view, checklist_view, rm_cards_container, trajectory_container],
152
- api_name="run_agent",
153
- concurrency_limit=32,
154
- show_progress=True
155
- )
156
-
157
- logger.info("Launching Gradio interface")
158
- # Set max_threads to allow multiple concurrent requests
159
- demo.launch(share=True, max_threads=32)
160
-
161
- if __name__ == "__main__":
162
- # Add support for multiprocessing on Windows
163
- multiprocessing.freeze_support()
164
- main()
 
2
  import subprocess
3
  import sys
4
 
5
+ print("Installing BrowserGym dependencies...")
6
+ subprocess.run("cd BrowserGym && make install", shell=True, check=True)
7
+ print("BrowserGym installation completed successfully")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Add BrowserGym directory to sys.path directly
10
+ current_dir = os.path.dirname(os.path.abspath(__file__))
11
+ browsergym_path = os.path.join(current_dir, "BrowserGym")
12
+ print(f"Added BrowserGym path: {browsergym_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Prepare env with updated PYTHONPATH
15
+ env = os.environ.copy()
16
+ env["PYTHONPATH"] = f"{browsergym_path}:{env.get('PYTHONPATH', '')}"
17
 
18
+ # 🔁 Now run app.py in a clean subprocess with correct PYTHONPATH
19
+ subprocess.run([sys.executable, "app.py"], env=env)