Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP migrate to uv #158

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions .github/workflows/blackisort.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
# https://black.readthedocs.io/en/stable/integrations/github_actions.html
- name: running Black
uses: psf/black@stable
with:
options: "--check --diff --line-length 79"
version: "23.3.0"
# https://pycqa.github.io/isort/docs/configuration/github_action.html
- name: running isort
uses: jamescurtin/isort-action@master
python-version: 3.8
- name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
- name: Setup virtual environment and install black and isort
run: |
uv venv
source .venv/bin/activate
uv pip install black==23.3.0 isort
- name: Running Black
run: |
source .venv/bin/activate
black --check --diff --line-length 79 .
- name: Running isort
if: always()
with:
configuration: "--check-only --line-length 79 --profile black"
run: |
source .venv/bin/activate
isort --check-only --line-length 79 --profile black .
12 changes: 9 additions & 3 deletions .github/workflows/pythonlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ jobs:
with:
python-version: 3.8
architecture: 'x64'
- name: Install dependencies
- name: Install uv
run: |
python -m pip install --upgrade pip
pip install flake8
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
- name: Setup virtual environment and install dependencies
run: |
uv venv
source .venv/bin/activate
uv pip install flake8
- name: Lint with flake8
run: |
source .venv/bin/activate
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
16 changes: 12 additions & 4 deletions .github/workflows/pythontests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ jobs:
with:
python-version: 3.8
architecture: 'x64'
- name: Install dependencies
- name: Install uv
run: |
python -m pip install --upgrade pip
pip install coverage
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
- name: Setup virtual environment and install dependencies
run: |
uv venv
source .venv/bin/activate
uv pip install coverage
- name: Setup # needs -e flag for coverage tests to work
run: pip install -e .[harl]
run: |
source .venv/bin/activate
uv pip install -e ".[harl]"
- name: Run tests and generate coverage report
run: |
source .venv/bin/activate
coverage run -m unittest discover -s testing/ -p "*_test.py"
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,7 @@ node_modules/
**/__pycache__/
#manually correcting for demo
!**/static/**/*.png
!src/overcooked_demo/server/static/lib
!src/overcooked_demo/server/static/lib

# Ignore copied overcooked_ai directory in server
src/overcooked_demo/server/overcooked_ai/
9 changes: 9 additions & 0 deletions .uvrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[application]
default-python = "3.8"
isolated = true

[venv]
location = ".venv"

[pypi]
url = "https://pypi.org/simple"
55 changes: 55 additions & 0 deletions UV_MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Migrating to uv Package Manager

This project has been configured to use [uv](https://github.com/astral-sh/uv), a fast Python package manager. The following guide explains how to set up and use uv with this project.

## Installing uv

To install uv, run:

```sh
curl -LsSf https://astral.sh/uv/install.sh | sh
```

This adds uv to your system. You may need to restart your shell or add it to your PATH.

## Setup for Development

To set up a development environment:

```sh
# Clone the repository
git clone https://github.com/HumanCompatibleAI/overcooked_ai.git
cd overcooked_ai

# Create a virtual environment and install dependencies
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate

# Install the project in development mode with all dependencies
uv pip install -e ".[harl]"
```

## Common uv Commands

Replace pip commands with uv equivalents:

| pip | uv |
|-----|-----|
| `pip install package` | `uv pip install package` |
| `pip install -r requirements.txt` | `uv pip install -r requirements.txt` |
| `pip freeze > requirements.txt` | `uv pip freeze > requirements.txt` |

## Benefits of uv

- Faster installation speed
- Better dependency resolution
- Improved caching
- Single binary with no dependencies

## Project Structure

The project now uses a `pyproject.toml` file for configuration, which is compatible with uv and modern Python packaging standards. The `setup.py` file has been maintained for backward compatibility.

## CI/CD Integration

GitHub Actions workflows have been updated to use uv for dependency installation.
21 changes: 21 additions & 0 deletions create_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
import pickle
import os

class RandomAI:
def action(self, state):
import numpy as np
return np.random.choice(['up', 'down', 'left', 'right', 'stay', 'interact'])

def reset(self):
pass

if __name__ == "__main__":
# Create agent directory
os.makedirs('/app/data/agents/random_agent/agent', exist_ok=True)

# Save the agent
with open('/app/data/agents/random_agent/agent/agent.pickle', 'wb') as f:
pickle.dump(RandomAI(), f)

print("Agent created successfully!")
72 changes: 72 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "overcooked_ai"
version = "1.1.0"
description = "Cooperative multi-agent environment based on Overcooked"
readme = "README.md"
requires-python = ">=3.9"
license = {file = "LICENSE"}
authors = [
{name = "Micah Carroll", email = "mdc@berkeley.edu"}
]
urls = {homepage = "https://github.com/HumanCompatibleAI/overcooked_ai"}
keywords = ["Overcooked", "AI", "Reinforcement Learning"]
dependencies = [
"dill",
"numpy<2.0.0",
"scipy",
"tqdm",
"gymnasium",
"ipython",
"pygame",
"ipywidgets",
"opencv-python",
"eventlet>=0.39.0",
"flask",
"flask-socketio",
]

[project.optional-dependencies]
harl = [
"wandb",
"GitPython",
"memory_profiler",
"sacred",
"pymongo",
"matplotlib",
"requests",
"seaborn==0.9.0",
"ray[rllib]>=2.5.0",
"protobuf",
"tensorflow>=2.14.0",
]

[project.scripts]
overcooked-demo-up = "overcooked_demo:start_server"
overcooked-demo-move = "overcooked_demo:move_agent"

[tool.setuptools]
package-dir = {"" = "src"}
packages = {find = {where = ["src"]}}

[tool.setuptools.package-data]
"overcooked_ai_py" = [
"data/layouts/*.layout",
"data/planners/*.py",
"data/human_data/*.pickle",
"data/graphics/*.png",
"data/graphics/*.json",
"data/fonts/*.ttf",
]
"human_aware_rl" = [
"static/**/*.pickle",
"static/**/*.csv",
"ppo/trained_example/*.pkl",
"ppo/trained_example/*.json",
"ppo/trained_example/*/.is_checkpoint",
"ppo/trained_example/*/.tune_metadata",
"ppo/trained_example/*/checkpoint-500",
]
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
},
install_requires=[
"dill",
"numpy",
"numpy<2.0.0",
"scipy",
"tqdm",
"gymnasium",
"ipython",
"pygame",
"ipywidgets",
"opencv-python",
"flask",
"flask-socketio",
],
# removed overlapping dependencies
extras_require={
Expand All @@ -59,9 +61,9 @@
"matplotlib",
"requests",
"seaborn==0.9.0",
"ray[rllib]==2.0.0",
"ray[rllib]>=2.5.0",
"protobuf",
"tensorflow==2.10",
"tensorflow>=2.14.0",
]
},
entry_points={
Expand Down
7 changes: 5 additions & 2 deletions src/human_aware_rl/rllib/rllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import gymnasium
import numpy as np
import ray
from ray.rllib.agents.ppo import PPOTrainer
# Updated import for Ray 2.5+
from ray.rllib.algorithms.ppo import PPO as PPOTrainer
from ray.rllib.algorithms.callbacks import DefaultCallbacks
from ray.rllib.env.multi_agent_env import MultiAgentEnv
from ray.rllib.models import ModelCatalog
from ray.tune.logger import UnifiedLogger
from ray.tune.registry import register_env
from ray.tune.result import DEFAULT_RESULTS_DIR

# Define DEFAULT_RESULTS_DIR since it's no longer available in Ray 2.5+
DEFAULT_RESULTS_DIR = os.path.join(os.path.expanduser("~"), "ray_results")

from human_aware_rl.rllib.utils import (
get_base_ae,
Expand Down
26 changes: 26 additions & 0 deletions src/overcooked_demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ A web application where humans can play Overcooked with trained AI agents.

* [Installation](#installation)
* [Usage](#usage)
* [Docker Deployment](#docker-deployment)
* [Local Run with uv](#local-run-with-uv)
* [Command Line](#command-line)
* [Dependencies](#dependencies)
* [Using Pre-trained Agents](#using-pre-trained-agents)
* [Updating](#updating)
Expand All @@ -17,8 +20,14 @@ A web application where humans can play Overcooked with trained AI agents.

Building the server image requires [Docker](https://docs.docker.com/get-docker/)

For local development with uv, you'll need:
- Python 3.7 or later
- [uv](https://github.com/astral-sh/uv) - Install with `pip install uv`

## Usage

### Docker Deployment

The server can be deployed locally using the driver script included in the repo. To run the production server, use the command
```bash
./up.sh production
Expand All @@ -36,6 +45,23 @@ In order to kill the production server, run
./down.sh
```

### Local Run with uv

To run the server locally using uv without Docker, use:

```bash
./up.sh local
```

This will:
1. Create a virtual environment in the server directory if it doesn't exist
2. Install all dependencies using uv
3. Start the server on port 5000

After running this command, navigate to http://localhost:5000

### Command Line

You can also start the server via the command line. After installing the `overcooked_ai` via pip, you can start the server by typing

```
Expand Down
2 changes: 0 additions & 2 deletions src/overcooked_demo/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version : '3.7'

services:
app:
build:
Expand Down
Loading