Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import argparse
|
6 |
+
import functools
|
7 |
+
import os
|
8 |
+
import pickle
|
9 |
+
import sys
|
10 |
+
|
11 |
import gradio as gr
|
12 |
+
import numpy as np
|
13 |
+
import torch
|
14 |
+
import torch.nn as nn
|
15 |
+
from huggingface_hub import hf_hub_download
|
16 |
+
|
17 |
+
sys.path.insert(0, 'StyleGAN-Human')
|
18 |
+
|
19 |
+
TITLE = 'StyleGAN-Human'
|
20 |
+
DESCRIPTION = '''This is an unofficial demo for https://github.com/stylegan-human/StyleGAN-Human.
|
21 |
+
Expected execution time on Hugging Face Spaces: 0.8s
|
22 |
+
Related App: [StyleGAN-Human (Interpolation)](https://huggingface.co/spaces/hysts/StyleGAN-Human-Interpolation)
|
23 |
+
'''
|
24 |
+
ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.stylegan-human" alt="visitor badge"/></center>'
|
25 |
+
|
26 |
+
TOKEN = os.environ['TOKEN']
|
27 |
+
|
28 |
+
|
29 |
+
def parse_args() -> argparse.Namespace:
|
30 |
+
parser = argparse.ArgumentParser()
|
31 |
+
parser.add_argument('--device', type=str, default='cpu')
|
32 |
+
parser.add_argument('--theme', type=str)
|
33 |
+
parser.add_argument('--live', action='store_true')
|
34 |
+
parser.add_argument('--share', action='store_true')
|
35 |
+
parser.add_argument('--port', type=int)
|
36 |
+
parser.add_argument('--disable-queue',
|
37 |
+
dest='enable_queue',
|
38 |
+
action='store_false')
|
39 |
+
parser.add_argument('--allow-flagging', type=str, default='never')
|
40 |
+
return parser.parse_args()
|
41 |
+
|
42 |
+
|
43 |
+
def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
|
44 |
+
return torch.from_numpy(np.random.RandomState(seed).randn(
|
45 |
+
1, z_dim)).to(device).float()
|
46 |
+
|
47 |
+
|
48 |
+
@torch.inference_mode()
|
49 |
+
def generate_image(seed: int, truncation_psi: float, model: nn.Module,
|
50 |
+
device: torch.device) -> np.ndarray:
|
51 |
+
seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
|
52 |
+
|
53 |
+
z = generate_z(model.z_dim, seed, device)
|
54 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
55 |
+
|
56 |
+
out = model(z, label, truncation_psi=truncation_psi, force_fp32=True)
|
57 |
+
out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
58 |
+
return out[0].cpu().numpy()
|
59 |
+
|
60 |
+
|
61 |
+
def load_model(file_name: str, device: torch.device) -> nn.Module:
|
62 |
+
path = hf_hub_download('hysts/StyleGAN-Human',
|
63 |
+
f'models/{file_name}',
|
64 |
+
use_auth_token=TOKEN)
|
65 |
+
with open(path, 'rb') as f:
|
66 |
+
model = pickle.load(f)['G_ema']
|
67 |
+
model.eval()
|
68 |
+
model.to(device)
|
69 |
+
with torch.inference_mode():
|
70 |
+
z = torch.zeros((1, model.z_dim)).to(device)
|
71 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
72 |
+
model(z, label, force_fp32=True)
|
73 |
+
return model
|
74 |
+
|
75 |
+
|
76 |
+
def main():
|
77 |
+
args = parse_args()
|
78 |
+
device = torch.device(args.device)
|
79 |
+
|
80 |
+
model = load_model('stylegan_human_v2_1024.pkl', device)
|
81 |
+
|
82 |
+
func = functools.partial(generate_image, model=model, device=device)
|
83 |
+
func = functools.update_wrapper(func, generate_image)
|
84 |
+
|
85 |
+
gr.Interface(
|
86 |
+
func,
|
87 |
+
[
|
88 |
+
gr.inputs.Number(default=0, label='Seed'),
|
89 |
+
gr.inputs.Slider(
|
90 |
+
0, 2, step=0.05, default=0.7, label='Truncation psi'),
|
91 |
+
],
|
92 |
+
gr.outputs.Image(type='numpy', label='Output'),
|
93 |
+
title=TITLE,
|
94 |
+
description=DESCRIPTION,
|
95 |
+
article=ARTICLE,
|
96 |
+
theme=args.theme,
|
97 |
+
allow_flagging=args.allow_flagging,
|
98 |
+
live=args.live,
|
99 |
+
).launch(
|
100 |
+
enable_queue=args.enable_queue,
|
101 |
+
server_port=args.port,
|
102 |
+
share=args.share,
|
103 |
+
)
|
104 |
+
|
105 |
+
|
106 |
+
if __name__ == '__main__':
|
107 |
+
main()
|