hysts HF staff commited on
Commit
7be6148
1 Parent(s): b5c21a0
Files changed (4) hide show
  1. .pre-commit-config.yaml +60 -0
  2. README.md +1 -1
  3. app.py +34 -76
  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: red
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: red
5
  colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,10 +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
 
@@ -14,8 +11,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 Viewer'
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
  The original images are 512x512 in size, but they are resized to 128x128 here.
@@ -28,36 +25,21 @@ Related Apps:
28
  - [TADNE Image Selector](https://huggingface.co/spaces/hysts/TADNE-image-selector)
29
  - [TADNE Interpolation](https://huggingface.co/spaces/hysts/TADNE-interpolation)
30
  - [TADNE Image Search with DeepDanbooru](https://huggingface.co/spaces/hysts/TADNE-image-search-with-DeepDanbooru)
31
- '''
32
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.tadne-image-viewer" alt="visitor badge"/></center>'
33
 
34
- TOKEN = os.environ['TOKEN']
35
 
 
 
 
 
 
36
 
37
- def parse_args() -> argparse.Namespace:
38
- parser = argparse.ArgumentParser()
39
- parser.add_argument('--theme', type=str)
40
- parser.add_argument('--live', action='store_true')
41
- parser.add_argument('--share', action='store_true')
42
- parser.add_argument('--port', type=int)
43
- parser.add_argument('--disable-queue',
44
- dest='enable_queue',
45
- action='store_false')
46
- parser.add_argument('--allow-flagging', type=str, default='never')
47
- return parser.parse_args()
48
 
49
-
50
- def download_image_tarball(size: int, dirname: str) -> pathlib.Path:
51
- path = hf_hub_download('hysts/TADNE-sample-images',
52
- f'{size}/{dirname}.tar',
53
- repo_type='dataset',
54
- use_auth_token=TOKEN)
55
- return path
56
-
57
-
58
- def run(start_seed: int, nrows: int, ncols: int, image_size: int,
59
- min_seed: int, max_seed: int, dirname: str,
60
- tarball_path: pathlib.Path) -> np.ndarray:
61
  start_seed = int(start_seed)
62
  num = nrows * ncols
63
  images = []
@@ -67,57 +49,33 @@ def run(start_seed: int, nrows: int, ncols: int, image_size: int,
67
  if not min_seed <= seed <= max_seed:
68
  images.append(dummy)
69
  continue
70
- member = tar_file.getmember(f'{dirname}/{seed:07d}.jpg')
71
- with tar_file.extractfile(member) as f:
72
  data = io.BytesIO(f.read())
73
  image = PIL.Image.open(data)
74
  image = np.asarray(image)
75
  images.append(image)
76
- res = np.asarray(images).reshape(nrows, ncols, image_size, image_size,
77
- 3).transpose(0, 2, 1, 3, 4).reshape(
78
- nrows * image_size,
79
- ncols * image_size, 3)
 
 
80
  return res
81
 
82
 
83
- def main():
84
- args = parse_args()
85
-
86
- image_size = 128
87
- min_seed = 0
88
- max_seed = 99999
89
- dirname = '0-99999'
90
- tarball_path = download_image_tarball(image_size, dirname)
91
-
92
- func = functools.partial(run,
93
- image_size=image_size,
94
- min_seed=min_seed,
95
- max_seed=max_seed,
96
- dirname=dirname,
97
- tarball_path=tarball_path)
98
- func = functools.update_wrapper(func, run)
99
-
100
- gr.Interface(
101
- func,
102
- [
103
- gr.inputs.Number(default=0, label='Start Seed'),
104
- gr.inputs.Slider(1, 10, step=1, default=2, label='Number of Rows'),
105
- gr.inputs.Slider(
106
- 1, 10, step=1, default=5, label='Number of Columns'),
107
- ],
108
- gr.outputs.Image(type='numpy', label='Output'),
109
- title=TITLE,
110
- description=DESCRIPTION,
111
- article=ARTICLE,
112
- theme=args.theme,
113
- allow_flagging=args.allow_flagging,
114
- live=args.live,
115
- ).launch(
116
- enable_queue=args.enable_queue,
117
- server_port=args.port,
118
- share=args.share,
119
- )
120
 
121
 
122
- if __name__ == '__main__':
123
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
 
5
  import io
 
6
  import pathlib
7
  import tarfile
8
 
 
11
  import PIL.Image
12
  from huggingface_hub import hf_hub_download
13
 
14
+ TITLE = "TADNE (This Anime Does Not Exist) Image Viewer"
15
+ DESCRIPTION = """The original TADNE site is https://thisanimedoesnotexist.ai/.
16
 
17
  You can view images generated by the TADNE model with seed 0-99999.
18
  The original images are 512x512 in size, but they are resized to 128x128 here.
 
25
  - [TADNE Image Selector](https://huggingface.co/spaces/hysts/TADNE-image-selector)
26
  - [TADNE Interpolation](https://huggingface.co/spaces/hysts/TADNE-interpolation)
27
  - [TADNE Image Search with DeepDanbooru](https://huggingface.co/spaces/hysts/TADNE-image-search-with-DeepDanbooru)
28
+ """
 
29
 
 
30
 
31
+ image_size = 128
32
+ min_seed = 0
33
+ max_seed = 99999
34
+ dirname = "0-99999"
35
+ tarball_path = hf_hub_download("hysts/TADNE-sample-images", f"{image_size}/{dirname}.tar", repo_type="dataset")
36
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ def run(
39
+ start_seed: int,
40
+ nrows: int,
41
+ ncols: int,
42
+ ) -> np.ndarray:
 
 
 
 
 
 
 
43
  start_seed = int(start_seed)
44
  num = nrows * ncols
45
  images = []
 
49
  if not min_seed <= seed <= max_seed:
50
  images.append(dummy)
51
  continue
52
+ member = tar_file.getmember(f"{dirname}/{seed:07d}.jpg")
53
+ with tar_file.extractfile(member) as f: # type: ignore
54
  data = io.BytesIO(f.read())
55
  image = PIL.Image.open(data)
56
  image = np.asarray(image)
57
  images.append(image)
58
+ res = (
59
+ np.asarray(images)
60
+ .reshape(nrows, ncols, image_size, image_size, 3)
61
+ .transpose(0, 2, 1, 3, 4)
62
+ .reshape(nrows * image_size, ncols * image_size, 3)
63
+ )
64
  return res
65
 
66
 
67
+ demo = gr.Interface(
68
+ fn=run,
69
+ inputs=[
70
+ gr.Number(label="Start Seed", value=0),
71
+ gr.Slider(label="Number of Rows", minimum=1, maximum=10, step=1, value=2),
72
+ gr.Slider(label="Number of Columns", minimum=1, maximum=10, step=1, value=5),
73
+ ],
74
+ outputs=gr.Image(label="Output"),
75
+ title=TITLE,
76
+ description=DESCRIPTION,
77
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
 
80
+ if __name__ == "__main__":
81
+ 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