Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing-su committed Jun 16, 2024
2 parents a89c01d + d8aaaa8 commit a7d9611
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 89 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/rbubley/mirrors-prettier
rev: v3.2.5
rev: v3.3.2
hooks:
- id: prettier

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
rev: v0.4.9
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 2024-06-16

- v24.6.0
- webui 1.6.0 미만 버전을 위한 기능들을 제거하고, 최소 버전을 1.6.0으로 올림
- 허깅페이스 연결을 체크하는데 1초만 소요되도록 함
- 허깅페이스 미러 (hf-mirror.com)도 체크함 (합쳐서 2초)
- InputAccordion을 적용함

## 2024-05-20

- v24.5.1
Expand Down
18 changes: 10 additions & 8 deletions aaaaaa/conditional.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from __future__ import annotations

from PIL import Image
from rich import print
msg = "[-] ADetailer: WebUI versions below 1.6.0 are not supported."

try:
from modules.processing import create_binary_mask
except ImportError:
msg = "[-] ADetailer: Support for webui versions below 1.6.0 will be discontinued."
print(msg)
from modules.processing import create_binary_mask # noqa: F401
except ImportError as e:
raise RuntimeError(msg) from e


def create_binary_mask(image: Image.Image):
return image.convert("L")
try:
from modules.ui_components import InputAccordion # noqa: F401
except ImportError as e:
raise RuntimeError(msg) from e


try:
from modules.sd_schedulers import schedulers
except ImportError:
# webui < 1.9.0
schedulers = []
10 changes: 8 additions & 2 deletions aaaaaa/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import io
import platform
import sys
from collections.abc import Callable
from importlib.metadata import version
from typing import Any, Callable
from typing import Any, TypeVar

from rich.console import Console, Group
from rich.panel import Panel
from rich.table import Table
from rich.traceback import Traceback
from typing_extensions import ParamSpec

from adetailer.__version__ import __version__
from adetailer.args import ADetailerArgs
Expand Down Expand Up @@ -137,7 +139,11 @@ def get_table(title: str, data: dict[str, Any]) -> Table:
return table


def rich_traceback(func: Callable) -> Callable:
P = ParamSpec("P")
T = TypeVar("T")


def rich_traceback(func: Callable[P, T]) -> Callable[P, T]:
def wrapper(*args, **kwargs):
string = io.StringIO()
width = Console().width
Expand Down
30 changes: 14 additions & 16 deletions aaaaaa/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import gradio as gr

from adetailer import AFTER_DETAILER, __version__
from aaaaaa.conditional import InputAccordion
from adetailer import ADETAILER, __version__
from adetailer.args import ALL_ARGS, MASK_MERGE_INVERT
from controlnet_ext import controlnet_exists, controlnet_type, get_cn_models

Expand Down Expand Up @@ -105,9 +106,9 @@ def on_cn_model_update(cn_model_name: str):


def elem_id(item_id: str, n: int, is_img2img: bool) -> str:
tap = "img2img" if is_img2img else "txt2img"
tab = "img2img" if is_img2img else "txt2img"
suf = suffix(n, "_")
return f"script_{tap}_adetailer_{item_id}{suf}"
return f"script_{tab}_adetailer_{item_id}{suf}"


def state_init(w: Widgets) -> dict[str, Any]:
Expand All @@ -123,17 +124,14 @@ def adui(
infotext_fields = []
eid = partial(elem_id, n=0, is_img2img=is_img2img)

with gr.Accordion(AFTER_DETAILER, open=False, elem_id=eid("ad_main_accordion")):
with InputAccordion(
value=False,
elem_id=eid("ad_main_accordion"),
label=ADETAILER,
visible=True,
) as ad_enable:
with gr.Row():
with gr.Column(scale=6):
ad_enable = gr.Checkbox(
label="Enable ADetailer",
value=False,
visible=True,
elem_id=eid("ad_enable"),
)

with gr.Column(scale=6):
with gr.Column(scale=8):
ad_skip_img2img = gr.Checkbox(
label="Skip img2img",
value=False,
Expand Down Expand Up @@ -179,11 +177,11 @@ def one_ui_group(n: int, is_img2img: bool, webui_info: WebuiInfo):

with gr.Group():
with gr.Row(variant="compact"):
w.ad_tap_enable = gr.Checkbox(
label=f"Enable this tap ({ordinal(n + 1)})",
w.ad_tab_enable = gr.Checkbox(
label=f"Enable this tab ({ordinal(n + 1)})",
value=True,
visible=True,
elem_id=eid("ad_tap_enable"),
elem_id=eid("ad_tab_enable"),
)

with gr.Row():
Expand Down
4 changes: 2 additions & 2 deletions adetailer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from .mediapipe import mediapipe_predict
from .ultralytics import ultralytics_predict

AFTER_DETAILER = "ADetailer"
ADETAILER = "ADetailer"

__all__ = [
"__version__",
"ADetailerArgs",
"AFTER_DETAILER",
"ADETAILER",
"ALL_ARGS",
"PredictOutput",
"get_models",
Expand Down
2 changes: 1 addition & 1 deletion adetailer/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "24.5.1"
__version__ = "24.6.0"
8 changes: 4 additions & 4 deletions adetailer/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def names(self) -> tuple[str, ...]:
class ADetailerArgs(BaseModel, extra=Extra.forbid):
ad_model: str = "None"
ad_model_classes: str = ""
ad_tap_enable: bool = True
ad_tab_enable: bool = True
ad_prompt: str = ""
ad_negative_prompt: str = ""
ad_confidence: confloat(ge=0.0, le=1.0) = 0.3
Expand Down Expand Up @@ -129,7 +129,7 @@ def extra_params(self, suffix: str = "") -> dict[str, Any]:
ppop("ADetailer model classes")
ppop("ADetailer prompt")
ppop("ADetailer negative prompt")
p.pop("ADetailer tap enable", None) # always pop
p.pop("ADetailer tab enable", None) # always pop
ppop("ADetailer mask only top k largest", cond=0)
ppop("ADetailer mask min ratio", cond=0.0)
ppop("ADetailer mask max ratio", cond=1.0)
Expand Down Expand Up @@ -206,13 +206,13 @@ def is_mediapipe(self) -> bool:
return self.ad_model.lower().startswith("mediapipe")

def need_skip(self) -> bool:
return self.ad_model == "None" or self.ad_tap_enable is False
return self.ad_model == "None" or self.ad_tab_enable is False


_all_args = [
("ad_model", "ADetailer model"),
("ad_model_classes", "ADetailer model classes"),
("ad_tap_enable", "ADetailer tap enable"),
("ad_tab_enable", "ADetailer tab enable"),
("ad_prompt", "ADetailer prompt"),
("ad_negative_prompt", "ADetailer negative prompt"),
("ad_confidence", "ADetailer confidence"),
Expand Down
57 changes: 36 additions & 21 deletions adetailer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
from contextlib import suppress
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Generic, Optional, TypeVar
Expand All @@ -24,14 +25,22 @@ class PredictOutput(Generic[T]):
preview: Optional[Image.Image] = None


def hf_download(file: str, repo_id: str = REPO_ID) -> str:
try:
path = hf_hub_download(repo_id, file)
except Exception:
msg = f"[-] ADetailer: Failed to load model {file!r} from huggingface"
print(msg)
path = "INVALID"
return path
def hf_download(file: str, repo_id: str = REPO_ID, check_remote: bool = True) -> str:
if check_remote:
with suppress(Exception):
return hf_hub_download(repo_id, file, etag_timeout=1)

with suppress(Exception):
return hf_hub_download(
repo_id, file, etag_timeout=1, endpoint="https://hf-mirror.com"
)

with suppress(Exception):
return hf_hub_download(repo_id, file, local_files_only=True)

msg = f"[-] ADetailer: Failed to load model {file!r} from huggingface"
print(msg)
return "INVALID"


def safe_mkdir(path: str | os.PathLike[str]) -> None:
Expand All @@ -46,16 +55,23 @@ def scan_model_dir(path: Path) -> list[Path]:
return [p for p in path.rglob("*") if p.is_file() and p.suffix == ".pt"]


def download_models(*names: str) -> dict[str, str]:
def download_models(*names: str, check_remote: bool = True) -> dict[str, str]:
models = OrderedDict()
with ThreadPoolExecutor() as executor:
for name in names:
if "-world" in name:
models[name] = executor.submit(
hf_download, name, repo_id="Bingsu/yolo-world-mirror"
hf_download,
name,
repo_id="Bingsu/yolo-world-mirror",
check_remote=check_remote,
)
else:
models[name] = executor.submit(hf_download, name)
models[name] = executor.submit(
hf_download,
name,
check_remote=check_remote,
)
return {name: future.result() for name, future in models.items()}


Expand All @@ -70,16 +86,15 @@ def get_models(
model_paths.extend(scan_model_dir(Path(dir_)))

models = OrderedDict()
if huggingface:
to_download = [
"face_yolov8n.pt",
"face_yolov8s.pt",
"hand_yolov8n.pt",
"person_yolov8n-seg.pt",
"person_yolov8s-seg.pt",
"yolov8x-worldv2.pt",
]
models.update(download_models(*to_download))
to_download = [
"face_yolov8n.pt",
"face_yolov8s.pt",
"hand_yolov8n.pt",
"person_yolov8n-seg.pt",
"person_yolov8s-seg.pt",
"yolov8x-worldv2.pt",
]
models.update(download_models(*to_download, check_remote=huggingface))

models.update(
{
Expand Down
12 changes: 1 addition & 11 deletions controlnet_ext/controlnet_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@
import sys
from functools import lru_cache
from pathlib import Path
from textwrap import dedent

from modules import extensions, sd_models, shared
from modules.paths import extensions_builtin_dir, extensions_dir, models_path

from .common import cn_model_module, cn_model_regex

try:
from modules.paths import extensions_builtin_dir, extensions_dir, models_path
except ImportError as e:
msg = """
[-] ADetailer: `stable-diffusion-webui < 1.1.0` is no longer supported.
Please upgrade to stable-diffusion-webui >= 1.1.0.
or you can use ADetailer v23.10.1 (https://github.com/Bing-su/adetailer/archive/refs/tags/v23.10.1.zip)
"""
raise RuntimeError(dedent(msg)) from e

ext_path = Path(extensions_dir)
ext_builtin_path = Path(extensions_builtin_dir)
controlnet_exists = False
Expand Down
2 changes: 0 additions & 2 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def install():
("ultralytics", "8.2.0", None),
("mediapipe", "0.10.13", None),
("rich", "13.0.0", None),
# mediapipe
("protobuf", "4.25.3", "4.9999"),
]

pkgs = []
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
name = "adetailer"
description = "An object detection and auto-mask extension for stable diffusion webui."
authors = [{ name = "dowon", email = "[email protected]" }]
requires-python = ">=3.8"
requires-python = ">=3.9"
readme = "README.md"
license = { text = "AGPL-3.0" }
dependencies = [
"ultralytics>=8.2",
"mediapipe>=0.10",
"mediapipe>=0.10.13",
"pydantic<3",
"rich>=13",
"huggingface_hub",
Expand Down Expand Up @@ -39,7 +39,7 @@ profile = "black"
known_first_party = ["launch", "modules"]

[tool.ruff]
target-version = "py38"
target-version = "py39"

[tool.ruff.lint]
select = [
Expand Down
Loading

0 comments on commit a7d9611

Please sign in to comment.