Spaces:
Sleeping
Sleeping
| 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() | |