hysts HF staff commited on
Commit
6487501
·
1 Parent(s): ef06509
Files changed (4) hide show
  1. .pre-commit-config.yaml +2 -12
  2. README.md +1 -1
  3. app.py +48 -79
  4. model.py +5 -5
.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: blue
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,31 +2,15 @@
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 = '# microsoft/StyleSwin'
13
- DESCRIPTION = '''This is an unofficial demo for [https://github.com/microsoft/StyleSwin](https://github.com/microsoft/StyleSwin).
14
 
15
- Expected execution time on Hugging Face Spaces: 3s (for 256x256 images), 7s (for 1024x1024 images)
16
  '''
17
- FOOTER = '<img id="visitor-badge" src="https://visitor-badge.glitch.me/badge?page_id=hysts.styleswin" alt="visitor badge" />'
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:
@@ -51,64 +35,49 @@ def get_sample_image_markdown(name: str) -> str:
51
  ![sample images]({url})'''
52
 
53
 
54
- def main():
55
- args = parse_args()
56
- model = Model(args.device)
57
-
58
- with gr.Blocks(theme=args.theme, css='style.css') as demo:
59
- gr.Markdown(TITLE)
60
- gr.Markdown(DESCRIPTION)
61
-
62
- with gr.Tabs():
63
- with gr.TabItem('App'):
64
- with gr.Row():
65
- with gr.Column():
66
- with gr.Group():
67
- model_name = gr.Dropdown(
68
- model.MODEL_NAMES,
69
- value=model.MODEL_NAMES[3],
70
- label='Model')
71
- seed = gr.Slider(0,
72
- np.iinfo(np.uint32).max,
73
- step=1,
74
- value=0,
75
- label='Seed')
76
- run_button = gr.Button('Run')
77
- with gr.Column():
78
- result = gr.Image(label='Result', elem_id='result')
79
-
80
- with gr.TabItem('Sample Images'):
81
- with gr.Row():
82
- model_name2 = gr.Dropdown([
83
- 'celeba-hq',
84
- 'ffhq',
85
- 'lsun-church',
86
- ],
87
- value='celeba-hq',
88
- label='Model')
89
- with gr.Row():
90
- text = get_sample_image_markdown(model_name2.value)
91
- sample_images = gr.Markdown(text)
92
-
93
- gr.Markdown(FOOTER)
94
-
95
- model_name.change(fn=model.set_model, inputs=model_name, outputs=None)
96
- run_button.click(fn=model.set_model_and_generate_image,
97
- inputs=[
98
- model_name,
99
- seed,
100
- ],
101
- outputs=result)
102
- model_name2.change(fn=get_sample_image_markdown,
103
- inputs=model_name2,
104
- outputs=sample_images)
105
-
106
- demo.launch(
107
- enable_queue=args.enable_queue,
108
- server_port=args.port,
109
- share=args.share,
110
- )
111
-
112
-
113
- if __name__ == '__main__':
114
- 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
+ DESCRIPTION = '''# StyleSwin
 
11
 
12
+ This is an unofficial demo for [https://github.com/microsoft/StyleSwin](https://github.com/microsoft/StyleSwin).
13
  '''
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
 
16
  def get_sample_image_url(name: str) -> str:
 
35
  ![sample images]({url})'''
36
 
37
 
38
+ model = Model()
39
+
40
+ with gr.Blocks(css='style.css') as demo:
41
+ gr.Markdown(DESCRIPTION)
42
+
43
+ with gr.Tabs():
44
+ with gr.TabItem('App'):
45
+ with gr.Row():
46
+ with gr.Column():
47
+ model_name = gr.Dropdown(model.MODEL_NAMES,
48
+ value=model.MODEL_NAMES[3],
49
+ label='Model')
50
+ seed = gr.Slider(0,
51
+ np.iinfo(np.uint32).max,
52
+ step=1,
53
+ value=0,
54
+ label='Seed')
55
+ run_button = gr.Button('Run')
56
+ with gr.Column():
57
+ result = gr.Image(label='Result', elem_id='result')
58
+
59
+ with gr.TabItem('Sample Images'):
60
+ with gr.Row():
61
+ model_name2 = gr.Dropdown([
62
+ 'celeba-hq',
63
+ 'ffhq',
64
+ 'lsun-church',
65
+ ],
66
+ value='celeba-hq',
67
+ label='Model')
68
+ with gr.Row():
69
+ text = get_sample_image_markdown(model_name2.value)
70
+ sample_images = gr.Markdown(text)
71
+
72
+ model_name.change(fn=model.set_model, inputs=model_name, outputs=None)
73
+ run_button.click(fn=model.set_model_and_generate_image,
74
+ inputs=[
75
+ model_name,
76
+ seed,
77
+ ],
78
+ outputs=result)
79
+ model_name2.change(fn=get_sample_image_markdown,
80
+ inputs=model_name2,
81
+ outputs=sample_images)
82
+
83
+ demo.queue().launch(show_api=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
model.py CHANGED
@@ -6,11 +6,10 @@ import sys
6
 
7
  import huggingface_hub
8
  import numpy as np
9
- import PIL.Image
10
  import torch
11
  import torch.nn as nn
12
 
13
- if os.environ.get('SYSTEM') == 'spaces':
14
  os.system("sed -i '14,21d' StyleSwin/op/fused_act.py")
15
  os.system("sed -i '12,19d' StyleSwin/op/upfirdn2d.py")
16
 
@@ -20,7 +19,7 @@ sys.path.insert(0, submodule_dir.as_posix())
20
 
21
  from models.generator import Generator
22
 
23
- HF_TOKEN = os.environ['HF_TOKEN']
24
 
25
 
26
  class Model:
@@ -32,8 +31,9 @@ class Model:
32
  'FFHQ_1024',
33
  ]
34
 
35
- def __init__(self, device: str | torch.device):
36
- self.device = torch.device(device)
 
37
  self._download_all_models()
38
  self.model_name = self.MODEL_NAMES[3]
39
  self.model = self._load_model(self.model_name)
 
6
 
7
  import huggingface_hub
8
  import numpy as np
 
9
  import torch
10
  import torch.nn as nn
11
 
12
+ if os.getenv('SYSTEM') == 'spaces':
13
  os.system("sed -i '14,21d' StyleSwin/op/fused_act.py")
14
  os.system("sed -i '12,19d' StyleSwin/op/upfirdn2d.py")
15
 
 
19
 
20
  from models.generator import Generator
21
 
22
+ HF_TOKEN = os.getenv('HF_TOKEN')
23
 
24
 
25
  class Model:
 
31
  'FFHQ_1024',
32
  ]
33
 
34
+ def __init__(self):
35
+ self.device = torch.device(
36
+ 'cuda:0' if torch.cuda.is_available() else 'cpu')
37
  self._download_all_models()
38
  self.model_name = self.MODEL_NAMES[3]
39
  self.model = self._load_model(self.model_name)