i72sijia commited on
Commit
c825910
1 Parent(s): 8aad34c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -59
app.py CHANGED
@@ -1,79 +1,32 @@
1
  import gradio as gr
2
- import os
3
  import numpy as np
4
- import torch
5
- import pickle
6
- import types
7
 
8
- from huggingface_hub import hf_hub_url, cached_download
9
 
10
- TOKEN = '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'
11
-
12
- with open(cached_download(hf_hub_url('i72sijia/ganbanales', 'ganbanales.pkl'), use_auth_token=TOKEN), 'rb') as f:
13
- G = pickle.load(f)['G_ema']# torch.nn.Module
14
-
15
- device = torch.device("cpu")
16
-
17
- if torch.cuda.is_available():
18
- device = torch.device("cuda")
19
- G = G.to(device)
20
- else:
21
- _old_forward = G.forward
22
-
23
- def _new_forward(self, *args, **kwargs):
24
- kwargs["force_fp32"] = True
25
- return _old_forward(*args, **kwargs)
26
-
27
- G.forward = types.MethodType(_new_forward, G)
28
-
29
- _old_synthesis_forward = G.synthesis.forward
30
 
31
- def _new_synthesis_forward(self, *args, **kwargs):
32
- kwargs["force_fp32"] = True
33
- return _old_synthesis_forward(*args, **kwargs)
34
 
35
- G.synthesis.forward = types.MethodType(_new_synthesis_forward, G.synthesis)
36
-
37
- ####################################################################
38
- # Image generation
39
 
40
- def generate(num_images, interpolate):
41
- if interpolate:
42
- z1 = torch.randn([1, G.z_dim])# latent codes
43
- z2 = torch.randn([1, G.z_dim])# latent codes
44
- zs = torch.cat([z1 + (z2 - z1) * i / (num_images-1) for i in range(num_images)], 0)
45
- else:
46
- zs = torch.randn([num_images, G.z_dim])# latent codes
47
- with torch.no_grad():
48
- zs = zs.to(device)
49
- img = G(zs, None, force_fp32=True, noise_mode='const')
50
- img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
51
- return img.cpu().numpy()
52
-
53
- ####################################################################
54
- # Graphical User Interface
55
- def infer(num_images, interpolate):
56
- img = generate(round(num_images), interpolate)
57
- imgs = list(img)
58
- return imgs
59
-
60
  demo = gr.Blocks()
61
 
62
  with demo:
63
- gr.Markdown(
64
- """
65
- # EmojiGAN
66
- Generate Emojis with AI
67
- """)
68
  images_num = gr.inputs.Slider(default=1, label="Num Images", minimum=1, maximum=16, step=1)
69
- interpolate = gr.inputs.Checkbox(default=False, label="Interpolate")
70
 
71
  submit = gr.Button("Generate")
72
 
73
  out = gr.Gallery()
74
 
75
  submit.click(fn=initiate,
76
- inputs=[images_num, interpolate],
 
77
  outputs=out)
78
 
79
  demo.launch(enable_queue =True)
 
1
  import gradio as gr
 
2
  import numpy as np
 
 
 
3
 
4
+ def initiate(images_num):
5
 
6
+ zeros = np.zeros([256,256,3], dtype=np.uint8)
7
+ zeros.fill(255)
8
+
9
+ img_array = []
10
+
11
+ for i in range(images_num):
12
+ img_array.append(zeros)
13
+
 
 
 
 
 
 
 
 
 
 
 
 
14
 
 
 
 
15
 
 
 
 
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  demo = gr.Blocks()
18
 
19
  with demo:
 
 
 
 
 
20
  images_num = gr.inputs.Slider(default=1, label="Num Images", minimum=1, maximum=16, step=1)
21
+ #interpolate = gr.inputs.Checkbox(default=False, label="Interpolate")
22
 
23
  submit = gr.Button("Generate")
24
 
25
  out = gr.Gallery()
26
 
27
  submit.click(fn=initiate,
28
+ #inputs=[images_num, interpolate],
29
+ inputs=images_num
30
  outputs=out)
31
 
32
  demo.launch(enable_queue =True)