Skip to content

Commit

Permalink
Merge pull request #1 from yaacov/main
Browse files Browse the repository at this point in the history
rename files
  • Loading branch information
yaacov committed Jan 9, 2024
2 parents ecab34f + 2184585 commit f51c62d
Show file tree
Hide file tree
Showing 28 changed files with 365 additions and 210 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = test_*.py
8 changes: 8 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[flake8]
show_source = True
statistics = True

# E501: line to long.
# E203: whitespace before ':' to accept black code style
# W503: line break before binary operator
ignore = E501,E203,W503
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI
on:
- push
- pull_request
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: |
3.9
3.12
- name: Install dependencies
run: pip install --no-cache-dir -r requirements.txt

- name: Install dev dependencies
run: pip install --no-cache-dir -r requirements-dev.txt

- name: Lint
run: make lint

- name: Run tests
run: make test

- name: Run code quality test
run: make code-quality
170 changes: 15 additions & 155 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,160 +1,20 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# Python
__pycache__
*.pyc
.pytest_cache

# C extensions
*.so
# Editors
.idea
.vscode

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
# Coverage
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/
htmlcov/

# Cython debug symbols
cython_debug/
rose_project.egg-info
dist

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Classroom setup
classroom/credentials.json
classroom/*.csv
classroom/token.pickle
31 changes: 30 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,37 @@ COPY . /ml

# Install the Python dependencies
RUN pip install --upgrade pip
RUN pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
RUN pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

ENTRYPOINT ["python", "main.py", "--listen", "0.0.0.0", "--driver", "/ml/driver.py"]
CMD ["--port", "8081"]

# --- Build Image ---
FROM registry.access.redhat.com/ubi9/python-39 AS build

WORKDIR /build

# Copy only the requirements file and install the Python dependencies
COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

# --- Runtime Image ---
FROM build

WORKDIR /app
COPY . /app

# Add the rose client package to the python path
ENV PYTHONPATH "${PYTHONPATH}:/app"

# Default values for environment variables
ENV DRIVER ./mydriver.py
ENV PORT 8081

# Inform Docker that the container listens on port 3000
EXPOSE 8081

# Define the command to run your app using CMD which defines your runtime
CMD ["sh", "-c", "python rose/main.py --listen 0.0.0.0 --driver ${DRIVER} --port ${PORT}"]
46 changes: 39 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
# Project variables
.PHONY: lint test lint-fix code-quality run build-image run-image clean

SRC_DIR = .

IMAGE_NAME ?= quay.io/rose/rose-game-ai-reference
DRIVER_PATH ?= mydriver.py
PORT ?= 8081

# By default, run both linting and tests
all: lint test

lint:
@echo "Running flake8 linting..."
flake8 --show-source --statistics .
black --check --diff .

lint-fix:
@echo "Running lint fixing..."
@black --verbose --color .

code-quality:
@echo "Running static code quality checks..."
radon cc .
radon mi .

test:
@echo "Running unittests..."
pytest

run:
@echo "Running driver logic server ..."
PYTHONPATH=$(SRC_DIR):$$PYTHONPATH python rose/main.py --port $(PORT) --driver $(DRIVER_PATH)

build-image:
@echo "Building Docker image..."
@echo "Building container image ..."
podman build -t $(IMAGE_NAME) .

run-image:
@echo "Running container image ..."
podman run --rm \
--network host \
-it $(IMAGE_NAME) \
--port $(PORT) \
--driver /ml/driver.py
podman run --rm -it --network host -e PORT=$(PORT) $(IMAGE_NAME)

clean:
-rm -rf .coverage
-rm -rf htmlcov
-rm -rf .pytest_cache
-find . -name '*.pyc' -exec rm {} \;
-find . -name '__pycache__' -exec rmdir {} \;
4 changes: 4 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# conftest.py
import sys

sys.path.append(".")
3 changes: 1 addition & 2 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
Trained models are expected to be in the checkpoints directory.
"""

import os

try:
import torch
except ImportError:
Expand Down Expand Up @@ -75,6 +73,7 @@ class DriverModel(nn.Module):
Note:
- The model expects a flattened version of the 3x4x7 input tensor, which should be reshaped to (batch_size, 84) before being passed to the model.
"""

def __init__(self):
super(DriverModel, self).__init__()

Expand Down
6 changes: 1 addition & 5 deletions driver.py → mydriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
"""

import os
import sys

# Get the directory of the current script
script_directory = os.path.dirname(os.path.abspath(__file__))

# Add the script directory to the system path
sys.path.append(script_directory)

# Try to import pytorch
try:
import torch
Expand All @@ -26,7 +22,7 @@
print(" see: https://pytorch.org/get-started/locally/")
exit()

from model import DriverModel, outputs_to_action, view_to_inputs
from model import DriverModel, outputs_to_action, view_to_inputs # noqa: E402

"""
Torch Car
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = -vv -rxs --timeout 10 --cov .
10 changes: 10 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# requirements.txt

flake8>=3.9.0
coverage>=7.3.0
radon>=6.0.0
black>=23.7.0
pytest
pytest-check-links
pytest-coverage
pytest-timeout
Loading

0 comments on commit f51c62d

Please sign in to comment.