ehristoforu commited on
Commit
0163a2c
·
verified ·
1 Parent(s): 436a6ff

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +6 -0
  2. adetailer/.github/ISSUE_TEMPLATE/bug_report.yaml +53 -0
  3. adetailer/.github/ISSUE_TEMPLATE/feature_request.yaml +24 -0
  4. adetailer/.github/ISSUE_TEMPLATE/question.yaml +10 -0
  5. adetailer/.github/workflows/stale.yml +13 -0
  6. adetailer/.gitignore +196 -0
  7. adetailer/.pre-commit-config.yaml +20 -0
  8. adetailer/.vscode/extensions.json +8 -0
  9. adetailer/.vscode/settings.json +8 -0
  10. adetailer/CHANGELOG.md +377 -0
  11. adetailer/LICENSE.md +662 -0
  12. adetailer/README.md +97 -0
  13. adetailer/Taskfile.yml +27 -0
  14. adetailer/adetailer/__init__.py +18 -0
  15. adetailer/adetailer/__version__.py +1 -0
  16. adetailer/adetailer/args.py +236 -0
  17. adetailer/adetailer/common.py +132 -0
  18. adetailer/adetailer/mask.py +256 -0
  19. adetailer/adetailer/mediapipe.py +168 -0
  20. adetailer/adetailer/traceback.py +161 -0
  21. adetailer/adetailer/ui.py +640 -0
  22. adetailer/adetailer/ultralytics.py +51 -0
  23. adetailer/controlnet_ext/__init__.py +7 -0
  24. adetailer/controlnet_ext/controlnet_ext.py +167 -0
  25. adetailer/controlnet_ext/restore.py +43 -0
  26. adetailer/install.py +76 -0
  27. adetailer/preload.py +9 -0
  28. adetailer/pyproject.toml +42 -0
  29. adetailer/scripts/!adetailer.py +1000 -0
  30. kohya-sd-scripts-webui/.gitignore +9 -0
  31. kohya-sd-scripts-webui/README.md +22 -0
  32. kohya-sd-scripts-webui/built-in-presets.json +126 -0
  33. kohya-sd-scripts-webui/install.py +116 -0
  34. kohya-sd-scripts-webui/kohya-sd-scripts-webui-colab.ipynb +157 -0
  35. kohya-sd-scripts-webui/launch.py +79 -0
  36. kohya-sd-scripts-webui/main.py +14 -0
  37. kohya-sd-scripts-webui/screenshots/installation-extension.png +0 -0
  38. kohya-sd-scripts-webui/screenshots/webui-01.png +0 -0
  39. kohya-sd-scripts-webui/script.js +87 -0
  40. kohya-sd-scripts-webui/scripts/main.py +98 -0
  41. kohya-sd-scripts-webui/scripts/ngrok.py +28 -0
  42. kohya-sd-scripts-webui/scripts/presets.py +179 -0
  43. kohya-sd-scripts-webui/scripts/runner.py +113 -0
  44. kohya-sd-scripts-webui/scripts/shared.py +32 -0
  45. kohya-sd-scripts-webui/scripts/tabs/networks/check_lora_weights.py +23 -0
  46. kohya-sd-scripts-webui/scripts/tabs/networks/extract_lora_from_models.py +25 -0
  47. kohya-sd-scripts-webui/scripts/tabs/networks/lora_interrogator.py +23 -0
  48. kohya-sd-scripts-webui/scripts/tabs/networks/merge_lora.py +23 -0
  49. kohya-sd-scripts-webui/scripts/tabs/networks/resize_lora.py +23 -0
  50. kohya-sd-scripts-webui/scripts/tabs/networks/svd_merge_lora.py +23 -0
.gitattributes ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ sd-civitai-browser-plus/aria2/lin/aria2 filter=lfs diff=lfs merge=lfs -text
2
+ sd-civitai-browser-plus/aria2/win/aria2.exe filter=lfs diff=lfs merge=lfs -text
3
+ sd-webui-controlnet/annotator/oneformer/oneformer/data/bpe_simple_vocab_16e6.txt.gz filter=lfs diff=lfs merge=lfs -text
4
+ sd-webui-inpaint-anything/images/inpaint_anything_ui_image_1.png filter=lfs diff=lfs merge=lfs -text
5
+ stable-diffusion-webui-aesthetic-gradients/ss.png filter=lfs diff=lfs merge=lfs -text
6
+ stable-diffusion-webui-rembg/preview.png filter=lfs diff=lfs merge=lfs -text
adetailer/.github/ISSUE_TEMPLATE/bug_report.yaml ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Bug report
2
+ description: Create a report
3
+ title: "[Bug]: "
4
+ labels:
5
+ - bug
6
+
7
+ body:
8
+ - type: textarea
9
+ attributes:
10
+ label: Describe the bug
11
+ description: A clear and concise description of what the bug is.
12
+ placeholder: |
13
+ Any language accepted
14
+ 아무 언어 사용가능
15
+ すべての言語に対応
16
+ 接受所有语言
17
+ Se aceptan todos los idiomas
18
+ Alle Sprachen werden akzeptiert
19
+ Toutes les langues sont acceptées
20
+ Принимаются все языки
21
+
22
+ - type: textarea
23
+ attributes:
24
+ label: Screenshots
25
+ description: Screenshots related to the issue.
26
+
27
+ - type: textarea
28
+ attributes:
29
+ label: Console logs, from start to end.
30
+ description: |
31
+ The full console log of your terminal.
32
+ placeholder: |
33
+ Python ...
34
+ Version: ...
35
+ Commit hash: ...
36
+ Installing requirements
37
+ ...
38
+
39
+ Launching Web UI with arguments: ...
40
+ [-] ADetailer initialized. version: ...
41
+ ...
42
+ ...
43
+
44
+ Traceback (most recent call last):
45
+ ...
46
+ ...
47
+ render: Shell
48
+ validations:
49
+ required: true
50
+
51
+ - type: textarea
52
+ attributes:
53
+ label: List of installed extensions
adetailer/.github/ISSUE_TEMPLATE/feature_request.yaml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Feature request
2
+ description: Suggest an idea for this project
3
+ title: "[Feature Request]: "
4
+
5
+ body:
6
+ - type: textarea
7
+ attributes:
8
+ label: Is your feature request related to a problem? Please describe.
9
+ description: A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
10
+
11
+ - type: textarea
12
+ attributes:
13
+ label: Describe the solution you'd like
14
+ description: A clear and concise description of what you want to happen.
15
+
16
+ - type: textarea
17
+ attributes:
18
+ label: Describe alternatives you've considered
19
+ description: A clear and concise description of any alternative solutions or features you've considered.
20
+
21
+ - type: textarea
22
+ attributes:
23
+ label: Additional context
24
+ description: Add any other context or screenshots about the feature request here.
adetailer/.github/ISSUE_TEMPLATE/question.yaml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Question
2
+ description: Write a question
3
+ labels:
4
+ - question
5
+
6
+ body:
7
+ - type: textarea
8
+ attributes:
9
+ label: Question
10
+ description: Please do not write bug reports or feature requests here.
adetailer/.github/workflows/stale.yml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: 'Close stale issues and PRs'
2
+ on:
3
+ schedule:
4
+ - cron: '30 1 * * *'
5
+
6
+ jobs:
7
+ stale:
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - uses: actions/stale@v8
11
+ with:
12
+ days-before-stale: 23
13
+ days-before-close: 3
adetailer/.gitignore ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
2
+ # Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode
3
+
4
+ ### Python ###
5
+ # Byte-compiled / optimized / DLL files
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .Python
15
+ build/
16
+ develop-eggs/
17
+ dist/
18
+ downloads/
19
+ eggs/
20
+ .eggs/
21
+ lib/
22
+ lib64/
23
+ parts/
24
+ sdist/
25
+ var/
26
+ wheels/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py,cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+ cover/
57
+
58
+ # Translations
59
+ *.mo
60
+ *.pot
61
+
62
+ # Django stuff:
63
+ *.log
64
+ local_settings.py
65
+ db.sqlite3
66
+ db.sqlite3-journal
67
+
68
+ # Flask stuff:
69
+ instance/
70
+ .webassets-cache
71
+
72
+ # Scrapy stuff:
73
+ .scrapy
74
+
75
+ # Sphinx documentation
76
+ docs/_build/
77
+
78
+ # PyBuilder
79
+ .pybuilder/
80
+ target/
81
+
82
+ # Jupyter Notebook
83
+ .ipynb_checkpoints
84
+
85
+ # IPython
86
+ profile_default/
87
+ ipython_config.py
88
+
89
+ # pyenv
90
+ # For a library or package, you might want to ignore these files since the code is
91
+ # intended to run in multiple environments; otherwise, check them in:
92
+ # .python-version
93
+
94
+ # pipenv
95
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
97
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
98
+ # install all needed dependencies.
99
+ #Pipfile.lock
100
+
101
+ # poetry
102
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
103
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
104
+ # commonly ignored for libraries.
105
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
106
+ #poetry.lock
107
+
108
+ # pdm
109
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
110
+ #pdm.lock
111
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
112
+ # in version control.
113
+ # https://pdm.fming.dev/#use-with-ide
114
+ .pdm.toml
115
+
116
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
117
+ __pypackages__/
118
+
119
+ # Celery stuff
120
+ celerybeat-schedule
121
+ celerybeat.pid
122
+
123
+ # SageMath parsed files
124
+ *.sage.py
125
+
126
+ # Environments
127
+ .env
128
+ .venv
129
+ env/
130
+ venv/
131
+ ENV/
132
+ env.bak/
133
+ venv.bak/
134
+
135
+ # Spyder project settings
136
+ .spyderproject
137
+ .spyproject
138
+
139
+ # Rope project settings
140
+ .ropeproject
141
+
142
+ # mkdocs documentation
143
+ /site
144
+
145
+ # mypy
146
+ .mypy_cache/
147
+ .dmypy.json
148
+ dmypy.json
149
+
150
+ # Pyre type checker
151
+ .pyre/
152
+
153
+ # pytype static type analyzer
154
+ .pytype/
155
+
156
+ # Cython debug symbols
157
+ cython_debug/
158
+
159
+ # PyCharm
160
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
163
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
164
+ #.idea/
165
+
166
+ ### Python Patch ###
167
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
168
+ poetry.toml
169
+
170
+ # ruff
171
+ .ruff_cache/
172
+
173
+ # LSP config files
174
+ pyrightconfig.json
175
+
176
+ ### VisualStudioCode ###
177
+ .vscode/*
178
+ !.vscode/settings.json
179
+ !.vscode/tasks.json
180
+ !.vscode/launch.json
181
+ !.vscode/extensions.json
182
+ !.vscode/*.code-snippets
183
+
184
+ # Local History for Visual Studio Code
185
+ .history/
186
+
187
+ # Built Visual Studio Code Extensions
188
+ *.vsix
189
+
190
+ ### VisualStudioCode Patch ###
191
+ # Ignore all local history of files
192
+ .history
193
+ .ionide
194
+
195
+ # End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
196
+ *.ipynb
adetailer/.pre-commit-config.yaml ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.5.0
4
+ hooks:
5
+ - id: check-ast
6
+ - id: trailing-whitespace
7
+ args: [--markdown-linebreak-ext=md]
8
+ - id: end-of-file-fixer
9
+ - id: mixed-line-ending
10
+
11
+ - repo: https://github.com/astral-sh/ruff-pre-commit
12
+ rev: v0.1.14
13
+ hooks:
14
+ - id: ruff
15
+ args: [--fix, --exit-non-zero-on-fix]
16
+
17
+ - repo: https://github.com/psf/black-pre-commit-mirror
18
+ rev: 23.12.1
19
+ hooks:
20
+ - id: black
adetailer/.vscode/extensions.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "recommendations": [
3
+ "ms-python.black-formatter",
4
+ "kevinrose.vsc-python-indent",
5
+ "charliermarsh.ruff",
6
+ "shardulm94.trailing-spaces"
7
+ ]
8
+ }
adetailer/.vscode/settings.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "explorer.fileNesting.enabled": true,
3
+ "explorer.fileNesting.patterns": {
4
+ "pyproject.toml": ".env, .gitignore, .pre-commit-config.yaml, Taskfile.yml",
5
+ "README.md": "LICENSE.md, CHANGELOG.md",
6
+ "install.py": "preload.py"
7
+ }
8
+ }
adetailer/CHANGELOG.md ADDED
@@ -0,0 +1,377 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Changelog
2
+
3
+ ## 2024-01-23
4
+
5
+ - v24.1.2
6
+ - controlnet 모델에 `Passthrough` 옵션 추가. 입력으로 들어온 컨트롤넷 옵션을 그대로 사용
7
+ - fastapi 엔드포인트 추가
8
+
9
+ ## 2024-01-10
10
+
11
+ - v24.1.1
12
+ - SDNext 호환 업데이트 (issue #466)
13
+ - 설정 값 state에 초기값 추가
14
+ - 위젯 값을 변경할 때마다 state도 변경되게 함 (기존에는 생성 버튼을 누를 때 적용되었음)
15
+ - `inpaint_depth_hand` 컨트롤넷 모델이 depth 모델로 인식되게 함 (issue #463)
16
+
17
+ ## 2024-01-04
18
+
19
+ - v24.1.0
20
+ - `depth_hand_refiner` ControlNet 추가 (PR #460)
21
+
22
+ ## 2023-12-30
23
+
24
+ - v23.12.0
25
+ - 파일을 인자로 추가하는 몇몇 스크립트에 대해 deepcopy의 에러를 피하기 위해 script_args 복사 방법을 변경함
26
+ - skip img2img 기능을 사용할 때 너비, 높이를 128로 고정하여 스킵 과정이 조금 더 나아짐
27
+ - img2img inpainting 모드에서 adetailer 자동 비활성화
28
+ - 처음 생성된 params.txt 파일을 항상 유지하도록 변경함
29
+
30
+ ## 2023-11-19
31
+
32
+ - v23.11.1
33
+ - 기본 스크립트 목록에 negpip 추가
34
+ - 기존에 설치한 사람에게 소급적용되지는 않음
35
+ - skip img2img 옵션이 2스텝 이상일 때, 제대로 적용되지 않는 문제 수정
36
+ - SD.Next에서 이미지가 np.ndarray로 입력되는 경우 수정
37
+ - 컨트롤넷 경로를 sys.path에 추가하여 --data-dir등을 지정한 경우에도 임포트 에러가 일어나지 않게 함.
38
+
39
+ ## 2023-10-30
40
+
41
+ - v23.11.0
42
+ - 이미지의 인덱스 계산방법 변경
43
+ - webui 1.1.0 미만에서 adetailer 실행 불가능하게 함
44
+ - 컨트롤넷 preprocessor 선택지 늘림
45
+ - 추가 yolo 모델 디렉터리를 설정할 수 있는 옵션 추가
46
+ - infotext에 `/`가 있는 항목이 exif에서 복원되지 않는 문제 수정
47
+ - 이전 버전에 생성된 이미지는 여전히 복원안됨
48
+ - 같은 탭에서 항상 같은 시드를 적용하게 하는 옵션 추가
49
+ - 컨트롤넷 1.1.411 (f2aafcf2beb99a03cbdf7db73852228ccd6bd1d6) 버전을 사용중일 경우,
50
+ webui 버전 1.6.0 미만에서 사용할 수 없다는 메세지 출력
51
+
52
+ ## 2023-10-15
53
+
54
+ - v23.10.1
55
+ - xyz grid에 prompt S/R 추가
56
+ - img2img에서 steps가 1일때 에러가 발생하는 샘플러의 처리를 위해 샘플러 이름도 변경하게 수정
57
+
58
+ ## 2023-10-07
59
+
60
+ - v23.10.0
61
+ - 허깅페이스 모델을 다운로드 실패했을 때, 계속 다운로드를 시도하지 않음
62
+ - img2img에서 img2img단계를 건너뛰는 기능 추가
63
+ - live preview에서 감지 단계를 보여줌 (PR #352)
64
+
65
+ ## 2023-09-20
66
+
67
+ - v23.9.3
68
+ - ultralytics 버전 8.0.181로 업데이트 (https://github.com/ultralytics/ultralytics/pull/4891)
69
+ - mediapipe와 ultralytics의 lazy import
70
+
71
+ ## 2023-09-10
72
+
73
+ - v23.9.2
74
+ - (실험적) VAE 선택 기능
75
+
76
+ ## 2023-09-01
77
+
78
+ - v23.9.1
79
+ - webui 1.6.0에 추가된 인자를 사용해서 생긴 하위 호환 문제 수정
80
+
81
+ ## 2023-08-31
82
+
83
+ - v23.9.0
84
+ - (실험적) 체크포인트 선택기능
85
+ - 버그가 있어 리프레시 버튼은 구현에서 빠짐
86
+ - 1.6.0 업데이트에 따라 img2img에서 사용불가능한 샘플러를 선택했을 때 더이상 Euler로 변경하지 않음
87
+ - 유효하지 않은 인자가 전달되었을 때, 에러를 일으키지 않고 대신 adetailer를 비활성화함
88
+
89
+
90
+ ## 2023-08-25
91
+
92
+ - v23.8.1
93
+ - xyz grid에서 model을 `None`으로 설정한 이후에 adetailer가 비활성화 되는 문제 수정
94
+ - skip을 눌렀을 때 진행을 멈춤
95
+ - `--medvram-sdxl`을 설정했을 때에도 cpu를 사용하게 함
96
+
97
+ ## 2023-08-14
98
+
99
+ - v23.8.0
100
+ - `[PROMPT]` 키워드 추가. `ad_prompt` 또는 `ad_negative_prompt`에 사용하면 입력 프롬프트로 대체됨 (PR #243)
101
+ - Only top k largest 옵션 추가 (PR #264)
102
+ - ultralytics 버전 업데이트
103
+
104
+
105
+ ## 2023-07-31
106
+
107
+ - v23.7.11
108
+ - separate clip skip 옵션 추가
109
+ - install requirements 정리 (ultralytics 새 버전, mediapipe~=3.20)
110
+
111
+ ## 2023-07-28
112
+
113
+ - v23.7.10
114
+ - ultralytics, mediapipe import문 정리
115
+ - traceback에서 컬러를 없앰 (api 때문), 라이브러리 버전도 보여주게 설정.
116
+ - huggingface_hub, pydantic을 install.py에서 없앰
117
+ - 안쓰는 컨트롤넷 관련 코드 삭제
118
+
119
+
120
+ ## 2023-07-23
121
+
122
+ - v23.7.9
123
+ - `ultralytics.utils` ModuleNotFoundError 해결 (https://github.com/ultralytics/ultralytics/issues/3856)
124
+ - `pydantic` 2.0 이상 버전 설치안되도록 함
125
+ - `controlnet_dir` cmd args 문제 수정 (PR #107)
126
+
127
+ ## 2023-07-20
128
+
129
+ - v23.7.8
130
+ - `paste_field_names` 추가했던 것을 되돌림
131
+
132
+ ## 2023-07-19
133
+
134
+ - v23.7.7
135
+ - 인페인팅 단계에서 별도의 샘플러를 선택할 수 있게 옵션을 추가함 (xyz그리드에도 추가)
136
+ - webui 1.0.0-pre 이하 버전에서 batch index 문제 수정
137
+ - 스크립트에 `paste_field_names`을 추가함. 사용되는지는 모르겠음
138
+
139
+ ## 2023-07-16
140
+
141
+ - v23.7.6
142
+ - `ultralytics 8.0.135`에 추가된 cpuinfo 기능을 위해 `py-cpuinfo`를 미리 설치하게 함. (미리 설치 안하면 cpu나 mps사용할 때 재시작해야함)
143
+ - init_image가 RGB 모드가 아닐 때 RGB로 변경.
144
+
145
+ ## 2023-07-07
146
+
147
+ - v23.7.4
148
+ - batch count > 1일때 프롬프트의 인덱스 문제 수정
149
+
150
+ - v23.7.5
151
+ - i2i의 `cached_uc`와 `cached_c`가 p의 `cached_uc`와 `cached_c`가 다른 인스턴스가 되도록 수정
152
+
153
+ ## 2023-07-05
154
+
155
+ - v23.7.3
156
+ - 버그 수정
157
+ - `object()`가 json 직렬화 안되는 문제
158
+ - `process`를 호출함에 따라 배치 카운트가 2이상일 때, all_prompts가 고정되는 문제
159
+ - `ad-before`와 `ad-preview` 이미지 파일명이 실제 파일명과 다른 문제
160
+ - pydantic 2.0 호환성 문제
161
+
162
+ ## 2023-07-04
163
+
164
+ - v23.7.2
165
+ - `mediapipe_face_mesh_eyes_only` 모델 추가: `mediapipe_face_mesh`로 감지한 뒤 눈만 사용함.
166
+ - 매 배치 시작 전에 `scripts.postprocess`를, 후에 `scripts.process`를 호출함.
167
+ - 컨트롤넷을 사용하면 소요 시간이 조금 늘어나지만 몇몇 문제 해결에 도움이 됨.
168
+ - `lora_block_weight`를 스크립트 화이트리스트에 추가함.
169
+ - 한번이라도 ADetailer를 사용한 사람은 수동으로 추가해야함.
170
+
171
+ ## 2023-07-03
172
+
173
+ - v23.7.1
174
+ - `process_images`를 진행한 뒤 `StableDiffusionProcessing` 오브젝트의 close를 호출함
175
+ - api 호출로 사용했는지 확인하는 속성 추가
176
+ - `NansException`이 발생했을 때 중지하지 않고 남은 과정 계속 진행함
177
+
178
+ ## 2023-07-02
179
+
180
+ - v23.7.0
181
+ - `NansException`이 발생하면 로그에 표시하고 원본 이미지를 반환하게 설정
182
+ - `rich`를 사용한 에러 트레이싱
183
+ - install.py에 `rich` 추가
184
+ - 생성 중에 컴포넌트의 값을 변경하면 args의 값도 함께 변경되는 문제 수정 (issue #180)
185
+ - 터미널 로그로 ad_prompt와 ad_negative_prompt에 적용된 실제 프롬프트 확인할 수 있음 (입력과 다를 경우에만)
186
+
187
+ ## 2023-06-28
188
+
189
+ - v23.6.4
190
+ - 최대 모델 수 5 -> 10개
191
+ - ad_prompt와 ad_negative_prompt에 빈칸으로 놔두면 입력 프롬프트가 사용된다는 문구 추가
192
+ - huggingface 모델 다운로드 실패시 로깅
193
+ - 1st 모델이 `None`일 경우 나머지 입력을 무시하던 문제 수정
194
+ - `--use-cpu` 에 `adetailer` 입력 시 cpu로 yolo모델을 사용함
195
+
196
+ ## 2023-06-20
197
+
198
+ - v23.6.3
199
+ - 컨트롤넷 inpaint 모델에 대해, 3가지 모듈을 사용할 수 있도록 함
200
+ - Noise Multiplier 옵션 추가 (PR #149)
201
+ - pydantic 최소 버전 1.10.8로 설정 (Issue #146)
202
+
203
+ ## 2023-06-05
204
+
205
+ - v23.6.2
206
+ - xyz_grid에서 ADetailer를 사용할 수 있게함.
207
+ - 8가지 옵션만 1st 탭에 적용되도록 함.
208
+
209
+ ## 2023-06-01
210
+
211
+ - v23.6.1
212
+ - `inpaint, scribble, lineart, openpose, tile` 5가지 컨트롤넷 모델 지원 (PR #107)
213
+ - controlnet guidance start, end 인자 추가 (PR #107)
214
+ - `modules.extensions`를 사용하여 컨트롤넷 확장을 불러오고 경로를 알아내로록 변경
215
+ - ui에서 컨트롤넷을 별도 함수로 분리
216
+
217
+ ## 2023-05-30
218
+
219
+ - v23.6.0
220
+ - 스크립트의 이름을 `After Detailer`에서 `ADetailer`로 변경
221
+ - API 사용자는 변경 필요함
222
+ - 몇몇 설정 변경
223
+ - `ad_conf` → `ad_confidence`. 0~100 사이의 int → 0.0~1.0 사이의 float
224
+ - `ad_inpaint_full_res` → `ad_inpaint_only_masked`
225
+ - `ad_inpaint_full_res_padding` → `ad_inpaint_only_masked_padding`
226
+ - mediapipe face mesh 모델 추가
227
+ - mediapipe 최소 버전 `0.10.0`
228
+
229
+ - rich traceback 제거함
230
+ - huggingface 다운로드 실패할 때 에러가 나지 않게 하고 해당 모델을 제거함
231
+
232
+ ## 2023-05-26
233
+
234
+ - v23.5.19
235
+ - 1번째 탭에도 `None` 옵션을 추가함
236
+ - api로 ad controlnet model에 inpaint가 아닌 다른 컨트롤넷 모델을 사용하지 못하도록 막음
237
+ - adetailer 진행중에 total tqdm 진행바 업데이트를 멈춤
238
+ - state.inturrupted 상태에서 adetailer 과정을 중지함
239
+ - 컨트롤넷 process를 각 batch가 끝난 순간에만 호출하도록 변경
240
+
241
+ ### 2023-05-25
242
+
243
+ - v23.5.18
244
+ - 컨트롤넷 관련 수정
245
+ - unit의 `input_mode`를 `SIMPLE`로 모두 변경
246
+ - 컨트롤넷 유넷 훅과 하이잭 함수들을 adetailer를 실행할 때에만 되돌리는 기능 추가
247
+ - adetailer 처리가 끝난 뒤 컨트롤넷 스크립트의 process를 다시 진행함. (batch count 2 이상일때의 문제 해결)
248
+ - 기본 활성 스크립트 목록에서 컨트롤넷을 뺌
249
+
250
+ ### 2023-05-22
251
+
252
+ - v23.5.17
253
+ - 컨트롤넷 확장이 있으면 컨트롤넷 스크립트를 활성화함. (컨트롤넷 관련 문제 해결)
254
+ - 모든 컴포넌트에 elem_id 설정
255
+ - ui에 버전을 표시함
256
+
257
+
258
+ ### 2023-05-19
259
+
260
+ - v23.5.16
261
+ - 추가한 옵션
262
+ - Mask min/max ratio
263
+ - Mask merge mode
264
+ - Restore faces after ADetailer
265
+ - 옵션들을 Accordion으로 묶음
266
+
267
+ ### 2023-05-18
268
+
269
+ - v23.5.15
270
+ - 필요한 것만 임포트하도록 변경 (vae 로딩 오류 없어짐. 로딩 속도 빨라짐)
271
+
272
+ ### 2023-05-17
273
+
274
+ - v23.5.14
275
+ - `[SKIP]`으로 ad prompt 일부를 건너뛰는 기능 추가
276
+ - bbox 정렬 옵션 추가
277
+ - sd_webui 타입힌트를 만들어냄
278
+ - enable checker와 관련된 api 오류 수정?
279
+
280
+ ### 2023-05-15
281
+
282
+ - v23.5.13
283
+ - `[SEP]`으로 ad prompt를 분리하여 적용하는 기능 추가
284
+ - enable checker를 다시 pydantic으로 변경함
285
+ - ui 관련 함수를 adetailer.ui 폴더로 분리함
286
+ - controlnet을 사용할 때 모든 controlnet unit 비활성화
287
+ - adetailer 폴더가 없으면 만들게 함
288
+
289
+ ### 2023-05-13
290
+
291
+ - v23.5.12
292
+ - `ad_enable`을 제외한 입력이 dict타입으로 들어오도록 변경
293
+ - web api로 사용할 때에 특히 사용하기 쉬움
294
+ - web api breaking change
295
+ - `mask_preprocess` 인자를 넣지 않았던 오류 수정 (PR #47)
296
+ - huggingface에서 모델을 다운로드하지 않는 옵션 추가 `--ad-no-huggingface`
297
+
298
+ ### 2023-05-12
299
+
300
+ - v23.5.11
301
+ - `ultralytics` 알람 제거
302
+ - 필요없는 exif 인자 더 제거함
303
+ - `use separate steps` 옵션 추가
304
+ - ui 배치를 조정함
305
+
306
+ ### 2023-05-09
307
+
308
+ - v23.5.10
309
+ - 선택한 스크립트만 ADetailer에 적용하는 옵션 추가, 기본값 `True`. 설정 탭에서 지정가능.
310
+ - 기본값: `dynamic_prompting,dynamic_thresholding,wildcards,wildcard_recursive`
311
+ - `person_yolov8s-seg.pt` 모델 추가
312
+ - `ultralytics`의 최소 버전을 `8.0.97`로 설정 (C:\\ 문제 해결된 버전)
313
+
314
+ ### 2023-05-08
315
+
316
+ - v23.5.9
317
+ - 2가지 이상의 모델을 사용할 수 있음. 기본값: 2, 최대: 5
318
+ - segment 모델을 사용할 수 있게 함. `person_yolov8n-seg.pt` 추가
319
+
320
+ ### 2023-05-07
321
+
322
+ - v23.5.8
323
+ - 프롬프트와 네거티브 프롬프트에 방향키 지원 (PR #24)
324
+ - `mask_preprocess`를 추가함. 이전 버전과 시드값이 달라질 가능성 있음!
325
+ - 이미지 처리가 일어났을 때에만 before이미지를 저장함
326
+ - 설정창의 레이블을 ADetailer 대신 더 적절하게 수정함
327
+
328
+ ### 2023-05-06
329
+
330
+ - v23.5.7
331
+ - `ad_use_cfg_scale` 옵션 추가. cfg 스케일을 따로 사용할지 말지 결정함.
332
+ - `ad_enable` 기본값을 `True`에서 `False`로 변경
333
+ - `ad_model`의 기본값을 `None`에서 첫번째 모델로 변경
334
+ - 최소 2개의 입력(ad_enable, ad_model)만 들어오면 작동하게 변경.
335
+
336
+ - v23.5.7.post0
337
+ - `init_controlnet_ext`을 controlnet_exists == True일때에만 실행
338
+ - webui를 C드라이브 바로 밑에 설치한 사람들에게 `ultralytics` 경고 표시
339
+
340
+ ### 2023-05-05 (어린이날)
341
+
342
+ - v23.5.5
343
+ - `Save images before ADetailer` 옵션 추가
344
+ - 입력으로 들어온 인자와 ALL_ARGS의 길이가 다르면 에러메세지
345
+ - README.md에 설치방법 추가
346
+
347
+ - v23.5.6
348
+ - get_args에서 IndexError가 발생하면 자세한 에러메세지를 볼 수 있음
349
+ - AdetailerArgs에 extra_params 내장
350
+ - scripts_args를 딥카피함
351
+ - postprocess_image를 약간 분리함
352
+
353
+ - v23.5.6.post0
354
+ - `init_controlnet_ext`에서 에러메세지를 자세히 볼 수 있음
355
+
356
+ ### 2023-05-04
357
+
358
+ - v23.5.4
359
+ - use pydantic for arguments validation
360
+ - revert: ad_model to `None` as default
361
+ - revert: `__future__` imports
362
+ - lazily import yolo and mediapipe
363
+
364
+ ### 2023-05-03
365
+
366
+ - v23.5.3.post0
367
+ - remove `__future__` imports
368
+ - change to copy scripts and scripts args
369
+
370
+ - v23.5.3.post1
371
+ - change default ad_model from `None`
372
+
373
+ ### 2023-05-02
374
+
375
+ - v23.5.3
376
+ - Remove `None` from model list and add `Enable ADetailer` checkbox.
377
+ - install.py `skip_install` fix.
adetailer/LICENSE.md ADDED
@@ -0,0 +1,662 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ GNU AFFERO GENERAL PUBLIC LICENSE
3
+ Version 3, 19 November 2007
4
+
5
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The GNU Affero General Public License is a free, copyleft license for
12
+ software and other kinds of works, specifically designed to ensure
13
+ cooperation with the community in the case of network server software.
14
+
15
+ The licenses for most software and other practical works are designed
16
+ to take away your freedom to share and change the works. By contrast,
17
+ our General Public Licenses are intended to guarantee your freedom to
18
+ share and change all versions of a program--to make sure it remains free
19
+ software for all its users.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ them if you wish), that you receive source code or can get it if you
25
+ want it, that you can change the software or use pieces of it in new
26
+ free programs, and that you know you can do these things.
27
+
28
+ Developers that use our General Public Licenses protect your rights
29
+ with two steps: (1) assert copyright on the software, and (2) offer
30
+ you this License which gives you legal permission to copy, distribute
31
+ and/or modify the software.
32
+
33
+ A secondary benefit of defending all users' freedom is that
34
+ improvements made in alternate versions of the program, if they
35
+ receive widespread use, become available for other developers to
36
+ incorporate. Many developers of free software are heartened and
37
+ encouraged by the resulting cooperation. However, in the case of
38
+ software used on network servers, this result may fail to come about.
39
+ The GNU General Public License permits making a modified version and
40
+ letting the public access it on a server without ever releasing its
41
+ source code to the public.
42
+
43
+ The GNU Affero General Public License is designed specifically to
44
+ ensure that, in such cases, the modified source code becomes available
45
+ to the community. It requires the operator of a network server to
46
+ provide the source code of the modified version running there to the
47
+ users of that server. Therefore, public use of a modified version, on
48
+ a publicly accessible server, gives the public access to the source
49
+ code of the modified version.
50
+
51
+ An older license, called the Affero General Public License and
52
+ published by Affero, was designed to accomplish similar goals. This is
53
+ a different license, not a version of the Affero GPL, but Affero has
54
+ released a new version of the Affero GPL which permits relicensing under
55
+ this license.
56
+
57
+ The precise terms and conditions for copying, distribution and
58
+ modification follow.
59
+
60
+ TERMS AND CONDITIONS
61
+
62
+ 0. Definitions.
63
+
64
+ "This License" refers to version 3 of the GNU Affero General Public License.
65
+
66
+ "Copyright" also means copyright-like laws that apply to other kinds of
67
+ works, such as semiconductor masks.
68
+
69
+ "The Program" refers to any copyrightable work licensed under this
70
+ License. Each licensee is addressed as "you". "Licensees" and
71
+ "recipients" may be individuals or organizations.
72
+
73
+ To "modify" a work means to copy from or adapt all or part of the work
74
+ in a fashion requiring copyright permission, other than the making of an
75
+ exact copy. The resulting work is called a "modified version" of the
76
+ earlier work or a work "based on" the earlier work.
77
+
78
+ A "covered work" means either the unmodified Program or a work based
79
+ on the Program.
80
+
81
+ To "propagate" a work means to do anything with it that, without
82
+ permission, would make you directly or secondarily liable for
83
+ infringement under applicable copyright law, except executing it on a
84
+ computer or modifying a private copy. Propagation includes copying,
85
+ distribution (with or without modification), making available to the
86
+ public, and in some countries other activities as well.
87
+
88
+ To "convey" a work means any kind of propagation that enables other
89
+ parties to make or receive copies. Mere interaction with a user through
90
+ a computer network, with no transfer of a copy, is not conveying.
91
+
92
+ An interactive user interface displays "Appropriate Legal Notices"
93
+ to the extent that it includes a convenient and prominently visible
94
+ feature that (1) displays an appropriate copyright notice, and (2)
95
+ tells the user that there is no warranty for the work (except to the
96
+ extent that warranties are provided), that licensees may convey the
97
+ work under this License, and how to view a copy of this License. If
98
+ the interface presents a list of user commands or options, such as a
99
+ menu, a prominent item in the list meets this criterion.
100
+
101
+ 1. Source Code.
102
+
103
+ The "source code" for a work means the preferred form of the work
104
+ for making modifications to it. "Object code" means any non-source
105
+ form of a work.
106
+
107
+ A "Standard Interface" means an interface that either is an official
108
+ standard defined by a recognized standards body, or, in the case of
109
+ interfaces specified for a particular programming language, one that
110
+ is widely used among developers working in that language.
111
+
112
+ The "System Libraries" of an executable work include anything, other
113
+ than the work as a whole, that (a) is included in the normal form of
114
+ packaging a Major Component, but which is not part of that Major
115
+ Component, and (b) serves only to enable use of the work with that
116
+ Major Component, or to implement a Standard Interface for which an
117
+ implementation is available to the public in source code form. A
118
+ "Major Component", in this context, means a major essential component
119
+ (kernel, window system, and so on) of the specific operating system
120
+ (if any) on which the executable work runs, or a compiler used to
121
+ produce the work, or an object code interpreter used to run it.
122
+
123
+ The "Corresponding Source" for a work in object code form means all
124
+ the source code needed to generate, install, and (for an executable
125
+ work) run the object code and to modify the work, including scripts to
126
+ control those activities. However, it does not include the work's
127
+ System Libraries, or general-purpose tools or generally available free
128
+ programs which are used unmodified in performing those activities but
129
+ which are not part of the work. For example, Corresponding Source
130
+ includes interface definition files associated with source files for
131
+ the work, and the source code for shared libraries and dynamically
132
+ linked subprograms that the work is specifically designed to require,
133
+ such as by intimate data communication or control flow between those
134
+ subprograms and other parts of the work.
135
+
136
+ The Corresponding Source need not include anything that users
137
+ can regenerate automatically from other parts of the Corresponding
138
+ Source.
139
+
140
+ The Corresponding Source for a work in source code form is that
141
+ same work.
142
+
143
+ 2. Basic Permissions.
144
+
145
+ All rights granted under this License are granted for the term of
146
+ copyright on the Program, and are irrevocable provided the stated
147
+ conditions are met. This License explicitly affirms your unlimited
148
+ permission to run the unmodified Program. The output from running a
149
+ covered work is covered by this License only if the output, given its
150
+ content, constitutes a covered work. This License acknowledges your
151
+ rights of fair use or other equivalent, as provided by copyright law.
152
+
153
+ You may make, run and propagate covered works that you do not
154
+ convey, without conditions so long as your license otherwise remains
155
+ in force. You may convey covered works to others for the sole purpose
156
+ of having them make modifications exclusively for you, or provide you
157
+ with facilities for running those works, provided that you comply with
158
+ the terms of this License in conveying all material for which you do
159
+ not control copyright. Those thus making or running the covered works
160
+ for you must do so exclusively on your behalf, under your direction
161
+ and control, on terms that prohibit them from making any copies of
162
+ your copyrighted material outside their relationship with you.
163
+
164
+ Conveying under any other circumstances is permitted solely under
165
+ the conditions stated below. Sublicensing is not allowed; section 10
166
+ makes it unnecessary.
167
+
168
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
169
+
170
+ No covered work shall be deemed part of an effective technological
171
+ measure under any applicable law fulfilling obligations under article
172
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
173
+ similar laws prohibiting or restricting circumvention of such
174
+ measures.
175
+
176
+ When you convey a covered work, you waive any legal power to forbid
177
+ circumvention of technological measures to the extent such circumvention
178
+ is effected by exercising rights under this License with respect to
179
+ the covered work, and you disclaim any intention to limit operation or
180
+ modification of the work as a means of enforcing, against the work's
181
+ users, your or third parties' legal rights to forbid circumvention of
182
+ technological measures.
183
+
184
+ 4. Conveying Verbatim Copies.
185
+
186
+ You may convey verbatim copies of the Program's source code as you
187
+ receive it, in any medium, provided that you conspicuously and
188
+ appropriately publish on each copy an appropriate copyright notice;
189
+ keep intact all notices stating that this License and any
190
+ non-permissive terms added in accord with section 7 apply to the code;
191
+ keep intact all notices of the absence of any warranty; and give all
192
+ recipients a copy of this License along with the Program.
193
+
194
+ You may charge any price or no price for each copy that you convey,
195
+ and you may offer support or warranty protection for a fee.
196
+
197
+ 5. Conveying Modified Source Versions.
198
+
199
+ You may convey a work based on the Program, or the modifications to
200
+ produce it from the Program, in the form of source code under the
201
+ terms of section 4, provided that you also meet all of these conditions:
202
+
203
+ a) The work must carry prominent notices stating that you modified
204
+ it, and giving a relevant date.
205
+
206
+ b) The work must carry prominent notices stating that it is
207
+ released under this License and any conditions added under section
208
+ 7. This requirement modifies the requirement in section 4 to
209
+ "keep intact all notices".
210
+
211
+ c) You must license the entire work, as a whole, under this
212
+ License to anyone who comes into possession of a copy. This
213
+ License will therefore apply, along with any applicable section 7
214
+ additional terms, to the whole of the work, and all its parts,
215
+ regardless of how they are packaged. This License gives no
216
+ permission to license the work in any other way, but it does not
217
+ invalidate such permission if you have separately received it.
218
+
219
+ d) If the work has interactive user interfaces, each must display
220
+ Appropriate Legal Notices; however, if the Program has interactive
221
+ interfaces that do not display Appropriate Legal Notices, your
222
+ work need not make them do so.
223
+
224
+ A compilation of a covered work with other separate and independent
225
+ works, which are not by their nature extensions of the covered work,
226
+ and which are not combined with it such as to form a larger program,
227
+ in or on a volume of a storage or distribution medium, is called an
228
+ "aggregate" if the compilation and its resulting copyright are not
229
+ used to limit the access or legal rights of the compilation's users
230
+ beyond what the individual works permit. Inclusion of a covered work
231
+ in an aggregate does not cause this License to apply to the other
232
+ parts of the aggregate.
233
+
234
+ 6. Conveying Non-Source Forms.
235
+
236
+ You may convey a covered work in object code form under the terms
237
+ of sections 4 and 5, provided that you also convey the
238
+ machine-readable Corresponding Source under the terms of this License,
239
+ in one of these ways:
240
+
241
+ a) Convey the object code in, or embodied in, a physical product
242
+ (including a physical distribution medium), accompanied by the
243
+ Corresponding Source fixed on a durable physical medium
244
+ customarily used for software interchange.
245
+
246
+ b) Convey the object code in, or embodied in, a physical product
247
+ (including a physical distribution medium), accompanied by a
248
+ written offer, valid for at least three years and valid for as
249
+ long as you offer spare parts or customer support for that product
250
+ model, to give anyone who possesses the object code either (1) a
251
+ copy of the Corresponding Source for all the software in the
252
+ product that is covered by this License, on a durable physical
253
+ medium customarily used for software interchange, for a price no
254
+ more than your reasonable cost of physically performing this
255
+ conveying of source, or (2) access to copy the
256
+ Corresponding Source from a network server at no charge.
257
+
258
+ c) Convey individual copies of the object code with a copy of the
259
+ written offer to provide the Corresponding Source. This
260
+ alternative is allowed only occasionally and noncommercially, and
261
+ only if you received the object code with such an offer, in accord
262
+ with subsection 6b.
263
+
264
+ d) Convey the object code by offering access from a designated
265
+ place (gratis or for a charge), and offer equivalent access to the
266
+ Corresponding Source in the same way through the same place at no
267
+ further charge. You need not require recipients to copy the
268
+ Corresponding Source along with the object code. If the place to
269
+ copy the object code is a network server, the Corresponding Source
270
+ may be on a different server (operated by you or a third party)
271
+ that supports equivalent copying facilities, provided you maintain
272
+ clear directions next to the object code saying where to find the
273
+ Corresponding Source. Regardless of what server hosts the
274
+ Corresponding Source, you remain obligated to ensure that it is
275
+ available for as long as needed to satisfy these requirements.
276
+
277
+ e) Convey the object code using peer-to-peer transmission, provided
278
+ you inform other peers where the object code and Corresponding
279
+ Source of the work are being offered to the general public at no
280
+ charge under subsection 6d.
281
+
282
+ A separable portion of the object code, whose source code is excluded
283
+ from the Corresponding Source as a System Library, need not be
284
+ included in conveying the object code work.
285
+
286
+ A "User Product" is either (1) a "consumer product", which means any
287
+ tangible personal property which is normally used for personal, family,
288
+ or household purposes, or (2) anything designed or sold for incorporation
289
+ into a dwelling. In determining whether a product is a consumer product,
290
+ doubtful cases shall be resolved in favor of coverage. For a particular
291
+ product received by a particular user, "normally used" refers to a
292
+ typical or common use of that class of product, regardless of the status
293
+ of the particular user or of the way in which the particular user
294
+ actually uses, or expects or is expected to use, the product. A product
295
+ is a consumer product regardless of whether the product has substantial
296
+ commercial, industrial or non-consumer uses, unless such uses represent
297
+ the only significant mode of use of the product.
298
+
299
+ "Installation Information" for a User Product means any methods,
300
+ procedures, authorization keys, or other information required to install
301
+ and execute modified versions of a covered work in that User Product from
302
+ a modified version of its Corresponding Source. The information must
303
+ suffice to ensure that the continued functioning of the modified object
304
+ code is in no case prevented or interfered with solely because
305
+ modification has been made.
306
+
307
+ If you convey an object code work under this section in, or with, or
308
+ specifically for use in, a User Product, and the conveying occurs as
309
+ part of a transaction in which the right of possession and use of the
310
+ User Product is transferred to the recipient in perpetuity or for a
311
+ fixed term (regardless of how the transaction is characterized), the
312
+ Corresponding Source conveyed under this section must be accompanied
313
+ by the Installation Information. But this requirement does not apply
314
+ if neither you nor any third party retains the ability to install
315
+ modified object code on the User Product (for example, the work has
316
+ been installed in ROM).
317
+
318
+ The requirement to provide Installation Information does not include a
319
+ requirement to continue to provide support service, warranty, or updates
320
+ for a work that has been modified or installed by the recipient, or for
321
+ the User Product in which it has been modified or installed. Access to a
322
+ network may be denied when the modification itself materially and
323
+ adversely affects the operation of the network or violates the rules and
324
+ protocols for communication across the network.
325
+
326
+ Corresponding Source conveyed, and Installation Information provided,
327
+ in accord with this section must be in a format that is publicly
328
+ documented (and with an implementation available to the public in
329
+ source code form), and must require no special password or key for
330
+ unpacking, reading or copying.
331
+
332
+ 7. Additional Terms.
333
+
334
+ "Additional permissions" are terms that supplement the terms of this
335
+ License by making exceptions from one or more of its conditions.
336
+ Additional permissions that are applicable to the entire Program shall
337
+ be treated as though they were included in this License, to the extent
338
+ that they are valid under applicable law. If additional permissions
339
+ apply only to part of the Program, that part may be used separately
340
+ under those permissions, but the entire Program remains governed by
341
+ this License without regard to the additional permissions.
342
+
343
+ When you convey a copy of a covered work, you may at your option
344
+ remove any additional permissions from that copy, or from any part of
345
+ it. (Additional permissions may be written to require their own
346
+ removal in certain cases when you modify the work.) You may place
347
+ additional permissions on material, added by you to a covered work,
348
+ for which you have or can give appropriate copyright permission.
349
+
350
+ Notwithstanding any other provision of this License, for material you
351
+ add to a covered work, you may (if authorized by the copyright holders of
352
+ that material) supplement the terms of this License with terms:
353
+
354
+ a) Disclaiming warranty or limiting liability differently from the
355
+ terms of sections 15 and 16 of this License; or
356
+
357
+ b) Requiring preservation of specified reasonable legal notices or
358
+ author attributions in that material or in the Appropriate Legal
359
+ Notices displayed by works containing it; or
360
+
361
+ c) Prohibiting misrepresentation of the origin of that material, or
362
+ requiring that modified versions of such material be marked in
363
+ reasonable ways as different from the original version; or
364
+
365
+ d) Limiting the use for publicity purposes of names of licensors or
366
+ authors of the material; or
367
+
368
+ e) Declining to grant rights under trademark law for use of some
369
+ trade names, trademarks, or service marks; or
370
+
371
+ f) Requiring indemnification of licensors and authors of that
372
+ material by anyone who conveys the material (or modified versions of
373
+ it) with contractual assumptions of liability to the recipient, for
374
+ any liability that these contractual assumptions directly impose on
375
+ those licensors and authors.
376
+
377
+ All other non-permissive additional terms are considered "further
378
+ restrictions" within the meaning of section 10. If the Program as you
379
+ received it, or any part of it, contains a notice stating that it is
380
+ governed by this License along with a term that is a further
381
+ restriction, you may remove that term. If a license document contains
382
+ a further restriction but permits relicensing or conveying under this
383
+ License, you may add to a covered work material governed by the terms
384
+ of that license document, provided that the further restriction does
385
+ not survive such relicensing or conveying.
386
+
387
+ If you add terms to a covered work in accord with this section, you
388
+ must place, in the relevant source files, a statement of the
389
+ additional terms that apply to those files, or a notice indicating
390
+ where to find the applicable terms.
391
+
392
+ Additional terms, permissive or non-permissive, may be stated in the
393
+ form of a separately written license, or stated as exceptions;
394
+ the above requirements apply either way.
395
+
396
+ 8. Termination.
397
+
398
+ You may not propagate or modify a covered work except as expressly
399
+ provided under this License. Any attempt otherwise to propagate or
400
+ modify it is void, and will automatically terminate your rights under
401
+ this License (including any patent licenses granted under the third
402
+ paragraph of section 11).
403
+
404
+ However, if you cease all violation of this License, then your
405
+ license from a particular copyright holder is reinstated (a)
406
+ provisionally, unless and until the copyright holder explicitly and
407
+ finally terminates your license, and (b) permanently, if the copyright
408
+ holder fails to notify you of the violation by some reasonable means
409
+ prior to 60 days after the cessation.
410
+
411
+ Moreover, your license from a particular copyright holder is
412
+ reinstated permanently if the copyright holder notifies you of the
413
+ violation by some reasonable means, this is the first time you have
414
+ received notice of violation of this License (for any work) from that
415
+ copyright holder, and you cure the violation prior to 30 days after
416
+ your receipt of the notice.
417
+
418
+ Termination of your rights under this section does not terminate the
419
+ licenses of parties who have received copies or rights from you under
420
+ this License. If your rights have been terminated and not permanently
421
+ reinstated, you do not qualify to receive new licenses for the same
422
+ material under section 10.
423
+
424
+ 9. Acceptance Not Required for Having Copies.
425
+
426
+ You are not required to accept this License in order to receive or
427
+ run a copy of the Program. Ancillary propagation of a covered work
428
+ occurring solely as a consequence of using peer-to-peer transmission
429
+ to receive a copy likewise does not require acceptance. However,
430
+ nothing other than this License grants you permission to propagate or
431
+ modify any covered work. These actions infringe copyright if you do
432
+ not accept this License. Therefore, by modifying or propagating a
433
+ covered work, you indicate your acceptance of this License to do so.
434
+
435
+ 10. Automatic Licensing of Downstream Recipients.
436
+
437
+ Each time you convey a covered work, the recipient automatically
438
+ receives a license from the original licensors, to run, modify and
439
+ propagate that work, subject to this License. You are not responsible
440
+ for enforcing compliance by third parties with this License.
441
+
442
+ An "entity transaction" is a transaction transferring control of an
443
+ organization, or substantially all assets of one, or subdividing an
444
+ organization, or merging organizations. If propagation of a covered
445
+ work results from an entity transaction, each party to that
446
+ transaction who receives a copy of the work also receives whatever
447
+ licenses to the work the party's predecessor in interest had or could
448
+ give under the previous paragraph, plus a right to possession of the
449
+ Corresponding Source of the work from the predecessor in interest, if
450
+ the predecessor has it or can get it with reasonable efforts.
451
+
452
+ You may not impose any further restrictions on the exercise of the
453
+ rights granted or affirmed under this License. For example, you may
454
+ not impose a license fee, royalty, or other charge for exercise of
455
+ rights granted under this License, and you may not initiate litigation
456
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
457
+ any patent claim is infringed by making, using, selling, offering for
458
+ sale, or importing the Program or any portion of it.
459
+
460
+ 11. Patents.
461
+
462
+ A "contributor" is a copyright holder who authorizes use under this
463
+ License of the Program or a work on which the Program is based. The
464
+ work thus licensed is called the contributor's "contributor version".
465
+
466
+ A contributor's "essential patent claims" are all patent claims
467
+ owned or controlled by the contributor, whether already acquired or
468
+ hereafter acquired, that would be infringed by some manner, permitted
469
+ by this License, of making, using, or selling its contributor version,
470
+ but do not include claims that would be infringed only as a
471
+ consequence of further modification of the contributor version. For
472
+ purposes of this definition, "control" includes the right to grant
473
+ patent sublicenses in a manner consistent with the requirements of
474
+ this License.
475
+
476
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
477
+ patent license under the contributor's essential patent claims, to
478
+ make, use, sell, offer for sale, import and otherwise run, modify and
479
+ propagate the contents of its contributor version.
480
+
481
+ In the following three paragraphs, a "patent license" is any express
482
+ agreement or commitment, however denominated, not to enforce a patent
483
+ (such as an express permission to practice a patent or covenant not to
484
+ sue for patent infringement). To "grant" such a patent license to a
485
+ party means to make such an agreement or commitment not to enforce a
486
+ patent against the party.
487
+
488
+ If you convey a covered work, knowingly relying on a patent license,
489
+ and the Corresponding Source of the work is not available for anyone
490
+ to copy, free of charge and under the terms of this License, through a
491
+ publicly available network server or other readily accessible means,
492
+ then you must either (1) cause the Corresponding Source to be so
493
+ available, or (2) arrange to deprive yourself of the benefit of the
494
+ patent license for this particular work, or (3) arrange, in a manner
495
+ consistent with the requirements of this License, to extend the patent
496
+ license to downstream recipients. "Knowingly relying" means you have
497
+ actual knowledge that, but for the patent license, your conveying the
498
+ covered work in a country, or your recipient's use of the covered work
499
+ in a country, would infringe one or more identifiable patents in that
500
+ country that you have reason to believe are valid.
501
+
502
+ If, pursuant to or in connection with a single transaction or
503
+ arrangement, you convey, or propagate by procuring conveyance of, a
504
+ covered work, and grant a patent license to some of the parties
505
+ receiving the covered work authorizing them to use, propagate, modify
506
+ or convey a specific copy of the covered work, then the patent license
507
+ you grant is automatically extended to all recipients of the covered
508
+ work and works based on it.
509
+
510
+ A patent license is "discriminatory" if it does not include within
511
+ the scope of its coverage, prohibits the exercise of, or is
512
+ conditioned on the non-exercise of one or more of the rights that are
513
+ specifically granted under this License. You may not convey a covered
514
+ work if you are a party to an arrangement with a third party that is
515
+ in the business of distributing software, under which you make payment
516
+ to the third party based on the extent of your activity of conveying
517
+ the work, and under which the third party grants, to any of the
518
+ parties who would receive the covered work from you, a discriminatory
519
+ patent license (a) in connection with copies of the covered work
520
+ conveyed by you (or copies made from those copies), or (b) primarily
521
+ for and in connection with specific products or compilations that
522
+ contain the covered work, unless you entered into that arrangement,
523
+ or that patent license was granted, prior to 28 March 2007.
524
+
525
+ Nothing in this License shall be construed as excluding or limiting
526
+ any implied license or other defenses to infringement that may
527
+ otherwise be available to you under applicable patent law.
528
+
529
+ 12. No Surrender of Others' Freedom.
530
+
531
+ If conditions are imposed on you (whether by court order, agreement or
532
+ otherwise) that contradict the conditions of this License, they do not
533
+ excuse you from the conditions of this License. If you cannot convey a
534
+ covered work so as to satisfy simultaneously your obligations under this
535
+ License and any other pertinent obligations, then as a consequence you may
536
+ not convey it at all. For example, if you agree to terms that obligate you
537
+ to collect a royalty for further conveying from those to whom you convey
538
+ the Program, the only way you could satisfy both those terms and this
539
+ License would be to refrain entirely from conveying the Program.
540
+
541
+ 13. Remote Network Interaction; Use with the GNU General Public License.
542
+
543
+ Notwithstanding any other provision of this License, if you modify the
544
+ Program, your modified version must prominently offer all users
545
+ interacting with it remotely through a computer network (if your version
546
+ supports such interaction) an opportunity to receive the Corresponding
547
+ Source of your version by providing access to the Corresponding Source
548
+ from a network server at no charge, through some standard or customary
549
+ means of facilitating copying of software. This Corresponding Source
550
+ shall include the Corresponding Source for any work covered by version 3
551
+ of the GNU General Public License that is incorporated pursuant to the
552
+ following paragraph.
553
+
554
+ Notwithstanding any other provision of this License, you have
555
+ permission to link or combine any covered work with a work licensed
556
+ under version 3 of the GNU General Public License into a single
557
+ combined work, and to convey the resulting work. The terms of this
558
+ License will continue to apply to the part which is the covered work,
559
+ but the work with which it is combined will remain governed by version
560
+ 3 of the GNU General Public License.
561
+
562
+ 14. Revised Versions of this License.
563
+
564
+ The Free Software Foundation may publish revised and/or new versions of
565
+ the GNU Affero General Public License from time to time. Such new versions
566
+ will be similar in spirit to the present version, but may differ in detail to
567
+ address new problems or concerns.
568
+
569
+ Each version is given a distinguishing version number. If the
570
+ Program specifies that a certain numbered version of the GNU Affero General
571
+ Public License "or any later version" applies to it, you have the
572
+ option of following the terms and conditions either of that numbered
573
+ version or of any later version published by the Free Software
574
+ Foundation. If the Program does not specify a version number of the
575
+ GNU Affero General Public License, you may choose any version ever published
576
+ by the Free Software Foundation.
577
+
578
+ If the Program specifies that a proxy can decide which future
579
+ versions of the GNU Affero General Public License can be used, that proxy's
580
+ public statement of acceptance of a version permanently authorizes you
581
+ to choose that version for the Program.
582
+
583
+ Later license versions may give you additional or different
584
+ permissions. However, no additional obligations are imposed on any
585
+ author or copyright holder as a result of your choosing to follow a
586
+ later version.
587
+
588
+ 15. Disclaimer of Warranty.
589
+
590
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
591
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
592
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
593
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
594
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
595
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
596
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
597
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
598
+
599
+ 16. Limitation of Liability.
600
+
601
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
602
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
603
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
604
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
605
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
606
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
607
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
608
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
609
+ SUCH DAMAGES.
610
+
611
+ 17. Interpretation of Sections 15 and 16.
612
+
613
+ If the disclaimer of warranty and limitation of liability provided
614
+ above cannot be given local legal effect according to their terms,
615
+ reviewing courts shall apply local law that most closely approximates
616
+ an absolute waiver of all civil liability in connection with the
617
+ Program, unless a warranty or assumption of liability accompanies a
618
+ copy of the Program in return for a fee.
619
+
620
+ END OF TERMS AND CONDITIONS
621
+
622
+ How to Apply These Terms to Your New Programs
623
+
624
+ If you develop a new program, and you want it to be of the greatest
625
+ possible use to the public, the best way to achieve this is to make it
626
+ free software which everyone can redistribute and change under these terms.
627
+
628
+ To do so, attach the following notices to the program. It is safest
629
+ to attach them to the start of each source file to most effectively
630
+ state the exclusion of warranty; and each file should have at least
631
+ the "copyright" line and a pointer to where the full notice is found.
632
+
633
+ <one line to give the program's name and a brief idea of what it does.>
634
+ Copyright (C) <year> <name of author>
635
+
636
+ This program is free software: you can redistribute it and/or modify
637
+ it under the terms of the GNU Affero General Public License as published
638
+ by the Free Software Foundation, either version 3 of the License, or
639
+ (at your option) any later version.
640
+
641
+ This program is distributed in the hope that it will be useful,
642
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
643
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
644
+ GNU Affero General Public License for more details.
645
+
646
+ You should have received a copy of the GNU Affero General Public License
647
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
648
+
649
+ Also add information on how to contact you by electronic and paper mail.
650
+
651
+ If your software can interact with users remotely through a computer
652
+ network, you should also make sure that it provides a way for users to
653
+ get its source. For example, if your program is a web application, its
654
+ interface could display a "Source" link that leads users to an archive
655
+ of the code. There are many ways you could offer source, and different
656
+ solutions will be better for different programs; see section 13 for the
657
+ specific requirements.
658
+
659
+ You should also get your employer (if you work as a programmer) or school,
660
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
661
+ For more information on this, and how to apply and follow the GNU AGPL, see
662
+ <http://www.gnu.org/licenses/>.
adetailer/README.md ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ADetailer
2
+
3
+ ADetailer is a extension for stable diffusion webui, similar to Detection Detailer, except it uses ultralytics instead of the mmdet.
4
+
5
+ ## Install
6
+
7
+ (from Mikubill/sd-webui-controlnet)
8
+
9
+ 1. Open "Extensions" tab.
10
+ 2. Open "Install from URL" tab in the tab.
11
+ 3. Enter `https://github.com/Bing-su/adetailer.git` to "URL for extension's git repository".
12
+ 4. Press "Install" button.
13
+ 5. Wait 5 seconds, and you will see the message "Installed into stable-diffusion-webui\extensions\adetailer. Use Installed tab to restart".
14
+ 6. Go to "Installed" tab, click "Check for updates", and then click "Apply and restart UI". (The next time you can also use this method to update extensions.)
15
+ 7. Completely restart A1111 webui including your terminal. (If you do not know what is a "terminal", you can reboot your computer: turn your computer off and turn it on again.)
16
+
17
+ You can now install it directly from the Extensions tab.
18
+
19
+ ![image](https://i.imgur.com/g6GdRBT.png)
20
+
21
+ You **DON'T** need to download any model from huggingface.
22
+
23
+ ## Options
24
+
25
+ | Model, Prompts | | |
26
+ | --------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------- |
27
+ | ADetailer model | Determine what to detect. | `None` = disable |
28
+ | ADetailer prompt, negative prompt | Prompts and negative prompts to apply | If left blank, it will use the same as the input. |
29
+ | Skip img2img | Skip img2img. In practice, this works by changing the step count of img2img to 1. | img2img only |
30
+
31
+ | Detection | | |
32
+ | ------------------------------------ | -------------------------------------------------------------------------------------------- | ------------ |
33
+ | Detection model confidence threshold | Only objects with a detection model confidence above this threshold are used for inpainting. | |
34
+ | Mask min/max ratio | Only use masks whose area is between those ratios for the area of the entire image. | |
35
+ | Mask only the top k largest | Only use the k objects with the largest area of the bbox. | 0 to disable |
36
+
37
+ If you want to exclude objects in the background, try setting the min ratio to around `0.01`.
38
+
39
+ | Mask Preprocessing | | |
40
+ | ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
41
+ | Mask x, y offset | Moves the mask horizontally and vertically by | |
42
+ | Mask erosion (-) / dilation (+) | Enlarge or reduce the detected mask. | [opencv example](https://docs.opencv.org/4.7.0/db/df6/tutorial_erosion_dilatation.html) |
43
+ | Mask merge mode | `None`: Inpaint each mask<br/>`Merge`: Merge all masks and inpaint<br/>`Merge and Invert`: Merge all masks and Invert, then inpaint | |
44
+
45
+ Applied in this order: x, y offset → erosion/dilation → merge/invert.
46
+
47
+ #### Inpainting
48
+
49
+ Each option corresponds to a corresponding option on the inpaint tab. Therefore, please refer to the inpaint tab for usage details on how to use each option.
50
+
51
+ ## ControlNet Inpainting
52
+
53
+ You can use the ControlNet extension if you have ControlNet installed and ControlNet models.
54
+
55
+ Support `inpaint, scribble, lineart, openpose, tile` controlnet models. Once you choose a model, the preprocessor is set automatically. It works separately from the model set by the Controlnet extension.
56
+
57
+ ## Advanced Options
58
+
59
+ API request example: [wiki/API](https://github.com/Bing-su/adetailer/wiki/API)
60
+
61
+ `ui-config.json` entries: [wiki/ui-config.json](https://github.com/Bing-su/adetailer/wiki/ui-config.json)
62
+
63
+ `[SEP], [SKIP]` tokens: [wiki/Advanced](https://github.com/Bing-su/adetailer/wiki/Advanced)
64
+
65
+ ## Media
66
+
67
+ - 🎥 [どこよりも詳しいAfter Detailer (adetailer)の使い方① 【Stable Diffusion】](https://youtu.be/sF3POwPUWCE)
68
+ - 🎥 [どこよりも詳しいAfter Detailer (adetailer)の使い方② 【Stable Diffusion】](https://youtu.be/urNISRdbIEg)
69
+
70
+ ## Model
71
+
72
+ | Model | Target | mAP 50 | mAP 50-95 |
73
+ | --------------------- | --------------------- | ----------------------------- | ----------------------------- |
74
+ | face_yolov8n.pt | 2D / realistic face | 0.660 | 0.366 |
75
+ | face_yolov8s.pt | 2D / realistic face | 0.713 | 0.404 |
76
+ | hand_yolov8n.pt | 2D / realistic hand | 0.767 | 0.505 |
77
+ | person_yolov8n-seg.pt | 2D / realistic person | 0.782 (bbox)<br/>0.761 (mask) | 0.555 (bbox)<br/>0.460 (mask) |
78
+ | person_yolov8s-seg.pt | 2D / realistic person | 0.824 (bbox)<br/>0.809 (mask) | 0.605 (bbox)<br/>0.508 (mask) |
79
+ | mediapipe_face_full | realistic face | - | - |
80
+ | mediapipe_face_short | realistic face | - | - |
81
+ | mediapipe_face_mesh | realistic face | - | - |
82
+
83
+ The yolo models can be found on huggingface [Bingsu/adetailer](https://huggingface.co/Bingsu/adetailer).
84
+
85
+ ### Additional Model
86
+
87
+ Put your [ultralytics](https://github.com/ultralytics/ultralytics) yolo model in `webui/models/adetailer`. The model name should end with `.pt` or `.pth`.
88
+
89
+ It must be a bbox detection or segment model and use all label.
90
+
91
+ ## How it works
92
+
93
+ ADetailer works in three simple steps.
94
+
95
+ 1. Create an image.
96
+ 2. Detect object with a detection model and create a mask image.
97
+ 3. Inpaint using the image from 1 and the mask from 2.
adetailer/Taskfile.yml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://taskfile.dev
2
+
3
+ version: "3"
4
+
5
+ dotenv:
6
+ - .env
7
+
8
+ tasks:
9
+ default:
10
+ cmds:
11
+ - echo "$PYTHON"
12
+ - echo "$WEBUI"
13
+ silent: true
14
+
15
+ launch:
16
+ dir: "{{.WEBUI}}"
17
+ cmds:
18
+ - "{{.PYTHON}} launch.py --xformers --api"
19
+ silent: true
20
+
21
+ lint:
22
+ cmds:
23
+ - pre-commit run -a
24
+
25
+ update:
26
+ cmds:
27
+ - "{{.PYTHON}} -m pip install -U ultralytics mediapipe ruff pre-commit black"
adetailer/adetailer/__init__.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .__version__ import __version__
2
+ from .args import ALL_ARGS, ADetailerArgs
3
+ from .common import PredictOutput, get_models
4
+ from .mediapipe import mediapipe_predict
5
+ from .ultralytics import ultralytics_predict
6
+
7
+ AFTER_DETAILER = "ADetailer"
8
+
9
+ __all__ = [
10
+ "__version__",
11
+ "ADetailerArgs",
12
+ "AFTER_DETAILER",
13
+ "ALL_ARGS",
14
+ "PredictOutput",
15
+ "get_models",
16
+ "mediapipe_predict",
17
+ "ultralytics_predict",
18
+ ]
adetailer/adetailer/__version__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __version__ = "24.1.2"
adetailer/adetailer/args.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from collections import UserList
4
+ from dataclasses import dataclass
5
+ from functools import cached_property, partial
6
+ from typing import Any, Literal, NamedTuple, Optional
7
+
8
+ import pydantic
9
+ from pydantic import (
10
+ BaseModel,
11
+ Extra,
12
+ NonNegativeFloat,
13
+ NonNegativeInt,
14
+ PositiveInt,
15
+ confloat,
16
+ conint,
17
+ constr,
18
+ validator,
19
+ )
20
+
21
+
22
+ @dataclass
23
+ class SkipImg2ImgOrig:
24
+ steps: int
25
+ sampler_name: str
26
+ width: int
27
+ height: int
28
+
29
+
30
+ class Arg(NamedTuple):
31
+ attr: str
32
+ name: str
33
+
34
+
35
+ class ArgsList(UserList):
36
+ @cached_property
37
+ def attrs(self) -> tuple[str]:
38
+ return tuple(attr for attr, _ in self)
39
+
40
+ @cached_property
41
+ def names(self) -> tuple[str]:
42
+ return tuple(name for _, name in self)
43
+
44
+
45
+ class ADetailerArgs(BaseModel, extra=Extra.forbid):
46
+ ad_model: str = "None"
47
+ ad_prompt: str = ""
48
+ ad_negative_prompt: str = ""
49
+ ad_confidence: confloat(ge=0.0, le=1.0) = 0.3
50
+ ad_mask_k_largest: NonNegativeInt = 0
51
+ ad_mask_min_ratio: confloat(ge=0.0, le=1.0) = 0.0
52
+ ad_mask_max_ratio: confloat(ge=0.0, le=1.0) = 1.0
53
+ ad_dilate_erode: int = 4
54
+ ad_x_offset: int = 0
55
+ ad_y_offset: int = 0
56
+ ad_mask_merge_invert: Literal["None", "Merge", "Merge and Invert"] = "None"
57
+ ad_mask_blur: NonNegativeInt = 4
58
+ ad_denoising_strength: confloat(ge=0.0, le=1.0) = 0.4
59
+ ad_inpaint_only_masked: bool = True
60
+ ad_inpaint_only_masked_padding: NonNegativeInt = 32
61
+ ad_use_inpaint_width_height: bool = False
62
+ ad_inpaint_width: PositiveInt = 512
63
+ ad_inpaint_height: PositiveInt = 512
64
+ ad_use_steps: bool = False
65
+ ad_steps: PositiveInt = 28
66
+ ad_use_cfg_scale: bool = False
67
+ ad_cfg_scale: NonNegativeFloat = 7.0
68
+ ad_use_checkpoint: bool = False
69
+ ad_checkpoint: Optional[str] = None
70
+ ad_use_vae: bool = False
71
+ ad_vae: Optional[str] = None
72
+ ad_use_sampler: bool = False
73
+ ad_sampler: str = "DPM++ 2M Karras"
74
+ ad_use_noise_multiplier: bool = False
75
+ ad_noise_multiplier: confloat(ge=0.5, le=1.5) = 1.0
76
+ ad_use_clip_skip: bool = False
77
+ ad_clip_skip: conint(ge=1, le=12) = 1
78
+ ad_restore_face: bool = False
79
+ ad_controlnet_model: str = "None"
80
+ ad_controlnet_module: str = "None"
81
+ ad_controlnet_weight: confloat(ge=0.0, le=1.0) = 1.0
82
+ ad_controlnet_guidance_start: confloat(ge=0.0, le=1.0) = 0.0
83
+ ad_controlnet_guidance_end: confloat(ge=0.0, le=1.0) = 1.0
84
+ is_api: bool = True
85
+
86
+ @validator("is_api", pre=True)
87
+ def is_api_validator(cls, v: Any): # noqa: N805
88
+ "tuple is json serializable but cannot be made with json deserialize."
89
+ return type(v) is not tuple
90
+
91
+ @staticmethod
92
+ def ppop(
93
+ p: dict[str, Any],
94
+ key: str,
95
+ pops: list[str] | None = None,
96
+ cond: Any = None,
97
+ ) -> None:
98
+ if pops is None:
99
+ pops = [key]
100
+ if key not in p:
101
+ return
102
+ value = p[key]
103
+ cond = (not bool(value)) if cond is None else value == cond
104
+
105
+ if cond:
106
+ for k in pops:
107
+ p.pop(k, None)
108
+
109
+ def extra_params(self, suffix: str = "") -> dict[str, Any]:
110
+ if self.ad_model == "None":
111
+ return {}
112
+
113
+ p = {name: getattr(self, attr) for attr, name in ALL_ARGS}
114
+ ppop = partial(self.ppop, p)
115
+
116
+ ppop("ADetailer prompt")
117
+ ppop("ADetailer negative prompt")
118
+ ppop("ADetailer mask only top k largest", cond=0)
119
+ ppop("ADetailer mask min ratio", cond=0.0)
120
+ ppop("ADetailer mask max ratio", cond=1.0)
121
+ ppop("ADetailer x offset", cond=0)
122
+ ppop("ADetailer y offset", cond=0)
123
+ ppop("ADetailer mask merge invert", cond="None")
124
+ ppop("ADetailer inpaint only masked", ["ADetailer inpaint padding"])
125
+ ppop(
126
+ "ADetailer use inpaint width height",
127
+ [
128
+ "ADetailer use inpaint width height",
129
+ "ADetailer inpaint width",
130
+ "ADetailer inpaint height",
131
+ ],
132
+ )
133
+ ppop(
134
+ "ADetailer use separate steps",
135
+ ["ADetailer use separate steps", "ADetailer steps"],
136
+ )
137
+ ppop(
138
+ "ADetailer use separate CFG scale",
139
+ ["ADetailer use separate CFG scale", "ADetailer CFG scale"],
140
+ )
141
+ ppop(
142
+ "ADetailer use separate checkpoint",
143
+ ["ADetailer use separate checkpoint", "ADetailer checkpoint"],
144
+ )
145
+ ppop(
146
+ "ADetailer use separate VAE",
147
+ ["ADetailer use separate VAE", "ADetailer VAE"],
148
+ )
149
+ ppop(
150
+ "ADetailer use separate sampler",
151
+ ["ADetailer use separate sampler", "ADetailer sampler"],
152
+ )
153
+ ppop(
154
+ "ADetailer use separate noise multiplier",
155
+ ["ADetailer use separate noise multiplier", "ADetailer noise multiplier"],
156
+ )
157
+
158
+ ppop(
159
+ "ADetailer use separate CLIP skip",
160
+ ["ADetailer use separate CLIP skip", "ADetailer CLIP skip"],
161
+ )
162
+
163
+ ppop("ADetailer restore face")
164
+ ppop(
165
+ "ADetailer ControlNet model",
166
+ [
167
+ "ADetailer ControlNet model",
168
+ "ADetailer ControlNet module",
169
+ "ADetailer ControlNet weight",
170
+ "ADetailer ControlNet guidance start",
171
+ "ADetailer ControlNet guidance end",
172
+ ],
173
+ cond="None",
174
+ )
175
+ ppop("ADetailer ControlNet module", cond="None")
176
+ ppop("ADetailer ControlNet weight", cond=1.0)
177
+ ppop("ADetailer ControlNet guidance start", cond=0.0)
178
+ ppop("ADetailer ControlNet guidance end", cond=1.0)
179
+
180
+ if suffix:
181
+ p = {k + suffix: v for k, v in p.items()}
182
+
183
+ return p
184
+
185
+
186
+ _all_args = [
187
+ ("ad_model", "ADetailer model"),
188
+ ("ad_prompt", "ADetailer prompt"),
189
+ ("ad_negative_prompt", "ADetailer negative prompt"),
190
+ ("ad_confidence", "ADetailer confidence"),
191
+ ("ad_mask_k_largest", "ADetailer mask only top k largest"),
192
+ ("ad_mask_min_ratio", "ADetailer mask min ratio"),
193
+ ("ad_mask_max_ratio", "ADetailer mask max ratio"),
194
+ ("ad_x_offset", "ADetailer x offset"),
195
+ ("ad_y_offset", "ADetailer y offset"),
196
+ ("ad_dilate_erode", "ADetailer dilate erode"),
197
+ ("ad_mask_merge_invert", "ADetailer mask merge invert"),
198
+ ("ad_mask_blur", "ADetailer mask blur"),
199
+ ("ad_denoising_strength", "ADetailer denoising strength"),
200
+ ("ad_inpaint_only_masked", "ADetailer inpaint only masked"),
201
+ ("ad_inpaint_only_masked_padding", "ADetailer inpaint padding"),
202
+ ("ad_use_inpaint_width_height", "ADetailer use inpaint width height"),
203
+ ("ad_inpaint_width", "ADetailer inpaint width"),
204
+ ("ad_inpaint_height", "ADetailer inpaint height"),
205
+ ("ad_use_steps", "ADetailer use separate steps"),
206
+ ("ad_steps", "ADetailer steps"),
207
+ ("ad_use_cfg_scale", "ADetailer use separate CFG scale"),
208
+ ("ad_cfg_scale", "ADetailer CFG scale"),
209
+ ("ad_use_checkpoint", "ADetailer use separate checkpoint"),
210
+ ("ad_checkpoint", "ADetailer checkpoint"),
211
+ ("ad_use_vae", "ADetailer use separate VAE"),
212
+ ("ad_vae", "ADetailer VAE"),
213
+ ("ad_use_sampler", "ADetailer use separate sampler"),
214
+ ("ad_sampler", "ADetailer sampler"),
215
+ ("ad_use_noise_multiplier", "ADetailer use separate noise multiplier"),
216
+ ("ad_noise_multiplier", "ADetailer noise multiplier"),
217
+ ("ad_use_clip_skip", "ADetailer use separate CLIP skip"),
218
+ ("ad_clip_skip", "ADetailer CLIP skip"),
219
+ ("ad_restore_face", "ADetailer restore face"),
220
+ ("ad_controlnet_model", "ADetailer ControlNet model"),
221
+ ("ad_controlnet_module", "ADetailer ControlNet module"),
222
+ ("ad_controlnet_weight", "ADetailer ControlNet weight"),
223
+ ("ad_controlnet_guidance_start", "ADetailer ControlNet guidance start"),
224
+ ("ad_controlnet_guidance_end", "ADetailer ControlNet guidance end"),
225
+ ]
226
+
227
+ _args = [Arg(*args) for args in _all_args]
228
+ ALL_ARGS = ArgsList(_args)
229
+
230
+ BBOX_SORTBY = [
231
+ "None",
232
+ "Position (left to right)",
233
+ "Position (center to edge)",
234
+ "Area (large to small)",
235
+ ]
236
+ MASK_MERGE_INVERT = ["None", "Merge", "Merge and Invert"]
adetailer/adetailer/common.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from collections import OrderedDict
4
+ from dataclasses import dataclass, field
5
+ from pathlib import Path
6
+ from typing import Optional, Union
7
+
8
+ from huggingface_hub import hf_hub_download
9
+ from PIL import Image, ImageDraw
10
+ from rich import print
11
+
12
+ repo_id = "Bingsu/adetailer"
13
+ _download_failed = False
14
+
15
+
16
+ @dataclass
17
+ class PredictOutput:
18
+ bboxes: list[list[int | float]] = field(default_factory=list)
19
+ masks: list[Image.Image] = field(default_factory=list)
20
+ preview: Optional[Image.Image] = None
21
+
22
+
23
+ def hf_download(file: str):
24
+ global _download_failed
25
+
26
+ if _download_failed:
27
+ return "INVALID"
28
+
29
+ try:
30
+ path = hf_hub_download(repo_id, file)
31
+ except Exception:
32
+ msg = f"[-] ADetailer: Failed to load model {file!r} from huggingface"
33
+ print(msg)
34
+ path = "INVALID"
35
+ _download_failed = True
36
+ return path
37
+
38
+
39
+ def scan_model_dir(path_: str | Path) -> list[Path]:
40
+ if not path_ or not (path := Path(path_)).is_dir():
41
+ return []
42
+ return [p for p in path.rglob("*") if p.is_file() and p.suffix in (".pt", ".pth")]
43
+
44
+
45
+ def get_models(
46
+ model_dir: str | Path, extra_dir: str | Path = "", huggingface: bool = True
47
+ ) -> OrderedDict[str, str | None]:
48
+ model_paths = [*scan_model_dir(model_dir), *scan_model_dir(extra_dir)]
49
+
50
+ models = OrderedDict()
51
+ if huggingface:
52
+ models.update(
53
+ {
54
+ "face_yolov8n.pt": hf_download("face_yolov8n.pt"),
55
+ "face_yolov8s.pt": hf_download("face_yolov8s.pt"),
56
+ "hand_yolov8n.pt": hf_download("hand_yolov8n.pt"),
57
+ "person_yolov8n-seg.pt": hf_download("person_yolov8n-seg.pt"),
58
+ "person_yolov8s-seg.pt": hf_download("person_yolov8s-seg.pt"),
59
+ }
60
+ )
61
+ models.update(
62
+ {
63
+ "mediapipe_face_full": None,
64
+ "mediapipe_face_short": None,
65
+ "mediapipe_face_mesh": None,
66
+ "mediapipe_face_mesh_eyes_only": None,
67
+ }
68
+ )
69
+
70
+ invalid_keys = [k for k, v in models.items() if v == "INVALID"]
71
+ for key in invalid_keys:
72
+ models.pop(key)
73
+
74
+ for path in model_paths:
75
+ if path.name in models:
76
+ continue
77
+ models[path.name] = str(path)
78
+
79
+ return models
80
+
81
+
82
+ def create_mask_from_bbox(
83
+ bboxes: list[list[float]], shape: tuple[int, int]
84
+ ) -> list[Image.Image]:
85
+ """
86
+ Parameters
87
+ ----------
88
+ bboxes: list[list[float]]
89
+ list of [x1, y1, x2, y2]
90
+ bounding boxes
91
+ shape: tuple[int, int]
92
+ shape of the image (width, height)
93
+
94
+ Returns
95
+ -------
96
+ masks: list[Image.Image]
97
+ A list of masks
98
+
99
+ """
100
+ masks = []
101
+ for bbox in bboxes:
102
+ mask = Image.new("L", shape, 0)
103
+ mask_draw = ImageDraw.Draw(mask)
104
+ mask_draw.rectangle(bbox, fill=255)
105
+ masks.append(mask)
106
+ return masks
107
+
108
+
109
+ def create_bbox_from_mask(
110
+ masks: list[Image.Image], shape: tuple[int, int]
111
+ ) -> list[list[int]]:
112
+ """
113
+ Parameters
114
+ ----------
115
+ masks: list[Image.Image]
116
+ A list of masks
117
+ shape: tuple[int, int]
118
+ shape of the image (width, height)
119
+
120
+ Returns
121
+ -------
122
+ bboxes: list[list[float]]
123
+ A list of bounding boxes
124
+
125
+ """
126
+ bboxes = []
127
+ for mask in masks:
128
+ mask = mask.resize(shape)
129
+ bbox = mask.getbbox()
130
+ if bbox is not None:
131
+ bboxes.append(list(bbox))
132
+ return bboxes
adetailer/adetailer/mask.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from enum import IntEnum
4
+ from functools import partial, reduce
5
+ from math import dist
6
+
7
+ import cv2
8
+ import numpy as np
9
+ from PIL import Image, ImageChops
10
+
11
+ from adetailer.args import MASK_MERGE_INVERT
12
+ from adetailer.common import PredictOutput
13
+
14
+
15
+ class SortBy(IntEnum):
16
+ NONE = 0
17
+ LEFT_TO_RIGHT = 1
18
+ CENTER_TO_EDGE = 2
19
+ AREA = 3
20
+
21
+
22
+ class MergeInvert(IntEnum):
23
+ NONE = 0
24
+ MERGE = 1
25
+ MERGE_INVERT = 2
26
+
27
+
28
+ def _dilate(arr: np.ndarray, value: int) -> np.ndarray:
29
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (value, value))
30
+ return cv2.dilate(arr, kernel, iterations=1)
31
+
32
+
33
+ def _erode(arr: np.ndarray, value: int) -> np.ndarray:
34
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (value, value))
35
+ return cv2.erode(arr, kernel, iterations=1)
36
+
37
+
38
+ def dilate_erode(img: Image.Image, value: int) -> Image.Image:
39
+ """
40
+ The dilate_erode function takes an image and a value.
41
+ If the value is positive, it dilates the image by that amount.
42
+ If the value is negative, it erodes the image by that amount.
43
+
44
+ Parameters
45
+ ----------
46
+ img: PIL.Image.Image
47
+ the image to be processed
48
+ value: int
49
+ kernel size of dilation or erosion
50
+
51
+ Returns
52
+ -------
53
+ PIL.Image.Image
54
+ The image that has been dilated or eroded
55
+ """
56
+ if value == 0:
57
+ return img
58
+
59
+ arr = np.array(img)
60
+ arr = _dilate(arr, value) if value > 0 else _erode(arr, -value)
61
+
62
+ return Image.fromarray(arr)
63
+
64
+
65
+ def offset(img: Image.Image, x: int = 0, y: int = 0) -> Image.Image:
66
+ """
67
+ The offset function takes an image and offsets it by a given x(→) and y(↑) value.
68
+
69
+ Parameters
70
+ ----------
71
+ mask: Image.Image
72
+ Pass the mask image to the function
73
+ x: int
74
+
75
+ y: int
76
+
77
+
78
+ Returns
79
+ -------
80
+ PIL.Image.Image
81
+ A new image that is offset by x and y
82
+ """
83
+ return ImageChops.offset(img, x, -y)
84
+
85
+
86
+ def is_all_black(img: Image.Image) -> bool:
87
+ arr = np.array(img)
88
+ return cv2.countNonZero(arr) == 0
89
+
90
+
91
+ def bbox_area(bbox: list[float]):
92
+ return (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
93
+
94
+
95
+ def mask_preprocess(
96
+ masks: list[Image.Image],
97
+ kernel: int = 0,
98
+ x_offset: int = 0,
99
+ y_offset: int = 0,
100
+ merge_invert: int | MergeInvert | str = MergeInvert.NONE,
101
+ ) -> list[Image.Image]:
102
+ """
103
+ The mask_preprocess function takes a list of masks and preprocesses them.
104
+ It dilates and erodes the masks, and offsets them by x_offset and y_offset.
105
+
106
+ Parameters
107
+ ----------
108
+ masks: list[Image.Image]
109
+ A list of masks
110
+ kernel: int
111
+ kernel size of dilation or erosion
112
+ x_offset: int
113
+
114
+ y_offset: int
115
+
116
+
117
+ Returns
118
+ -------
119
+ list[Image.Image]
120
+ A list of processed masks
121
+ """
122
+ if not masks:
123
+ return []
124
+
125
+ if x_offset != 0 or y_offset != 0:
126
+ masks = [offset(m, x_offset, y_offset) for m in masks]
127
+
128
+ if kernel != 0:
129
+ masks = [dilate_erode(m, kernel) for m in masks]
130
+ masks = [m for m in masks if not is_all_black(m)]
131
+
132
+ return mask_merge_invert(masks, mode=merge_invert)
133
+
134
+
135
+ # Bbox sorting
136
+ def _key_left_to_right(bbox: list[float]) -> float:
137
+ """
138
+ Left to right
139
+
140
+ Parameters
141
+ ----------
142
+ bbox: list[float]
143
+ list of [x1, y1, x2, y2]
144
+ """
145
+ return bbox[0]
146
+
147
+
148
+ def _key_center_to_edge(bbox: list[float], *, center: tuple[float, float]) -> float:
149
+ """
150
+ Center to edge
151
+
152
+ Parameters
153
+ ----------
154
+ bbox: list[float]
155
+ list of [x1, y1, x2, y2]
156
+ image: Image.Image
157
+ the image
158
+ """
159
+ bbox_center = ((bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2)
160
+ return dist(center, bbox_center)
161
+
162
+
163
+ def _key_area(bbox: list[float]) -> float:
164
+ """
165
+ Large to small
166
+
167
+ Parameters
168
+ ----------
169
+ bbox: list[float]
170
+ list of [x1, y1, x2, y2]
171
+ """
172
+ return -bbox_area(bbox)
173
+
174
+
175
+ def sort_bboxes(
176
+ pred: PredictOutput, order: int | SortBy = SortBy.NONE
177
+ ) -> PredictOutput:
178
+ if order == SortBy.NONE or len(pred.bboxes) <= 1:
179
+ return pred
180
+
181
+ if order == SortBy.LEFT_TO_RIGHT:
182
+ key = _key_left_to_right
183
+ elif order == SortBy.CENTER_TO_EDGE:
184
+ width, height = pred.preview.size
185
+ center = (width / 2, height / 2)
186
+ key = partial(_key_center_to_edge, center=center)
187
+ elif order == SortBy.AREA:
188
+ key = _key_area
189
+ else:
190
+ raise RuntimeError
191
+
192
+ items = len(pred.bboxes)
193
+ idx = sorted(range(items), key=lambda i: key(pred.bboxes[i]))
194
+ pred.bboxes = [pred.bboxes[i] for i in idx]
195
+ pred.masks = [pred.masks[i] for i in idx]
196
+ return pred
197
+
198
+
199
+ # Filter by ratio
200
+ def is_in_ratio(bbox: list[float], low: float, high: float, orig_area: int) -> bool:
201
+ area = bbox_area(bbox)
202
+ return low <= area / orig_area <= high
203
+
204
+
205
+ def filter_by_ratio(pred: PredictOutput, low: float, high: float) -> PredictOutput:
206
+ if not pred.bboxes:
207
+ return pred
208
+
209
+ w, h = pred.preview.size
210
+ orig_area = w * h
211
+ items = len(pred.bboxes)
212
+ idx = [i for i in range(items) if is_in_ratio(pred.bboxes[i], low, high, orig_area)]
213
+ pred.bboxes = [pred.bboxes[i] for i in idx]
214
+ pred.masks = [pred.masks[i] for i in idx]
215
+ return pred
216
+
217
+
218
+ def filter_k_largest(pred: PredictOutput, k: int = 0) -> PredictOutput:
219
+ if not pred.bboxes or k == 0:
220
+ return pred
221
+ areas = [bbox_area(bbox) for bbox in pred.bboxes]
222
+ idx = np.argsort(areas)[-k:]
223
+ idx = idx[::-1]
224
+ pred.bboxes = [pred.bboxes[i] for i in idx]
225
+ pred.masks = [pred.masks[i] for i in idx]
226
+ return pred
227
+
228
+
229
+ # Merge / Invert
230
+ def mask_merge(masks: list[Image.Image]) -> list[Image.Image]:
231
+ arrs = [np.array(m) for m in masks]
232
+ arr = reduce(cv2.bitwise_or, arrs)
233
+ return [Image.fromarray(arr)]
234
+
235
+
236
+ def mask_invert(masks: list[Image.Image]) -> list[Image.Image]:
237
+ return [ImageChops.invert(m) for m in masks]
238
+
239
+
240
+ def mask_merge_invert(
241
+ masks: list[Image.Image], mode: int | MergeInvert | str
242
+ ) -> list[Image.Image]:
243
+ if isinstance(mode, str):
244
+ mode = MASK_MERGE_INVERT.index(mode)
245
+
246
+ if mode == MergeInvert.NONE or not masks:
247
+ return masks
248
+
249
+ if mode == MergeInvert.MERGE:
250
+ return mask_merge(masks)
251
+
252
+ if mode == MergeInvert.MERGE_INVERT:
253
+ merged = mask_merge(masks)
254
+ return mask_invert(merged)
255
+
256
+ raise RuntimeError
adetailer/adetailer/mediapipe.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from functools import partial
4
+
5
+ import cv2
6
+ import numpy as np
7
+ from PIL import Image, ImageDraw
8
+
9
+ from adetailer import PredictOutput
10
+ from adetailer.common import create_bbox_from_mask, create_mask_from_bbox
11
+
12
+
13
+ def mediapipe_predict(
14
+ model_type: str, image: Image.Image, confidence: float = 0.3
15
+ ) -> PredictOutput:
16
+ mapping = {
17
+ "mediapipe_face_short": partial(mediapipe_face_detection, 0),
18
+ "mediapipe_face_full": partial(mediapipe_face_detection, 1),
19
+ "mediapipe_face_mesh": mediapipe_face_mesh,
20
+ "mediapipe_face_mesh_eyes_only": mediapipe_face_mesh_eyes_only,
21
+ }
22
+ if model_type in mapping:
23
+ func = mapping[model_type]
24
+ return func(image, confidence)
25
+ msg = f"[-] ADetailer: Invalid mediapipe model type: {model_type}, Available: {list(mapping.keys())!r}"
26
+ raise RuntimeError(msg)
27
+
28
+
29
+ def mediapipe_face_detection(
30
+ model_type: int, image: Image.Image, confidence: float = 0.3
31
+ ) -> PredictOutput:
32
+ import mediapipe as mp
33
+
34
+ img_width, img_height = image.size
35
+
36
+ mp_face_detection = mp.solutions.face_detection
37
+ draw_util = mp.solutions.drawing_utils
38
+
39
+ img_array = np.array(image)
40
+
41
+ with mp_face_detection.FaceDetection(
42
+ model_selection=model_type, min_detection_confidence=confidence
43
+ ) as face_detector:
44
+ pred = face_detector.process(img_array)
45
+
46
+ if pred.detections is None:
47
+ return PredictOutput()
48
+
49
+ preview_array = img_array.copy()
50
+
51
+ bboxes = []
52
+ for detection in pred.detections:
53
+ draw_util.draw_detection(preview_array, detection)
54
+
55
+ bbox = detection.location_data.relative_bounding_box
56
+ x1 = bbox.xmin * img_width
57
+ y1 = bbox.ymin * img_height
58
+ w = bbox.width * img_width
59
+ h = bbox.height * img_height
60
+ x2 = x1 + w
61
+ y2 = y1 + h
62
+
63
+ bboxes.append([x1, y1, x2, y2])
64
+
65
+ masks = create_mask_from_bbox(bboxes, image.size)
66
+ preview = Image.fromarray(preview_array)
67
+
68
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
69
+
70
+
71
+ def mediapipe_face_mesh(image: Image.Image, confidence: float = 0.3) -> PredictOutput:
72
+ import mediapipe as mp
73
+
74
+ mp_face_mesh = mp.solutions.face_mesh
75
+ draw_util = mp.solutions.drawing_utils
76
+ drawing_styles = mp.solutions.drawing_styles
77
+
78
+ w, h = image.size
79
+
80
+ with mp_face_mesh.FaceMesh(
81
+ static_image_mode=True, max_num_faces=20, min_detection_confidence=confidence
82
+ ) as face_mesh:
83
+ arr = np.array(image)
84
+ pred = face_mesh.process(arr)
85
+
86
+ if pred.multi_face_landmarks is None:
87
+ return PredictOutput()
88
+
89
+ preview = arr.copy()
90
+ masks = []
91
+
92
+ for landmarks in pred.multi_face_landmarks:
93
+ draw_util.draw_landmarks(
94
+ image=preview,
95
+ landmark_list=landmarks,
96
+ connections=mp_face_mesh.FACEMESH_TESSELATION,
97
+ landmark_drawing_spec=None,
98
+ connection_drawing_spec=drawing_styles.get_default_face_mesh_tesselation_style(),
99
+ )
100
+
101
+ points = np.intp([(land.x * w, land.y * h) for land in landmarks.landmark])
102
+ outline = cv2.convexHull(points).reshape(-1).tolist()
103
+
104
+ mask = Image.new("L", image.size, "black")
105
+ draw = ImageDraw.Draw(mask)
106
+ draw.polygon(outline, fill="white")
107
+ masks.append(mask)
108
+
109
+ bboxes = create_bbox_from_mask(masks, image.size)
110
+ preview = Image.fromarray(preview)
111
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
112
+
113
+
114
+ def mediapipe_face_mesh_eyes_only(
115
+ image: Image.Image, confidence: float = 0.3
116
+ ) -> PredictOutput:
117
+ import mediapipe as mp
118
+
119
+ mp_face_mesh = mp.solutions.face_mesh
120
+
121
+ left_idx = np.array(list(mp_face_mesh.FACEMESH_LEFT_EYE)).flatten()
122
+ right_idx = np.array(list(mp_face_mesh.FACEMESH_RIGHT_EYE)).flatten()
123
+
124
+ w, h = image.size
125
+
126
+ with mp_face_mesh.FaceMesh(
127
+ static_image_mode=True, max_num_faces=20, min_detection_confidence=confidence
128
+ ) as face_mesh:
129
+ arr = np.array(image)
130
+ pred = face_mesh.process(arr)
131
+
132
+ if pred.multi_face_landmarks is None:
133
+ return PredictOutput()
134
+
135
+ preview = image.copy()
136
+ masks = []
137
+
138
+ for landmarks in pred.multi_face_landmarks:
139
+ points = np.intp([(land.x * w, land.y * h) for land in landmarks.landmark])
140
+ left_eyes = points[left_idx]
141
+ right_eyes = points[right_idx]
142
+ left_outline = cv2.convexHull(left_eyes).reshape(-1).tolist()
143
+ right_outline = cv2.convexHull(right_eyes).reshape(-1).tolist()
144
+
145
+ mask = Image.new("L", image.size, "black")
146
+ draw = ImageDraw.Draw(mask)
147
+ for outline in (left_outline, right_outline):
148
+ draw.polygon(outline, fill="white")
149
+ masks.append(mask)
150
+
151
+ bboxes = create_bbox_from_mask(masks, image.size)
152
+ preview = draw_preview(preview, bboxes, masks)
153
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
154
+
155
+
156
+ def draw_preview(
157
+ preview: Image.Image, bboxes: list[list[int]], masks: list[Image.Image]
158
+ ) -> Image.Image:
159
+ red = Image.new("RGB", preview.size, "red")
160
+ for mask in masks:
161
+ masked = Image.composite(red, preview, mask)
162
+ preview = Image.blend(preview, masked, 0.25)
163
+
164
+ draw = ImageDraw.Draw(preview)
165
+ for bbox in bboxes:
166
+ draw.rectangle(bbox, outline="red", width=2)
167
+
168
+ return preview
adetailer/adetailer/traceback.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import io
4
+ import platform
5
+ import sys
6
+ from importlib.metadata import version
7
+ from typing import Any, Callable
8
+
9
+ from rich.console import Console, Group
10
+ from rich.panel import Panel
11
+ from rich.table import Table
12
+ from rich.traceback import Traceback
13
+
14
+ from adetailer.__version__ import __version__
15
+
16
+
17
+ def processing(*args: Any) -> dict[str, Any]:
18
+ try:
19
+ from modules.processing import (
20
+ StableDiffusionProcessingImg2Img,
21
+ StableDiffusionProcessingTxt2Img,
22
+ )
23
+ except ImportError:
24
+ return {}
25
+
26
+ p = None
27
+ for arg in args:
28
+ if isinstance(
29
+ arg, (StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img)
30
+ ):
31
+ p = arg
32
+ break
33
+
34
+ if p is None:
35
+ return {}
36
+
37
+ info = {
38
+ "prompt": p.prompt,
39
+ "negative_prompt": p.negative_prompt,
40
+ "n_iter": p.n_iter,
41
+ "batch_size": p.batch_size,
42
+ "width": p.width,
43
+ "height": p.height,
44
+ "sampler_name": p.sampler_name,
45
+ "enable_hr": getattr(p, "enable_hr", False),
46
+ "hr_upscaler": getattr(p, "hr_upscaler", ""),
47
+ }
48
+
49
+ info.update(sd_models())
50
+ return info
51
+
52
+
53
+ def sd_models() -> dict[str, str]:
54
+ try:
55
+ from modules import shared
56
+
57
+ opts = shared.opts
58
+ except Exception:
59
+ return {}
60
+
61
+ return {
62
+ "checkpoint": getattr(opts, "sd_model_checkpoint", "------"),
63
+ "vae": getattr(opts, "sd_vae", "------"),
64
+ "unet": getattr(opts, "sd_unet", "------"),
65
+ }
66
+
67
+
68
+ def ad_args(*args: Any) -> dict[str, Any]:
69
+ ad_args = [
70
+ arg
71
+ for arg in args
72
+ if isinstance(arg, dict) and arg.get("ad_model", "None") != "None"
73
+ ]
74
+ if not ad_args:
75
+ return {}
76
+
77
+ arg0 = ad_args[0]
78
+ is_api = arg0.get("is_api", True)
79
+ return {
80
+ "version": __version__,
81
+ "ad_model": arg0["ad_model"],
82
+ "ad_prompt": arg0.get("ad_prompt", ""),
83
+ "ad_negative_prompt": arg0.get("ad_negative_prompt", ""),
84
+ "ad_controlnet_model": arg0.get("ad_controlnet_model", "None"),
85
+ "is_api": type(is_api) is not tuple,
86
+ }
87
+
88
+
89
+ def library_version():
90
+ libraries = ["torch", "torchvision", "ultralytics", "mediapipe"]
91
+ d = {}
92
+ for lib in libraries:
93
+ try:
94
+ d[lib] = version(lib)
95
+ except Exception: # noqa: PERF203
96
+ d[lib] = "Unknown"
97
+ return d
98
+
99
+
100
+ def sys_info() -> dict[str, Any]:
101
+ try:
102
+ import launch
103
+
104
+ version = launch.git_tag()
105
+ commit = launch.commit_hash()
106
+ except Exception:
107
+ version = "Unknown (too old or vladmandic)"
108
+ commit = "Unknown"
109
+
110
+ return {
111
+ "Platform": platform.platform(),
112
+ "Python": sys.version,
113
+ "Version": version,
114
+ "Commit": commit,
115
+ "Commandline": sys.argv,
116
+ "Libraries": library_version(),
117
+ }
118
+
119
+
120
+ def get_table(title: str, data: dict[str, Any]) -> Table:
121
+ table = Table(title=title, highlight=True)
122
+ table.add_column(" ", justify="right", style="dim")
123
+ table.add_column("Value")
124
+ for key, value in data.items():
125
+ if not isinstance(value, str):
126
+ value = repr(value)
127
+ table.add_row(key, value)
128
+
129
+ return table
130
+
131
+
132
+ def rich_traceback(func: Callable) -> Callable:
133
+ def wrapper(*args, **kwargs):
134
+ string = io.StringIO()
135
+ width = Console().width
136
+ width = width - 4 if width > 4 else None
137
+ console = Console(file=string, width=width)
138
+ try:
139
+ return func(*args, **kwargs)
140
+ except Exception as e:
141
+ tables = [
142
+ get_table(title, data)
143
+ for title, data in [
144
+ ("System info", sys_info()),
145
+ ("Inputs", processing(*args)),
146
+ ("ADetailer", ad_args(*args)),
147
+ ]
148
+ if data
149
+ ]
150
+ tables.append(Traceback(extra_lines=1))
151
+
152
+ console.print(Panel(Group(*tables)))
153
+ output = "\n" + string.getvalue()
154
+
155
+ try:
156
+ error = e.__class__(output)
157
+ except Exception:
158
+ error = RuntimeError(output)
159
+ raise error from None
160
+
161
+ return wrapper
adetailer/adetailer/ui.py ADDED
@@ -0,0 +1,640 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from functools import partial
5
+ from types import SimpleNamespace
6
+ from typing import Any
7
+
8
+ import gradio as gr
9
+
10
+ from adetailer import AFTER_DETAILER, __version__
11
+ from adetailer.args import ALL_ARGS, MASK_MERGE_INVERT
12
+ from controlnet_ext import controlnet_exists, get_cn_models
13
+
14
+ cn_module_choices = {
15
+ "inpaint": [
16
+ "inpaint_global_harmonious",
17
+ "inpaint_only",
18
+ "inpaint_only+lama",
19
+ ],
20
+ "lineart": [
21
+ "lineart_coarse",
22
+ "lineart_realistic",
23
+ "lineart_anime",
24
+ "lineart_anime_denoise",
25
+ ],
26
+ "openpose": ["openpose_full", "dw_openpose_full"],
27
+ "tile": ["tile_resample", "tile_colorfix", "tile_colorfix+sharp"],
28
+ "scribble": ["t2ia_sketch_pidi"],
29
+ "depth": ["depth_midas", "depth_hand_refiner"],
30
+ }
31
+
32
+
33
+ class Widgets(SimpleNamespace):
34
+ def tolist(self):
35
+ return [getattr(self, attr) for attr in ALL_ARGS.attrs]
36
+
37
+
38
+ @dataclass
39
+ class WebuiInfo:
40
+ ad_model_list: list[str]
41
+ sampler_names: list[str]
42
+ t2i_button: gr.Button
43
+ i2i_button: gr.Button
44
+ checkpoints_list: list[str]
45
+ vae_list: list[str]
46
+
47
+
48
+ def gr_interactive(value: bool = True):
49
+ return gr.update(interactive=value)
50
+
51
+
52
+ def ordinal(n: int) -> str:
53
+ d = {1: "st", 2: "nd", 3: "rd"}
54
+ return str(n) + ("th" if 11 <= n % 100 <= 13 else d.get(n % 10, "th"))
55
+
56
+
57
+ def suffix(n: int, c: str = " ") -> str:
58
+ return "" if n == 0 else c + ordinal(n + 1)
59
+
60
+
61
+ def on_widget_change(state: dict, value: Any, *, attr: str):
62
+ if "is_api" in state:
63
+ state = state.copy()
64
+ state.pop("is_api")
65
+ state[attr] = value
66
+ return state
67
+
68
+
69
+ def on_generate_click(state: dict, *values: Any):
70
+ for attr, value in zip(ALL_ARGS.attrs, values):
71
+ state[attr] = value
72
+ state["is_api"] = ()
73
+ return state
74
+
75
+
76
+ def on_cn_model_update(cn_model_name: str):
77
+ cn_model_name = cn_model_name.replace("inpaint_depth", "depth")
78
+ for t in cn_module_choices:
79
+ if t in cn_model_name:
80
+ choices = cn_module_choices[t]
81
+ return gr.update(visible=True, choices=choices, value=choices[0])
82
+ return gr.update(visible=False, choices=["None"], value="None")
83
+
84
+
85
+ def elem_id(item_id: str, n: int, is_img2img: bool) -> str:
86
+ tap = "img2img" if is_img2img else "txt2img"
87
+ suf = suffix(n, "_")
88
+ return f"script_{tap}_adetailer_{item_id}{suf}"
89
+
90
+
91
+ def state_init(w: Widgets) -> dict[str, Any]:
92
+ return {attr: getattr(w, attr).value for attr in ALL_ARGS.attrs}
93
+
94
+
95
+ def adui(
96
+ num_models: int,
97
+ is_img2img: bool,
98
+ webui_info: WebuiInfo,
99
+ ):
100
+ states = []
101
+ infotext_fields = []
102
+ eid = partial(elem_id, n=0, is_img2img=is_img2img)
103
+
104
+ with gr.Accordion(AFTER_DETAILER, open=False, elem_id=eid("ad_main_accordion")):
105
+ with gr.Row():
106
+ with gr.Column(scale=6):
107
+ ad_enable = gr.Checkbox(
108
+ label="Enable ADetailer",
109
+ value=False,
110
+ visible=True,
111
+ elem_id=eid("ad_enable"),
112
+ )
113
+
114
+ with gr.Column(scale=6):
115
+ ad_skip_img2img = gr.Checkbox(
116
+ label="Skip img2img",
117
+ value=False,
118
+ visible=is_img2img,
119
+ elem_id=eid("ad_skip_img2img"),
120
+ )
121
+
122
+ with gr.Column(scale=1, min_width=180):
123
+ gr.Markdown(
124
+ f"v{__version__}",
125
+ elem_id=eid("ad_version"),
126
+ )
127
+
128
+ infotext_fields.append((ad_enable, "ADetailer enable"))
129
+ infotext_fields.append((ad_skip_img2img, "ADetailer skip img2img"))
130
+
131
+ with gr.Group(), gr.Tabs():
132
+ for n in range(num_models):
133
+ with gr.Tab(ordinal(n + 1)):
134
+ state, infofields = one_ui_group(
135
+ n=n,
136
+ is_img2img=is_img2img,
137
+ webui_info=webui_info,
138
+ )
139
+
140
+ states.append(state)
141
+ infotext_fields.extend(infofields)
142
+
143
+ # components: [bool, dict, dict, ...]
144
+ components = [ad_enable, ad_skip_img2img, *states]
145
+ return components, infotext_fields
146
+
147
+
148
+ def one_ui_group(n: int, is_img2img: bool, webui_info: WebuiInfo):
149
+ w = Widgets()
150
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
151
+
152
+ with gr.Row():
153
+ model_choices = (
154
+ [*webui_info.ad_model_list, "None"]
155
+ if n == 0
156
+ else ["None", *webui_info.ad_model_list]
157
+ )
158
+
159
+ w.ad_model = gr.Dropdown(
160
+ label="ADetailer model" + suffix(n),
161
+ choices=model_choices,
162
+ value=model_choices[0],
163
+ visible=True,
164
+ type="value",
165
+ elem_id=eid("ad_model"),
166
+ )
167
+
168
+ with gr.Group():
169
+ with gr.Row(elem_id=eid("ad_toprow_prompt")):
170
+ w.ad_prompt = gr.Textbox(
171
+ label="ad_prompt" + suffix(n),
172
+ show_label=False,
173
+ lines=3,
174
+ placeholder="ADetailer prompt"
175
+ + suffix(n)
176
+ + "\nIf blank, the main prompt is used.",
177
+ elem_id=eid("ad_prompt"),
178
+ )
179
+
180
+ with gr.Row(elem_id=eid("ad_toprow_negative_prompt")):
181
+ w.ad_negative_prompt = gr.Textbox(
182
+ label="ad_negative_prompt" + suffix(n),
183
+ show_label=False,
184
+ lines=2,
185
+ placeholder="ADetailer negative prompt"
186
+ + suffix(n)
187
+ + "\nIf blank, the main negative prompt is used.",
188
+ elem_id=eid("ad_negative_prompt"),
189
+ )
190
+
191
+ with gr.Group():
192
+ with gr.Accordion(
193
+ "Detection", open=False, elem_id=eid("ad_detection_accordion")
194
+ ):
195
+ detection(w, n, is_img2img)
196
+
197
+ with gr.Accordion(
198
+ "Mask Preprocessing",
199
+ open=False,
200
+ elem_id=eid("ad_mask_preprocessing_accordion"),
201
+ ):
202
+ mask_preprocessing(w, n, is_img2img)
203
+
204
+ with gr.Accordion(
205
+ "Inpainting", open=False, elem_id=eid("ad_inpainting_accordion")
206
+ ):
207
+ inpainting(w, n, is_img2img, webui_info)
208
+
209
+ with gr.Group():
210
+ controlnet(w, n, is_img2img)
211
+
212
+ state = gr.State(lambda: state_init(w))
213
+
214
+ for attr in ALL_ARGS.attrs:
215
+ widget = getattr(w, attr)
216
+ on_change = partial(on_widget_change, attr=attr)
217
+ widget.change(fn=on_change, inputs=[state, widget], outputs=state, queue=False)
218
+
219
+ all_inputs = [state, *w.tolist()]
220
+ target_button = webui_info.i2i_button if is_img2img else webui_info.t2i_button
221
+ target_button.click(
222
+ fn=on_generate_click, inputs=all_inputs, outputs=state, queue=False
223
+ )
224
+
225
+ infotext_fields = [(getattr(w, attr), name + suffix(n)) for attr, name in ALL_ARGS]
226
+
227
+ return state, infotext_fields
228
+
229
+
230
+ def detection(w: Widgets, n: int, is_img2img: bool):
231
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
232
+
233
+ with gr.Row():
234
+ with gr.Column(variant="compact"):
235
+ w.ad_confidence = gr.Slider(
236
+ label="Detection model confidence threshold" + suffix(n),
237
+ minimum=0.0,
238
+ maximum=1.0,
239
+ step=0.01,
240
+ value=0.3,
241
+ visible=True,
242
+ elem_id=eid("ad_confidence"),
243
+ )
244
+ w.ad_mask_k_largest = gr.Slider(
245
+ label="Mask only the top k largest (0 to disable)" + suffix(n),
246
+ minimum=0,
247
+ maximum=10,
248
+ step=1,
249
+ value=0,
250
+ visible=True,
251
+ elem_id=eid("ad_mask_k_largest"),
252
+ )
253
+
254
+ with gr.Column(variant="compact"):
255
+ w.ad_mask_min_ratio = gr.Slider(
256
+ label="Mask min area ratio" + suffix(n),
257
+ minimum=0.0,
258
+ maximum=1.0,
259
+ step=0.001,
260
+ value=0.0,
261
+ visible=True,
262
+ elem_id=eid("ad_mask_min_ratio"),
263
+ )
264
+ w.ad_mask_max_ratio = gr.Slider(
265
+ label="Mask max area ratio" + suffix(n),
266
+ minimum=0.0,
267
+ maximum=1.0,
268
+ step=0.001,
269
+ value=1.0,
270
+ visible=True,
271
+ elem_id=eid("ad_mask_max_ratio"),
272
+ )
273
+
274
+
275
+ def mask_preprocessing(w: Widgets, n: int, is_img2img: bool):
276
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
277
+
278
+ with gr.Group():
279
+ with gr.Row():
280
+ with gr.Column(variant="compact"):
281
+ w.ad_x_offset = gr.Slider(
282
+ label="Mask x(→) offset" + suffix(n),
283
+ minimum=-200,
284
+ maximum=200,
285
+ step=1,
286
+ value=0,
287
+ visible=True,
288
+ elem_id=eid("ad_x_offset"),
289
+ )
290
+ w.ad_y_offset = gr.Slider(
291
+ label="Mask y(↑) offset" + suffix(n),
292
+ minimum=-200,
293
+ maximum=200,
294
+ step=1,
295
+ value=0,
296
+ visible=True,
297
+ elem_id=eid("ad_y_offset"),
298
+ )
299
+
300
+ with gr.Column(variant="compact"):
301
+ w.ad_dilate_erode = gr.Slider(
302
+ label="Mask erosion (-) / dilation (+)" + suffix(n),
303
+ minimum=-128,
304
+ maximum=128,
305
+ step=4,
306
+ value=4,
307
+ visible=True,
308
+ elem_id=eid("ad_dilate_erode"),
309
+ )
310
+
311
+ with gr.Row():
312
+ w.ad_mask_merge_invert = gr.Radio(
313
+ label="Mask merge mode" + suffix(n),
314
+ choices=MASK_MERGE_INVERT,
315
+ value="None",
316
+ elem_id=eid("ad_mask_merge_invert"),
317
+ )
318
+
319
+
320
+ def inpainting(w: Widgets, n: int, is_img2img: bool, webui_info: WebuiInfo):
321
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
322
+
323
+ with gr.Group():
324
+ with gr.Row():
325
+ w.ad_mask_blur = gr.Slider(
326
+ label="Inpaint mask blur" + suffix(n),
327
+ minimum=0,
328
+ maximum=64,
329
+ step=1,
330
+ value=4,
331
+ visible=True,
332
+ elem_id=eid("ad_mask_blur"),
333
+ )
334
+
335
+ w.ad_denoising_strength = gr.Slider(
336
+ label="Inpaint denoising strength" + suffix(n),
337
+ minimum=0.0,
338
+ maximum=1.0,
339
+ step=0.01,
340
+ value=0.4,
341
+ visible=True,
342
+ elem_id=eid("ad_denoising_strength"),
343
+ )
344
+
345
+ with gr.Row():
346
+ with gr.Column(variant="compact"):
347
+ w.ad_inpaint_only_masked = gr.Checkbox(
348
+ label="Inpaint only masked" + suffix(n),
349
+ value=True,
350
+ visible=True,
351
+ elem_id=eid("ad_inpaint_only_masked"),
352
+ )
353
+ w.ad_inpaint_only_masked_padding = gr.Slider(
354
+ label="Inpaint only masked padding, pixels" + suffix(n),
355
+ minimum=0,
356
+ maximum=256,
357
+ step=4,
358
+ value=32,
359
+ visible=True,
360
+ elem_id=eid("ad_inpaint_only_masked_padding"),
361
+ )
362
+
363
+ w.ad_inpaint_only_masked.change(
364
+ gr_interactive,
365
+ inputs=w.ad_inpaint_only_masked,
366
+ outputs=w.ad_inpaint_only_masked_padding,
367
+ queue=False,
368
+ )
369
+
370
+ with gr.Column(variant="compact"):
371
+ w.ad_use_inpaint_width_height = gr.Checkbox(
372
+ label="Use separate width/height" + suffix(n),
373
+ value=False,
374
+ visible=True,
375
+ elem_id=eid("ad_use_inpaint_width_height"),
376
+ )
377
+
378
+ w.ad_inpaint_width = gr.Slider(
379
+ label="inpaint width" + suffix(n),
380
+ minimum=64,
381
+ maximum=2048,
382
+ step=4,
383
+ value=512,
384
+ visible=True,
385
+ elem_id=eid("ad_inpaint_width"),
386
+ )
387
+
388
+ w.ad_inpaint_height = gr.Slider(
389
+ label="inpaint height" + suffix(n),
390
+ minimum=64,
391
+ maximum=2048,
392
+ step=4,
393
+ value=512,
394
+ visible=True,
395
+ elem_id=eid("ad_inpaint_height"),
396
+ )
397
+
398
+ w.ad_use_inpaint_width_height.change(
399
+ lambda value: (gr_interactive(value), gr_interactive(value)),
400
+ inputs=w.ad_use_inpaint_width_height,
401
+ outputs=[w.ad_inpaint_width, w.ad_inpaint_height],
402
+ queue=False,
403
+ )
404
+
405
+ with gr.Row():
406
+ with gr.Column(variant="compact"):
407
+ w.ad_use_steps = gr.Checkbox(
408
+ label="Use separate steps" + suffix(n),
409
+ value=False,
410
+ visible=True,
411
+ elem_id=eid("ad_use_steps"),
412
+ )
413
+
414
+ w.ad_steps = gr.Slider(
415
+ label="ADetailer steps" + suffix(n),
416
+ minimum=1,
417
+ maximum=150,
418
+ step=1,
419
+ value=28,
420
+ visible=True,
421
+ elem_id=eid("ad_steps"),
422
+ )
423
+
424
+ w.ad_use_steps.change(
425
+ gr_interactive,
426
+ inputs=w.ad_use_steps,
427
+ outputs=w.ad_steps,
428
+ queue=False,
429
+ )
430
+
431
+ with gr.Column(variant="compact"):
432
+ w.ad_use_cfg_scale = gr.Checkbox(
433
+ label="Use separate CFG scale" + suffix(n),
434
+ value=False,
435
+ visible=True,
436
+ elem_id=eid("ad_use_cfg_scale"),
437
+ )
438
+
439
+ w.ad_cfg_scale = gr.Slider(
440
+ label="ADetailer CFG scale" + suffix(n),
441
+ minimum=0.0,
442
+ maximum=30.0,
443
+ step=0.5,
444
+ value=7.0,
445
+ visible=True,
446
+ elem_id=eid("ad_cfg_scale"),
447
+ )
448
+
449
+ w.ad_use_cfg_scale.change(
450
+ gr_interactive,
451
+ inputs=w.ad_use_cfg_scale,
452
+ outputs=w.ad_cfg_scale,
453
+ queue=False,
454
+ )
455
+
456
+ with gr.Row():
457
+ with gr.Column(variant="compact"):
458
+ w.ad_use_checkpoint = gr.Checkbox(
459
+ label="Use separate checkpoint" + suffix(n),
460
+ value=False,
461
+ visible=True,
462
+ elem_id=eid("ad_use_checkpoint"),
463
+ )
464
+
465
+ ckpts = ["Use same checkpoint", *webui_info.checkpoints_list]
466
+
467
+ w.ad_checkpoint = gr.Dropdown(
468
+ label="ADetailer checkpoint" + suffix(n),
469
+ choices=ckpts,
470
+ value=ckpts[0],
471
+ visible=True,
472
+ elem_id=eid("ad_checkpoint"),
473
+ )
474
+
475
+ with gr.Column(variant="compact"):
476
+ w.ad_use_vae = gr.Checkbox(
477
+ label="Use separate VAE" + suffix(n),
478
+ value=False,
479
+ visible=True,
480
+ elem_id=eid("ad_use_vae"),
481
+ )
482
+
483
+ vaes = ["Use same VAE", *webui_info.vae_list]
484
+
485
+ w.ad_vae = gr.Dropdown(
486
+ label="ADetailer VAE" + suffix(n),
487
+ choices=vaes,
488
+ value=vaes[0],
489
+ visible=True,
490
+ elem_id=eid("ad_vae"),
491
+ )
492
+
493
+ with gr.Row(), gr.Column(variant="compact"):
494
+ w.ad_use_sampler = gr.Checkbox(
495
+ label="Use separate sampler" + suffix(n),
496
+ value=False,
497
+ visible=True,
498
+ elem_id=eid("ad_use_sampler"),
499
+ )
500
+
501
+ w.ad_sampler = gr.Dropdown(
502
+ label="ADetailer sampler" + suffix(n),
503
+ choices=webui_info.sampler_names,
504
+ value=webui_info.sampler_names[0],
505
+ visible=True,
506
+ elem_id=eid("ad_sampler"),
507
+ )
508
+
509
+ w.ad_use_sampler.change(
510
+ gr_interactive,
511
+ inputs=w.ad_use_sampler,
512
+ outputs=w.ad_sampler,
513
+ queue=False,
514
+ )
515
+
516
+ with gr.Row():
517
+ with gr.Column(variant="compact"):
518
+ w.ad_use_noise_multiplier = gr.Checkbox(
519
+ label="Use separate noise multiplier" + suffix(n),
520
+ value=False,
521
+ visible=True,
522
+ elem_id=eid("ad_use_noise_multiplier"),
523
+ )
524
+
525
+ w.ad_noise_multiplier = gr.Slider(
526
+ label="Noise multiplier for img2img" + suffix(n),
527
+ minimum=0.5,
528
+ maximum=1.5,
529
+ step=0.01,
530
+ value=1.0,
531
+ visible=True,
532
+ elem_id=eid("ad_noise_multiplier"),
533
+ )
534
+
535
+ w.ad_use_noise_multiplier.change(
536
+ gr_interactive,
537
+ inputs=w.ad_use_noise_multiplier,
538
+ outputs=w.ad_noise_multiplier,
539
+ queue=False,
540
+ )
541
+
542
+ with gr.Column(variant="compact"):
543
+ w.ad_use_clip_skip = gr.Checkbox(
544
+ label="Use separate CLIP skip" + suffix(n),
545
+ value=False,
546
+ visible=True,
547
+ elem_id=eid("ad_use_clip_skip"),
548
+ )
549
+
550
+ w.ad_clip_skip = gr.Slider(
551
+ label="ADetailer CLIP skip" + suffix(n),
552
+ minimum=1,
553
+ maximum=12,
554
+ step=1,
555
+ value=1,
556
+ visible=True,
557
+ elem_id=eid("ad_clip_skip"),
558
+ )
559
+
560
+ w.ad_use_clip_skip.change(
561
+ gr_interactive,
562
+ inputs=w.ad_use_clip_skip,
563
+ outputs=w.ad_clip_skip,
564
+ queue=False,
565
+ )
566
+
567
+ with gr.Row(), gr.Column(variant="compact"):
568
+ w.ad_restore_face = gr.Checkbox(
569
+ label="Restore faces after ADetailer" + suffix(n),
570
+ value=False,
571
+ elem_id=eid("ad_restore_face"),
572
+ )
573
+
574
+
575
+ def controlnet(w: Widgets, n: int, is_img2img: bool):
576
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
577
+ cn_models = ["None", "Passthrough", *get_cn_models()]
578
+
579
+ with gr.Row(variant="panel"):
580
+ with gr.Column(variant="compact"):
581
+ w.ad_controlnet_model = gr.Dropdown(
582
+ label="ControlNet model" + suffix(n),
583
+ choices=cn_models,
584
+ value="None",
585
+ visible=True,
586
+ type="value",
587
+ interactive=controlnet_exists,
588
+ elem_id=eid("ad_controlnet_model"),
589
+ )
590
+
591
+ w.ad_controlnet_module = gr.Dropdown(
592
+ label="ControlNet module" + suffix(n),
593
+ choices=["None"],
594
+ value="None",
595
+ visible=False,
596
+ type="value",
597
+ interactive=controlnet_exists,
598
+ elem_id=eid("ad_controlnet_module"),
599
+ )
600
+
601
+ w.ad_controlnet_weight = gr.Slider(
602
+ label="ControlNet weight" + suffix(n),
603
+ minimum=0.0,
604
+ maximum=1.0,
605
+ step=0.01,
606
+ value=1.0,
607
+ visible=True,
608
+ interactive=controlnet_exists,
609
+ elem_id=eid("ad_controlnet_weight"),
610
+ )
611
+
612
+ w.ad_controlnet_model.change(
613
+ on_cn_model_update,
614
+ inputs=w.ad_controlnet_model,
615
+ outputs=w.ad_controlnet_module,
616
+ queue=False,
617
+ )
618
+
619
+ with gr.Column(variant="compact"):
620
+ w.ad_controlnet_guidance_start = gr.Slider(
621
+ label="ControlNet guidance start" + suffix(n),
622
+ minimum=0.0,
623
+ maximum=1.0,
624
+ step=0.01,
625
+ value=0.0,
626
+ visible=True,
627
+ interactive=controlnet_exists,
628
+ elem_id=eid("ad_controlnet_guidance_start"),
629
+ )
630
+
631
+ w.ad_controlnet_guidance_end = gr.Slider(
632
+ label="ControlNet guidance end" + suffix(n),
633
+ minimum=0.0,
634
+ maximum=1.0,
635
+ step=0.01,
636
+ value=1.0,
637
+ visible=True,
638
+ interactive=controlnet_exists,
639
+ elem_id=eid("ad_controlnet_guidance_end"),
640
+ )
adetailer/adetailer/ultralytics.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+
5
+ import cv2
6
+ from PIL import Image
7
+ from torchvision.transforms.functional import to_pil_image
8
+
9
+ from adetailer import PredictOutput
10
+ from adetailer.common import create_mask_from_bbox
11
+
12
+
13
+ def ultralytics_predict(
14
+ model_path: str | Path,
15
+ image: Image.Image,
16
+ confidence: float = 0.3,
17
+ device: str = "",
18
+ ) -> PredictOutput:
19
+ from ultralytics import YOLO
20
+
21
+ model = YOLO(model_path)
22
+ pred = model(image, conf=confidence, device=device)
23
+
24
+ bboxes = pred[0].boxes.xyxy.cpu().numpy()
25
+ if bboxes.size == 0:
26
+ return PredictOutput()
27
+ bboxes = bboxes.tolist()
28
+
29
+ if pred[0].masks is None:
30
+ masks = create_mask_from_bbox(bboxes, image.size)
31
+ else:
32
+ masks = mask_to_pil(pred[0].masks.data, image.size)
33
+ preview = pred[0].plot()
34
+ preview = cv2.cvtColor(preview, cv2.COLOR_BGR2RGB)
35
+ preview = Image.fromarray(preview)
36
+
37
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
38
+
39
+
40
+ def mask_to_pil(masks, shape: tuple[int, int]) -> list[Image.Image]:
41
+ """
42
+ Parameters
43
+ ----------
44
+ masks: torch.Tensor, dtype=torch.float32, shape=(N, H, W).
45
+ The device can be CUDA, but `to_pil_image` takes care of that.
46
+
47
+ shape: tuple[int, int]
48
+ (width, height) of the original image
49
+ """
50
+ n = masks.shape[0]
51
+ return [to_pil_image(masks[i], mode="L").resize(shape) for i in range(n)]
adetailer/controlnet_ext/__init__.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from .controlnet_ext import ControlNetExt, controlnet_exists, get_cn_models
2
+
3
+ __all__ = [
4
+ "ControlNetExt",
5
+ "controlnet_exists",
6
+ "get_cn_models",
7
+ ]
adetailer/controlnet_ext/controlnet_ext.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import importlib
4
+ import re
5
+ import sys
6
+ from functools import lru_cache
7
+ from pathlib import Path
8
+ from textwrap import dedent
9
+
10
+ from modules import extensions, sd_models, shared
11
+
12
+ try:
13
+ from modules.paths import extensions_builtin_dir, extensions_dir, models_path
14
+ except ImportError as e:
15
+ msg = """
16
+ [-] ADetailer: `stable-diffusion-webui < 1.1.0` is no longer supported.
17
+ Please upgrade to stable-diffusion-webui >= 1.1.0.
18
+ or you can use ADetailer v23.10.1 (https://github.com/Bing-su/adetailer/archive/refs/tags/v23.10.1.zip)
19
+ """
20
+ raise RuntimeError(dedent(msg)) from e
21
+
22
+ ext_path = Path(extensions_dir)
23
+ ext_builtin_path = Path(extensions_builtin_dir)
24
+ controlnet_exists = False
25
+ controlnet_path = None
26
+ cn_base_path = ""
27
+
28
+ for extension in extensions.active():
29
+ if not extension.enabled:
30
+ continue
31
+ # For cases like sd-webui-controlnet-master
32
+ if "sd-webui-controlnet" in extension.name:
33
+ controlnet_exists = True
34
+ controlnet_path = Path(extension.path)
35
+ cn_base_path = ".".join(controlnet_path.parts[-2:])
36
+ break
37
+
38
+ if controlnet_path is not None:
39
+ sd_webui_controlnet_path = controlnet_path.resolve().parent
40
+ if sd_webui_controlnet_path.stem in ("extensions", "extensions-builtin"):
41
+ target_path = str(sd_webui_controlnet_path.parent)
42
+ if target_path not in sys.path:
43
+ sys.path.append(target_path)
44
+
45
+ cn_model_module = {
46
+ "inpaint": "inpaint_global_harmonious",
47
+ "scribble": "t2ia_sketch_pidi",
48
+ "lineart": "lineart_coarse",
49
+ "openpose": "openpose_full",
50
+ "tile": "tile_resample",
51
+ "depth": "depth_midas",
52
+ }
53
+ cn_model_regex = re.compile("|".join(cn_model_module.keys()))
54
+
55
+
56
+ class ControlNetExt:
57
+ def __init__(self):
58
+ self.cn_models = ["None"]
59
+ self.cn_available = False
60
+ self.external_cn = None
61
+
62
+ def init_controlnet(self):
63
+ import_path = cn_base_path + ".scripts.external_code"
64
+
65
+ self.external_cn = importlib.import_module(import_path, "external_code")
66
+ self.cn_available = True
67
+ models = self.external_cn.get_models()
68
+ self.cn_models.extend(m for m in models if cn_model_regex.search(m))
69
+
70
+ def update_scripts_args(
71
+ self,
72
+ p,
73
+ model: str,
74
+ module: str | None,
75
+ weight: float,
76
+ guidance_start: float,
77
+ guidance_end: float,
78
+ ):
79
+ if (not self.cn_available) or model == "None":
80
+ return
81
+
82
+ if module is None or module == "None":
83
+ for m, v in cn_model_module.items():
84
+ if m in model:
85
+ module = v
86
+ break
87
+ else:
88
+ module = None
89
+
90
+ cn_units = [
91
+ self.external_cn.ControlNetUnit(
92
+ model=model,
93
+ weight=weight,
94
+ control_mode=self.external_cn.ControlMode.BALANCED,
95
+ module=module,
96
+ guidance_start=guidance_start,
97
+ guidance_end=guidance_end,
98
+ pixel_perfect=True,
99
+ )
100
+ ]
101
+
102
+ try:
103
+ self.external_cn.update_cn_script_in_processing(p, cn_units)
104
+ except AttributeError as e:
105
+ if "script_args_value" not in str(e):
106
+ raise
107
+ msg = "[-] Adetailer: ControlNet option not available in WEBUI version lower than 1.6.0 due to updates in ControlNet"
108
+ raise RuntimeError(msg) from e
109
+
110
+
111
+ def get_cn_model_dirs() -> list[Path]:
112
+ cn_model_dir = Path(models_path, "ControlNet")
113
+ if controlnet_path is not None:
114
+ cn_model_dir_old = controlnet_path.joinpath("models")
115
+ else:
116
+ cn_model_dir_old = None
117
+ ext_dir1 = shared.opts.data.get("control_net_models_path", "")
118
+ ext_dir2 = getattr(shared.cmd_opts, "controlnet_dir", "")
119
+
120
+ dirs = [cn_model_dir]
121
+ dirs += [
122
+ Path(ext_dir) for ext_dir in [cn_model_dir_old, ext_dir1, ext_dir2] if ext_dir
123
+ ]
124
+
125
+ return dirs
126
+
127
+
128
+ @lru_cache
129
+ def _get_cn_models() -> list[str]:
130
+ """
131
+ Since we can't import ControlNet, we use a function that does something like
132
+ controlnet's `list(global_state.cn_models_names.values())`.
133
+ """
134
+ cn_model_exts = (".pt", ".pth", ".ckpt", ".safetensors")
135
+ dirs = get_cn_model_dirs()
136
+ name_filter = shared.opts.data.get("control_net_models_name_filter", "")
137
+ name_filter = name_filter.strip(" ").lower()
138
+
139
+ model_paths = []
140
+
141
+ for base in dirs:
142
+ if not base.exists():
143
+ continue
144
+
145
+ for p in base.rglob("*"):
146
+ if (
147
+ p.is_file()
148
+ and p.suffix in cn_model_exts
149
+ and cn_model_regex.search(p.name)
150
+ ):
151
+ if name_filter and name_filter not in p.name.lower():
152
+ continue
153
+ model_paths.append(p)
154
+ model_paths.sort(key=lambda p: p.name)
155
+
156
+ models = []
157
+ for p in model_paths:
158
+ model_hash = sd_models.model_hash(p)
159
+ name = f"{p.stem} [{model_hash}]"
160
+ models.append(name)
161
+ return models
162
+
163
+
164
+ def get_cn_models() -> list[str]:
165
+ if controlnet_exists:
166
+ return _get_cn_models()
167
+ return []
adetailer/controlnet_ext/restore.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from contextlib import contextmanager
4
+
5
+ from modules import img2img, processing, shared
6
+
7
+
8
+ class CNHijackRestore:
9
+ def __init__(self):
10
+ self.process = hasattr(processing, "__controlnet_original_process_images_inner")
11
+ self.img2img = hasattr(img2img, "__controlnet_original_process_batch")
12
+
13
+ def __enter__(self):
14
+ if self.process:
15
+ self.orig_process = processing.process_images_inner
16
+ processing.process_images_inner = getattr(
17
+ processing, "__controlnet_original_process_images_inner"
18
+ )
19
+ if self.img2img:
20
+ self.orig_img2img = img2img.process_batch
21
+ img2img.process_batch = getattr(
22
+ img2img, "__controlnet_original_process_batch"
23
+ )
24
+
25
+ def __exit__(self, *args, **kwargs):
26
+ if self.process:
27
+ processing.process_images_inner = self.orig_process
28
+ if self.img2img:
29
+ img2img.process_batch = self.orig_img2img
30
+
31
+
32
+ @contextmanager
33
+ def cn_allow_script_control():
34
+ orig = False
35
+ if "control_net_allow_script_control" in shared.opts.data:
36
+ try:
37
+ orig = shared.opts.data["control_net_allow_script_control"]
38
+ shared.opts.data["control_net_allow_script_control"] = True
39
+ yield
40
+ finally:
41
+ shared.opts.data["control_net_allow_script_control"] = orig
42
+ else:
43
+ yield
adetailer/install.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import importlib.util
4
+ import subprocess
5
+ import sys
6
+ from importlib.metadata import version # python >= 3.8
7
+
8
+ from packaging.version import parse
9
+
10
+ import_name = {"py-cpuinfo": "cpuinfo", "protobuf": "google.protobuf"}
11
+
12
+
13
+ def is_installed(
14
+ package: str, min_version: str | None = None, max_version: str | None = None
15
+ ):
16
+ name = import_name.get(package, package)
17
+ try:
18
+ spec = importlib.util.find_spec(name)
19
+ except ModuleNotFoundError:
20
+ return False
21
+
22
+ if spec is None:
23
+ return False
24
+
25
+ if not min_version and not max_version:
26
+ return True
27
+
28
+ if not min_version:
29
+ min_version = "0.0.0"
30
+ if not max_version:
31
+ max_version = "99999999.99999999.99999999"
32
+
33
+ try:
34
+ pkg_version = version(package)
35
+ return parse(min_version) <= parse(pkg_version) <= parse(max_version)
36
+ except Exception:
37
+ return False
38
+
39
+
40
+ def run_pip(*args):
41
+ subprocess.run([sys.executable, "-m", "pip", "install", *args])
42
+
43
+
44
+ def install():
45
+ deps = [
46
+ # requirements
47
+ ("ultralytics", "8.1.0", None),
48
+ ("mediapipe", "0.10.9", None),
49
+ ("rich", "13.0.0", None),
50
+ # mediapipe
51
+ ("protobuf", "3.20", "3.9999"),
52
+ ]
53
+
54
+ for pkg, low, high in deps:
55
+ if not is_installed(pkg, low, high):
56
+ if low and high:
57
+ cmd = f"{pkg}>={low},<={high}"
58
+ elif low:
59
+ cmd = f"{pkg}>={low}"
60
+ elif high:
61
+ cmd = f"{pkg}<={high}"
62
+ else:
63
+ cmd = pkg
64
+
65
+ run_pip("-U", cmd)
66
+
67
+
68
+ try:
69
+ import launch
70
+
71
+ skip_install = launch.args.skip_install
72
+ except Exception:
73
+ skip_install = False
74
+
75
+ if not skip_install:
76
+ install()
adetailer/preload.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+
3
+
4
+ def preload(parser: argparse.ArgumentParser):
5
+ parser.add_argument(
6
+ "--ad-no-huggingface",
7
+ action="store_true",
8
+ help="Don't use adetailer models from huggingface",
9
+ )
adetailer/pyproject.toml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "adetailer"
3
+ description = "An object detection and auto-mask extension for stable diffusion webui."
4
+ authors = [{ name = "dowon", email = "[email protected]" }]
5
+ requires-python = ">=3.8,<3.12"
6
+ readme = "README.md"
7
+ license = { text = "AGPL-3.0" }
8
+
9
+ [project.urls]
10
+ repository = "https://github.com/Bing-su/adetailer"
11
+
12
+ [tool.isort]
13
+ profile = "black"
14
+ known_first_party = ["launch", "modules"]
15
+
16
+ [tool.ruff]
17
+ select = [
18
+ "A",
19
+ "B",
20
+ "C4",
21
+ "C90",
22
+ "E",
23
+ "EM",
24
+ "F",
25
+ "FA",
26
+ "I001",
27
+ "ISC",
28
+ "N",
29
+ "PERF",
30
+ "PIE",
31
+ "PT",
32
+ "PTH",
33
+ "RET",
34
+ "RUF",
35
+ "SIM",
36
+ "UP",
37
+ "W",
38
+ ]
39
+ ignore = ["B008", "B905", "E501", "F401", "UP007"]
40
+
41
+ [tool.ruff.isort]
42
+ known-first-party = ["launch", "modules"]
adetailer/scripts/!adetailer.py ADDED
@@ -0,0 +1,1000 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import platform
5
+ import re
6
+ import sys
7
+ import traceback
8
+ from contextlib import contextmanager, suppress
9
+ from copy import copy
10
+ from functools import partial
11
+ from pathlib import Path
12
+ from textwrap import dedent
13
+ from typing import TYPE_CHECKING, Any, NamedTuple
14
+
15
+ import gradio as gr
16
+ import torch
17
+ from PIL import Image
18
+ from rich import print
19
+ from torchvision.transforms.functional import to_pil_image
20
+
21
+ import modules
22
+ from adetailer import (
23
+ AFTER_DETAILER,
24
+ __version__,
25
+ get_models,
26
+ mediapipe_predict,
27
+ ultralytics_predict,
28
+ )
29
+ from adetailer.args import ALL_ARGS, BBOX_SORTBY, ADetailerArgs, SkipImg2ImgOrig
30
+ from adetailer.common import PredictOutput
31
+ from adetailer.mask import (
32
+ filter_by_ratio,
33
+ filter_k_largest,
34
+ mask_preprocess,
35
+ sort_bboxes,
36
+ )
37
+ from adetailer.traceback import rich_traceback
38
+ from adetailer.ui import WebuiInfo, adui, ordinal, suffix
39
+ from controlnet_ext import ControlNetExt, controlnet_exists, get_cn_models
40
+ from controlnet_ext.restore import (
41
+ CNHijackRestore,
42
+ cn_allow_script_control,
43
+ )
44
+ from modules import images, paths, safe, script_callbacks, scripts, shared
45
+ from modules.devices import NansException
46
+ from modules.processing import (
47
+ Processed,
48
+ StableDiffusionProcessingImg2Img,
49
+ create_infotext,
50
+ process_images,
51
+ )
52
+ from modules.sd_samplers import all_samplers
53
+ from modules.shared import cmd_opts, opts, state
54
+
55
+ if TYPE_CHECKING:
56
+ from fastapi import FastAPI
57
+
58
+ no_huggingface = getattr(cmd_opts, "ad_no_huggingface", False)
59
+ adetailer_dir = Path(paths.models_path, "adetailer")
60
+ extra_models_dir = shared.opts.data.get("ad_extra_models_dir", "")
61
+ model_mapping = get_models(
62
+ adetailer_dir, extra_dir=extra_models_dir, huggingface=not no_huggingface
63
+ )
64
+ txt2img_submit_button = img2img_submit_button = None
65
+ SCRIPT_DEFAULT = "dynamic_prompting,dynamic_thresholding,wildcard_recursive,wildcards,lora_block_weight,negpip"
66
+
67
+ if (
68
+ not adetailer_dir.exists()
69
+ and adetailer_dir.parent.exists()
70
+ and os.access(adetailer_dir.parent, os.W_OK)
71
+ ):
72
+ adetailer_dir.mkdir()
73
+
74
+ print(
75
+ f"[-] ADetailer initialized. version: {__version__}, num models: {len(model_mapping)}"
76
+ )
77
+
78
+
79
+ @contextmanager
80
+ def change_torch_load():
81
+ orig = torch.load
82
+ try:
83
+ torch.load = safe.unsafe_torch_load
84
+ yield
85
+ finally:
86
+ torch.load = orig
87
+
88
+
89
+ @contextmanager
90
+ def pause_total_tqdm():
91
+ orig = opts.data.get("multiple_tqdm", True)
92
+ try:
93
+ opts.data["multiple_tqdm"] = False
94
+ yield
95
+ finally:
96
+ opts.data["multiple_tqdm"] = orig
97
+
98
+
99
+ @contextmanager
100
+ def preseve_prompts(p):
101
+ all_pt = copy(p.all_prompts)
102
+ all_ng = copy(p.all_negative_prompts)
103
+ try:
104
+ yield
105
+ finally:
106
+ p.all_prompts = all_pt
107
+ p.all_negative_prompts = all_ng
108
+
109
+
110
+ class AfterDetailerScript(scripts.Script):
111
+ def __init__(self):
112
+ super().__init__()
113
+ self.ultralytics_device = self.get_ultralytics_device()
114
+
115
+ self.controlnet_ext = None
116
+
117
+ def __repr__(self):
118
+ return f"{self.__class__.__name__}(version={__version__})"
119
+
120
+ def title(self):
121
+ return AFTER_DETAILER
122
+
123
+ def show(self, is_img2img):
124
+ return scripts.AlwaysVisible
125
+
126
+ def ui(self, is_img2img):
127
+ num_models = opts.data.get("ad_max_models", 2)
128
+ ad_model_list = list(model_mapping.keys())
129
+ sampler_names = [sampler.name for sampler in all_samplers]
130
+
131
+ try:
132
+ checkpoint_list = modules.sd_models.checkpoint_tiles(use_shorts=True)
133
+ except TypeError:
134
+ checkpoint_list = modules.sd_models.checkpoint_tiles()
135
+ vae_list = modules.shared_items.sd_vae_items()
136
+
137
+ webui_info = WebuiInfo(
138
+ ad_model_list=ad_model_list,
139
+ sampler_names=sampler_names,
140
+ t2i_button=txt2img_submit_button,
141
+ i2i_button=img2img_submit_button,
142
+ checkpoints_list=checkpoint_list,
143
+ vae_list=vae_list,
144
+ )
145
+
146
+ components, infotext_fields = adui(num_models, is_img2img, webui_info)
147
+
148
+ self.infotext_fields = infotext_fields
149
+ return components
150
+
151
+ def init_controlnet_ext(self) -> None:
152
+ if self.controlnet_ext is not None:
153
+ return
154
+ self.controlnet_ext = ControlNetExt()
155
+
156
+ if controlnet_exists:
157
+ try:
158
+ self.controlnet_ext.init_controlnet()
159
+ except ImportError:
160
+ error = traceback.format_exc()
161
+ print(
162
+ f"[-] ADetailer: ControlNetExt init failed:\n{error}",
163
+ file=sys.stderr,
164
+ )
165
+
166
+ def update_controlnet_args(self, p, args: ADetailerArgs) -> None:
167
+ if self.controlnet_ext is None:
168
+ self.init_controlnet_ext()
169
+
170
+ if (
171
+ self.controlnet_ext is not None
172
+ and self.controlnet_ext.cn_available
173
+ and args.ad_controlnet_model != "None"
174
+ ):
175
+ self.controlnet_ext.update_scripts_args(
176
+ p,
177
+ model=args.ad_controlnet_model,
178
+ module=args.ad_controlnet_module,
179
+ weight=args.ad_controlnet_weight,
180
+ guidance_start=args.ad_controlnet_guidance_start,
181
+ guidance_end=args.ad_controlnet_guidance_end,
182
+ )
183
+
184
+ def is_ad_enabled(self, *args_) -> bool:
185
+ arg_list = [arg for arg in args_ if isinstance(arg, dict)]
186
+ if not args_ or not arg_list:
187
+ message = f"""
188
+ [-] ADetailer: Invalid arguments passed to ADetailer.
189
+ input: {args_!r}
190
+ ADetailer disabled.
191
+ """
192
+ print(dedent(message), file=sys.stderr)
193
+ return False
194
+
195
+ ad_enabled = args_[0] if isinstance(args_[0], bool) else True
196
+ not_none = any(arg.get("ad_model", "None") != "None" for arg in arg_list)
197
+ return ad_enabled and not_none
198
+
199
+ def check_skip_img2img(self, p, *args_) -> None:
200
+ if (
201
+ hasattr(p, "_ad_skip_img2img")
202
+ or not hasattr(p, "init_images")
203
+ or not p.init_images
204
+ ):
205
+ return
206
+
207
+ if len(args_) >= 2 and isinstance(args_[1], bool):
208
+ p._ad_skip_img2img = args_[1]
209
+ if args_[1]:
210
+ p._ad_orig = SkipImg2ImgOrig(
211
+ steps=p.steps,
212
+ sampler_name=p.sampler_name,
213
+ width=p.width,
214
+ height=p.height,
215
+ )
216
+ p.steps = 1
217
+ p.sampler_name = "Euler"
218
+ p.width = 128
219
+ p.height = 128
220
+ else:
221
+ p._ad_skip_img2img = False
222
+
223
+ @staticmethod
224
+ def get_i(p) -> int:
225
+ it = p.iteration
226
+ bs = p.batch_size
227
+ i = p.batch_index
228
+ return it * bs + i
229
+
230
+ def get_args(self, p, *args_) -> list[ADetailerArgs]:
231
+ """
232
+ `args_` is at least 1 in length by `is_ad_enabled` immediately above
233
+ """
234
+ args = [arg for arg in args_ if isinstance(arg, dict)]
235
+
236
+ if not args:
237
+ message = f"[-] ADetailer: Invalid arguments passed to ADetailer: {args_!r}"
238
+ raise ValueError(message)
239
+
240
+ if hasattr(p, "_ad_xyz"):
241
+ args[0] = {**args[0], **p._ad_xyz}
242
+
243
+ all_inputs = []
244
+
245
+ for n, arg_dict in enumerate(args, 1):
246
+ try:
247
+ inp = ADetailerArgs(**arg_dict)
248
+ except ValueError as e:
249
+ msgs = [
250
+ f"[-] ADetailer: ValidationError when validating {ordinal(n)} arguments: {e}\n"
251
+ ]
252
+ for attr in ALL_ARGS.attrs:
253
+ arg = arg_dict.get(attr)
254
+ dtype = type(arg)
255
+ arg = "DEFAULT" if arg is None else repr(arg)
256
+ msgs.append(f" {attr}: {arg} ({dtype})")
257
+ raise ValueError("\n".join(msgs)) from e
258
+
259
+ all_inputs.append(inp)
260
+
261
+ return all_inputs
262
+
263
+ def extra_params(self, arg_list: list[ADetailerArgs]) -> dict:
264
+ params = {}
265
+ for n, args in enumerate(arg_list):
266
+ params.update(args.extra_params(suffix=suffix(n)))
267
+ params["ADetailer version"] = __version__
268
+ return params
269
+
270
+ @staticmethod
271
+ def get_ultralytics_device() -> str:
272
+ if "adetailer" in shared.cmd_opts.use_cpu:
273
+ return "cpu"
274
+
275
+ if platform.system() == "Darwin":
276
+ return ""
277
+
278
+ vram_args = ["lowvram", "medvram", "medvram_sdxl"]
279
+ if any(getattr(cmd_opts, vram, False) for vram in vram_args):
280
+ return "cpu"
281
+
282
+ return ""
283
+
284
+ def prompt_blank_replacement(
285
+ self, all_prompts: list[str], i: int, default: str
286
+ ) -> str:
287
+ if not all_prompts:
288
+ return default
289
+ if i < len(all_prompts):
290
+ return all_prompts[i]
291
+ j = i % len(all_prompts)
292
+ return all_prompts[j]
293
+
294
+ def _get_prompt(
295
+ self,
296
+ ad_prompt: str,
297
+ all_prompts: list[str],
298
+ i: int,
299
+ default: str,
300
+ replacements: list[PromptSR],
301
+ ) -> list[str]:
302
+ prompts = re.split(r"\s*\[SEP\]\s*", ad_prompt)
303
+ blank_replacement = self.prompt_blank_replacement(all_prompts, i, default)
304
+ for n in range(len(prompts)):
305
+ if not prompts[n]:
306
+ prompts[n] = blank_replacement
307
+ elif "[PROMPT]" in prompts[n]:
308
+ prompts[n] = prompts[n].replace("[PROMPT]", f" {blank_replacement} ")
309
+
310
+ for pair in replacements:
311
+ prompts[n] = prompts[n].replace(pair.s, pair.r)
312
+ return prompts
313
+
314
+ def get_prompt(self, p, args: ADetailerArgs) -> tuple[list[str], list[str]]:
315
+ i = self.get_i(p)
316
+ prompt_sr = p._ad_xyz_prompt_sr if hasattr(p, "_ad_xyz_prompt_sr") else []
317
+
318
+ prompt = self._get_prompt(args.ad_prompt, p.all_prompts, i, p.prompt, prompt_sr)
319
+ negative_prompt = self._get_prompt(
320
+ args.ad_negative_prompt,
321
+ p.all_negative_prompts,
322
+ i,
323
+ p.negative_prompt,
324
+ prompt_sr,
325
+ )
326
+
327
+ return prompt, negative_prompt
328
+
329
+ def get_seed(self, p) -> tuple[int, int]:
330
+ i = self.get_i(p)
331
+
332
+ if not p.all_seeds:
333
+ seed = p.seed
334
+ elif i < len(p.all_seeds):
335
+ seed = p.all_seeds[i]
336
+ else:
337
+ j = i % len(p.all_seeds)
338
+ seed = p.all_seeds[j]
339
+
340
+ if not p.all_subseeds:
341
+ subseed = p.subseed
342
+ elif i < len(p.all_subseeds):
343
+ subseed = p.all_subseeds[i]
344
+ else:
345
+ j = i % len(p.all_subseeds)
346
+ subseed = p.all_subseeds[j]
347
+
348
+ return seed, subseed
349
+
350
+ def get_width_height(self, p, args: ADetailerArgs) -> tuple[int, int]:
351
+ if args.ad_use_inpaint_width_height:
352
+ width = args.ad_inpaint_width
353
+ height = args.ad_inpaint_height
354
+ elif hasattr(p, "_ad_orig"):
355
+ width = p._ad_orig.width
356
+ height = p._ad_orig.height
357
+ else:
358
+ width = p.width
359
+ height = p.height
360
+
361
+ return width, height
362
+
363
+ def get_steps(self, p, args: ADetailerArgs) -> int:
364
+ if args.ad_use_steps:
365
+ return args.ad_steps
366
+ if hasattr(p, "_ad_orig"):
367
+ return p._ad_orig.steps
368
+ return p.steps
369
+
370
+ def get_cfg_scale(self, p, args: ADetailerArgs) -> float:
371
+ return args.ad_cfg_scale if args.ad_use_cfg_scale else p.cfg_scale
372
+
373
+ def get_sampler(self, p, args: ADetailerArgs) -> str:
374
+ if args.ad_use_sampler:
375
+ return args.ad_sampler
376
+ if hasattr(p, "_ad_orig"):
377
+ return p._ad_orig.sampler_name
378
+ return p.sampler_name
379
+
380
+ def get_override_settings(self, p, args: ADetailerArgs) -> dict[str, Any]:
381
+ d = {}
382
+
383
+ if args.ad_use_clip_skip:
384
+ d["CLIP_stop_at_last_layers"] = args.ad_clip_skip
385
+
386
+ if (
387
+ args.ad_use_checkpoint
388
+ and args.ad_checkpoint
389
+ and args.ad_checkpoint not in ("None", "Use same checkpoint")
390
+ ):
391
+ d["sd_model_checkpoint"] = args.ad_checkpoint
392
+
393
+ if (
394
+ args.ad_use_vae
395
+ and args.ad_vae
396
+ and args.ad_vae not in ("None", "Use same VAE")
397
+ ):
398
+ d["sd_vae"] = args.ad_vae
399
+ return d
400
+
401
+ def get_initial_noise_multiplier(self, p, args: ADetailerArgs) -> float | None:
402
+ return args.ad_noise_multiplier if args.ad_use_noise_multiplier else None
403
+
404
+ @staticmethod
405
+ def infotext(p) -> str:
406
+ return create_infotext(
407
+ p, p.all_prompts, p.all_seeds, p.all_subseeds, None, 0, 0
408
+ )
409
+
410
+ def write_params_txt(self, content: str) -> None:
411
+ params_txt = Path(paths.data_path, "params.txt")
412
+ with suppress(Exception):
413
+ params_txt.write_text(content, encoding="utf-8")
414
+
415
+ @staticmethod
416
+ def script_args_copy(script_args):
417
+ type_: type[list] | type[tuple] = type(script_args)
418
+ result = []
419
+ for arg in script_args:
420
+ try:
421
+ a = copy(arg)
422
+ except TypeError:
423
+ a = arg
424
+ result.append(a)
425
+ return type_(result)
426
+
427
+ def script_filter(self, p, args: ADetailerArgs):
428
+ script_runner = copy(p.scripts)
429
+ script_args = self.script_args_copy(p.script_args)
430
+
431
+ ad_only_seleted_scripts = opts.data.get("ad_only_seleted_scripts", True)
432
+ if not ad_only_seleted_scripts:
433
+ return script_runner, script_args
434
+
435
+ ad_script_names = opts.data.get("ad_script_names", SCRIPT_DEFAULT)
436
+ script_names_set = {
437
+ name
438
+ for script_name in ad_script_names.split(",")
439
+ for name in (script_name, script_name.strip())
440
+ }
441
+
442
+ if args.ad_controlnet_model != "None":
443
+ script_names_set.add("controlnet")
444
+
445
+ filtered_alwayson = []
446
+ for script_object in script_runner.alwayson_scripts:
447
+ filepath = script_object.filename
448
+ filename = Path(filepath).stem
449
+ if filename in script_names_set:
450
+ filtered_alwayson.append(script_object)
451
+
452
+ script_runner.alwayson_scripts = filtered_alwayson
453
+ return script_runner, script_args
454
+
455
+ def disable_controlnet_units(
456
+ self, script_args: list[Any] | tuple[Any, ...]
457
+ ) -> None:
458
+ for obj in script_args:
459
+ if "controlnet" in obj.__class__.__name__.lower():
460
+ if hasattr(obj, "enabled"):
461
+ obj.enabled = False
462
+ if hasattr(obj, "input_mode"):
463
+ obj.input_mode = getattr(obj.input_mode, "SIMPLE", "simple")
464
+
465
+ elif isinstance(obj, dict) and "module" in obj:
466
+ obj["enabled"] = False
467
+
468
+ def get_i2i_p(self, p, args: ADetailerArgs, image):
469
+ seed, subseed = self.get_seed(p)
470
+ width, height = self.get_width_height(p, args)
471
+ steps = self.get_steps(p, args)
472
+ cfg_scale = self.get_cfg_scale(p, args)
473
+ initial_noise_multiplier = self.get_initial_noise_multiplier(p, args)
474
+ sampler_name = self.get_sampler(p, args)
475
+ override_settings = self.get_override_settings(p, args)
476
+
477
+ i2i = StableDiffusionProcessingImg2Img(
478
+ init_images=[image],
479
+ resize_mode=0,
480
+ denoising_strength=args.ad_denoising_strength,
481
+ mask=None,
482
+ mask_blur=args.ad_mask_blur,
483
+ inpainting_fill=1,
484
+ inpaint_full_res=args.ad_inpaint_only_masked,
485
+ inpaint_full_res_padding=args.ad_inpaint_only_masked_padding,
486
+ inpainting_mask_invert=0,
487
+ initial_noise_multiplier=initial_noise_multiplier,
488
+ sd_model=p.sd_model,
489
+ outpath_samples=p.outpath_samples,
490
+ outpath_grids=p.outpath_grids,
491
+ prompt="", # replace later
492
+ negative_prompt="",
493
+ styles=p.styles,
494
+ seed=seed,
495
+ subseed=subseed,
496
+ subseed_strength=p.subseed_strength,
497
+ seed_resize_from_h=p.seed_resize_from_h,
498
+ seed_resize_from_w=p.seed_resize_from_w,
499
+ sampler_name=sampler_name,
500
+ batch_size=1,
501
+ n_iter=1,
502
+ steps=steps,
503
+ cfg_scale=cfg_scale,
504
+ width=width,
505
+ height=height,
506
+ restore_faces=args.ad_restore_face,
507
+ tiling=p.tiling,
508
+ extra_generation_params=p.extra_generation_params,
509
+ do_not_save_samples=True,
510
+ do_not_save_grid=True,
511
+ override_settings=override_settings,
512
+ )
513
+
514
+ i2i.cached_c = [None, None]
515
+ i2i.cached_uc = [None, None]
516
+ i2i.scripts, i2i.script_args = self.script_filter(p, args)
517
+ i2i._ad_disabled = True
518
+ i2i._ad_inner = True
519
+
520
+ if args.ad_controlnet_model != "Passthrough":
521
+ self.disable_controlnet_units(i2i.script_args)
522
+
523
+ if args.ad_controlnet_model not in ["None", "Passthrough"]:
524
+ self.update_controlnet_args(i2i, args)
525
+ elif args.ad_controlnet_model == "None":
526
+ i2i.control_net_enabled = False
527
+
528
+ return i2i
529
+
530
+ def save_image(self, p, image, *, condition: str, suffix: str) -> None:
531
+ i = self.get_i(p)
532
+ if p.all_prompts:
533
+ i %= len(p.all_prompts)
534
+ save_prompt = p.all_prompts[i]
535
+ else:
536
+ save_prompt = p.prompt
537
+ seed, _ = self.get_seed(p)
538
+
539
+ if opts.data.get(condition, False):
540
+ images.save_image(
541
+ image=image,
542
+ path=p.outpath_samples,
543
+ basename="",
544
+ seed=seed,
545
+ prompt=save_prompt,
546
+ extension=opts.samples_format,
547
+ info=self.infotext(p),
548
+ p=p,
549
+ suffix=suffix,
550
+ )
551
+
552
+ def get_ad_model(self, name: str):
553
+ if name not in model_mapping:
554
+ msg = f"[-] ADetailer: Model {name!r} not found. Available models: {list(model_mapping.keys())}"
555
+ raise ValueError(msg)
556
+ return model_mapping[name]
557
+
558
+ def sort_bboxes(self, pred: PredictOutput) -> PredictOutput:
559
+ sortby = opts.data.get("ad_bbox_sortby", BBOX_SORTBY[0])
560
+ sortby_idx = BBOX_SORTBY.index(sortby)
561
+ return sort_bboxes(pred, sortby_idx)
562
+
563
+ def pred_preprocessing(self, pred: PredictOutput, args: ADetailerArgs):
564
+ pred = filter_by_ratio(
565
+ pred, low=args.ad_mask_min_ratio, high=args.ad_mask_max_ratio
566
+ )
567
+ pred = filter_k_largest(pred, k=args.ad_mask_k_largest)
568
+ pred = self.sort_bboxes(pred)
569
+ return mask_preprocess(
570
+ pred.masks,
571
+ kernel=args.ad_dilate_erode,
572
+ x_offset=args.ad_x_offset,
573
+ y_offset=args.ad_y_offset,
574
+ merge_invert=args.ad_mask_merge_invert,
575
+ )
576
+
577
+ @staticmethod
578
+ def ensure_rgb_image(image: Any):
579
+ if not isinstance(image, Image.Image):
580
+ image = to_pil_image(image)
581
+ if image.mode != "RGB":
582
+ image = image.convert("RGB")
583
+ return image
584
+
585
+ @staticmethod
586
+ def i2i_prompts_replace(
587
+ i2i, prompts: list[str], negative_prompts: list[str], j: int
588
+ ) -> None:
589
+ i1 = min(j, len(prompts) - 1)
590
+ i2 = min(j, len(negative_prompts) - 1)
591
+ prompt = prompts[i1]
592
+ negative_prompt = negative_prompts[i2]
593
+ i2i.prompt = prompt
594
+ i2i.negative_prompt = negative_prompt
595
+
596
+ @staticmethod
597
+ def compare_prompt(p, processed, n: int = 0):
598
+ if p.prompt != processed.all_prompts[0]:
599
+ print(
600
+ f"[-] ADetailer: applied {ordinal(n + 1)} ad_prompt: {processed.all_prompts[0]!r}"
601
+ )
602
+
603
+ if p.negative_prompt != processed.all_negative_prompts[0]:
604
+ print(
605
+ f"[-] ADetailer: applied {ordinal(n + 1)} ad_negative_prompt: {processed.all_negative_prompts[0]!r}"
606
+ )
607
+
608
+ @staticmethod
609
+ def need_call_process(p) -> bool:
610
+ if p.scripts is None:
611
+ return False
612
+ i = p.batch_index
613
+ bs = p.batch_size
614
+ return i == bs - 1
615
+
616
+ @staticmethod
617
+ def need_call_postprocess(p) -> bool:
618
+ if p.scripts is None:
619
+ return False
620
+ return p.batch_index == 0
621
+
622
+ @staticmethod
623
+ def get_i2i_init_image(p, pp):
624
+ if getattr(p, "_ad_skip_img2img", False):
625
+ return p.init_images[0]
626
+ return pp.image
627
+
628
+ @staticmethod
629
+ def get_each_tap_seed(seed: int, i: int):
630
+ use_same_seed = shared.opts.data.get("ad_same_seed_for_each_tap", False)
631
+ return seed if use_same_seed else seed + i
632
+
633
+ @staticmethod
634
+ def is_img2img_inpaint(p) -> bool:
635
+ return hasattr(p, "image_mask") and bool(p.image_mask)
636
+
637
+ @rich_traceback
638
+ def process(self, p, *args_):
639
+ if getattr(p, "_ad_disabled", False):
640
+ return
641
+
642
+ if self.is_img2img_inpaint(p):
643
+ p._ad_disabled = True
644
+ msg = "[-] ADetailer: img2img inpainting detected. adetailer disabled."
645
+ print(msg)
646
+ return
647
+
648
+ if self.is_ad_enabled(*args_):
649
+ arg_list = self.get_args(p, *args_)
650
+ self.check_skip_img2img(p, *args_)
651
+ extra_params = self.extra_params(arg_list)
652
+ p.extra_generation_params.update(extra_params)
653
+ else:
654
+ p._ad_disabled = True
655
+
656
+ def _postprocess_image_inner(
657
+ self, p, pp, args: ADetailerArgs, *, n: int = 0
658
+ ) -> bool:
659
+ """
660
+ Returns
661
+ -------
662
+ bool
663
+
664
+ `True` if image was processed, `False` otherwise.
665
+ """
666
+ if state.interrupted or state.skipped:
667
+ return False
668
+
669
+ i = self.get_i(p)
670
+
671
+ i2i = self.get_i2i_p(p, args, pp.image)
672
+ seed, subseed = self.get_seed(p)
673
+ ad_prompts, ad_negatives = self.get_prompt(p, args)
674
+
675
+ is_mediapipe = args.ad_model.lower().startswith("mediapipe")
676
+
677
+ kwargs = {}
678
+ if is_mediapipe:
679
+ predictor = mediapipe_predict
680
+ ad_model = args.ad_model
681
+ else:
682
+ predictor = ultralytics_predict
683
+ ad_model = self.get_ad_model(args.ad_model)
684
+ kwargs["device"] = self.ultralytics_device
685
+
686
+ with change_torch_load():
687
+ pred = predictor(ad_model, pp.image, args.ad_confidence, **kwargs)
688
+
689
+ masks = self.pred_preprocessing(pred, args)
690
+ shared.state.assign_current_image(pred.preview)
691
+
692
+ if not masks:
693
+ print(
694
+ f"[-] ADetailer: nothing detected on image {i + 1} with {ordinal(n + 1)} settings."
695
+ )
696
+ return False
697
+
698
+ self.save_image(
699
+ p,
700
+ pred.preview,
701
+ condition="ad_save_previews",
702
+ suffix="-ad-preview" + suffix(n, "-"),
703
+ )
704
+
705
+ steps = len(masks)
706
+ processed = None
707
+ state.job_count += steps
708
+
709
+ if is_mediapipe:
710
+ print(f"mediapipe: {steps} detected.")
711
+
712
+ p2 = copy(i2i)
713
+ for j in range(steps):
714
+ p2.image_mask = masks[j]
715
+ p2.init_images[0] = self.ensure_rgb_image(p2.init_images[0])
716
+ self.i2i_prompts_replace(p2, ad_prompts, ad_negatives, j)
717
+
718
+ if re.match(r"^\s*\[SKIP\]\s*$", p2.prompt):
719
+ continue
720
+
721
+ p2.seed = self.get_each_tap_seed(seed, j)
722
+ p2.subseed = self.get_each_tap_seed(subseed, j)
723
+
724
+ try:
725
+ processed = process_images(p2)
726
+ except NansException as e:
727
+ msg = f"[-] ADetailer: 'NansException' occurred with {ordinal(n + 1)} settings.\n{e}"
728
+ print(msg, file=sys.stderr)
729
+ continue
730
+ finally:
731
+ p2.close()
732
+
733
+ self.compare_prompt(p2, processed, n=n)
734
+ p2 = copy(i2i)
735
+ p2.init_images = [processed.images[0]]
736
+
737
+ if processed is not None:
738
+ pp.image = processed.images[0]
739
+ return True
740
+
741
+ return False
742
+
743
+ @rich_traceback
744
+ def postprocess_image(self, p, pp, *args_):
745
+ if getattr(p, "_ad_disabled", False) or not self.is_ad_enabled(*args_):
746
+ return
747
+
748
+ pp.image = self.get_i2i_init_image(p, pp)
749
+ pp.image = self.ensure_rgb_image(pp.image)
750
+ init_image = copy(pp.image)
751
+ arg_list = self.get_args(p, *args_)
752
+ params_txt_content = Path(paths.data_path, "params.txt").read_text("utf-8")
753
+
754
+ if self.need_call_postprocess(p):
755
+ dummy = Processed(p, [], p.seed, "")
756
+ with preseve_prompts(p):
757
+ p.scripts.postprocess(copy(p), dummy)
758
+
759
+ is_processed = False
760
+ with CNHijackRestore(), pause_total_tqdm(), cn_allow_script_control():
761
+ for n, args in enumerate(arg_list):
762
+ if args.ad_model == "None":
763
+ continue
764
+ is_processed |= self._postprocess_image_inner(p, pp, args, n=n)
765
+
766
+ if is_processed and not getattr(p, "_ad_skip_img2img", False):
767
+ self.save_image(
768
+ p, init_image, condition="ad_save_images_before", suffix="-ad-before"
769
+ )
770
+
771
+ if self.need_call_process(p):
772
+ with preseve_prompts(p):
773
+ copy_p = copy(p)
774
+ if hasattr(p.scripts, "before_process"):
775
+ p.scripts.before_process(copy_p)
776
+ p.scripts.process(copy_p)
777
+
778
+ self.write_params_txt(params_txt_content)
779
+
780
+
781
+ def on_after_component(component, **_kwargs):
782
+ global txt2img_submit_button, img2img_submit_button
783
+ if getattr(component, "elem_id", None) == "txt2img_generate":
784
+ txt2img_submit_button = component
785
+ return
786
+
787
+ if getattr(component, "elem_id", None) == "img2img_generate":
788
+ img2img_submit_button = component
789
+
790
+
791
+ def on_ui_settings():
792
+ section = ("ADetailer", AFTER_DETAILER)
793
+ shared.opts.add_option(
794
+ "ad_max_models",
795
+ shared.OptionInfo(
796
+ default=2,
797
+ label="Max models",
798
+ component=gr.Slider,
799
+ component_args={"minimum": 1, "maximum": 10, "step": 1},
800
+ section=section,
801
+ ),
802
+ )
803
+
804
+ shared.opts.add_option(
805
+ "ad_extra_models_dir",
806
+ shared.OptionInfo(
807
+ default="",
808
+ label="Extra path to scan adetailer models",
809
+ component=gr.Textbox,
810
+ section=section,
811
+ ),
812
+ )
813
+
814
+ shared.opts.add_option(
815
+ "ad_save_previews",
816
+ shared.OptionInfo(False, "Save mask previews", section=section),
817
+ )
818
+
819
+ shared.opts.add_option(
820
+ "ad_save_images_before",
821
+ shared.OptionInfo(False, "Save images before ADetailer", section=section),
822
+ )
823
+
824
+ shared.opts.add_option(
825
+ "ad_only_seleted_scripts",
826
+ shared.OptionInfo(
827
+ True, "Apply only selected scripts to ADetailer", section=section
828
+ ),
829
+ )
830
+
831
+ textbox_args = {
832
+ "placeholder": "comma-separated list of script names",
833
+ "interactive": True,
834
+ }
835
+
836
+ shared.opts.add_option(
837
+ "ad_script_names",
838
+ shared.OptionInfo(
839
+ default=SCRIPT_DEFAULT,
840
+ label="Script names to apply to ADetailer (separated by comma)",
841
+ component=gr.Textbox,
842
+ component_args=textbox_args,
843
+ section=section,
844
+ ),
845
+ )
846
+
847
+ shared.opts.add_option(
848
+ "ad_bbox_sortby",
849
+ shared.OptionInfo(
850
+ default="None",
851
+ label="Sort bounding boxes by",
852
+ component=gr.Radio,
853
+ component_args={"choices": BBOX_SORTBY},
854
+ section=section,
855
+ ),
856
+ )
857
+
858
+ shared.opts.add_option(
859
+ "ad_same_seed_for_each_tap",
860
+ shared.OptionInfo(
861
+ False, "Use same seed for each tab in adetailer", section=section
862
+ ),
863
+ )
864
+
865
+
866
+ # xyz_grid
867
+
868
+
869
+ class PromptSR(NamedTuple):
870
+ s: str
871
+ r: str
872
+
873
+
874
+ def set_value(p, x: Any, xs: Any, *, field: str):
875
+ if not hasattr(p, "_ad_xyz"):
876
+ p._ad_xyz = {}
877
+ p._ad_xyz[field] = x
878
+
879
+
880
+ def search_and_replace_prompt(p, x: Any, xs: Any, replace_in_main_prompt: bool):
881
+ if replace_in_main_prompt:
882
+ p.prompt = p.prompt.replace(xs[0], x)
883
+ p.negative_prompt = p.negative_prompt.replace(xs[0], x)
884
+
885
+ if not hasattr(p, "_ad_xyz_prompt_sr"):
886
+ p._ad_xyz_prompt_sr = []
887
+ p._ad_xyz_prompt_sr.append(PromptSR(s=xs[0], r=x))
888
+
889
+
890
+ def make_axis_on_xyz_grid():
891
+ xyz_grid = None
892
+ for script in scripts.scripts_data:
893
+ if script.script_class.__module__ == "xyz_grid.py":
894
+ xyz_grid = script.module
895
+ break
896
+
897
+ if xyz_grid is None:
898
+ return
899
+
900
+ model_list = ["None", *model_mapping.keys()]
901
+ samplers = [sampler.name for sampler in all_samplers]
902
+
903
+ axis = [
904
+ xyz_grid.AxisOption(
905
+ "[ADetailer] ADetailer model 1st",
906
+ str,
907
+ partial(set_value, field="ad_model"),
908
+ choices=lambda: model_list,
909
+ ),
910
+ xyz_grid.AxisOption(
911
+ "[ADetailer] ADetailer prompt 1st",
912
+ str,
913
+ partial(set_value, field="ad_prompt"),
914
+ ),
915
+ xyz_grid.AxisOption(
916
+ "[ADetailer] ADetailer negative prompt 1st",
917
+ str,
918
+ partial(set_value, field="ad_negative_prompt"),
919
+ ),
920
+ xyz_grid.AxisOption(
921
+ "[ADetailer] Prompt S/R (AD 1st)",
922
+ str,
923
+ partial(search_and_replace_prompt, replace_in_main_prompt=False),
924
+ ),
925
+ xyz_grid.AxisOption(
926
+ "[ADetailer] Prompt S/R (AD 1st and main prompt)",
927
+ str,
928
+ partial(search_and_replace_prompt, replace_in_main_prompt=True),
929
+ ),
930
+ xyz_grid.AxisOption(
931
+ "[ADetailer] Mask erosion / dilation 1st",
932
+ int,
933
+ partial(set_value, field="ad_dilate_erode"),
934
+ ),
935
+ xyz_grid.AxisOption(
936
+ "[ADetailer] Inpaint denoising strength 1st",
937
+ float,
938
+ partial(set_value, field="ad_denoising_strength"),
939
+ ),
940
+ xyz_grid.AxisOption(
941
+ "[ADetailer] Inpaint only masked 1st",
942
+ str,
943
+ partial(set_value, field="ad_inpaint_only_masked"),
944
+ choices=lambda: ["True", "False"],
945
+ ),
946
+ xyz_grid.AxisOption(
947
+ "[ADetailer] Inpaint only masked padding 1st",
948
+ int,
949
+ partial(set_value, field="ad_inpaint_only_masked_padding"),
950
+ ),
951
+ xyz_grid.AxisOption(
952
+ "[ADetailer] ADetailer sampler 1st",
953
+ str,
954
+ partial(set_value, field="ad_sampler"),
955
+ choices=lambda: samplers,
956
+ ),
957
+ xyz_grid.AxisOption(
958
+ "[ADetailer] ControlNet model 1st",
959
+ str,
960
+ partial(set_value, field="ad_controlnet_model"),
961
+ choices=lambda: ["None", *get_cn_models()],
962
+ ),
963
+ ]
964
+
965
+ if not any(x.label.startswith("[ADetailer]") for x in xyz_grid.axis_options):
966
+ xyz_grid.axis_options.extend(axis)
967
+
968
+
969
+ def on_before_ui():
970
+ try:
971
+ make_axis_on_xyz_grid()
972
+ except Exception:
973
+ error = traceback.format_exc()
974
+ print(
975
+ f"[-] ADetailer: xyz_grid error:\n{error}",
976
+ file=sys.stderr,
977
+ )
978
+
979
+
980
+ # api
981
+
982
+
983
+ def add_api_endpoints(_: gr.Blocks, app: FastAPI):
984
+ @app.get("/adetailer/v1/version")
985
+ def version():
986
+ return {"version": __version__}
987
+
988
+ @app.get("/adetailer/v1/schema")
989
+ def schema():
990
+ return ADetailerArgs.schema()
991
+
992
+ @app.get("/adetailer/v1/ad_model")
993
+ def ad_model():
994
+ return {"ad_model": list(model_mapping)}
995
+
996
+
997
+ script_callbacks.on_ui_settings(on_ui_settings)
998
+ script_callbacks.on_after_component(on_after_component)
999
+ script_callbacks.on_app_started(add_api_endpoints)
1000
+ script_callbacks.on_before_ui(on_before_ui)
kohya-sd-scripts-webui/.gitignore ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ venv
3
+ tmp
4
+
5
+ kohya_ss
6
+ wd14_tagger_model
7
+ presets.json
8
+ meta.json
9
+ presets
kohya-sd-scripts-webui/README.md ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # kohya sd-scripts webui
2
+
3
+ [![](https://img.shields.io/static/v1?message=Open%20in%20Colab&logo=googlecolab&labelColor=5c5c5c&color=0f80c1&label=%20&style=for-the-badge)](https://colab.research.google.com/github/ddPn08/kohya-sd-scripts-webui/blob/main/kohya-sd-scripts-webui-colab.ipynb)
4
+
5
+ Gradio wrapper for [sd-scripts](https://github.com/kohya-ss/sd-scripts) by kohya
6
+
7
+ It can be used as an extension of [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui) or can be launched standalone.
8
+
9
+ ![](/screenshots/webui-01.png)
10
+
11
+ # Usage
12
+ ## As an extension of stable-diffusion-webui
13
+
14
+ Go to `Extensions` > `Install from URL`, enter the following URL and press the install button.
15
+
16
+ https://github.com/ddpn08/kohya-sd-scripts-webui.git
17
+
18
+ ![](/screenshots/installation-extension.png)
19
+
20
+ ## Start standalone
21
+
22
+ Run `webui.bat` for Windows, `webui.sh` for Linux, MacOS
kohya-sd-scripts-webui/built-in-presets.json ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "train_network": {
3
+ "lora-x512": {
4
+ "v2": null,
5
+ "v_parameterization": null,
6
+ "pretrained_model_name_or_path": null,
7
+ "train_data_dir": null,
8
+ "shuffle_caption": true,
9
+ "caption_extension": ".caption",
10
+ "caption_extention": null,
11
+ "keep_tokens": null,
12
+ "color_aug": null,
13
+ "flip_aug": true,
14
+ "face_crop_aug_range": null,
15
+ "random_crop": null,
16
+ "debug_dataset": null,
17
+ "resolution": "512",
18
+ "cache_latents": null,
19
+ "enable_bucket": true,
20
+ "min_bucket_reso": 256,
21
+ "max_bucket_reso": 1024,
22
+ "reg_data_dir": null,
23
+ "in_json": null,
24
+ "dataset_repeats": 1,
25
+ "output_dir": null,
26
+ "output_name": null,
27
+ "save_precision": null,
28
+ "save_every_n_epochs": 5,
29
+ "save_n_epoch_ratio": null,
30
+ "save_last_n_epochs": null,
31
+ "save_last_n_epochs_state": null,
32
+ "save_state": null,
33
+ "resume": null,
34
+ "train_batch_size": 1,
35
+ "max_token_length": null,
36
+ "use_8bit_adam": true,
37
+ "mem_eff_attn": null,
38
+ "xformers": true,
39
+ "vae": null,
40
+ "learning_rate": 0.0001,
41
+ "max_train_steps": 1600,
42
+ "max_train_epochs": null,
43
+ "max_data_loader_n_workers": 8,
44
+ "seed": null,
45
+ "gradient_checkpointing": true,
46
+ "gradient_accumulation_steps": 1,
47
+ "mixed_precision": "no",
48
+ "full_fp16": null,
49
+ "clip_skip": 2,
50
+ "logging_dir": null,
51
+ "log_prefix": null,
52
+ "lr_scheduler": "constant",
53
+ "lr_warmup_steps": 0,
54
+ "prior_loss_weight": 1.0,
55
+ "no_metadata": null,
56
+ "save_model_as": "safetensors",
57
+ "unet_lr": null,
58
+ "text_encoder_lr": null,
59
+ "network_weights": null,
60
+ "network_module": "networks.lora",
61
+ "network_dim": 16,
62
+ "network_alpha": 1.0,
63
+ "network_args": null,
64
+ "network_train_unet_only": null,
65
+ "network_train_text_encoder_only": null,
66
+ "training_comment": null
67
+ }
68
+ },
69
+ "train_db": {
70
+ "db-x512": {
71
+ "v2": null,
72
+ "v_parameterization": null,
73
+ "pretrained_model_name_or_path": null,
74
+ "train_data_dir": null,
75
+ "shuffle_caption": true,
76
+ "caption_extension": ".caption",
77
+ "caption_extention": null,
78
+ "keep_tokens": null,
79
+ "color_aug": null,
80
+ "flip_aug": true,
81
+ "face_crop_aug_range": null,
82
+ "random_crop": null,
83
+ "debug_dataset": null,
84
+ "resolution": null,
85
+ "cache_latents": null,
86
+ "enable_bucket": true,
87
+ "min_bucket_reso": 256,
88
+ "max_bucket_reso": 1024,
89
+ "reg_data_dir": null,
90
+ "output_dir": null,
91
+ "output_name": null,
92
+ "save_precision": null,
93
+ "save_every_n_epochs": 5,
94
+ "save_n_epoch_ratio": null,
95
+ "save_last_n_epochs": null,
96
+ "save_last_n_epochs_state": null,
97
+ "save_state": null,
98
+ "resume": null,
99
+ "train_batch_size": 1,
100
+ "max_token_length": null,
101
+ "use_8bit_adam": true,
102
+ "mem_eff_attn": null,
103
+ "xformers": true,
104
+ "vae": null,
105
+ "learning_rate": 1e-06,
106
+ "max_train_steps": 1600,
107
+ "max_train_epochs": null,
108
+ "max_data_loader_n_workers": 8,
109
+ "seed": null,
110
+ "gradient_checkpointing": null,
111
+ "gradient_accumulation_steps": 1,
112
+ "mixed_precision": "no",
113
+ "full_fp16": null,
114
+ "clip_skip": 2,
115
+ "logging_dir": null,
116
+ "log_prefix": null,
117
+ "lr_scheduler": "constant",
118
+ "lr_warmup_steps": 0,
119
+ "prior_loss_weight": 1.0,
120
+ "save_model_as": "safetensors",
121
+ "use_safetensors": null,
122
+ "no_token_padding": null,
123
+ "stop_text_encoder_training": null
124
+ }
125
+ }
126
+ }
kohya-sd-scripts-webui/install.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import launch
3
+ import platform
4
+ import os
5
+ import shutil
6
+ import site
7
+ import glob
8
+ import re
9
+
10
+ dirname = os.path.dirname(__file__)
11
+ repo_dir = os.path.join(dirname, "kohya_ss")
12
+
13
+
14
+ def prepare_environment():
15
+ torch_command = os.environ.get(
16
+ "TORCH_COMMAND",
17
+ "pip install torch==2.0.0+cu118 torchvision==0.15.1+cu118 --extra-index-url https://download.pytorch.org/whl/cu118",
18
+ )
19
+ sd_scripts_repo = os.environ.get("SD_SCRIPTS_REPO", "https://github.com/kohya-ss/sd-scripts.git")
20
+ sd_scripts_branch = os.environ.get("SD_SCRIPTS_BRANCH", "main")
21
+ requirements_file = os.environ.get("REQS_FILE", "requirements.txt")
22
+
23
+ sys.argv, skip_install = launch.extract_arg(sys.argv, "--skip-install")
24
+ sys.argv, disable_strict_version = launch.extract_arg(
25
+ sys.argv, "--disable-strict-version"
26
+ )
27
+ sys.argv, skip_torch_cuda_test = launch.extract_arg(
28
+ sys.argv, "--skip-torch-cuda-test"
29
+ )
30
+ sys.argv, skip_checkout_repo = launch.extract_arg(sys.argv, "--skip-checkout-repo")
31
+ sys.argv, update = launch.extract_arg(sys.argv, "--update")
32
+ sys.argv, reinstall_xformers = launch.extract_arg(sys.argv, "--reinstall-xformers")
33
+ sys.argv, reinstall_torch = launch.extract_arg(sys.argv, "--reinstall-torch")
34
+ xformers = "--xformers" in sys.argv
35
+ ngrok = "--ngrok" in sys.argv
36
+
37
+ if skip_install:
38
+ return
39
+
40
+
41
+ if (
42
+ reinstall_torch
43
+ or not launch.is_installed("torch")
44
+ or not launch.is_installed("torchvision")
45
+ ) and not disable_strict_version:
46
+ launch.run(
47
+ f'"{launch.python}" -m {torch_command}',
48
+ "Installing torch and torchvision",
49
+ "Couldn't install torch",
50
+ )
51
+
52
+ if not skip_torch_cuda_test:
53
+ launch.run_python(
54
+ "import torch; assert torch.cuda.is_available(), 'Torch is not able to use GPU; add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check'"
55
+ )
56
+
57
+ if (not launch.is_installed("xformers") or reinstall_xformers) and xformers:
58
+ launch.run_pip("install xformers --pre", "xformers")
59
+
60
+ if update and os.path.exists(repo_dir):
61
+ launch.run(f'cd "{repo_dir}" && {launch.git} fetch --prune')
62
+ launch.run(f'cd "{repo_dir}" && {launch.git} reset --hard origin/main')
63
+ elif not os.path.exists(repo_dir):
64
+ launch.run(
65
+ f'{launch.git} clone {sd_scripts_repo} "{repo_dir}"'
66
+ )
67
+
68
+ if not skip_checkout_repo:
69
+ launch.run(f'cd "{repo_dir}" && {launch.git} checkout {sd_scripts_branch}')
70
+
71
+ if not launch.is_installed("gradio"):
72
+ launch.run_pip("install gradio==3.16.2", "gradio")
73
+
74
+ if not launch.is_installed("pyngrok") and ngrok:
75
+ launch.run_pip("install pyngrok", "ngrok")
76
+
77
+ if platform.system() == "Linux":
78
+ if not launch.is_installed("triton"):
79
+ launch.run_pip("install triton", "triton")
80
+
81
+ if disable_strict_version:
82
+ with open(os.path.join(repo_dir, requirements_file), "r") as f:
83
+ txt = f.read()
84
+ requirements = [
85
+ re.split("==|<|>", a)[0]
86
+ for a in txt.split("\n")
87
+ if (not a.startswith("#") and a != ".")
88
+ ]
89
+ requirements = " ".join(requirements)
90
+ launch.run_pip(
91
+ f'install "{requirements}" "{repo_dir}"',
92
+ "requirements for kohya sd-scripts",
93
+ )
94
+ else:
95
+ launch.run(
96
+ f'cd "{repo_dir}" && "{launch.python}" -m pip install -r requirements.txt',
97
+ desc=f"Installing requirements for kohya sd-scripts",
98
+ errdesc=f"Couldn't install requirements for kohya sd-scripts",
99
+ )
100
+
101
+ if platform.system() == "Windows":
102
+ for file in glob.glob(os.path.join(repo_dir, "bitsandbytes_windows", "*")):
103
+ filename = os.path.basename(file)
104
+ for dir in site.getsitepackages():
105
+ outfile = (
106
+ os.path.join(dir, "bitsandbytes", "cuda_setup", filename)
107
+ if filename == "main.py"
108
+ else os.path.join(dir, "bitsandbytes", filename)
109
+ )
110
+ if not os.path.exists(os.path.dirname(outfile)):
111
+ continue
112
+ shutil.copy(file, outfile)
113
+
114
+
115
+ if __name__ == "__main__":
116
+ prepare_environment()
kohya-sd-scripts-webui/kohya-sd-scripts-webui-colab.ipynb ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "colab_type": "text",
7
+ "id": "view-in-github"
8
+ },
9
+ "source": [
10
+ "<a href=\"https://colab.research.google.com/github/ddPn08/kohya-sd-scripts-webui/blob/main/kohya-sd-scripts-webui-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "markdown",
15
+ "metadata": {
16
+ "id": "zSM6HuYmkYCt"
17
+ },
18
+ "source": [
19
+ "# [kohya sd-scripts webui](https://github.com/ddPn08/kohya-sd-scripts-webui)\n",
20
+ "\n",
21
+ "This notebook is for running [sd-scripts](https://github.com/kohya-ss/sd-scripts) by [Kohya](https://github.com/kohya-ss).\n",
22
+ "\n",
23
+ "このノートブックは[Kohya](https://github.com/kohya-ss)さんによる[sd-scripts](https://github.com/kohya-ss/sd-scripts)を実行するためのものです。\n",
24
+ "\n",
25
+ "# Repository\n",
26
+ "[kohya_ss/sd-scripts](https://github.com/kohya-ss/sd-scripts)"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": null,
32
+ "metadata": {
33
+ "id": "zXcznGdeyb2I"
34
+ },
35
+ "outputs": [],
36
+ "source": [
37
+ "! nvidia-smi\n",
38
+ "! nvcc -V\n",
39
+ "! free -h"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": null,
45
+ "metadata": {
46
+ "cellView": "form",
47
+ "id": "tj65Tb_oyxtP"
48
+ },
49
+ "outputs": [],
50
+ "source": [
51
+ "# @markdown # Mount Google Drive\n",
52
+ "mount_gdrive = True # @param {type:\"boolean\"}\n",
53
+ "gdrive_preset_path = \"/content/drive/MyDrive/AI/kohya-sd-scripts-webui/presets\" # @param {type:\"string\"}\n",
54
+ "\n",
55
+ "if mount_gdrive:\n",
56
+ " from google.colab import drive\n",
57
+ " drive.mount('/content/drive', force_remount=False)"
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "execution_count": null,
63
+ "metadata": {
64
+ "cellView": "form",
65
+ "id": "FN7UJvSdzBFF"
66
+ },
67
+ "outputs": [],
68
+ "source": [
69
+ "# @markdown # Initialize environment\n",
70
+ "\n",
71
+ "! git clone https://github.com/ddPn08/kohya-sd-scripts-webui.git\n",
72
+ "\n",
73
+ "import os\n",
74
+ "\n",
75
+ "if not os.path.exists(gdrive_preset_path):\n",
76
+ " os.makedirs(gdrive_preset_path, exist_ok=True)\n",
77
+ "\n",
78
+ "! rm -f ./kohya-sd-scripts-webui/presets.json\n",
79
+ "! ln -s {gdrive_preset_path} ./kohya-sd-scripts-webui/presets\n",
80
+ "\n",
81
+ "conda_dir = \"/opt/conda\" # @param{type:\"string\"}\n",
82
+ "conda_bin = os.path.join(conda_dir, \"bin\", \"conda\")\n",
83
+ "\n",
84
+ "if not os.path.exists(conda_bin):\n",
85
+ " ! curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh\n",
86
+ " ! chmod +x Miniconda3-latest-Linux-x86_64.sh\n",
87
+ " ! bash ./Miniconda3-latest-Linux-x86_64.sh -b -f -p {conda_dir}\n",
88
+ " ! rm Miniconda3-latest-Linux-x86_64.sh\n",
89
+ "\n",
90
+ "def run_script(s):\n",
91
+ " ! {s}\n",
92
+ "\n",
93
+ "def make_args(d):\n",
94
+ " arguments = \"\"\n",
95
+ " for k, v in d.items():\n",
96
+ " if type(v) == bool:\n",
97
+ " arguments += f\"--{k} \" if v else \"\"\n",
98
+ " elif type(v) == str and v:\n",
99
+ " arguments += f\"--{k} \\\"{v}\\\" \"\n",
100
+ " elif v:\n",
101
+ " arguments += f\"--{k}={v} \"\n",
102
+ " return arguments"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "metadata": {
109
+ "cellView": "form",
110
+ "id": "uetu1lShs6aJ"
111
+ },
112
+ "outputs": [],
113
+ "source": [
114
+ "# @markdown # Run\n",
115
+ "\n",
116
+ "# @markdown <br>\n",
117
+ "\n",
118
+ "# @markdown ## Optional | Ngrok Tunnel\n",
119
+ "# @markdown Get token from [here](https://dashboard.ngrok.com/get-started/your-authtoken)\n",
120
+ "\n",
121
+ "ngrok_token = \"\" # @param {type:\"string\"}\n",
122
+ "ngrok_region = \"us\" # @param [\"us\", \"eu\", \"au\", \"ap\", \"sa\", \"jp\", \"in\"]\n",
123
+ "\n",
124
+ "arguments = {\n",
125
+ " \"ngrok\": ngrok_token,\n",
126
+ " \"ngrok-region\": ngrok_region,\n",
127
+ " \"share\": ngrok_token is None,\n",
128
+ " \"xformers\": True,\n",
129
+ " \"enable-console-log\": True\n",
130
+ "}\n",
131
+ "\n",
132
+ "run_script(f\"\"\"\n",
133
+ "eval \"$({conda_bin} shell.bash hook)\"\n",
134
+ "cd kohya-sd-scripts-webui\n",
135
+ "python launch.py {make_args(arguments)}\n",
136
+ "\"\"\")"
137
+ ]
138
+ }
139
+ ],
140
+ "metadata": {
141
+ "accelerator": "GPU",
142
+ "colab": {
143
+ "include_colab_link": true,
144
+ "provenance": []
145
+ },
146
+ "gpuClass": "standard",
147
+ "kernelspec": {
148
+ "display_name": "Python 3",
149
+ "name": "python3"
150
+ },
151
+ "language_info": {
152
+ "name": "python"
153
+ }
154
+ },
155
+ "nbformat": 4,
156
+ "nbformat_minor": 0
157
+ }
kohya-sd-scripts-webui/launch.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import install
2
+ import subprocess
3
+ import os
4
+ import sys
5
+ import importlib.util
6
+
7
+ python = sys.executable
8
+ git = os.environ.get("GIT", "git")
9
+ index_url = os.environ.get("INDEX_URL", "")
10
+ skip_install = False
11
+
12
+
13
+ def run(command, desc=None, errdesc=None, custom_env=None):
14
+ if desc is not None:
15
+ print(desc)
16
+
17
+ result = subprocess.run(
18
+ command,
19
+ stdout=subprocess.PIPE,
20
+ stderr=subprocess.PIPE,
21
+ shell=True,
22
+ env=os.environ if custom_env is None else custom_env,
23
+ )
24
+
25
+ if result.returncode != 0:
26
+
27
+ message = f"""{errdesc or 'Error running command'}.
28
+ Command: {command}
29
+ Error code: {result.returncode}
30
+ stdout: {result.stdout.decode(encoding="utf8", errors="ignore") if len(result.stdout)>0 else '<empty>'}
31
+ stderr: {result.stderr.decode(encoding="utf8", errors="ignore") if len(result.stderr)>0 else '<empty>'}
32
+ """
33
+ raise RuntimeError(message)
34
+
35
+ return result.stdout.decode(encoding="utf8", errors="ignore")
36
+
37
+
38
+ def check_run(command):
39
+ result = subprocess.run(
40
+ command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
41
+ )
42
+ return result.returncode == 0
43
+
44
+
45
+ def is_installed(package):
46
+ try:
47
+ spec = importlib.util.find_spec(package)
48
+ except ModuleNotFoundError:
49
+ return False
50
+
51
+ return spec is not None
52
+
53
+
54
+ def run_pip(args, desc=None):
55
+ if skip_install:
56
+ return
57
+
58
+ index_url_line = f" --index-url {index_url}" if index_url != "" else ""
59
+ return run(
60
+ f'"{python}" -m pip {args} --prefer-binary{index_url_line}',
61
+ desc=f"Installing {desc}",
62
+ errdesc=f"Couldn't install {desc}",
63
+ )
64
+
65
+
66
+ def run_python(code, desc=None, errdesc=None):
67
+ return run(f'"{python}" -c "{code}"', desc, errdesc)
68
+
69
+
70
+ def extract_arg(args, name):
71
+ return [x for x in args if x != name], name in args
72
+
73
+
74
+ if __name__ == "__main__":
75
+ install.prepare_environment()
76
+
77
+ from scripts import main
78
+
79
+ main.launch()
kohya-sd-scripts-webui/main.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import sys
3
+ import subprocess
4
+
5
+ ps = subprocess.Popen(
6
+ [sys.executable, "-u", "./sub.py"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
7
+ )
8
+
9
+ reader = io.TextIOWrapper(ps.stdout, encoding='utf8')
10
+ while ps.poll() is None:
11
+ char = reader.read(1)
12
+ if char == '\n':
13
+ print('break')
14
+ sys.stdout.write(char)
kohya-sd-scripts-webui/screenshots/installation-extension.png ADDED
kohya-sd-scripts-webui/screenshots/webui-01.png ADDED
kohya-sd-scripts-webui/script.js ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function gradioApp() {
2
+ const elems = document.getElementsByTagName('gradio-app')
3
+ const gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot
4
+ return !!gradioShadowRoot ? gradioShadowRoot : document;
5
+ }
6
+
7
+ let executed = false
8
+
9
+ /** @type {(() => void)[]} */
10
+
11
+ /**
12
+ * @param {string} tab
13
+ * @param {boolean} show
14
+ */
15
+ function kohya_sd_webui__toggle_runner_button(tab, show) {
16
+ gradioApp().getElementById(`kohya_sd_webui__${tab}_run_button`).style.display = show ? 'block' : 'none'
17
+ gradioApp().getElementById(`kohya_sd_webui__${tab}_stop_button`).style.display = show ? 'none' : 'block'
18
+ }
19
+
20
+ window.addEventListener('DOMContentLoaded', () => {
21
+ const observer = new MutationObserver((m) => {
22
+ if (!executed && gradioApp().querySelector('#kohya_sd_webui__root')) {
23
+ executed = true;
24
+
25
+ /** @type {Record<string, string>} */
26
+ const helps = kohya_sd_webui__help_map
27
+ /** @type {string[]} */
28
+ const all_tabs = kohya_sd_webui__all_tabs
29
+
30
+ const initializeTerminalObserver = () => {
31
+ const container = gradioApp().querySelector("#kohya_sd_webui__terminal_outputs")
32
+ const parentContainer = container.parentElement
33
+ const clearBtn = document.createElement('button')
34
+ clearBtn.innerText = 'Clear The Terminal'
35
+ clearBtn.style.color = 'yellow';
36
+ parentContainer.insertBefore(clearBtn, container)
37
+ let clearTerminal = false;
38
+ clearBtn.addEventListener('click', () => {
39
+ container.innerHTML = ''
40
+ clearTerminal = true
41
+ })
42
+ setInterval(async () => {
43
+ const res = await fetch('./internal/extensions/kohya-sd-scripts-webui/terminal/outputs', {
44
+ method: "POST",
45
+ headers: { 'Content-Type': 'application/json' },
46
+ body: JSON.stringify({
47
+ output_index: container.children.length,
48
+ clear_terminal: clearTerminal,
49
+ }),
50
+ })
51
+ clearTerminal = false
52
+ const obj = await res.json()
53
+ const isBottom = container.scrollHeight - container.scrollTop === container.clientHeight
54
+ for(const line of obj.outputs){
55
+ const el = document.createElement('div')
56
+ el.innerText = line
57
+ container.appendChild(el)
58
+ }
59
+ if(isBottom) container.scrollTop = container.scrollHeight
60
+ }, 1000)
61
+ }
62
+
63
+ const checkProcessIsAlive = () => {
64
+ setInterval(async () => {
65
+ const res = await fetch('./internal/extensions/kohya-sd-scripts-webui/process/alive')
66
+ const obj = await res.json()
67
+ for (const tab of all_tabs)
68
+ kohya_sd_webui__toggle_runner_button(tab, !obj.alive)
69
+
70
+ }, 1000)
71
+ }
72
+
73
+ initializeTerminalObserver()
74
+ checkProcessIsAlive()
75
+
76
+ for (const tab of all_tabs)
77
+ gradioApp().querySelector(`#kohya_sd_webui__${tab}_run_button`).addEventListener('click', () => kohya_sd_webui__toggle_runner_button(tab, false))
78
+
79
+ for (const [k, v] of Object.entries(helps)) {
80
+ el = gradioApp().getElementById(k)
81
+ if (!el) continue
82
+ el.title = v
83
+ }
84
+ }
85
+ })
86
+ observer.observe(gradioApp(), { childList: true, subtree: true })
87
+ })
kohya-sd-scripts-webui/scripts/main.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import time
4
+
5
+ import gradio.routes
6
+
7
+ import scripts.runner as runner
8
+ import scripts.shared as shared
9
+ from scripts.shared import ROOT_DIR, is_webui_extension
10
+ from scripts.ui import create_ui
11
+
12
+
13
+ def create_js():
14
+ jsfile = os.path.join(ROOT_DIR, "script.js")
15
+ with open(jsfile, mode="r") as f:
16
+ js = f.read()
17
+
18
+ js = js.replace("kohya_sd_webui__help_map", json.dumps(shared.help_title_map))
19
+ js = js.replace(
20
+ "kohya_sd_webui__all_tabs",
21
+ json.dumps(shared.loaded_tabs),
22
+ )
23
+ return js
24
+
25
+
26
+ def create_head():
27
+ head = f'<script type="text/javascript">{create_js()}</script>'
28
+
29
+ def template_response_for_webui(*args, **kwargs):
30
+ res = shared.gradio_template_response_original(*args, **kwargs)
31
+ res.body = res.body.replace(b"</head>", f"{head}</head>".encode("utf8"))
32
+ return res
33
+
34
+ def template_response(*args, **kwargs):
35
+ res = template_response_for_webui(*args, **kwargs)
36
+ res.init_headers()
37
+ return res
38
+
39
+ if is_webui_extension():
40
+ import modules.shared
41
+
42
+ modules.shared.GradioTemplateResponseOriginal = template_response_for_webui
43
+ else:
44
+ gradio.routes.templates.TemplateResponse = template_response
45
+
46
+
47
+ def wait_on_server():
48
+ while 1:
49
+ time.sleep(0.5)
50
+
51
+
52
+ def on_ui_tabs():
53
+ cssfile = os.path.join(ROOT_DIR, "style.css")
54
+ with open(cssfile, mode="r") as f:
55
+ css = f.read()
56
+ sd_scripts = create_ui(css)
57
+ create_head()
58
+ return [(sd_scripts, "Kohya sd-scripts", "kohya_sd_scripts")]
59
+
60
+
61
+ def launch():
62
+ block, _, _ = on_ui_tabs()[0]
63
+ if shared.cmd_opts.ngrok is not None:
64
+ import scripts.ngrok as ngrok
65
+
66
+ address = ngrok.connect(
67
+ shared.cmd_opts.ngrok,
68
+ shared.cmd_opts.port if shared.cmd_opts.port is not None else 7860,
69
+ shared.cmd_opts.ngrok_region,
70
+ )
71
+ print("Running on ngrok URL: " + address)
72
+
73
+ app, local_url, share_url = block.launch(
74
+ share=shared.cmd_opts.share,
75
+ server_port=shared.cmd_opts.port,
76
+ server_name=shared.cmd_opts.host,
77
+ prevent_thread_lock=True,
78
+ )
79
+
80
+ runner.initialize_api(app)
81
+
82
+ wait_on_server()
83
+
84
+
85
+ if not hasattr(shared, "gradio_template_response_original"):
86
+ shared.gradio_template_response_original = gradio.routes.templates.TemplateResponse
87
+
88
+ if is_webui_extension():
89
+ from modules import script_callbacks
90
+
91
+ def initialize_api(_, app):
92
+ runner.initialize_api(app)
93
+
94
+ script_callbacks.on_ui_tabs(on_ui_tabs)
95
+ script_callbacks.on_app_started(initialize_api)
96
+
97
+ if __name__ == "__main__":
98
+ launch()
kohya-sd-scripts-webui/scripts/ngrok.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def connect(token, port, region):
2
+ from pyngrok import conf, exception, ngrok
3
+
4
+ account = None
5
+ if token is None:
6
+ token = "None"
7
+ else:
8
+ if ":" in token:
9
+ account = token.split(":")[1] + ":" + token.split(":")[-1]
10
+ token = token.split(":")[0]
11
+
12
+ config = conf.PyngrokConfig(auth_token=token, region=region)
13
+ try:
14
+ if account is None:
15
+ public_url = ngrok.connect(
16
+ port, pyngrok_config=config, bind_tls=True
17
+ ).public_url
18
+ else:
19
+ public_url = ngrok.connect(
20
+ port, pyngrok_config=config, bind_tls=True, auth=account
21
+ ).public_url
22
+ except exception.PyngrokNgrokError:
23
+ print(
24
+ f"Invalid ngrok authtoken, ngrok connection aborted.\n"
25
+ f"Your token: {token}, get the right one on https://dashboard.ngrok.com/get-started/your-authtoken"
26
+ )
27
+ else:
28
+ return public_url
kohya-sd-scripts-webui/scripts/presets.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import inspect
3
+ import os
4
+ from pathlib import Path
5
+ import toml
6
+ from kohya_ss.library import train_util, config_util
7
+
8
+ import gradio as gr
9
+
10
+ from scripts.shared import ROOT_DIR
11
+ from scripts.utilities import gradio_to_args
12
+
13
+ PRESET_DIR = os.path.join(ROOT_DIR, "presets")
14
+ PRESET_PATH = os.path.join(ROOT_DIR, "presets.json")
15
+
16
+
17
+ def get_arg_templates(fn):
18
+ parser = argparse.ArgumentParser()
19
+ args = [parser]
20
+ sig = inspect.signature(fn)
21
+ args.extend([True] * (len(sig.parameters) - 1))
22
+ fn(*args)
23
+ keys = [
24
+ x.replace("--", "") for x in parser.__dict__["_option_string_actions"].keys()
25
+ ]
26
+ keys = [x for x in keys if x not in ["help", "-h"]]
27
+ return keys, fn.__name__.replace("add_", "")
28
+
29
+
30
+ arguments_functions = [
31
+ train_util.add_dataset_arguments,
32
+ train_util.add_optimizer_arguments,
33
+ train_util.add_sd_models_arguments,
34
+ train_util.add_sd_saving_arguments,
35
+ train_util.add_training_arguments,
36
+ config_util.add_config_arguments,
37
+ ]
38
+
39
+ arg_templates = [get_arg_templates(x) for x in arguments_functions]
40
+
41
+
42
+ def load_presets():
43
+ obj = {}
44
+ os.makedirs(PRESET_DIR, exist_ok=True)
45
+ preset_names = os.listdir(PRESET_DIR)
46
+ for preset_name in preset_names:
47
+ preset_path = os.path.join(PRESET_DIR, preset_name)
48
+ obj[preset_name] = {}
49
+ for key in os.listdir(preset_path):
50
+ key = key.replace(".toml", "")
51
+ obj[preset_name][key] = load_preset(preset_name, key)
52
+ return obj
53
+
54
+
55
+ def load_preset(key, name):
56
+ filepath = os.path.join(PRESET_DIR, key, name + ".toml")
57
+ if not os.path.exists(filepath):
58
+ return {}
59
+ with open(filepath, mode="r") as f:
60
+ obj = toml.load(f)
61
+
62
+ flatten = {}
63
+ for k, v in obj.items():
64
+ if not isinstance(v, dict):
65
+ flatten[k] = v
66
+ else:
67
+ for k2, v2 in v.items():
68
+ flatten[k2] = v2
69
+ return flatten
70
+
71
+
72
+ def save_preset(key, name, value):
73
+ obj = {}
74
+ for k, v in value.items():
75
+ if isinstance(v, Path):
76
+ v = str(v)
77
+ for (template, category) in arg_templates:
78
+ if k in template:
79
+ if category not in obj:
80
+ obj[category] = {}
81
+ obj[category][k] = v
82
+ break
83
+ else:
84
+ obj[k] = v
85
+
86
+ filepath = os.path.join(PRESET_DIR, key, name + ".toml")
87
+ os.makedirs(os.path.dirname(filepath), exist_ok=True)
88
+ with open(filepath, mode="w") as f:
89
+ toml.dump(obj, f)
90
+
91
+
92
+ def delete_preset(key, name):
93
+ filepath = os.path.join(PRESET_DIR, key, name + ".toml")
94
+ if os.path.exists(filepath):
95
+ os.remove(filepath)
96
+
97
+
98
+ def create_ui(key, tmpls, opts):
99
+ get_templates = lambda: tmpls() if callable(tmpls) else tmpls
100
+ get_options = lambda: opts() if callable(opts) else opts
101
+
102
+ presets = load_presets()
103
+
104
+ if key not in presets:
105
+ presets[key] = {}
106
+
107
+ with gr.Box():
108
+ with gr.Row():
109
+ with gr.Column() as c:
110
+ load_preset_button = gr.Button("Load preset", variant="primary")
111
+ delete_preset_button = gr.Button("Delete preset")
112
+ with gr.Column() as c:
113
+ load_preset_name = gr.Dropdown(
114
+ list(presets[key].keys()), show_label=False
115
+ ).style(container=False)
116
+ reload_presets_button = gr.Button("🔄️")
117
+ with gr.Column() as c:
118
+ c.scale = 0.5
119
+ save_preset_name = gr.Textbox(
120
+ "", placeholder="Preset name", lines=1, show_label=False
121
+ ).style(container=False)
122
+ save_preset_button = gr.Button("Save preset", variant="primary")
123
+
124
+ def update_dropdown():
125
+ presets = load_presets()
126
+ if key not in presets:
127
+ presets[key] = {}
128
+ return gr.Dropdown.update(choices=list(presets[key].keys()))
129
+
130
+ def _save_preset(args):
131
+ name = args[save_preset_name]
132
+ if not name:
133
+ return update_dropdown()
134
+ args = gradio_to_args(get_templates(), get_options(), args)
135
+ save_preset(key, name, args)
136
+ return update_dropdown()
137
+
138
+ def _load_preset(args):
139
+ name = args[load_preset_name]
140
+ if not name:
141
+ return update_dropdown()
142
+ args = gradio_to_args(get_templates(), get_options(), args)
143
+ preset = load_preset(key, name)
144
+ result = []
145
+ for k, _ in args.items():
146
+ if k == load_preset_name:
147
+ continue
148
+ if k not in preset:
149
+ result.append(None)
150
+ continue
151
+ v = preset[k]
152
+ if type(v) == list:
153
+ v = " ".join(v)
154
+ result.append(v)
155
+ return result[0] if len(result) == 1 else result
156
+
157
+ def _delete_preset(name):
158
+ if not name:
159
+ return update_dropdown()
160
+ delete_preset(key, name)
161
+ return update_dropdown()
162
+
163
+ def init():
164
+ save_preset_button.click(
165
+ _save_preset,
166
+ set([save_preset_name, *get_options().values()]),
167
+ [load_preset_name],
168
+ )
169
+ load_preset_button.click(
170
+ _load_preset,
171
+ set([load_preset_name, *get_options().values()]),
172
+ [*get_options().values()],
173
+ )
174
+ delete_preset_button.click(_delete_preset, load_preset_name, [load_preset_name])
175
+ reload_presets_button.click(
176
+ update_dropdown, inputs=[], outputs=[load_preset_name]
177
+ )
178
+
179
+ return init
kohya-sd-scripts-webui/scripts/runner.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import sys
3
+
4
+ import fastapi
5
+ import gradio as gr
6
+ from pydantic import BaseModel, Field
7
+
8
+ import scripts.shared as shared
9
+ from scripts.utilities import run_python
10
+
11
+ proc = None
12
+ outputs = []
13
+
14
+
15
+ def alive():
16
+ return proc is not None
17
+
18
+
19
+ def initialize_runner(script_file, tmpls, opts):
20
+ run_button = gr.Button(
21
+ "Run",
22
+ variant="primary",
23
+ elem_id=f"kohya_sd_webui__{shared.current_tab}_run_button",
24
+ )
25
+ stop_button = gr.Button(
26
+ "Stop",
27
+ variant="secondary",
28
+ elem_id=f"kohya_sd_webui__{shared.current_tab}_stop_button",
29
+ )
30
+ get_templates = lambda: tmpls() if callable(tmpls) else tmpls
31
+ get_options = lambda: opts() if callable(opts) else opts
32
+
33
+ def run(args):
34
+ global proc
35
+ global outputs
36
+ if alive():
37
+ return
38
+ proc = run_python(script_file, get_templates(), get_options(), args)
39
+ reader = io.TextIOWrapper(proc.stdout, encoding="utf-8-sig")
40
+ line = ""
41
+ while proc is not None and proc.poll() is None:
42
+ try:
43
+ char = reader.read(1)
44
+ if shared.cmd_opts.enable_console_log:
45
+ sys.stdout.write(char)
46
+ if char == "\n":
47
+ outputs.append(line)
48
+ line = ""
49
+ continue
50
+ line += char
51
+ except:
52
+ ()
53
+ proc = None
54
+
55
+ def stop():
56
+ global proc
57
+ print("killed the running process")
58
+ proc.kill()
59
+ proc = None
60
+
61
+ def init():
62
+ run_button.click(
63
+ run,
64
+ set(get_options().values()),
65
+ )
66
+ stop_button.click(stop)
67
+
68
+ return init
69
+
70
+
71
+ class GetOutputRequest(BaseModel):
72
+ output_index: int = Field(
73
+ default=0, title="Index of the beginning of the log to retrieve"
74
+ )
75
+ clear_terminal: bool = Field(
76
+ default=False, title="Whether to clear the terminal"
77
+ )
78
+
79
+
80
+ class GetOutputResponse(BaseModel):
81
+ outputs: list = Field(title="List of terminal output")
82
+
83
+
84
+ class ProcessAliveResponse(BaseModel):
85
+ alive: bool = Field(title="Whether the process is running.")
86
+
87
+
88
+ def api_get_outputs(req: GetOutputRequest):
89
+ i = req.output_index
90
+ if req.clear_terminal:
91
+ global outputs
92
+ outputs = []
93
+ out = outputs[i:] if len(outputs) > i else []
94
+ return GetOutputResponse(outputs=out)
95
+
96
+
97
+ def api_get_isalive(req: fastapi.Request):
98
+ return ProcessAliveResponse(alive=alive())
99
+
100
+
101
+ def initialize_api(app: fastapi.FastAPI):
102
+ app.add_api_route(
103
+ "/internal/extensions/kohya-sd-scripts-webui/terminal/outputs",
104
+ api_get_outputs,
105
+ methods=["POST"],
106
+ response_model=GetOutputResponse,
107
+ )
108
+ app.add_api_route(
109
+ "/internal/extensions/kohya-sd-scripts-webui/process/alive",
110
+ api_get_isalive,
111
+ methods=["GET"],
112
+ response_model=ProcessAliveResponse,
113
+ )
kohya-sd-scripts-webui/scripts/shared.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import importlib
3
+ import os
4
+ import sys
5
+
6
+
7
+ def is_webui_extension():
8
+ try:
9
+ importlib.import_module("webui")
10
+ return True
11
+ except:
12
+ return False
13
+
14
+
15
+ ROOT_DIR = (
16
+ importlib.import_module("modules.scripts").basedir()
17
+ if is_webui_extension()
18
+ else os.path.dirname(os.path.dirname(__file__))
19
+ )
20
+
21
+ current_tab = None
22
+ loaded_tabs = []
23
+ help_title_map = {}
24
+
25
+ parser = argparse.ArgumentParser()
26
+ parser.add_argument("--share", action="store_true")
27
+ parser.add_argument("--port", type=int, default=None)
28
+ parser.add_argument("--host", type=str, default=None)
29
+ parser.add_argument("--ngrok", type=str, default=None)
30
+ parser.add_argument("--ngrok-region", type=str, default="us")
31
+ parser.add_argument("--enable-console-log", action="store_true")
32
+ cmd_opts, _ = parser.parse_known_args(sys.argv)
kohya-sd-scripts-webui/scripts/tabs/networks/check_lora_weights.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from scripts import ui
4
+ from scripts.runner import initialize_runner
5
+ from scripts.utilities import load_args_template, options_to_gradio
6
+
7
+
8
+ def title():
9
+ return "Check lora wights"
10
+
11
+
12
+ def create_ui():
13
+ options = {}
14
+ templates, script_file = load_args_template("networks", "check_lora_weights.py")
15
+
16
+ with gr.Column():
17
+ init = initialize_runner(script_file, templates, options)
18
+ with gr.Box():
19
+ ui.title("Options")
20
+ with gr.Column():
21
+ options_to_gradio(templates, options)
22
+
23
+ init()
kohya-sd-scripts-webui/scripts/tabs/networks/extract_lora_from_models.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from scripts import ui
4
+ from scripts.runner import initialize_runner
5
+ from scripts.utilities import load_args_template, options_to_gradio
6
+
7
+
8
+ def title():
9
+ return "Extract lora from models"
10
+
11
+
12
+ def create_ui():
13
+ options = {}
14
+ templates, script_file = load_args_template(
15
+ "networks", "extract_lora_from_models.py"
16
+ )
17
+
18
+ with gr.Column():
19
+ init = initialize_runner(script_file, templates, options)
20
+ with gr.Box():
21
+ ui.title("Options")
22
+ with gr.Column():
23
+ options_to_gradio(templates, options)
24
+
25
+ init()
kohya-sd-scripts-webui/scripts/tabs/networks/lora_interrogator.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from scripts import ui
4
+ from scripts.runner import initialize_runner
5
+ from scripts.utilities import load_args_template, options_to_gradio
6
+
7
+
8
+ def title():
9
+ return "Lora interrogator"
10
+
11
+
12
+ def create_ui():
13
+ options = {}
14
+ templates, script_file = load_args_template("networks", "lora_interrogator.py")
15
+
16
+ with gr.Column():
17
+ init = initialize_runner(script_file, templates, options)
18
+ with gr.Box():
19
+ ui.title("Options")
20
+ with gr.Column():
21
+ options_to_gradio(templates, options)
22
+
23
+ init()
kohya-sd-scripts-webui/scripts/tabs/networks/merge_lora.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from scripts import ui
4
+ from scripts.runner import initialize_runner
5
+ from scripts.utilities import load_args_template, options_to_gradio
6
+
7
+
8
+ def title():
9
+ return "Merge lora"
10
+
11
+
12
+ def create_ui():
13
+ options = {}
14
+ templates, script_file = load_args_template("networks", "merge_lora.py")
15
+
16
+ with gr.Column():
17
+ init = initialize_runner(script_file, templates, options)
18
+ with gr.Box():
19
+ ui.title("Options")
20
+ with gr.Column():
21
+ options_to_gradio(templates, options)
22
+
23
+ init()
kohya-sd-scripts-webui/scripts/tabs/networks/resize_lora.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from scripts import ui
4
+ from scripts.runner import initialize_runner
5
+ from scripts.utilities import load_args_template, options_to_gradio
6
+
7
+
8
+ def title():
9
+ return "Resize lora"
10
+
11
+
12
+ def create_ui():
13
+ options = {}
14
+ templates, script_file = load_args_template("networks", "resize_lora.py")
15
+
16
+ with gr.Column():
17
+ init = initialize_runner(script_file, templates, options)
18
+ with gr.Box():
19
+ ui.title("Options")
20
+ with gr.Column():
21
+ options_to_gradio(templates, options)
22
+
23
+ init()
kohya-sd-scripts-webui/scripts/tabs/networks/svd_merge_lora.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from scripts import ui
4
+ from scripts.runner import initialize_runner
5
+ from scripts.utilities import load_args_template, options_to_gradio
6
+
7
+
8
+ def title():
9
+ return "Svd merge lora"
10
+
11
+
12
+ def create_ui():
13
+ options = {}
14
+ templates, script_file = load_args_template("networks", "svd_merge_lora.py")
15
+
16
+ with gr.Column():
17
+ init = initialize_runner(script_file, templates, options)
18
+ with gr.Box():
19
+ ui.title("Options")
20
+ with gr.Column():
21
+ options_to_gradio(templates, options)
22
+
23
+ init()