Spaces:
Sleeping
Sleeping
Create app_.py
Browse files
app_.py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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('good!!')
|
39 |
+
from browsergym.core.action.highlevel import HighLevelActionSet
|
40 |
+
from browsergym.utils.obs import (
|
41 |
+
flatten_axtree_to_str,
|
42 |
+
flatten_dom_to_str,
|
43 |
+
prune_html,
|
44 |
+
)
|
45 |
+
from browsergym.experiments import Agent
|
46 |
+
print("BrowserGym successfully imported")
|
47 |
+
except ImportError as e:
|
48 |
+
print(f"BrowserGym import verification failed: {e}")
|
49 |
+
print(f"Current sys.path: {sys.path}")
|
50 |
+
raise
|
51 |
+
|
52 |
+
except subprocess.CalledProcessError as e:
|
53 |
+
print(f"Error installing BrowserGym: {e}")
|
54 |
+
raise
|
55 |
+
|
56 |
+
# Install BrowserGym first
|
57 |
+
install_browsergym()
|
58 |
+
|
59 |
+
# Now import the rest of the modules
|
60 |
+
import logging
|
61 |
+
import gradio as gr
|
62 |
+
import openai
|
63 |
+
import multiprocessing
|
64 |
+
|
65 |
+
from process_run import process_run
|
66 |
+
|
67 |
+
# Configure logging
|
68 |
+
logging.basicConfig(
|
69 |
+
level=logging.INFO,
|
70 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
71 |
+
handlers=[
|
72 |
+
logging.StreamHandler(),
|
73 |
+
]
|
74 |
+
)
|
75 |
+
logger = logging.getLogger(__name__)
|
76 |
+
logger.setLevel('INFO')
|
77 |
+
|
78 |
+
# Set your OpenAI API key
|
79 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
80 |
+
|
81 |
+
|
82 |
+
# Example instructions to display
|
83 |
+
EXAMPLES = [
|
84 |
+
"When did the solar system form? Find on wikipedia.",
|
85 |
+
"Find the rating of Monopoly (1935) on boardgamegeek.com",
|
86 |
+
]
|
87 |
+
|
88 |
+
URL_EXAMPLES = [
|
89 |
+
"about:blank",
|
90 |
+
"https://www.wikipedia.org",
|
91 |
+
"https://www.boardgamegeek.com"
|
92 |
+
]
|
93 |
+
|
94 |
+
def main():
|
95 |
+
logger.info("Starting BrowserGym web agent")
|
96 |
+
|
97 |
+
with gr.Blocks(title="WebShephered Demo") as demo:
|
98 |
+
# Add CSS for outlined groups
|
99 |
+
gr.Markdown("# WebShephered Demo")
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column(scale=2):
|
102 |
+
with gr.Column():
|
103 |
+
instruction = gr.Textbox(
|
104 |
+
label="Instruction",
|
105 |
+
placeholder="Enter your instruction here",
|
106 |
+
lines=2,
|
107 |
+
)
|
108 |
+
gr.Examples(
|
109 |
+
examples=[[e] for e in EXAMPLES],
|
110 |
+
inputs=instruction,
|
111 |
+
cache_examples=False,
|
112 |
+
)
|
113 |
+
|
114 |
+
gr.Markdown("\n\n")
|
115 |
+
|
116 |
+
with gr.Column():
|
117 |
+
start_url = gr.Textbox(
|
118 |
+
label="Starting URL",
|
119 |
+
placeholder="URL to start the browser at",
|
120 |
+
value="about:blank"
|
121 |
+
)
|
122 |
+
gr.Examples(
|
123 |
+
examples=URL_EXAMPLES,
|
124 |
+
inputs=start_url,
|
125 |
+
cache_examples=False,
|
126 |
+
)
|
127 |
+
|
128 |
+
gr.Markdown("\n\n")
|
129 |
+
|
130 |
+
model_name = gr.Dropdown(
|
131 |
+
label="Agent Model",
|
132 |
+
choices=["gpt-4o"],
|
133 |
+
value="gpt-4o"
|
134 |
+
)
|
135 |
+
run_btn = gr.Button("Run Demo")
|
136 |
+
|
137 |
+
gr.Markdown("---")
|
138 |
+
|
139 |
+
with gr.Column():
|
140 |
+
gr.Markdown("## Current State")
|
141 |
+
state_view = gr.Markdown()
|
142 |
+
browser_view = gr.Image(label="Browser View")
|
143 |
+
|
144 |
+
gr.Markdown("### Task Checklist from WebShephered")
|
145 |
+
checklist_view = gr.Markdown()
|
146 |
+
|
147 |
+
gr.Markdown("### Action Selection in current step")
|
148 |
+
with gr.Row() as rm_row:
|
149 |
+
rm_cards_container = gr.HTML()
|
150 |
+
with gr.Column(scale=2):
|
151 |
+
gr.Markdown("## Trajectory")
|
152 |
+
trajectory_container = gr.HTML() # Placeholder for our custom trajectory component
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
run_btn.click(
|
157 |
+
fn=process_run,
|
158 |
+
inputs=[instruction, model_name, start_url],
|
159 |
+
outputs=[state_view, browser_view, checklist_view, rm_cards_container, trajectory_container],
|
160 |
+
api_name="run_agent",
|
161 |
+
concurrency_limit=32,
|
162 |
+
show_progress=True
|
163 |
+
)
|
164 |
+
|
165 |
+
logger.info("Launching Gradio interface")
|
166 |
+
# Set max_threads to allow multiple concurrent requests
|
167 |
+
demo.launch(share=True, max_threads=32)
|
168 |
+
|
169 |
+
if __name__ == "__main__":
|
170 |
+
# Add support for multiprocessing on Windows
|
171 |
+
multiprocessing.freeze_support()
|
172 |
+
main()
|