File size: 6,771 Bytes
d38fcf4
 
 
 
 
 
 
 
 
 
 
 
ffaf291
 
d38fcf4
 
 
4a36ff1
 
 
 
 
 
 
 
 
 
 
 
1d36332
ffaf291
d38fcf4
 
 
ffaf291
d38fcf4
 
 
 
 
 
ffaf291
 
 
 
 
d38fcf4
 
 
ffaf291
 
 
 
 
 
 
 
 
 
d38fcf4
 
 
 
a49cb7c
d38fcf4
a49cb7c
ffaf291
d38fcf4
 
 
 
ffaf291
 
 
 
 
 
 
 
 
 
 
 
 
 
cef99be
 
ffaf291
d38fcf4
 
 
 
 
 
 
 
 
 
 
 
 
954c0e5
d38fcf4
 
 
 
ffaf291
 
d38fcf4
 
 
 
ffaf291
 
 
 
 
 
d38fcf4
 
ffaf291
d38fcf4
 
ffaf291
 
 
 
 
 
 
 
 
 
 
d38fcf4
ffaf291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d38fcf4
ffaf291
 
 
 
 
 
 
 
 
 
 
 
 
d38fcf4
ffaf291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a49cb7c
ffaf291
 
 
 
 
 
d38fcf4
ffaf291
 
 
 
 
 
 
 
 
a49cb7c
ffaf291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python

from __future__ import annotations

import io
import tarfile

import gradio as gr
import numpy as np
import PIL.Image
from huggingface_hub import hf_hub_download

TITLE = "TADNE (This Anime Does Not Exist) Image Selector"
DESCRIPTION = """The original TADNE site is https://thisanimedoesnotexist.ai/.

You can view images generated by the TADNE model with seed 0-99999.
You can filter images based on predictions by the [DeepDanbooru](https://github.com/KichangKim/DeepDanbooru) model.

The resolution of the output images in this app is 128x128, but you can
check the original 512x512 images from URLs like
https://thisanimedoesnotexist.ai/slider.html?seed=10000 using the output seeds.

Expected execution time on Hugging Face Spaces: 4s

Related Apps:
- [TADNE](https://huggingface.co/spaces/hysts/TADNE)
- [TADNE Image Viewer](https://huggingface.co/spaces/hysts/TADNE-image-viewer)
- [TADNE Interpolation](https://huggingface.co/spaces/hysts/TADNE-interpolation)
- [TADNE Image Search with DeepDanbooru](https://huggingface.co/spaces/hysts/TADNE-image-search-with-DeepDanbooru)
- [DeepDanbooru](https://huggingface.co/spaces/hysts/DeepDanbooru)
"""


def load_deepdanbooru_tag_dict() -> dict[str, int]:
    path = hf_hub_download("public-data/DeepDanbooru", "tags.txt")
    with open(path) as f:
        tags = [line.strip() for line in f.readlines()]
    return {tag: i for i, tag in enumerate(tags)}


def load_deepdanbooru_predictions(dirname: str) -> np.ndarray:
    path = hf_hub_download(
        "hysts/TADNE-sample-images",
        f"prediction_results/deepdanbooru/{dirname}.npy",
        repo_type="dataset",
    )
    return np.load(path)


image_size = 128
min_seed = 0
max_seed = 99999
dirname = "0-99999"
tarball_path = hf_hub_download("hysts/TADNE-sample-images", f"{image_size}/{dirname}.tar", repo_type="dataset")

deepdanbooru_tag_dict = load_deepdanbooru_tag_dict()
deepdanbooru_predictions = load_deepdanbooru_predictions(dirname)


def run(
    general_tags: list[str],
    hair_color_tags: list[str],
    hair_style_tags: list[str],
    eye_color_tags: list[str],
    image_color_tags: list[str],
    other_tags: list[str],
    additional_tags_str: str,
    score_threshold: float,
    start_index: int,
    nrows: int,
    ncols: int,
) -> tuple[int, np.ndarray, np.ndarray, str]:
    hair_color_tags = [f"{color}_hair" for color in hair_color_tags]
    eye_color_tags = [f"{color}_eyes" for color in eye_color_tags]
    additional_tags = additional_tags_str.split(",")

    tags = (
        general_tags
        + hair_color_tags
        + hair_style_tags
        + eye_color_tags
        + image_color_tags
        + other_tags
        + additional_tags
    )
    missing_tags = [tag for tag in tags if tag not in deepdanbooru_tag_dict]

    tag_indices = [deepdanbooru_tag_dict[tag] for tag in tags if tag in deepdanbooru_tag_dict]

    conditions = deepdanbooru_predictions[:, tag_indices] > score_threshold
    image_indices = np.arange(len(deepdanbooru_predictions))
    image_indices = image_indices[conditions.all(axis=1)]

    start_index = int(start_index)
    num = nrows * ncols
    seeds = []
    images = []
    dummy = np.ones((image_size, image_size, 3), dtype=np.uint8) * 255
    with tarfile.TarFile(tarball_path) as tar_file:
        for index in range(start_index, start_index + num):
            if index >= len(image_indices):
                seeds.append(np.nan)
                images.append(dummy)
                continue
            image_index = image_indices[index]
            seeds.append(image_index)
            member = tar_file.getmember(f"{dirname}/{image_index:07d}.jpg")
            with tar_file.extractfile(member) as f:  # type: ignore
                data = io.BytesIO(f.read())
            image = PIL.Image.open(data)
            image = np.asarray(image)
            images.append(image)
    res = (
        np.asarray(images)
        .reshape(nrows, ncols, image_size, image_size, 3)
        .transpose(0, 2, 1, 3, 4)
        .reshape(nrows * image_size, ncols * image_size, 3)
    )
    seeds = np.asarray(seeds).reshape(nrows, ncols)

    return len(image_indices), res, seeds, ",".join(missing_tags)


demo = gr.Interface(
    fn=run,
    inputs=[
        gr.CheckboxGroup(
            label="General",
            choices=[
                "1girl",
                "1boy",
                "multiple_girls",
                "multiple_boys",
                "looking_at_viewer",
            ],
        ),
        gr.CheckboxGroup(
            label="Hair Color",
            choices=[
                "aqua",
                "black",
                "blonde",
                "blue",
                "brown",
                "green",
                "grey",
                "orange",
                "pink",
                "purple",
                "red",
                "silver",
                "white",
            ],
        ),
        gr.CheckboxGroup(
            label="Hair Style",
            choices=[
                "bangs",
                "curly_hair",
                "long_hair",
                "medium_hair",
                "messy_hair",
                "ponytail",
                "short_hair",
                "straight_hair",
                "twintails",
            ],
        ),
        gr.CheckboxGroup(
            label="Eye Color",
            choices=[
                "aqua",
                "black",
                "blue",
                "brown",
                "green",
                "grey",
                "orange",
                "pink",
                "purple",
                "red",
                "white",
                "yellow",
            ],
        ),
        gr.CheckboxGroup(
            label="Image Color",
            choices=[
                "greyscale",
                "monochrome",
            ],
        ),
        gr.CheckboxGroup(
            label="Others",
            choices=[
                "animal_ears",
                "closed_eyes",
                "full_body",
                "hat",
                "smile",
            ],
        ),
        gr.Textbox(label="Additional Tags"),
        gr.Slider(label="DeepDanbooru Score Threshold", minimum=0, maximum=1, step=0.1, value=0.5),
        gr.Number(label="Start Index", value=0),
        gr.Slider(label="Number of Rows", minimum=0, maximum=10, step=1, value=2),
        gr.Slider(label="Number of Columns", minimum=0, maximum=10, step=1, value=5),
    ],
    outputs=[
        gr.Textbox(label="Number of Found Images"),
        gr.Image(label="Output"),
        gr.Dataframe(label="Seed"),
        gr.Textbox(label="Missing Tags"),
    ],
    title=TITLE,
    description=DESCRIPTION,
)


if __name__ == "__main__":
    demo.queue().launch()