Skip to content

Commit 4b1284c

Browse files
akxLinearFalcon
authored andcommitted
Set up Python packaging (Stability-AI#17)
* Sort .gitignore; add dist and *.py[cod] * Use pyproject.toml + Hatch instead of setup.py Sibling of Stability-AI/stablediffusion#269 * Add packaging documentation
1 parent 3a4e63e commit 4b1284c

File tree

6 files changed

+78
-18
lines changed

6 files changed

+78
-18
lines changed

Diff for: .gitignore

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
*.egg-info
2+
*.py[cod]
3+
.pt13
14
.pt2
25
.pt2_2
3-
.pt13
4-
*.egg-info
5-
build
6+
/checkpoints
7+
/dist
68
/outputs
7-
/checkpoints
9+
build

Diff for: README.md

+17
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ pip3 install wheel
7676
pip3 install -r requirements_pt2.txt
7777
```
7878

79+
## Packaging
80+
81+
This repository uses PEP 517 compliant packaging using [Hatch](https://hatch.pypa.io/latest/).
82+
83+
To build a distributable wheel, install `hatch` and run `hatch build`
84+
(specifying `-t wheel` will skip building a sdist, which is not necessary).
85+
86+
```
87+
pip install hatch
88+
hatch build -t wheel
89+
```
90+
91+
You will find the built package in `dist/`. You can install the wheel with `pip install dist/*.whl`.
92+
93+
Note that the package does **not** currently specify dependencies; you will need to install the required packages,
94+
depending on your use case and PyTorch version, manually.
95+
7996
## Inference:
8097

8198
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:

Diff for: pyproject.toml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "sgm"
7+
dynamic = ["version"]
8+
description = "Stability Generative Models"
9+
readme = "README.md"
10+
license-files = { paths = ["LICENSE"] }
11+
requires-python = ">=3.8"
12+
13+
[project.urls]
14+
Homepage = "https://github.com/Stability-AI/generative-models"
15+
16+
[tool.hatch.version]
17+
path = "sgm/__init__.py"
18+
19+
[tool.hatch.build]
20+
# This needs to be explicitly set so the configuration files
21+
# grafted into the `sgm` directory get included in the wheel's
22+
# RECORD file.
23+
include = [
24+
"sgm",
25+
]
26+
# The force-include configurations below make Hatch copy
27+
# the configs/ directory (containing the various YAML files required
28+
# to generatively model) into the source distribution and the wheel.
29+
30+
[tool.hatch.build.targets.sdist.force-include]
31+
"./configs" = "sgm/configs"
32+
33+
[tool.hatch.build.targets.wheel.force-include]
34+
"./configs" = "sgm/configs"

Diff for: setup.py

-13
This file was deleted.

Diff for: sgm/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
from .data import StableDataModuleFromConfig
22
from .models import AutoencodingEngine, DiffusionEngine
3-
from .util import instantiate_from_config
3+
from .util import instantiate_from_config, get_configs_path
4+
5+
__version__ = "0.0.1"

Diff for: sgm/util.py

+18
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,21 @@ def load_model_from_config(config, ckpt, verbose=True, freeze=True):
229229

230230
model.eval()
231231
return model
232+
233+
234+
def get_configs_path() -> str:
235+
"""
236+
Get the `configs` directory.
237+
For a working copy, this is the one in the root of the repository,
238+
but for an installed copy, it's in the `sgm` package (see pyproject.toml).
239+
"""
240+
this_dir = os.path.dirname(__file__)
241+
candidates = (
242+
os.path.join(this_dir, "configs"),
243+
os.path.join(this_dir, "..", "configs"),
244+
)
245+
for candidate in candidates:
246+
candidate = os.path.abspath(candidate)
247+
if os.path.isdir(candidate):
248+
return candidate
249+
raise FileNotFoundError(f"Could not find SGM configs in {candidates}")

0 commit comments

Comments
 (0)