nielsr HF staff commited on
Commit
40b1ad3
1 Parent(s): 6358443

Stack images horizontally

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -37,19 +37,17 @@ def process_image(image):
37
  context = torch.tensor(context).to(device)
38
  output = model.generate(input_ids=context, max_length=n_px*n_px + 1, temperature=1.0, do_sample=True, top_k=40)
39
 
40
- # decode back to images
41
  samples = output[:,1:].cpu().detach().numpy()
42
- samples_img = [np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [n_px, n_px, 3]).astype(np.uint8) for s in samples] # convert color cluster tokens back to pixels
43
 
44
- # save as list of files
45
- completions = []
46
- output_dir = '.'
47
- for i in range(len(samples_img)):
48
- fname = os.path.join(output_dir, "completion" + str(i) + ".png")
49
- plt.imsave(fname=fname, arr=samples_img[i], format='png')
50
- completions.append(fname)
51
 
52
- return completions
53
 
54
  title = "Interactive demo: ImageGPT"
55
  description = "Demo for OpenAI's ImageGPT: Generative Pretraining from Pixels. To use it, simply upload an image or use the example image below and click 'submit'. Results will show up in a few seconds."
@@ -58,7 +56,7 @@ examples =[["image_0.png"]]
58
 
59
  iface = gr.Interface(fn=process_image,
60
  inputs=gr.inputs.Image(type="pil"),
61
- outputs=[gr.outputs.Image(type='file', label=f'completion_{i}') for i in range(8)],
62
  title=title,
63
  description=description,
64
  article=article,
 
37
  context = torch.tensor(context).to(device)
38
  output = model.generate(input_ids=context, max_length=n_px*n_px + 1, temperature=1.0, do_sample=True, top_k=40)
39
 
40
+ # decode back to images (convert color cluster tokens back to pixels)
41
  samples = output[:,1:].cpu().detach().numpy()
42
+ samples_img = [np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [n_px, n_px, 3]).astype(np.uint8) for s in samples]
43
 
44
+ # stack images horizontally
45
+ result = np.hstack(samples_img)
46
+
47
+ # return as PIL Image
48
+ completion = Image.fromarray(result)
 
 
49
 
50
+ return completion
51
 
52
  title = "Interactive demo: ImageGPT"
53
  description = "Demo for OpenAI's ImageGPT: Generative Pretraining from Pixels. To use it, simply upload an image or use the example image below and click 'submit'. Results will show up in a few seconds."
 
56
 
57
  iface = gr.Interface(fn=process_image,
58
  inputs=gr.inputs.Image(type="pil"),
59
+ outputs=gr.outputs.Image(type="pil"),
60
  title=title,
61
  description=description,
62
  article=article,