hysts HF staff commited on
Commit
86f89b5
1 Parent(s): 4e5056d
Files changed (2) hide show
  1. app.py +118 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
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
13
+ 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.
22
+ '''
23
+ ARTICLE = None
24
+
25
+ TOKEN = os.environ['TOKEN']
26
+
27
+
28
+ def parse_args() -> argparse.Namespace:
29
+ parser = argparse.ArgumentParser()
30
+ parser.add_argument('--theme', type=str)
31
+ parser.add_argument('--live', action='store_true')
32
+ parser.add_argument('--share', action='store_true')
33
+ parser.add_argument('--port', type=int)
34
+ parser.add_argument('--disable-queue',
35
+ dest='enable_queue',
36
+ action='store_false')
37
+ parser.add_argument('--allow-flagging', type=str, default='never')
38
+ parser.add_argument('--allow-screenshot', action='store_true')
39
+ return parser.parse_args()
40
+
41
+
42
+ def download_image_tarball(size: int, dirname: str) -> pathlib.Path:
43
+ path = hf_hub_download('hysts/TADNE-sample-images',
44
+ f'{size}/{dirname}.tar',
45
+ repo_type='dataset',
46
+ use_auth_token=TOKEN)
47
+ return path
48
+
49
+
50
+ def run(start_seed: int, nrows: int, ncols: int, image_size: int,
51
+ min_seed: int, max_seed: int, dirname: str,
52
+ tarball_path: pathlib.Path) -> np.ndarray:
53
+ start_seed = int(start_seed)
54
+ num = nrows * ncols
55
+ images = []
56
+ dummy = np.ones((image_size, image_size, 3), dtype=np.uint8) * 255
57
+ with tarfile.TarFile(tarball_path) as tar_file:
58
+ for seed in range(start_seed, start_seed + num):
59
+ if not min_seed <= seed <= max_seed:
60
+ images.append(dummy)
61
+ continue
62
+ member = tar_file.getmember(f'{dirname}/{seed:07d}.jpg')
63
+ with tar_file.extractfile(member) as f:
64
+ data = io.BytesIO(f.read())
65
+ image = PIL.Image.open(data)
66
+ image = np.asarray(image)
67
+ images.append(image)
68
+ res = np.asarray(images).reshape(nrows, ncols, image_size, image_size,
69
+ 3).transpose(0, 2, 1, 3, 4).reshape(
70
+ nrows * image_size,
71
+ ncols * image_size, 3)
72
+ return res
73
+
74
+
75
+ def main():
76
+ gr.close_all()
77
+
78
+ args = parse_args()
79
+
80
+ image_size = 128
81
+ min_seed = 0
82
+ max_seed = 99999
83
+ dirname = '0-99999'
84
+ tarball_path = download_image_tarball(image_size, dirname)
85
+
86
+ func = functools.partial(run,
87
+ image_size=image_size,
88
+ min_seed=min_seed,
89
+ max_seed=max_seed,
90
+ dirname=dirname,
91
+ tarball_path=tarball_path)
92
+ func = functools.update_wrapper(func, run)
93
+
94
+ gr.Interface(
95
+ func,
96
+ [
97
+ gr.inputs.Number(default=0, label='Seed'),
98
+ gr.inputs.Slider(1, 10, step=1, default=2, label='Number of Rows'),
99
+ gr.inputs.Slider(
100
+ 1, 10, step=1, default=5, label='Number of Columns'),
101
+ ],
102
+ gr.outputs.Image(type='numpy', label='Output'),
103
+ title=TITLE,
104
+ description=DESCRIPTION,
105
+ article=ARTICLE,
106
+ theme=args.theme,
107
+ allow_screenshot=args.allow_screenshot,
108
+ allow_flagging=args.allow_flagging,
109
+ live=args.live,
110
+ ).launch(
111
+ enable_queue=args.enable_queue,
112
+ server_port=args.port,
113
+ share=args.share,
114
+ )
115
+
116
+
117
+ if __name__ == '__main__':
118
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ numpy==1.22.3
2
+ Pillow==9.0.1