Skip to content

Commit

Permalink
Move all downloaded models to one folder for caching
Browse files Browse the repository at this point in the history
- adds cache to github workflow
  • Loading branch information
Acly committed Dec 5, 2023
1 parent 80c8473 commit bd2000a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run installer and basic tests (no diffusion)
name: Lint, Typecheck and Test
on: [push, pull_request]

jobs:
Expand Down Expand Up @@ -30,8 +30,13 @@ jobs:
cache: 'pip'
- name: Dependencies
run: pip install -r requirements.txt
- name: Cache models
uses: actions/cache@v3
with:
path: script/docker/downloads
key: models-v1
- name: Download models
run: python scripts/download_models.py --minimal script/docker
run: python scripts/download_models.py --minimal script/docker/downloads
- name: Test installer
run: python -m pytest tests/test_server.py -vs --test-install
- name: Test
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
.server
.vscode
__pycache__
scripts/docker/models
scripts/docker/custom_nodes/*
scripts/docker/downloads
tests/.results
settings.json
ai_diffusion/styles/*
Expand Down
10 changes: 6 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The easiest way to run a development version of the plugin is to use symlinks:

### Code formatting

The codebase uses [black](https://github.com/psf/black) for formatting. The project root contains a `pyproject.toml` to configure the line length, it should be picked up automatically.
The codebase uses [black](https://github.com/psf/black) for formatting. You can check locally by running `black` in the repository root, or use an IDE integration.

### Code style

Expand All @@ -37,7 +37,9 @@ Code style follows the official Python recommendations. Only exception: no `ALL_

Type annotations should be used where types can't be inferred. Basic type checks are enabled for the project and should not report errors.

The `Krita` module is special in that it is usually only available when running inside Krita. To make type checking work, include `scripts/typeshed` in your `PYTHONPATH`.
The `Krita` module is special in that it is usually only available when running inside Krita. To make type checking work an interface file is located in `scripts/typeshed`.

You can run `pyright` from the repository root to perform type checks on the entire codebase. This is also done by the CI.

Configuration for VSCode with Pylance (.vscode/settings.json):
```
Expand Down Expand Up @@ -77,9 +79,9 @@ Everything else has tests. Mostly. If effort is reasonable, tests are expected.

Testing changes to the installer is annoying because of the file sizes involved. There are some things that help. You can preload model files with the following script:
```
python scripts/download_models.py --minimal scripts/docker
python scripts/download_models.py --minimal scripts/docker/downloads
```
This will download the minimum required models and store them in `scripts/docker` (used as default location because that way the docker build script can use them too).
This will download the minimum required models and store them in `scripts/docker/downloads` (used as default location because that way the docker build script can use them too).

The following command does some automated testing for installation and upgrade. It starts a local file server which pulls preloaded models, so it's reasonably fast and doesn't download the entire internet.
```
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# This file is for development and running tests.
# The plugin itself will run inside Krita's embedded Python, and only has access to the Python standard library and Qt5.

# Development
black

# Testing
aiohttp
markdown
numpy
Expand Down
4 changes: 2 additions & 2 deletions scripts/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ RUN git clone https://github.com/ltdrdata/ComfyUI-Manager.git /ComfyUI/custom_no

# Copy models
RUN mkdir -p models
COPY models/ /models/
COPY custom_nodes/ComfyUI_IPAdapter_plus/models/* /ComfyUI/custom_nodes/ComfyUI_IPAdapter_plus/models/
COPY downloads/models/ /models/
COPY downloads/custom_nodes/ComfyUI_IPAdapter_plus/models/* /ComfyUI/custom_nodes/ComfyUI_IPAdapter_plus/models/
COPY extra_model_paths.yaml /ComfyUI/

# Install Jupyter
Expand Down
4 changes: 2 additions & 2 deletions scripts/file_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Simple HTTP server for testing the installation process.
1) Run the docker.py script to download all required models.
1) Run the download_models.py script to download all required models.
2) Run this script to serve the model files on localhost.
3) Set environment variable HOSTMAP=1 to replace all huggingface / civitai urls.
"""
Expand All @@ -12,7 +12,7 @@
sys.path.append(str(Path(__file__).parent.parent))
from ai_diffusion import resources

dir = Path(__file__).parent / "docker"
dir = Path(__file__).parent / "docker" / "downloads"


def url_strip(url: str):
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ def local_download_server():
port = 51222

with subprocess.Popen([sys.executable, str(script), str(port)]) as proc:
assert proc.poll() is None
network.HOSTMAP = network.HOSTMAP_LOCAL
yield f"http://localhost:{port}"
network.HOSTMAP = {}

proc.kill()
proc.terminate()
proc.wait()
18 changes: 11 additions & 7 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_install_and_run(qtapp, pytestconfig, local_download_server):
"""Test installing and running ComfyUI server from scratch.
* Takes a while, only runs with --test-install
* Starts and downloads from local file server instead of huggingface/civitai
* Required to run `scripts/download_models.py -m scripts/docker` to download models once
* Required to run `scripts/download_models.py -m scripts/docker/downloads` to download models once
* Remove `local_download_server` fixture to download from original urls
* Also tests upgrading server from "previous" version
* In this case it's the same version, but it removes & re-installs anyway
Expand All @@ -62,16 +62,20 @@ def test_install_and_run(qtapp, pytestconfig, local_download_server):
server.backend = ServerBackend.cpu
assert server.state in [ServerState.not_installed, ServerState.missing_resources]

last_stage = ""

def handle_progress(report: InstallationProgress):
nonlocal last_stage
assert (
report.progress is None
or report.progress.value == -1
or report.progress.value >= 0
and report.progress.value <= 1
)
assert report.stage != ""
if report.progress is None:
print(report.stage, report.message)
if report.progress is None and report.stage != last_stage:
last_stage = report.stage
print(report.stage)

async def main():
await server.install(handle_progress)
Expand All @@ -81,9 +85,9 @@ async def main():
await server.download(workload_sd15, handle_progress)
assert server.state is ServerState.stopped and server.version == resources.version

url = await server.start()
url = await server.start(port=8191)
assert server.state is ServerState.running
assert url == "127.0.0.1:8188"
assert url == "127.0.0.1:8191"

await server.stop()
assert server.state is ServerState.stopped
Expand Down Expand Up @@ -112,9 +116,9 @@ def test_run_external(qtapp, pytestconfig):
assert server.state in [ServerState.stopped, ServerState.missing_resources]

async def main():
url = await server.start()
url = await server.start(port=8192)
assert server.state is ServerState.running
assert url == "127.0.0.1:8188"
assert url == "127.0.0.1:8192"

await server.stop()
assert server.state is ServerState.stopped
Expand Down

0 comments on commit bd2000a

Please sign in to comment.