Spaces:
Runtime error
Runtime error
Commit
·
181927b
1
Parent(s):
4e419a9
Delete app.py
Browse files
app.py
DELETED
@@ -1,87 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
import re
|
4 |
-
from typing import List, Optional, Tuple, Union
|
5 |
-
import random
|
6 |
-
|
7 |
-
sys.path.append('stylegan3-fun') # change this to the path where dnnlib is located
|
8 |
-
|
9 |
-
import numpy as np
|
10 |
-
import PIL.Image
|
11 |
-
import torch
|
12 |
-
import streamlit as st
|
13 |
-
import dnnlib
|
14 |
-
import legacy
|
15 |
-
|
16 |
-
|
17 |
-
def parse_range(s: Union[str, List]) -> List[int]:
|
18 |
-
'''Parse a comma separated list of numbers or ranges and return a list of ints.
|
19 |
-
|
20 |
-
Example: '1,2,5-10' returns [1, 2, 5, 6, 7]
|
21 |
-
'''
|
22 |
-
if isinstance(s, list): return s
|
23 |
-
ranges = []
|
24 |
-
range_re = re.compile(r'^(\d+)-(\d+)$')
|
25 |
-
for p in s.split(','):
|
26 |
-
m = range_re.match(p)
|
27 |
-
if m:
|
28 |
-
ranges.extend(range(int(m.group(1)), int(m.group(2))+1))
|
29 |
-
else:
|
30 |
-
ranges.append(int(p))
|
31 |
-
return ranges
|
32 |
-
|
33 |
-
def make_transform(translate: Tuple[float,float], angle: float):
|
34 |
-
m = np.eye(3)
|
35 |
-
s = np.sin(angle/360.0*np.pi*2)
|
36 |
-
c = np.cos(angle/360.0*np.pi*2)
|
37 |
-
m[0][0] = c
|
38 |
-
m[0][1] = s
|
39 |
-
m[0][2] = translate[0]
|
40 |
-
m[1][0] = -s
|
41 |
-
m[1][1] = c
|
42 |
-
m[1][2] = translate[1]
|
43 |
-
return m
|
44 |
-
|
45 |
-
def generate_image(network_pkl: str, seed: int, truncation_psi: float, noise_mode: str, translate: Tuple[float,float], rotate: float, class_idx: Optional[int]):
|
46 |
-
print('Loading networks from "%s"...' % network_pkl)
|
47 |
-
device = torch.device('cuda')
|
48 |
-
with open(network_pkl, 'rb') as f:
|
49 |
-
G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
|
50 |
-
|
51 |
-
# Labels.
|
52 |
-
label = torch.zeros([1, G.c_dim], device=device)
|
53 |
-
if G.c_dim != 0:
|
54 |
-
if class_idx is None:
|
55 |
-
raise Exception('Must specify class label when using a conditional network')
|
56 |
-
label[:, class_idx] = 1
|
57 |
-
|
58 |
-
z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
|
59 |
-
|
60 |
-
if hasattr(G.synthesis, 'input'):
|
61 |
-
m = make_transform(translate, rotate)
|
62 |
-
m = np.linalg.inv(m)
|
63 |
-
G.synthesis.input.transform.copy_(torch.from_numpy(m))
|
64 |
-
|
65 |
-
img = G(z, label, truncation_psi=truncation_psi, noise_mode=noise_mode)
|
66 |
-
img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
67 |
-
img = PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')
|
68 |
-
return img
|
69 |
-
|
70 |
-
def main():
|
71 |
-
st.title('Kpop Face Generator')
|
72 |
-
|
73 |
-
st.write('Press the button below to generate a new image:')
|
74 |
-
if st.button('Generate'):
|
75 |
-
network_pkl = 'kpopGG.pkl'
|
76 |
-
seed = random.randint(0, 99999)
|
77 |
-
truncation_psi = 0.45
|
78 |
-
noise_mode = 'const'
|
79 |
-
translate = (0.0, 0.0)
|
80 |
-
rotate = 0.0
|
81 |
-
class_idx = None
|
82 |
-
|
83 |
-
image = generate_image(network_pkl, seed, truncation_psi, noise_mode, translate, rotate, class_idx)
|
84 |
-
st.image(image)
|
85 |
-
|
86 |
-
if __name__ == "__main__":
|
87 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|