Spaces:
Runtime error
Runtime error
CorvaeOboro
commited on
Commit
β’
7bd3343
1
Parent(s):
fb2aafe
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, 'gen_ability_icon')
|
18 |
+
|
19 |
+
TITLE = 'gen_ability_icon'
|
20 |
+
DESCRIPTION = '''creates circular magic ability icons from stylegan2ada model trained on synthetic dataset .
|
21 |
+
more information here : https://github.com/CorvaeOboro/gen_ability_icon.
|
22 |
+
'''
|
23 |
+
|
24 |
+
|
25 |
+
def parse_args() -> argparse.Namespace:
|
26 |
+
parser = argparse.ArgumentParser()
|
27 |
+
parser.add_argument('--device', type=str, default='cpu')
|
28 |
+
parser.add_argument('--theme', type=str)
|
29 |
+
parser.add_argument('--live', action='store_true')
|
30 |
+
parser.add_argument('--share', action='store_true')
|
31 |
+
parser.add_argument('--port', type=int)
|
32 |
+
parser.add_argument('--disable-queue',
|
33 |
+
dest='enable_queue',
|
34 |
+
action='store_false')
|
35 |
+
parser.add_argument('--allow-flagging', type=str, default='never')
|
36 |
+
return parser.parse_args()
|
37 |
+
|
38 |
+
|
39 |
+
def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
|
40 |
+
return torch.from_numpy(np.random.RandomState(seed).randn(
|
41 |
+
1, z_dim)).to(device).float()
|
42 |
+
|
43 |
+
|
44 |
+
@torch.inference_mode()
|
45 |
+
def generate_image(seed: int, truncation_psi: float, model: nn.Module,
|
46 |
+
device: torch.device) -> np.ndarray:
|
47 |
+
seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
|
48 |
+
|
49 |
+
z = generate_z(model.z_dim, seed, device)
|
50 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
51 |
+
|
52 |
+
out = model(z, label, truncation_psi=truncation_psi, force_fp32=True)
|
53 |
+
out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
54 |
+
return out[0].cpu().numpy()
|
55 |
+
|
56 |
+
|
57 |
+
def load_model(file_name: str, device: torch.device) -> nn.Module:
|
58 |
+
path = hf_hub_download(f'CorvaeOboro/gen_ability_icon',
|
59 |
+
f'{file_name}',
|
60 |
+
use_auth_token=TOKEN)
|
61 |
+
with open(path, 'rb') as f:
|
62 |
+
model = pickle.load(f)['G_ema']
|
63 |
+
model.eval()
|
64 |
+
model.to(device)
|
65 |
+
with torch.inference_mode():
|
66 |
+
z = torch.zeros((1, model.z_dim)).to(device)
|
67 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
68 |
+
model(z, label, force_fp32=True)
|
69 |
+
return model
|
70 |
+
|
71 |
+
|
72 |
+
def main():
|
73 |
+
args = parse_args()
|
74 |
+
device = torch.device(args.device)
|
75 |
+
|
76 |
+
model = load_model('gen_ability_icon_stylegan2ada_20220801.pkl', device)
|
77 |
+
|
78 |
+
func = functools.partial(generate_image, model=model, device=device)
|
79 |
+
func = functools.update_wrapper(func, generate_image)
|
80 |
+
|
81 |
+
gr.Interface(
|
82 |
+
func,
|
83 |
+
[
|
84 |
+
gr.inputs.Number(default=0, label='Seed'),
|
85 |
+
gr.inputs.Slider(
|
86 |
+
0, 2, step=0.05, default=0.7, label='Truncation psi'),
|
87 |
+
],
|
88 |
+
gr.outputs.Image(type='numpy', label='Output'),
|
89 |
+
title=TITLE,
|
90 |
+
description=DESCRIPTION,
|
91 |
+
theme=args.theme,
|
92 |
+
allow_flagging=args.allow_flagging,
|
93 |
+
live=args.live,
|
94 |
+
).launch(
|
95 |
+
enable_queue=args.enable_queue,
|
96 |
+
server_port=args.port,
|
97 |
+
share=args.share,
|
98 |
+
)
|
99 |
+
|
100 |
+
|
101 |
+
if __name__ == '__main__':
|
102 |
+
main()
|