hysts HF staff commited on
Commit
fefcd29
·
1 Parent(s): b13b851
Files changed (4) hide show
  1. .pre-commit-config.yaml +2 -12
  2. README.md +1 -1
  3. app.py +76 -105
  4. model.py +4 -3
.pre-commit-config.yaml CHANGED
@@ -21,11 +21,11 @@ repos:
21
  - id: docformatter
22
  args: ['--in-place']
23
  - repo: https://github.com/pycqa/isort
24
- rev: 5.10.1
25
  hooks:
26
  - id: isort
27
  - repo: https://github.com/pre-commit/mirrors-mypy
28
- rev: v0.812
29
  hooks:
30
  - id: mypy
31
  args: ['--ignore-missing-imports']
@@ -34,13 +34,3 @@ repos:
34
  hooks:
35
  - id: yapf
36
  args: ['--parallel', '--in-place']
37
- - repo: https://github.com/kynan/nbstripout
38
- rev: 0.5.0
39
- hooks:
40
- - id: nbstripout
41
- args: ['--extra-keys', 'metadata.interpreter metadata.kernelspec cell.metadata.pycharm']
42
- - repo: https://github.com/nbQA-dev/nbQA
43
- rev: 1.3.1
44
- hooks:
45
- - id: nbqa-isort
46
- - id: nbqa-yapf
 
21
  - id: docformatter
22
  args: ['--in-place']
23
  - repo: https://github.com/pycqa/isort
24
+ rev: 5.12.0
25
  hooks:
26
  - id: isort
27
  - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v0.991
29
  hooks:
30
  - id: mypy
31
  args: ['--ignore-missing-imports']
 
34
  hooks:
35
  - id: yapf
36
  args: ['--parallel', '--in-place']
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🐨
4
  colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,31 +2,16 @@
2
 
3
  from __future__ import annotations
4
 
5
- import argparse
6
-
7
  import gradio as gr
8
  import numpy as np
9
 
10
  from model import Model
11
 
12
- TITLE = '# NVlabs/stylegan3'
13
- DESCRIPTION = '''This is an unofficial demo for [https://github.com/NVlabs/stylegan3](https://github.com/NVlabs/stylegan3).
14
 
15
- Expected execution time on Hugging Face Spaces: 50s
16
  '''
17
- FOOTER = '<img id="visitor-badge" alt="visitor badge" src="https://visitor-badge.glitch.me/badge?page_id=hysts.stylegan3" />'
18
-
19
-
20
- def parse_args() -> argparse.Namespace:
21
- parser = argparse.ArgumentParser()
22
- parser.add_argument('--device', type=str, default='cpu')
23
- parser.add_argument('--theme', type=str)
24
- parser.add_argument('--share', action='store_true')
25
- parser.add_argument('--port', type=int)
26
- parser.add_argument('--disable-queue',
27
- dest='enable_queue',
28
- action='store_false')
29
- return parser.parse_args()
30
 
31
 
32
  def get_sample_image_url(name: str) -> str:
@@ -45,90 +30,76 @@ def get_sample_image_markdown(name: str) -> str:
45
  ![sample images]({url})'''
46
 
47
 
48
- def main():
49
- args = parse_args()
50
- model = Model(args.device)
51
-
52
- with gr.Blocks(theme=args.theme, css='style.css') as demo:
53
- gr.Markdown(TITLE)
54
- gr.Markdown(DESCRIPTION)
55
-
56
- with gr.Tabs():
57
- with gr.TabItem('App'):
58
- with gr.Row():
59
- with gr.Column():
60
- with gr.Group():
61
- model_name = gr.Dropdown(list(
62
- model.MODEL_NAME_DICT.keys()),
63
- value='FFHQ-1024-R',
64
- label='Model')
65
- seed = gr.Slider(0,
66
- np.iinfo(np.uint32).max,
67
- step=1,
68
- value=0,
69
- label='Seed')
70
- psi = gr.Slider(0,
71
- 2,
72
- step=0.05,
73
- value=0.7,
74
- label='Truncation psi')
75
- tx = gr.Slider(-1,
76
- 1,
77
- step=0.05,
78
- value=0,
79
- label='Translate X')
80
- ty = gr.Slider(-1,
81
- 1,
82
- step=0.05,
83
- value=0,
84
- label='Translate Y')
85
- angle = gr.Slider(-180,
86
- 180,
87
- step=5,
88
- value=0,
89
- label='Angle')
90
- run_button = gr.Button('Run')
91
- with gr.Column():
92
- result = gr.Image(label='Result', elem_id='result')
93
-
94
- with gr.TabItem('Sample Images'):
95
- with gr.Row():
96
- model_name2 = gr.Dropdown([
97
- 'afhqv2',
98
- 'ffhq',
99
- 'ffhq-u',
100
- 'metfaces',
101
- 'metfaces-u',
102
- ],
103
- value='afhqv2',
104
- label='Model')
105
- with gr.Row():
106
- text = get_sample_image_markdown(model_name2.value)
107
- sample_images = gr.Markdown(text)
108
-
109
- gr.Markdown(FOOTER)
110
-
111
- model_name.change(fn=model.set_model, inputs=model_name, outputs=None)
112
- run_button.click(fn=model.set_model_and_generate_image,
113
- inputs=[
114
- model_name,
115
- seed,
116
- psi,
117
- tx,
118
- ty,
119
- angle,
120
- ],
121
- outputs=result)
122
- model_name2.change(fn=get_sample_image_markdown,
123
- inputs=model_name2,
124
- outputs=sample_images)
125
-
126
- demo.launch(
127
- enable_queue=args.enable_queue,
128
- server_port=args.port,
129
- share=args.share,
130
- )
131
-
132
-
133
- if __name__ == '__main__':
134
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
 
5
  import gradio as gr
6
  import numpy as np
7
 
8
  from model import Model
9
 
10
+ TITLE = ''
11
+ DESCRIPTION = '''# StyleGAN3
12
 
13
+ This is an unofficial demo for [https://github.com/NVlabs/stylegan3](https://github.com/NVlabs/stylegan3).
14
  '''
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
 
17
  def get_sample_image_url(name: str) -> str:
 
30
  ![sample images]({url})'''
31
 
32
 
33
+ model = Model()
34
+
35
+ with gr.Blocks(css='style.css') as demo:
36
+ gr.Markdown(DESCRIPTION)
37
+
38
+ with gr.Tabs():
39
+ with gr.TabItem('App'):
40
+ with gr.Row():
41
+ with gr.Column():
42
+ model_name = gr.Dropdown(list(
43
+ model.MODEL_NAME_DICT.keys()),
44
+ value='FFHQ-1024-R',
45
+ label='Model')
46
+ seed = gr.Slider(0,
47
+ np.iinfo(np.uint32).max,
48
+ step=1,
49
+ value=0,
50
+ label='Seed')
51
+ psi = gr.Slider(0,
52
+ 2,
53
+ step=0.05,
54
+ value=0.7,
55
+ label='Truncation psi')
56
+ tx = gr.Slider(-1,
57
+ 1,
58
+ step=0.05,
59
+ value=0,
60
+ label='Translate X')
61
+ ty = gr.Slider(-1,
62
+ 1,
63
+ step=0.05,
64
+ value=0,
65
+ label='Translate Y')
66
+ angle = gr.Slider(-180,
67
+ 180,
68
+ step=5,
69
+ value=0,
70
+ label='Angle')
71
+ run_button = gr.Button('Run')
72
+ with gr.Column():
73
+ result = gr.Image(label='Result', elem_id='result')
74
+
75
+ with gr.TabItem('Sample Images'):
76
+ with gr.Row():
77
+ model_name2 = gr.Dropdown([
78
+ 'afhqv2',
79
+ 'ffhq',
80
+ 'ffhq-u',
81
+ 'metfaces',
82
+ 'metfaces-u',
83
+ ],
84
+ value='afhqv2',
85
+ label='Model')
86
+ with gr.Row():
87
+ text = get_sample_image_markdown(model_name2.value)
88
+ sample_images = gr.Markdown(text)
89
+
90
+ model_name.change(fn=model.set_model, inputs=model_name, outputs=None)
91
+ run_button.click(fn=model.set_model_and_generate_image,
92
+ inputs=[
93
+ model_name,
94
+ seed,
95
+ psi,
96
+ tx,
97
+ ty,
98
+ angle,
99
+ ],
100
+ outputs=result)
101
+ model_name2.change(fn=get_sample_image_markdown,
102
+ inputs=model_name2,
103
+ outputs=sample_images)
104
+
105
+ demo.queue().launch(show_api=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
model.py CHANGED
@@ -14,7 +14,7 @@ current_dir = pathlib.Path(__file__).parent
14
  submodule_dir = current_dir / 'stylegan3'
15
  sys.path.insert(0, submodule_dir.as_posix())
16
 
17
- HF_TOKEN = os.environ['HF_TOKEN']
18
 
19
 
20
  class Model:
@@ -33,8 +33,9 @@ class Model:
33
  'MetFaces-U-1024-T': 'stylegan3-t-metfacesu-1024x1024.pkl',
34
  }
35
 
36
- def __init__(self, device: str | torch.device):
37
- self.device = torch.device(device)
 
38
  self._download_all_models()
39
  self.model_name = 'FFHQ-1024-R'
40
  self.model = self._load_model(self.model_name)
 
14
  submodule_dir = current_dir / 'stylegan3'
15
  sys.path.insert(0, submodule_dir.as_posix())
16
 
17
+ HF_TOKEN = os.getenv('HF_TOKEN')
18
 
19
 
20
  class Model:
 
33
  'MetFaces-U-1024-T': 'stylegan3-t-metfacesu-1024x1024.pkl',
34
  }
35
 
36
+ def __init__(self):
37
+ self.device = torch.device(
38
+ 'cuda:0' if torch.cuda.is_available() else 'cpu')
39
  self._download_all_models()
40
  self.model_name = 'FFHQ-1024-R'
41
  self.model = self._load_model(self.model_name)