rahulved commited on
Commit
d351f5c
·
1 Parent(s): c49f38d

Fixed app.py

Browse files
Files changed (2) hide show
  1. app.py +29 -8
  2. stylegan3-r-afhqv2-512x512.pkl +3 -0
app.py CHANGED
@@ -10,26 +10,47 @@ import pickle
10
  import torchvision.transforms as transforms
11
  from PIL import Image
12
 
13
- network_pkl = 'network-snapshot-003200.pkl'
14
- with open(network_pkl, 'rb') as f:
15
- G = pickle.load(f)['G_ema'].cpu() # torch.nn.Module
 
16
 
17
- def gen_image(text_input):
18
- z = torch.randn([1, G.z_dim]).cpu() # latent codes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  c = None # class labels (not used in this example)
20
  img = G(z, c)
21
  img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
22
- image=PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')
23
  transform = transforms.Resize((image.height * 2, image.width * 2), interpolation=transforms.InterpolationMode.BILINEAR)
24
  upscaled_image = transform(image)
25
  return upscaled_image
26
 
27
  demo = gr.Interface(
28
  fn=gen_image,
29
- inputs=gr.Textbox(lines=2, placeholder="Enter your text here..."),
30
  outputs=gr.Image(type="pil"),
31
  title="Text to Image Generator",
32
  description="Enter text to generate an image using a custom PyTorch model."
33
  )
34
 
35
- demo.launch()
 
 
10
  import torchvision.transforms as transforms
11
  from PIL import Image
12
 
13
+ network_pkl_a = 'stylegan3-r-afhqv2-512x512.pkl'
14
+ network_pkl_d = 'network-snapshot.pkl'
15
+ with open(network_pkl_d, 'rb') as f:
16
+ G = pickle.load(f)['G_ema'] # torch.nn.Module
17
 
18
+ with open(network_pkl_a, 'rb') as f:
19
+ G_a = pickle.load(f)['G_ema'] # torch.nn.Module
20
+
21
+ def gen_image(text):
22
+ if text=='show me':
23
+ return gen_image_d()
24
+ else:
25
+ return gen_image_a()
26
+
27
+ def gen_image_a():
28
+ z = torch.randn([1, G_a.z_dim]) # latent codes
29
+ c = None # class labels (not used in this example)
30
+ img = G_a(z, c)
31
+ img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
32
+ image=PIL.Image.fromarray(img[0].numpy(), 'RGB')
33
+ transform = transforms.Resize((image.height * 2, image.width * 2), interpolation=transforms.InterpolationMode.BILINEAR)
34
+ upscaled_image = transform(image)
35
+ return upscaled_image
36
+
37
+ def gen_image_d():
38
+ z = torch.randn([1, G.z_dim]) # latent codes
39
  c = None # class labels (not used in this example)
40
  img = G(z, c)
41
  img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
42
+ image=PIL.Image.fromarray(img[0].numpy(), 'RGB')
43
  transform = transforms.Resize((image.height * 2, image.width * 2), interpolation=transforms.InterpolationMode.BILINEAR)
44
  upscaled_image = transform(image)
45
  return upscaled_image
46
 
47
  demo = gr.Interface(
48
  fn=gen_image,
49
+ inputs=gr.Textbox(lines=2, placeholder="Prompt here..."),
50
  outputs=gr.Image(type="pil"),
51
  title="Text to Image Generator",
52
  description="Enter text to generate an image using a custom PyTorch model."
53
  )
54
 
55
+ if __name__ == "__main__":
56
+ demo.launch()
stylegan3-r-afhqv2-512x512.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:77b9cd6bbaf0a2dfeb4372c9f86b7280a98f293aa1b9d35465864895596628c8
3
+ size 249525556