hysts HF staff commited on
Commit
873974f
1 Parent(s): 6abf639
Files changed (1) hide show
  1. app.py +39 -25
app.py CHANGED
@@ -26,23 +26,6 @@ from model.dualstylegan import DualStyleGAN
26
  from model.encoder.align_all_parallel import align_face
27
  from model.encoder.psp import pSp
28
 
29
- STYLE_IMAGE_PATHS = {
30
- 'cartoon':
31
- 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/cartoon_overview.jpg',
32
- 'caricature':
33
- 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/caricature_overview.jpg',
34
- 'anime':
35
- 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/anime_overview.jpg',
36
- 'arcane':
37
- 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/Reconstruction_arcane_overview.jpg',
38
- 'comic':
39
- 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/Reconstruction_comic_overview.jpg',
40
- 'pixar':
41
- 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/Reconstruction_pixar_overview.jpg',
42
- 'slamdunk':
43
- 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/Reconstruction_slamdunk_overview.jpg',
44
- }
45
-
46
  TOKEN = os.environ['TOKEN']
47
  MODEL_REPO = 'hysts/DualStyleGAN'
48
 
@@ -199,6 +182,25 @@ class App:
199
  return img_gen
200
 
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  def update_slider(choice: str) -> dict:
203
  max_vals = {
204
  'cartoon': 316,
@@ -212,9 +214,8 @@ def update_slider(choice: str) -> dict:
212
  return gr.Slider.update(maximum=max_vals[choice] + 1, value=26)
213
 
214
 
215
- def update_style_image(choice: str) -> dict:
216
- style_image_path = STYLE_IMAGE_PATHS[choice]
217
- text = f'<center><img src="{style_image_path}" alt="style image" width="800" height="400"></center>'
218
  return gr.Markdown.update(value=text)
219
 
220
 
@@ -222,13 +223,27 @@ def main():
222
  args = parse_args()
223
  app = App(device=torch.device(args.device))
224
 
225
- with gr.Blocks(theme=args.theme) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  gr.Markdown(
227
- '''<center><h1>Portrait Style Transfer with DualStyleGAN</h1></center>
228
 
229
  This is an unofficial demo app for https://github.com/williamyang1991/DualStyleGAN.
230
 
231
- <center><img src="https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/overview.jpg" alt="overview" width="800" height="400"></center>
232
 
233
  Related App: https://huggingface.co/spaces/hysts/DualStyleGAN
234
  ''')
@@ -278,8 +293,7 @@ Related App: https://huggingface.co/spaces/hysts/DualStyleGAN
278
  step=1,
279
  label='Style Image Index',
280
  interactive=True)
281
- style_image_path = STYLE_IMAGE_PATHS['cartoon']
282
- text = f'<center><img src="{style_image_path}" alt="style image" width="800" height="400"></center>'
283
  style_image = gr.Markdown(value=text)
284
 
285
  with gr.Box():
 
26
  from model.encoder.align_all_parallel import align_face
27
  from model.encoder.psp import pSp
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  TOKEN = os.environ['TOKEN']
30
  MODEL_REPO = 'hysts/DualStyleGAN'
31
 
 
182
  return img_gen
183
 
184
 
185
+ def get_style_image_url(style_name: str) -> str:
186
+ base_url = 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images'
187
+ filenames = {
188
+ 'cartoon': 'cartoon_overview.jpg',
189
+ 'caricature': 'caricature_overview.jpg',
190
+ 'anime': 'anime_overview.jpg',
191
+ 'arcane': 'Reconstruction_arcane_overview.jpg',
192
+ 'comic': 'Reconstruction_comic_overview.jpg',
193
+ 'pixar': 'Reconstruction_pixar_overview.jpg',
194
+ 'slamdunk': 'Reconstruction_slamdunk_overview.jpg',
195
+ }
196
+ return f'{base_url}/{filenames[style_name]}'
197
+
198
+
199
+ def get_style_image_markdown_text(style_name: str) -> str:
200
+ url = get_style_image_url(style_name)
201
+ return f'<center><img id="style-image" src="{url}" alt="style image"></center>'
202
+
203
+
204
  def update_slider(choice: str) -> dict:
205
  max_vals = {
206
  'cartoon': 316,
 
214
  return gr.Slider.update(maximum=max_vals[choice] + 1, value=26)
215
 
216
 
217
+ def update_style_image(style_name: str) -> dict:
218
+ text = get_style_image_markdown_text(style_name)
 
219
  return gr.Markdown.update(value=text)
220
 
221
 
 
223
  args = parse_args()
224
  app = App(device=torch.device(args.device))
225
 
226
+ css = '''
227
+ h1#title {
228
+ text-align: center;
229
+ }
230
+ img#overview {
231
+ max-width: 800px;
232
+ max-height: 600px;
233
+ }
234
+ img#style-image {
235
+ max-width: 1000px;
236
+ max-height: 600px;
237
+ }
238
+ '''
239
+
240
+ with gr.Blocks(theme=args.theme, css=css) as demo:
241
  gr.Markdown(
242
+ '''<h1 id="title">Portrait Style Transfer with DualStyleGAN</h1>
243
 
244
  This is an unofficial demo app for https://github.com/williamyang1991/DualStyleGAN.
245
 
246
+ <center><img id="overview" src="https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/overview.jpg" alt="overview"></center>
247
 
248
  Related App: https://huggingface.co/spaces/hysts/DualStyleGAN
249
  ''')
 
293
  step=1,
294
  label='Style Image Index',
295
  interactive=True)
296
+ text = get_style_image_markdown_text('cartoon')
 
297
  style_image = gr.Markdown(value=text)
298
 
299
  with gr.Box():