Spaces:
Runtime error
Runtime error
Update
Browse files- .pre-commit-config.yaml +60 -0
- README.md +1 -1
- app.py +136 -181
- requirements.txt +2 -2
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.6.0
|
4 |
+
hooks:
|
5 |
+
- id: check-executables-have-shebangs
|
6 |
+
- id: check-json
|
7 |
+
- id: check-merge-conflict
|
8 |
+
- id: check-shebang-scripts-are-executable
|
9 |
+
- id: check-toml
|
10 |
+
- id: check-yaml
|
11 |
+
- id: end-of-file-fixer
|
12 |
+
- id: mixed-line-ending
|
13 |
+
args: ["--fix=lf"]
|
14 |
+
- id: requirements-txt-fixer
|
15 |
+
- id: trailing-whitespace
|
16 |
+
- repo: https://github.com/myint/docformatter
|
17 |
+
rev: v1.7.5
|
18 |
+
hooks:
|
19 |
+
- id: docformatter
|
20 |
+
args: ["--in-place"]
|
21 |
+
- repo: https://github.com/pycqa/isort
|
22 |
+
rev: 5.13.2
|
23 |
+
hooks:
|
24 |
+
- id: isort
|
25 |
+
args: ["--profile", "black"]
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v1.10.0
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ["--ignore-missing-imports"]
|
31 |
+
additional_dependencies:
|
32 |
+
[
|
33 |
+
"types-python-slugify",
|
34 |
+
"types-requests",
|
35 |
+
"types-PyYAML",
|
36 |
+
"types-pytz",
|
37 |
+
]
|
38 |
+
- repo: https://github.com/psf/black
|
39 |
+
rev: 24.4.2
|
40 |
+
hooks:
|
41 |
+
- id: black
|
42 |
+
language_version: python3.10
|
43 |
+
args: ["--line-length", "119"]
|
44 |
+
- repo: https://github.com/kynan/nbstripout
|
45 |
+
rev: 0.7.1
|
46 |
+
hooks:
|
47 |
+
- id: nbstripout
|
48 |
+
args:
|
49 |
+
[
|
50 |
+
"--extra-keys",
|
51 |
+
"metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
|
52 |
+
]
|
53 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
54 |
+
rev: 1.8.5
|
55 |
+
hooks:
|
56 |
+
- id: nbqa-black
|
57 |
+
- id: nbqa-pyupgrade
|
58 |
+
args: ["--py37-plus"]
|
59 |
+
- id: nbqa-isort
|
60 |
+
args: ["--float-to-top"]
|
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🐠
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.36.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -2,11 +2,7 @@
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
5 |
-
import argparse
|
6 |
-
import functools
|
7 |
import io
|
8 |
-
import os
|
9 |
-
import pathlib
|
10 |
import tarfile
|
11 |
|
12 |
import gradio as gr
|
@@ -14,8 +10,8 @@ import numpy as np
|
|
14 |
import PIL.Image
|
15 |
from huggingface_hub import hf_hub_download
|
16 |
|
17 |
-
TITLE =
|
18 |
-
DESCRIPTION =
|
19 |
|
20 |
You can view images generated by the TADNE model with seed 0-99999.
|
21 |
You can filter images based on predictions by the [DeepDanbooru](https://github.com/KichangKim/DeepDanbooru) model.
|
@@ -32,50 +28,35 @@ Related Apps:
|
|
32 |
- [TADNE Interpolation](https://huggingface.co/spaces/hysts/TADNE-interpolation)
|
33 |
- [TADNE Image Search with DeepDanbooru](https://huggingface.co/spaces/hysts/TADNE-image-search-with-DeepDanbooru)
|
34 |
- [DeepDanbooru](https://huggingface.co/spaces/hysts/DeepDanbooru)
|
35 |
-
|
36 |
-
ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.tadne-image-selector" alt="visitor badge"/></center>'
|
37 |
-
|
38 |
-
TOKEN = os.environ['TOKEN']
|
39 |
-
|
40 |
-
|
41 |
-
def parse_args() -> argparse.Namespace:
|
42 |
-
parser = argparse.ArgumentParser()
|
43 |
-
parser.add_argument('--theme', type=str)
|
44 |
-
parser.add_argument('--live', action='store_true')
|
45 |
-
parser.add_argument('--share', action='store_true')
|
46 |
-
parser.add_argument('--port', type=int)
|
47 |
-
parser.add_argument('--disable-queue',
|
48 |
-
dest='enable_queue',
|
49 |
-
action='store_false')
|
50 |
-
parser.add_argument('--allow-flagging', type=str, default='never')
|
51 |
-
return parser.parse_args()
|
52 |
-
|
53 |
-
|
54 |
-
def download_image_tarball(size: int, dirname: str) -> pathlib.Path:
|
55 |
-
path = hf_hub_download('hysts/TADNE-sample-images',
|
56 |
-
f'{size}/{dirname}.tar',
|
57 |
-
repo_type='dataset',
|
58 |
-
use_auth_token=TOKEN)
|
59 |
-
return path
|
60 |
|
61 |
|
62 |
def load_deepdanbooru_tag_dict() -> dict[str, int]:
|
63 |
-
path = hf_hub_download(
|
64 |
-
'tags.txt',
|
65 |
-
use_auth_token=TOKEN)
|
66 |
with open(path) as f:
|
67 |
tags = [line.strip() for line in f.readlines()]
|
68 |
return {tag: i for i, tag in enumerate(tags)}
|
69 |
|
70 |
|
71 |
def load_deepdanbooru_predictions(dirname: str) -> np.ndarray:
|
72 |
-
path = hf_hub_download(
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
76 |
return np.load(path)
|
77 |
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def run(
|
80 |
general_tags: list[str],
|
81 |
hair_color_tags: list[str],
|
@@ -83,31 +64,28 @@ def run(
|
|
83 |
eye_color_tags: list[str],
|
84 |
image_color_tags: list[str],
|
85 |
other_tags: list[str],
|
86 |
-
|
87 |
score_threshold: float,
|
88 |
start_index: int,
|
89 |
nrows: int,
|
90 |
ncols: int,
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
missing_tags = [tag for tag in tags if tag not in deepdanbooru_tag_dict]
|
106 |
|
107 |
-
tag_indices = [
|
108 |
-
deepdanbooru_tag_dict[tag] for tag in tags
|
109 |
-
if tag in deepdanbooru_tag_dict
|
110 |
-
]
|
111 |
|
112 |
conditions = deepdanbooru_predictions[:, tag_indices] > score_threshold
|
113 |
image_indices = np.arange(len(deepdanbooru_predictions))
|
@@ -126,141 +104,118 @@ def run(
|
|
126 |
continue
|
127 |
image_index = image_indices[index]
|
128 |
seeds.append(image_index)
|
129 |
-
member = tar_file.getmember(f
|
130 |
-
with tar_file.extractfile(member) as f:
|
131 |
data = io.BytesIO(f.read())
|
132 |
image = PIL.Image.open(data)
|
133 |
image = np.asarray(image)
|
134 |
images.append(image)
|
135 |
-
res =
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
139 |
seeds = np.asarray(seeds).reshape(nrows, ncols)
|
140 |
|
141 |
-
return len(image_indices), res, seeds,
|
142 |
-
|
143 |
-
|
144 |
-
def main():
|
145 |
-
args = parse_args()
|
146 |
-
|
147 |
-
image_size = 128
|
148 |
-
min_seed = 0
|
149 |
-
max_seed = 99999
|
150 |
-
dirname = '0-99999'
|
151 |
-
tarball_path = download_image_tarball(image_size, dirname)
|
152 |
|
153 |
-
deepdanbooru_tag_dict = load_deepdanbooru_tag_dict()
|
154 |
-
deepdanbooru_predictions = load_deepdanbooru_predictions(dirname)
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
gr.Interface(
|
169 |
-
func,
|
170 |
-
[
|
171 |
-
gr.inputs.CheckboxGroup([
|
172 |
-
'1girl',
|
173 |
-
'1boy',
|
174 |
-
'multiple_girls',
|
175 |
-
'multiple_boys',
|
176 |
-
'looking_at_viewer',
|
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 |
-
|
223 |
-
|
224 |
-
|
|
|
|
|
225 |
],
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
233 |
],
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
article=ARTICLE,
|
255 |
-
theme=args.theme,
|
256 |
-
allow_flagging=args.allow_flagging,
|
257 |
-
live=args.live,
|
258 |
-
).launch(
|
259 |
-
enable_queue=args.enable_queue,
|
260 |
-
server_port=args.port,
|
261 |
-
share=args.share,
|
262 |
-
)
|
263 |
-
|
264 |
-
|
265 |
-
if __name__ == '__main__':
|
266 |
-
main()
|
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
|
|
|
|
5 |
import io
|
|
|
|
|
6 |
import tarfile
|
7 |
|
8 |
import gradio as gr
|
|
|
10 |
import PIL.Image
|
11 |
from huggingface_hub import hf_hub_download
|
12 |
|
13 |
+
TITLE = "TADNE (This Anime Does Not Exist) Image Selector"
|
14 |
+
DESCRIPTION = """The original TADNE site is https://thisanimedoesnotexist.ai/.
|
15 |
|
16 |
You can view images generated by the TADNE model with seed 0-99999.
|
17 |
You can filter images based on predictions by the [DeepDanbooru](https://github.com/KichangKim/DeepDanbooru) model.
|
|
|
28 |
- [TADNE Interpolation](https://huggingface.co/spaces/hysts/TADNE-interpolation)
|
29 |
- [TADNE Image Search with DeepDanbooru](https://huggingface.co/spaces/hysts/TADNE-image-search-with-DeepDanbooru)
|
30 |
- [DeepDanbooru](https://huggingface.co/spaces/hysts/DeepDanbooru)
|
31 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
|
34 |
def load_deepdanbooru_tag_dict() -> dict[str, int]:
|
35 |
+
path = hf_hub_download("public-data/DeepDanbooru", "tags.txt")
|
|
|
|
|
36 |
with open(path) as f:
|
37 |
tags = [line.strip() for line in f.readlines()]
|
38 |
return {tag: i for i, tag in enumerate(tags)}
|
39 |
|
40 |
|
41 |
def load_deepdanbooru_predictions(dirname: str) -> np.ndarray:
|
42 |
+
path = hf_hub_download(
|
43 |
+
"hysts/TADNE-sample-images",
|
44 |
+
f"prediction_results/deepdanbooru/{dirname}.npy",
|
45 |
+
repo_type="dataset",
|
46 |
+
)
|
47 |
return np.load(path)
|
48 |
|
49 |
|
50 |
+
image_size = 128
|
51 |
+
min_seed = 0
|
52 |
+
max_seed = 99999
|
53 |
+
dirname = "0-99999"
|
54 |
+
tarball_path = hf_hub_download("hysts/TADNE-sample-images", f"{image_size}/{dirname}.tar", repo_type="dataset")
|
55 |
+
|
56 |
+
deepdanbooru_tag_dict = load_deepdanbooru_tag_dict()
|
57 |
+
deepdanbooru_predictions = load_deepdanbooru_predictions(dirname)
|
58 |
+
|
59 |
+
|
60 |
def run(
|
61 |
general_tags: list[str],
|
62 |
hair_color_tags: list[str],
|
|
|
64 |
eye_color_tags: list[str],
|
65 |
image_color_tags: list[str],
|
66 |
other_tags: list[str],
|
67 |
+
additional_tags_str: str,
|
68 |
score_threshold: float,
|
69 |
start_index: int,
|
70 |
nrows: int,
|
71 |
ncols: int,
|
72 |
+
) -> tuple[int, np.ndarray, np.ndarray, str]:
|
73 |
+
hair_color_tags = [f"{color}_hair" for color in hair_color_tags]
|
74 |
+
eye_color_tags = [f"{color}_eyes" for color in eye_color_tags]
|
75 |
+
additional_tags = additional_tags_str.split(",")
|
76 |
+
|
77 |
+
tags = (
|
78 |
+
general_tags
|
79 |
+
+ hair_color_tags
|
80 |
+
+ hair_style_tags
|
81 |
+
+ eye_color_tags
|
82 |
+
+ image_color_tags
|
83 |
+
+ other_tags
|
84 |
+
+ additional_tags
|
85 |
+
)
|
86 |
missing_tags = [tag for tag in tags if tag not in deepdanbooru_tag_dict]
|
87 |
|
88 |
+
tag_indices = [deepdanbooru_tag_dict[tag] for tag in tags if tag in deepdanbooru_tag_dict]
|
|
|
|
|
|
|
89 |
|
90 |
conditions = deepdanbooru_predictions[:, tag_indices] > score_threshold
|
91 |
image_indices = np.arange(len(deepdanbooru_predictions))
|
|
|
104 |
continue
|
105 |
image_index = image_indices[index]
|
106 |
seeds.append(image_index)
|
107 |
+
member = tar_file.getmember(f"{dirname}/{image_index:07d}.jpg")
|
108 |
+
with tar_file.extractfile(member) as f: # type: ignore
|
109 |
data = io.BytesIO(f.read())
|
110 |
image = PIL.Image.open(data)
|
111 |
image = np.asarray(image)
|
112 |
images.append(image)
|
113 |
+
res = (
|
114 |
+
np.asarray(images)
|
115 |
+
.reshape(nrows, ncols, image_size, image_size, 3)
|
116 |
+
.transpose(0, 2, 1, 3, 4)
|
117 |
+
.reshape(nrows * image_size, ncols * image_size, 3)
|
118 |
+
)
|
119 |
seeds = np.asarray(seeds).reshape(nrows, ncols)
|
120 |
|
121 |
+
return len(image_indices), res, seeds, ",".join(missing_tags)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
|
|
|
|
123 |
|
124 |
+
demo = gr.Interface(
|
125 |
+
fn=run,
|
126 |
+
inputs=[
|
127 |
+
gr.CheckboxGroup(
|
128 |
+
label="General",
|
129 |
+
choices=[
|
130 |
+
"1girl",
|
131 |
+
"1boy",
|
132 |
+
"multiple_girls",
|
133 |
+
"multiple_boys",
|
134 |
+
"looking_at_viewer",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
],
|
136 |
+
),
|
137 |
+
gr.CheckboxGroup(
|
138 |
+
label="Hair Color",
|
139 |
+
choices=[
|
140 |
+
"aqua",
|
141 |
+
"black",
|
142 |
+
"blonde",
|
143 |
+
"blue",
|
144 |
+
"brown",
|
145 |
+
"green",
|
146 |
+
"grey",
|
147 |
+
"orange",
|
148 |
+
"pink",
|
149 |
+
"purple",
|
150 |
+
"red",
|
151 |
+
"silver",
|
152 |
+
"white",
|
153 |
],
|
154 |
+
),
|
155 |
+
gr.CheckboxGroup(
|
156 |
+
label="Hair Style",
|
157 |
+
choices=[
|
158 |
+
"bangs",
|
159 |
+
"curly_hair",
|
160 |
+
"long_hair",
|
161 |
+
"medium_hair",
|
162 |
+
"messy_hair",
|
163 |
+
"ponytail",
|
164 |
+
"short_hair",
|
165 |
+
"straight_hair",
|
166 |
+
"twintails",
|
167 |
],
|
168 |
+
),
|
169 |
+
gr.CheckboxGroup(
|
170 |
+
label="Eye Color",
|
171 |
+
choices=[
|
172 |
+
"aqua",
|
173 |
+
"black",
|
174 |
+
"blue",
|
175 |
+
"brown",
|
176 |
+
"green",
|
177 |
+
"grey",
|
178 |
+
"orange",
|
179 |
+
"pink",
|
180 |
+
"purple",
|
181 |
+
"red",
|
182 |
+
"white",
|
183 |
+
"yellow",
|
184 |
],
|
185 |
+
),
|
186 |
+
gr.CheckboxGroup(
|
187 |
+
label="Image Color",
|
188 |
+
choices=[
|
189 |
+
"greyscale",
|
190 |
+
"monochrome",
|
191 |
],
|
192 |
+
),
|
193 |
+
gr.CheckboxGroup(
|
194 |
+
label="Others",
|
195 |
+
choices=[
|
196 |
+
"animal_ears",
|
197 |
+
"closed_eyes",
|
198 |
+
"full_body",
|
199 |
+
"hat",
|
200 |
+
"smile",
|
201 |
],
|
202 |
+
),
|
203 |
+
gr.Textbox(label="Additional Tags"),
|
204 |
+
gr.Slider(label="DeepDanbooru Score Threshold", minimum=0, maximum=1, step=0.1, value=0.5),
|
205 |
+
gr.Number(label="Start Index", value=0),
|
206 |
+
gr.Slider(label="Number of Rows", minimum=0, maximum=10, step=1, value=2),
|
207 |
+
gr.Slider(label="Number of Columns", minimum=0, maximum=10, step=1, value=5),
|
208 |
+
],
|
209 |
+
outputs=[
|
210 |
+
gr.Textbox(label="Number of Found Images"),
|
211 |
+
gr.Image(label="Output"),
|
212 |
+
gr.Dataframe(label="Seed"),
|
213 |
+
gr.Textbox(label="Missing Tags"),
|
214 |
+
],
|
215 |
+
title=TITLE,
|
216 |
+
description=DESCRIPTION,
|
217 |
+
)
|
218 |
+
|
219 |
+
|
220 |
+
if __name__ == "__main__":
|
221 |
+
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
-
numpy==1.
|
2 |
-
Pillow==
|
|
|
1 |
+
numpy==1.26.4
|
2 |
+
Pillow==10.3.0
|