Skip to content

Commit

Permalink
Feature/GitHub action Python formatting/linting #3 (#14)
Browse files Browse the repository at this point in the history
* linting stage for the github action workflow

* linting stage for the github action workflow

* more complete gitignore

* formatting all python code with black

* restricting to ubuntu

* black and isort ran locally

* fixing compatibility in examples

* format/linting before pushing

* removed dead code

* sorting imports on 007

* removing docformatter

* removing pycodestyle

* adding the args options to the github action

* removed py3.10 as an option

* disabling docformatter

* disabling pycodestyle

* trying again to disable pycodestyle

* trying something new

* Revert "trying something new"

This reverts commit 6e0f6c3.

* getting rid of the magical action

* folding the actions together

* allowing end of file to be a blank line

* end of file blank line rule for flake8

* reverted python files to the upstream main state

* ran black locally

* ran isort locally

* ran autoflake locally

* reformatting via black

* missing import for load_model

* flake8 will ignore the line length for now

* f-string unneeded

* reverted changes to the init files in sdkit module

* installed dev requirements

* running isort
  • Loading branch information
thoroc authored Feb 15, 2023
1 parent cb49807 commit f093056
Show file tree
Hide file tree
Showing 61 changed files with 1,492 additions and 836 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 120
extend-ignore = E203, E402, E722, W391
per-file-ignores = __init__.py:F401
59 changes: 59 additions & 0 deletions .github/workflows/py-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Python Linting

on:
push:
branches:
- "!main"
pull_request:
branches:
- main

jobs:
linting:
name: Python Linting
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
# apparently only ubuntu latest is available to run a contenairised job
os: [ubuntu-latest]
python-version: ["3.9"]
architecture: ["x64"]

steps:
#----------------------------------------------
# check-out repo
#----------------------------------------------
- name: Check out code
uses: actions/checkout@v3

#----------------------------------------------
# set-up python
#----------------------------------------------
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

#----------------------------------------------
# Formatting & Linting
#----------------------------------------------
- name: Format and lint the code
run: |
pip install black isort autoflake flake8
black tests scripts sdkit examples --line-length 120 --include="\.py"
isort tests scripts sdkit examples --profile black
autoflake --in-place --remove-all-unused-imports --remove-unused-variables --recursive .
flake8 tests scripts sdkit examples --max-line-length 120 --extend-ignore=E203,E402,E501,E722,W391 --per-file-ignores=__init__.py:F401
#----------------------------------------------
# Committing all changes
#----------------------------------------------
- name: Commit changes
uses: EndBug/add-and-commit@v4
with:
author_name: ${{ github.actor }}
author_email: ${{ github.actor }}@users.noreply.github.com
message: "Code formatted and linted"
add: "."
branch: ${{ github.ref }}
163 changes: 160 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,160 @@
__pycache__
sdkit.egg-info
dist
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# 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.*
.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/

# Cython debug symbols
cython_debug/

# 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/
12 changes: 6 additions & 6 deletions examples/001-generate-model_file_on_disk.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import sdkit
from sdkit.models import load_model
from sdkit.generate import generate_images
from sdkit.utils import save_images, log
from sdkit.models import load_model
from sdkit.utils import log, save_images

context = sdkit.Context()

# set the path to the model file on the disk (.ckpt or .safetensors file)
context.model_paths['stable-diffusion'] = 'D:\\path\\to\\512-base-ema.ckpt'
load_model(context, 'stable-diffusion')
context.model_paths["stable-diffusion"] = "D:\\path\\to\\512-base-ema.ckpt"
load_model(context, "stable-diffusion")

# generate the image
images = generate_images(context, prompt='Photograph of an astronaut riding a horse', seed=42, width=512, height=512)
images = generate_images(context, prompt="Photograph of an astronaut riding a horse", seed=42, width=512, height=512)

# save the image
save_images(images, dir_path='.')
save_images(images, dir_path=".")

log.info("Generated images!")
16 changes: 9 additions & 7 deletions examples/002-generate-download_known_model.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import sdkit
from sdkit.models import load_model, download_model, resolve_downloaded_model_path
from sdkit.generate import generate_images
from sdkit.utils import save_images, log
from sdkit.models import download_model, load_model, resolve_downloaded_model_path
from sdkit.utils import log, save_images

context = sdkit.Context()

# download the model (skips if already downloaded, resumes if downloaded partially)
download_model(model_type='stable-diffusion', model_id='1.5-pruned-emaonly')
download_model(model_type="stable-diffusion", model_id="1.5-pruned-emaonly")

# set the path to the auto-downloaded model
context.model_paths['stable-diffusion'] = resolve_downloaded_model_path(context, 'stable-diffusion', '1.5-pruned-emaonly')
load_model(context, 'stable-diffusion')
context.model_paths["stable-diffusion"] = resolve_downloaded_model_path(
context, "stable-diffusion", "1.5-pruned-emaonly"
)
load_model(context, "stable-diffusion")

# generate the image
images = generate_images(context, prompt='Photograph of an astronaut riding a horse', seed=42, width=512, height=512)
images = generate_images(context, prompt="Photograph of an astronaut riding a horse", seed=42, width=512, height=512)

# save the image
save_images(images, dir_path='D:\\path\\to\\images\\directory')
save_images(images, dir_path="D:\\path\\to\\images\\directory")

log.info("Generated images!")
10 changes: 6 additions & 4 deletions examples/002b-generate-download_multiple_known_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from sdkit.models import download_models

# download all three models (skips if already downloaded, resumes if downloaded partially)
download_models(models={
'stable-diffusion': ['1.4', '1.5-pruned-emaonly'],
'gfpgan': '1.3',
})
download_models(
models={
"stable-diffusion": ["1.4", "1.5-pruned-emaonly"],
"gfpgan": "1.3",
}
)
14 changes: 7 additions & 7 deletions examples/003-generate-custom_model.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import sdkit
from sdkit.models import load_model
from sdkit.generate import generate_images
from sdkit.utils import save_images, log
from sdkit.models import load_model
from sdkit.utils import log, save_images

context = sdkit.Context()

# set the path to the custom model file on the disk
context.model_paths['stable-diffusion'] = 'D:\\path\\to\\cmodelUpgradeStableD.safetensors'
context.model_configs['stable-diffusion'] = 'D:\\path\\to\\Cmodelsafetensor.yaml'
context.model_paths["stable-diffusion"] = "D:\\path\\to\\cmodelUpgradeStableD.safetensors"
context.model_configs["stable-diffusion"] = "D:\\path\\to\\Cmodelsafetensor.yaml"
# the yaml config file is required if it's an unknown model to use.
# it is not necessary for known models present in the models_db.

load_model(context, 'stable-diffusion')
load_model(context, "stable-diffusion")

# generate the image
images = generate_images(context, prompt='Photograph of an astronaut riding a horse', seed=42, width=512, height=512)
images = generate_images(context, prompt="Photograph of an astronaut riding a horse", seed=42, width=512, height=512)

# save the image
save_images(images, dir_path='D:\\path\\to\\images\\directory')
save_images(images, dir_path="D:\\path\\to\\images\\directory")

log.info("Generated images!")
16 changes: 8 additions & 8 deletions examples/004-generate-custom_vae.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import sdkit
from sdkit.models import load_model
from sdkit.generate import generate_images
from sdkit.utils import save_images, log
from sdkit.models import load_model
from sdkit.utils import log, save_images

context = sdkit.Context()

# set the path to the model and VAE file on the disk
context.model_paths['stable-diffusion'] = 'D:\\path\\to\\model.ckpt'
context.model_paths['vae'] = 'D:\\path\\to\\vae.ckpt'
load_model(context, 'stable-diffusion')
load_model(context, 'vae')
context.model_paths["stable-diffusion"] = "D:\\path\\to\\model.ckpt"
context.model_paths["vae"] = "D:\\path\\to\\vae.ckpt"
load_model(context, "stable-diffusion")
load_model(context, "vae")

# generate the image
images = generate_images(context, prompt="Photograph of an astronaut riding a horse", seed=42, width=512, height=512)

# save the image
save_images(images, dir_path='D:\\path\\to\\images\\directory')
save_images(images, dir_path="D:\\path\\to\\images\\directory")

log.info('Generated images with a custom VAE!')
log.info("Generated images with a custom VAE!")
25 changes: 16 additions & 9 deletions examples/005-generate-custom_hypernetwork.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import sdkit
from sdkit.models import load_model
from sdkit.generate import generate_images
from sdkit.utils import save_images, log
from sdkit.models import load_model
from sdkit.utils import log, save_images

context = sdkit.Context()

# set the path to the model and hypernetwork file on the disk
context.model_paths['stable-diffusion'] = 'D:\\path\\to\\model.ckpt'
context.model_paths['hypernetwork'] = 'D:\\path\\to\\hypernetwork.pt'
load_model(context, 'stable-diffusion')
load_model(context, 'hypernetwork')
context.model_paths["stable-diffusion"] = "D:\\path\\to\\model.ckpt"
context.model_paths["hypernetwork"] = "D:\\path\\to\\hypernetwork.pt"
load_model(context, "stable-diffusion")
load_model(context, "hypernetwork")

# generate the image, hypernetwork_strength at 0.3
images = generate_images(context, prompt="Photograph of an astronaut riding a horse", seed=42, width=512, height=512, hypernetwork_strength=0.3)
images = generate_images(
context,
prompt="Photograph of an astronaut riding a horse",
seed=42,
width=512,
height=512,
hypernetwork_strength=0.3,
)

# save the image
save_images(images, dir_path='D:\\path\\to\\images\\directory')
save_images(images, dir_path="D:\\path\\to\\images\\directory")

log.info('Generated images with a custom VAE!')
log.info("Generated images with a custom VAE!")
Loading

0 comments on commit f093056

Please sign in to comment.