Ahsen Khaliq commited on
Commit
8828289
β€’
1 Parent(s): 102d8e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -31,11 +31,9 @@ import nvidia_smi
31
  nvidia_smi.nvmlInit()
32
  handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
33
  # card id 0 hardcoded here, there is also a call to get all available card ids, so we could iterate
34
-
35
  torch.hub.download_url_to_file('https://images.pexels.com/photos/158028/bellingrath-gardens-alabama-landscape-scenic-158028.jpeg', 'garden.jpeg')
36
  torch.hub.download_url_to_file('https://images.pexels.com/photos/68767/divers-underwater-ocean-swim-68767.jpeg', 'coralreef.jpeg')
37
  torch.hub.download_url_to_file('https://images.pexels.com/photos/803975/pexels-photo-803975.jpeg', 'cabin.jpeg')
38
-
39
  def sinc(x):
40
  return torch.where(x != 0, torch.sin(math.pi * x) / (math.pi * x), x.new_ones([]))
41
  def lanczos(x, a):
@@ -200,6 +198,7 @@ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
200
  print('Using device:', device)
201
  model = load_vqgan_model(args.vqgan_config, args.vqgan_checkpoint).to(device)
202
  perceptor = clip.load(args.clip_model, jit=False)[0].eval().requires_grad_(False).to(device)
 
203
  def inference(text, seed, step_size, max_iterations, width, height, init_image, init_weight):
204
  size=[width, height]
205
  texts = text
@@ -319,7 +318,9 @@ def inference(text, seed, step_size, max_iterations, width, height, init_image,
319
  result.append(prompt(iii))
320
  img = np.array(out.mul(255).clamp(0, 255)[0].cpu().detach().numpy().astype(np.uint8))[:,:,:]
321
  img = np.transpose(img, (1, 2, 0))
322
- #imageio.imwrite('./steps/' + str(i) + '.png', np.array(img))
 
 
323
  return result, np.array(img)
324
  def train(i):
325
  opt.zero_grad()
@@ -344,13 +345,14 @@ def inference(text, seed, step_size, max_iterations, width, height, init_image,
344
  pbar.update()
345
  except KeyboardInterrupt:
346
  pass
347
- return image
 
 
348
  def load_image( infilename ) :
349
  img = Image.open( infilename )
350
  img.load()
351
  data = np.asarray( img, dtype="int32" )
352
  return data
353
-
354
  title = "VQGAN + CLIP"
355
  description = "Gradio demo for VQGAN + CLIP. To use it, simply add your text, or click one of the examples to load them. Read more at the links below. Please click submit only once. Results will show up in under a minute."
356
  article = "<p style='text-align: center'>Originally made by Katherine Crowson (https://github.com/crowsonkb, https://twitter.com/RiversHaveWings). The original BigGAN+CLIP method was by https://twitter.com/advadnoun. Added some explanations and modifications by Eleiber#8347, pooling trick by Crimeacs#8222 (https://twitter.com/EarthML1) and the GUI was made with the help of Abulafia#3734. | <a href='https://colab.research.google.com/drive/1ZAus_gn2RhTZWzOWUpPERNC0Q8OhZRTZ'>Colab</a> | <a href='https://github.com/CompVis/taming-transformers'>Taming Transformers Github Repo</a> | <a href='https://github.com/openai/CLIP'>CLIP Github Repo</a> | Special thanks to BoneAmputee (https://twitter.com/BoneAmputee) for suggestions and advice</p>"
@@ -365,7 +367,7 @@ gr.Interface(
365
  gr.inputs.Image(type="file", label="Initial Image"),
366
  gr.inputs.Slider(minimum=0.0, maximum=15.0, default=0.0, label='Initial Weight', step=1.0),
367
  ],
368
- gr.outputs.Image(type="numpy", label="Output"),
369
  title=title,
370
  description=description,
371
  article=article,
 
31
  nvidia_smi.nvmlInit()
32
  handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
33
  # card id 0 hardcoded here, there is also a call to get all available card ids, so we could iterate
 
34
  torch.hub.download_url_to_file('https://images.pexels.com/photos/158028/bellingrath-gardens-alabama-landscape-scenic-158028.jpeg', 'garden.jpeg')
35
  torch.hub.download_url_to_file('https://images.pexels.com/photos/68767/divers-underwater-ocean-swim-68767.jpeg', 'coralreef.jpeg')
36
  torch.hub.download_url_to_file('https://images.pexels.com/photos/803975/pexels-photo-803975.jpeg', 'cabin.jpeg')
 
37
  def sinc(x):
38
  return torch.where(x != 0, torch.sin(math.pi * x) / (math.pi * x), x.new_ones([]))
39
  def lanczos(x, a):
 
198
  print('Using device:', device)
199
  model = load_vqgan_model(args.vqgan_config, args.vqgan_checkpoint).to(device)
200
  perceptor = clip.load(args.clip_model, jit=False)[0].eval().requires_grad_(False).to(device)
201
+ all_frames = []
202
  def inference(text, seed, step_size, max_iterations, width, height, init_image, init_weight):
203
  size=[width, height]
204
  texts = text
 
318
  result.append(prompt(iii))
319
  img = np.array(out.mul(255).clamp(0, 255)[0].cpu().detach().numpy().astype(np.uint8))[:,:,:]
320
  img = np.transpose(img, (1, 2, 0))
321
+ # imageio.imwrite('./steps/' + str(i) + '.png', np.array(img))
322
+ img = Image.fromarray(img).convert('RGB')
323
+ all_frames.append(img)
324
  return result, np.array(img)
325
  def train(i):
326
  opt.zero_grad()
 
345
  pbar.update()
346
  except KeyboardInterrupt:
347
  pass
348
+ all_frames[0].save('out.gif',
349
+ save_all=True, append_images=all_frames[1:], optimize=False, duration=40, loop=0)
350
+ return image, 'out.gif'
351
  def load_image( infilename ) :
352
  img = Image.open( infilename )
353
  img.load()
354
  data = np.asarray( img, dtype="int32" )
355
  return data
 
356
  title = "VQGAN + CLIP"
357
  description = "Gradio demo for VQGAN + CLIP. To use it, simply add your text, or click one of the examples to load them. Read more at the links below. Please click submit only once. Results will show up in under a minute."
358
  article = "<p style='text-align: center'>Originally made by Katherine Crowson (https://github.com/crowsonkb, https://twitter.com/RiversHaveWings). The original BigGAN+CLIP method was by https://twitter.com/advadnoun. Added some explanations and modifications by Eleiber#8347, pooling trick by Crimeacs#8222 (https://twitter.com/EarthML1) and the GUI was made with the help of Abulafia#3734. | <a href='https://colab.research.google.com/drive/1ZAus_gn2RhTZWzOWUpPERNC0Q8OhZRTZ'>Colab</a> | <a href='https://github.com/CompVis/taming-transformers'>Taming Transformers Github Repo</a> | <a href='https://github.com/openai/CLIP'>CLIP Github Repo</a> | Special thanks to BoneAmputee (https://twitter.com/BoneAmputee) for suggestions and advice</p>"
 
367
  gr.inputs.Image(type="file", label="Initial Image"),
368
  gr.inputs.Slider(minimum=0.0, maximum=15.0, default=0.0, label='Initial Weight', step=1.0),
369
  ],
370
+ [gr.outputs.Image(type="numpy", label="Output Image"),gr.outputs.Image(type="file", label="Output GIF")],
371
  title=title,
372
  description=description,
373
  article=article,