OS-Genesis Framework

repository·main·Indexed 41 days ago

https://github.com/os-copilot/os-genesis

A framework for automating the construction of GUI agent trajectory data using reverse task synthesis. OS-Genesis provides tools and models, including versions based on InternVL2 and Qwen2-VL, to train agents for interacting with Android and Web interfaces. It includes pipelines for collecting interaction triples via random walks in AndroidWorld and WebArena, a trajectory reward model for scoring, and evaluation tools for the AndroidControl benchmark.

Tokens
3.3K
Snippets
10
Records
15
Agent score
52%

What's inside OS-Genesis

  1. Overview of OS-Genesis

    main
    OS-Genesis is an interaction-driven pipeline designed to synthesize high-quality and diverse GUI agent trajectory data without human supervision or predefined tasks. It utilizes reverse task synthesis and a trajectory reward model to enable effective end-to-end training of GUI agents across Mobile (Android) and Web environments.
  2. Build trajectories in the AndroidWorld Environment

    main

    Transform instructions into trajectories in AndroidWorld by following these steps:

    1. Prepare App States: Unzip params_new.zip into the android_env/android_world directory, replacing existing folders if necessary.
    2. Prepare Instructions: Move your instruction file (e.g., aw_instructions.json) into the android_env/androidworld directory.
    3. Execute Runner: Run run_gpt_task_runner.py to generate augmented trajectory information and screenshots.

    Outputs:

    • aw_instructions.json: Contains augmented trajectory information.
    • A screenshot folder (e.g., screenshots_gpt_241103).

    Note on Implementation: The trajectory collection uses inference logic adapted from the M3A agent. Specific modifications are documented via comments within gpt_task_runner.py.

    # Move instruction file to the environment directory
    # Then run the task runner
    python run_gpt_task_runner.py
  3. Evaluate the AndroidControl Benchmark

    main

    To evaluate the AndroidControl Benchmark using OS-Genesis, follow these three steps:

    1. Clone the repository to your local environment.
    2. Run Inference using the provided shell script, passing the dataset and checkpoint as arguments.
    3. Run Evaluation using the Python evaluation script.
    # 1. Clone the repository
    git clone https://github.com/OS-Copilot/OS-Genesis.git
    
    # 2. Inference
    cd OS-Genesis/evaluation/android_control
    bash run_ac_inference.sh $dataset $checkpoint
    
    # 3. Evaluation
    python ac_eval.py
  4. Collect interaction triples via Random Walk in AndroidWorld

    main

    To collect <screen_pre, action, screen_after> triples using a random walk in the AndroidWorld environment, follow these steps:

    1. Setup: Clone the AndroidWorld repository. Place random_walk_aw.py and run_random_walk_aw.py inside the AndroidWorld directory.
    2. Start Emulator: Launch the Android Virtual Machine using the emulator command. Ensure you use the correct EMULATOR_NAME (e.g., AndroidWorldAvd).
    3. Run Collection: Execute the runner script to begin large-scale interaction collection.

    Outputs include screenshots in both the original format and an allytree annotated format.

    # 1. Start the emulator
    EMULATOR_NAME=AndroidWorldAvd
    ~/Library/Android/sdk/emulator/emulator -avd $EMULATOR_NAME -no-snapshot -grpc 8554
    
    # 2. Run the random walk collection
    python run_random_walk_aw.py
  5. Collect interaction triples via Random Walk in WebArena

    main

    To collect <screen_pre, action, screen_after> triples in the WebArena environment:

    1. Setup: Configure WebArena and ensure the specified ports are open to access the website.
    2. Prepare Script: Place random_walk_web.py in the WebArena directory.
    3. Run Collection: Execute the script to perform the random walk and collect large-scale interaction triples.
    python random_walk_web.py
  6. Evaluate using AndroidWorld

    main

    To evaluate a trained model using AndroidWorld, you must align the model's action space with the environment. This is achieved by integrating the Genesis agent files into the original AndroidWorld agent directory and replacing the inference script.

    Follow these steps:

    1. Relocate Genesis files: Move genesis_agent.py and genesis_utils.py to the directory android_env/android_world/android_world/agents (this is the original path for agents in AndroidWorld).
    2. Replace inference script: Replace the existing infer.py in android_env/android_world/android_world/agents with your Genesis inference script.
    3. Run evaluation: Execute the evaluation using the run.py script with the android_world suite family.

    This process follows the standard AndroidWorld framework.

    python run.py \
      --suite_family=android_world \
      --agent_name=genesis_agent \
      --perform_emulator_setup
  7. Extract instructions and trajectories from dataset JSON files

    main

    The diversity analysis expects data in specific JSON formats. To prepare data for analysis, you must extract two types of information:

    1. Instructions: A list of strings extracted from the instruction key of each entry.
    2. Trajectories: A list of strings representing the reasoning or actions taken during a task.
      • For Mobile data, extract the reason field from each step within the steps list.
      • For Web data, extract the reason_action field from each step within the steps list.

    Note: When processing trajectories, it is common practice to limit the number of samples (e.g., using a MAX_NUM constant) to maintain computational efficiency during embedding generation.

    # Extracting instructions
    instructions = [d['instruction'] for d in data_list]
    
    # Extracting Mobile trajectories (using 'reason')
    mobile_traj = []
    for episode in mobile_data:
        if 'steps' in episode:
            for step in episode['steps']:
                mobile_traj.append(step['reason'])
    
    # Extracting Web trajectories (using 'reason_action')
    web_traj = []
    for episode in web_data:
        if 'steps' in episode:
            for step in episode['steps']:
                web_traj.append(step['reason_action'])
  8. Troubleshoot unresponsive Android VM during long runs

    main

    During long data collection runs, the Android Virtual Machine may become unresponsive, likely due to memory issues within the VM.

    Workaround: Reset the device directly within Android Studio to release occupied memory and restore normal operation.

  9. Available OS-Genesis Models for Mobile and Web

    main

    OS-Genesis provides pre-trained models for Android (AndroidControl and AndroidWorld) and Web environments. These models are based on either InternVL2 or Qwen2-VL architectures.

    AndroidControl Models

    • OS-Genesis-4B-AC: Based on InternVL2-4B. Trained on OS-Genesis-ac-training-data.
    • OS-Genesis-7B-AC: Based on Qwen2-VL-7B-Instruct. Trained on OS-Genesis-ac-training-data.
    • OS-Genesis-8B-AC: Based on InternVL2-8B. Trained on OS-Genesis-ac-training-data.

    AndroidWorld Models

    • OS-Genesis-4B-AW: Based on InternVL2-4B. Trained on OS-Genesis-aw-training-data.
    • OS-Genesis-7B-AW: Based on Qwen2-VL-7B-Instruct. Trained on OS-Genesis-aw-training-data.
    • OS-Genesis-8B-AW: Based on InternVL2-8B. Trained on OS-Genesis-aw-training-data.

    Web Models

    • OS-Genesis-4B-WA: Based on InternVL2-4B. Trained on OS-Genesis-web-training-data.
    • OS-Genesis-7B-WA: Based on Qwen2-VL-7B-Instruct. Trained on OS-Genesis-web-training-data.
    • OS-Genesis-8B-WA: Based on InternVL2-8B. Trained on OS-Genesis-web-training-data.
  10. Visualize text embeddings with HDBSCAN clustering

    main

    To visually inspect how semantic clusters are distributed in the embedding space, you can project the embeddings and color them by cluster labels generated via HDBSCAN.

    Steps:

    1. Generate embeddings using SentenceTransformer.
    2. Apply HDBSCAN(min_cluster_size=2) to the embeddings to identify clusters.
    3. Use a scatter plot to visualize the first two dimensions of the embeddings, coloring points by their cluster ID.
    from sentence_transformers import SentenceTransformer
    from hdbscan import HDBSCAN
    import matplotlib.pyplot as plt
    
    # 1. Get embeddings
    model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
    embeddings = model.encode(sentences)
    
    # 2. Cluster
    clusterer = HDBSCAN(min_cluster_size=2).fit(embeddings)
    
    # 3. Visualize
    plt.scatter(embeddings[:, 0], embeddings[:, 1], c=clusterer.labels_, cmap="Spectral", s=10)
    plt.show()