Skip to content

Commit

Permalink
Set up Python packaging (#17)
Browse files Browse the repository at this point in the history
* Sort .gitignore; add dist and *.py[cod]

* Use pyproject.toml + Hatch instead of setup.py

Sibling of Stability-AI/stablediffusion#269

* Add packaging documentation
  • Loading branch information
akx authored Jul 18, 2023
1 parent 5c10dee commit e5dc966
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 18 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
*.egg-info
*.py[cod]
.pt13
.pt2
.pt2_2
.pt13
*.egg-info
build
/checkpoints
/dist
/outputs
/checkpoints
build
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ pip3 install wheel
pip3 install -r requirements_pt2.txt
```

## Packaging

This repository uses PEP 517 compliant packaging using [Hatch](https://hatch.pypa.io/latest/).

To build a distributable wheel, install `hatch` and run `hatch build`
(specifying `-t wheel` will skip building a sdist, which is not necessary).

```
pip install hatch
hatch build -t wheel
```

You will find the built package in `dist/`. You can install the wheel with `pip install dist/*.whl`.

Note that the package does **not** currently specify dependencies; you will need to install the required packages,
depending on your use case and PyTorch version, manually.

## Inference:

We provide a [streamlit](https://streamlit.io/) demo for text-to-image and image-to-image sampling in `scripts/demo/sampling.py`. The following models are currently supported:
Expand Down
34 changes: 34 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "sgm"
dynamic = ["version"]
description = "Stability Generative Models"
readme = "README.md"
license-files = { paths = ["LICENSE"] }
requires-python = ">=3.8"

[project.urls]
Homepage = "https://github.com/Stability-AI/generative-models"

[tool.hatch.version]
path = "sgm/__init__.py"

[tool.hatch.build]
# This needs to be explicitly set so the configuration files
# grafted into the `sgm` directory get included in the wheel's
# RECORD file.
include = [
"sgm",
]
# The force-include configurations below make Hatch copy
# the configs/ directory (containing the various YAML files required
# to generatively model) into the source distribution and the wheel.

[tool.hatch.build.targets.sdist.force-include]
"./configs" = "sgm/configs"

[tool.hatch.build.targets.wheel.force-include]
"./configs" = "sgm/configs"
13 changes: 0 additions & 13 deletions setup.py

This file was deleted.

4 changes: 3 additions & 1 deletion sgm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .data import StableDataModuleFromConfig
from .models import AutoencodingEngine, DiffusionEngine
from .util import instantiate_from_config
from .util import instantiate_from_config, get_configs_path

__version__ = "0.0.1"
18 changes: 18 additions & 0 deletions sgm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,21 @@ def load_model_from_config(config, ckpt, verbose=True, freeze=True):

model.eval()
return model


def get_configs_path() -> str:
"""
Get the `configs` directory.
For a working copy, this is the one in the root of the repository,
but for an installed copy, it's in the `sgm` package (see pyproject.toml).
"""
this_dir = os.path.dirname(__file__)
candidates = (
os.path.join(this_dir, "configs"),
os.path.join(this_dir, "..", "configs"),
)
for candidate in candidates:
candidate = os.path.abspath(candidate)
if os.path.isdir(candidate):
return candidate
raise FileNotFoundError(f"Could not find SGM configs in {candidates}")

0 comments on commit e5dc966

Please sign in to comment.