hysts HF staff commited on
Commit
ffaf291
1 Parent(s): 2039fce
Files changed (4) hide show
  1. .pre-commit-config.yaml +60 -0
  2. README.md +1 -1
  3. app.py +136 -181
  4. 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: 3.0.5
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 = 'TADNE (This Anime Does Not Exist) Image Selector'
18
- DESCRIPTION = '''The original TADNE site is https://thisanimedoesnotexist.ai/.
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('hysts/DeepDanbooru',
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('hysts/TADNE-sample-images',
73
- f'prediction_results/deepdanbooru/{dirname}.npy',
74
- repo_type='dataset',
75
- use_auth_token=TOKEN)
 
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
- additional_tags: str,
87
  score_threshold: float,
88
  start_index: int,
89
  nrows: int,
90
  ncols: int,
91
- image_size: int,
92
- min_seed: int,
93
- max_seed: int,
94
- dirname: str,
95
- tarball_path: pathlib.Path,
96
- deepdanbooru_tag_dict: dict[str, int],
97
- deepdanbooru_predictions: np.ndarray,
98
- ) -> tuple[int, np.ndarray, np.ndarray]:
99
- hair_color_tags = [f'{color}_hair' for color in hair_color_tags]
100
- eye_color_tags = [f'{color}_eyes' for color in eye_color_tags]
101
- additional_tags = additional_tags.split(',')
102
-
103
- tags = general_tags + hair_color_tags + hair_style_tags + \
104
- eye_color_tags + image_color_tags + other_tags + additional_tags
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'{dirname}/{image_index:07d}.jpg')
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 = np.asarray(images).reshape(nrows, ncols, image_size, image_size,
136
- 3).transpose(0, 2, 1, 3, 4).reshape(
137
- nrows * image_size,
138
- ncols * image_size, 3)
 
 
139
  seeds = np.asarray(seeds).reshape(nrows, ncols)
140
 
141
- return len(image_indices), res, seeds, ','.join(missing_tags)
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
- func = functools.partial(
157
- run,
158
- image_size=image_size,
159
- min_seed=min_seed,
160
- max_seed=max_seed,
161
- dirname=dirname,
162
- tarball_path=tarball_path,
163
- deepdanbooru_tag_dict=deepdanbooru_tag_dict,
164
- deepdanbooru_predictions=deepdanbooru_predictions,
165
- )
166
- func = functools.update_wrapper(func, run)
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
- label='General'),
179
- gr.inputs.CheckboxGroup([
180
- 'aqua',
181
- 'black',
182
- 'blonde',
183
- 'blue',
184
- 'brown',
185
- 'green',
186
- 'grey',
187
- 'orange',
188
- 'pink',
189
- 'purple',
190
- 'red',
191
- 'silver',
192
- 'white',
 
 
193
  ],
194
- label='Hair Color'),
195
- gr.inputs.CheckboxGroup([
196
- 'bangs',
197
- 'curly_hair',
198
- 'long_hair',
199
- 'medium_hair',
200
- 'messy_hair',
201
- 'ponytail',
202
- 'short_hair',
203
- 'straight_hair',
204
- 'twintails',
 
 
205
  ],
206
- label='Hair Style'),
207
- gr.inputs.CheckboxGroup([
208
- 'aqua',
209
- 'black',
210
- 'blue',
211
- 'brown',
212
- 'green',
213
- 'grey',
214
- 'orange',
215
- 'pink',
216
- 'purple',
217
- 'red',
218
- 'white',
219
- 'yellow',
 
 
220
  ],
221
- label='Eye Color'),
222
- gr.inputs.CheckboxGroup([
223
- 'greyscale',
224
- 'monochrome',
 
 
225
  ],
226
- label='Image Color'),
227
- gr.inputs.CheckboxGroup([
228
- 'animal_ears',
229
- 'closed_eyes',
230
- 'full_body',
231
- 'hat',
232
- 'smile',
 
 
233
  ],
234
- label='Others'),
235
- gr.inputs.Textbox(label='Additional Tags'),
236
- gr.inputs.Slider(0,
237
- 1,
238
- step=0.1,
239
- default=0.5,
240
- label='DeepDanbooru Score Threshold'),
241
- gr.inputs.Number(default=0, label='Start Index'),
242
- gr.inputs.Slider(1, 10, step=1, default=2, label='Number of Rows'),
243
- gr.inputs.Slider(
244
- 1, 10, step=1, default=5, label='Number of Columns'),
245
- ],
246
- [
247
- gr.outputs.Textbox(type='number', label='Number of Found Images'),
248
- gr.outputs.Image(type='numpy', label='Output'),
249
- gr.outputs.Dataframe(type='numpy', label='Seed'),
250
- gr.outputs.Textbox(type='str', label='Missing Tags'),
251
- ],
252
- title=TITLE,
253
- description=DESCRIPTION,
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.22.3
2
- Pillow==9.0.1
 
1
+ numpy==1.26.4
2
+ Pillow==10.3.0