PKUWilliamYang commited on
Commit
1481a58
β€’
1 Parent(s): aa02444

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import pathlib
5
+ import torch
6
+ import gradio as gr
7
+
8
+ from webUI.app_task import *
9
+ from webUI.styleganex_model import Model
10
+
11
+ def parse_args() -> argparse.Namespace:
12
+ parser = argparse.ArgumentParser()
13
+ parser.add_argument('--device', type=str, default='cpu')
14
+ parser.add_argument('--theme', type=str)
15
+ parser.add_argument('--share', action='store_true')
16
+ parser.add_argument('--port', type=int)
17
+ parser.add_argument('--disable-queue',
18
+ dest='enable_queue',
19
+ action='store_false')
20
+ return parser.parse_args()
21
+
22
+ DESCRIPTION = '''
23
+ <div align=center>
24
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
25
+ Face Manipulation with <a href="https://github.com/williamyang1991/StyleGANEX">StyleGANEX</a>
26
+ </h1>
27
+ <p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.
28
+ <a href="https://huggingface.co/spaces/PKUWilliamYang/StyleGANEX?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>
29
+ <p/>
30
+ <img style="margin-top: 0em" src="https://raw.githubusercontent.com/williamyang1991/tmpfile/master/imgs/example.jpg" alt="example">
31
+ </div>
32
+ '''
33
+ ARTICLE = r"""
34
+ If StyleGANEX is helpful, please help to ⭐ the <a href='https://github.com/williamyang1991/StyleGANEX' target='_blank'>Github Repo</a>. Thanks!
35
+ [![GitHub Stars](https://img.shields.io/github/stars/williamyang1991/StyleGANEX?style=social)](https://github.com/williamyang1991/StyleGANEX)
36
+ ---
37
+ πŸ“ **Citation**
38
+ If our work is useful for your research, please consider citing:
39
+ ```bibtex
40
+ @article{yang2023styleganex,
41
+ title = {StyleGANEX: StyleGAN-Based Manipulation Beyond Cropped Aligned Faces},
42
+ author = {Yang, Shuai and Jiang, Liming and Liu, Ziwei and and Loy, Chen Change},
43
+ journal = {arXiv preprint arXiv:2303.06146},
44
+ year={2023},
45
+ }
46
+ ```
47
+ πŸ“‹ **License**
48
+ This project is licensed under <a rel="license" href="https://github.com/williamyang1991/VToonify/blob/main/LICENSE.md">S-Lab License 1.0</a>.
49
+ Redistribution and use for non-commercial purposes should follow this license.
50
+
51
+ πŸ“§ **Contact**
52
+ If you have any questions, please feel free to reach me out at <b>williamyang@pku.edu.cn</b>.
53
+ """
54
+
55
+ FOOTER = '<div align=center><img id="visitor-badge" alt="visitor badge" src="https://visitor-badge.laobi.icu/badge?page_id=williamyang1991/styleganex" /></div>'
56
+
57
+
58
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/williamyang1991/StyleGANEX/main/data/234_sketch.jpg',
59
+ '234_sketch.jpg')
60
+ torch.hub.download_url_to_file('https://github.com/williamyang1991/StyleGANEX/raw/main/output/ILip77SbmOE_inversion.pt',
61
+ 'ILip77SbmOE_inversion.pt')
62
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/williamyang1991/StyleGANEX/main/data/ILip77SbmOE.png',
63
+ 'ILip77SbmOE.png')
64
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/williamyang1991/StyleGANEX/main/data/ILip77SbmOE_mask.png',
65
+ 'ILip77SbmOE_mask.png')
66
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/williamyang1991/StyleGANEX/main/data/pexels-daniel-xavier-1239291.jpg',
67
+ 'pexels-daniel-xavier-1239291.jpg')
68
+ torch.hub.download_url_to_file('https://github.com/williamyang1991/StyleGANEX/raw/main/data/529_2.mp4',
69
+ '529_2.mp4')
70
+ torch.hub.download_url_to_file('https://github.com/williamyang1991/StyleGANEX/raw/main/data/684.mp4',
71
+ '684.mp4')
72
+ torch.hub.download_url_to_file('https://github.com/williamyang1991/StyleGANEX/raw/main/data/pexels-anthony-shkraba-production-8136210.mp4',
73
+ 'pexels-anthony-shkraba-production-8136210.mp4')
74
+
75
+
76
+ def main():
77
+ args = parse_args()
78
+ args.device = 'cuda' if torch.cuda.is_available() else 'cpu'
79
+ print('*** Now using %s.'%(args.device))
80
+ model = Model(device=args.device)
81
+
82
+ with gr.Blocks(css='style.css') as demo:
83
+ gr.Markdown(DESCRIPTION)
84
+ with gr.Tabs():
85
+ with gr.TabItem('Inversion for Editing'):
86
+ create_demo_inversion(model.process_inversion, allow_optimization=False)
87
+ with gr.TabItem('Image Face Toonify'):
88
+ create_demo_toonify(model.process_toonify)
89
+ with gr.TabItem('Video Face Toonify'):
90
+ create_demo_vtoonify(model.process_vtoonify, max_frame_num=4)
91
+ with gr.TabItem('Image Face Editing'):
92
+ create_demo_editing(model.process_editing)
93
+ with gr.TabItem('Video Face Editing'):
94
+ create_demo_vediting(model.process_vediting, max_frame_num=4)
95
+ with gr.TabItem('Sketch2Face'):
96
+ create_demo_s2f(model.process_s2f)
97
+ with gr.TabItem('Mask2Face'):
98
+ create_demo_m2f(model.process_m2f)
99
+ with gr.TabItem('SR'):
100
+ create_demo_sr(model.process_sr)
101
+ gr.Markdown(ARTICLE)
102
+ gr.Markdown(FOOTER)
103
+
104
+ demo.launch(
105
+ enable_queue=args.enable_queue,
106
+ server_port=args.port,
107
+ share=args.share,
108
+ )
109
+
110
+ if __name__ == '__main__':
111
+ main()
112
+