Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system('pip install git+https://github.com/huggingface/transformers --upgrade')
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import ImageGPTFeatureExtractor, ImageGPTForCausalLM
|
| 6 |
+
import torch
|
| 7 |
+
import requests
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import os
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
|
| 12 |
+
feature_extractor = ImageGPTFeatureExtractor.from_pretrained("openai/imagegpt-small")
|
| 13 |
+
model = ImageGPTForCausalLM.from_pretrained("openai/imagegpt-small")
|
| 14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
model.to(device)
|
| 16 |
+
|
| 17 |
+
# load image examples
|
| 18 |
+
urls = ['https://assetsnffrgf-a.akamaihd.net/assets/m/502013285/univ/art/502013285_univ_sqr_xl.jpg']
|
| 19 |
+
for idx, url in enumerate(urls):
|
| 20 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 21 |
+
image.save(f"image_{idx}.png")
|
| 22 |
+
|
| 23 |
+
def process_image(image):
|
| 24 |
+
# prepare 8 images, shape (8, 1024)
|
| 25 |
+
encoding = feature_extractor([image for _ in range(8)], return_tensors="pt")
|
| 26 |
+
|
| 27 |
+
# create primers
|
| 28 |
+
samples = encoding.pixel_values.numpy()
|
| 29 |
+
n_px_crop = 16
|
| 30 |
+
primers = samples.reshape(-1,n_px*n_px)[:,:n_px_crop*n_px] # crop top n_px_crop rows. These will be the conditioning tokens
|
| 31 |
+
|
| 32 |
+
# generate (no beam search)
|
| 33 |
+
context = np.concatenate((np.full((batch_size, 1), model.config.vocab_size - 1), primers), axis=1)
|
| 34 |
+
context = torch.tensor(context).to(device)
|
| 35 |
+
output = model.generate(input_ids=context, max_length=n_px*n_px + 1, temperature=1.0, do_sample=True, top_k=40)
|
| 36 |
+
|
| 37 |
+
# decode back to images
|
| 38 |
+
samples = output[:,1:].cpu().detach().numpy()
|
| 39 |
+
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
|
| 40 |
+
|
| 41 |
+
# save as list of files
|
| 42 |
+
completions = []
|
| 43 |
+
output_dir = '.'
|
| 44 |
+
for i in range(len(samples_img)):
|
| 45 |
+
fname = os.path.join(output_dir, "completion" + str(i) + ".png")
|
| 46 |
+
plt.imsave(fname=fname, arr=samples_img[i], format='png')
|
| 47 |
+
completions.append(fname)
|
| 48 |
+
|
| 49 |
+
return completions
|
| 50 |
+
|
| 51 |
+
title = "Interactive demo: ImageGPT"
|
| 52 |
+
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."
|
| 53 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.10282'>ImageGPT: Generative Pretraining from Pixels</a> | <a href='https://openai.com/blog/image-gpt/'>Official blog</a></p>"
|
| 54 |
+
examples =[["image_0.png"]]
|
| 55 |
+
|
| 56 |
+
iface = gr.Interface(fn=process_image,
|
| 57 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 58 |
+
outputs=[gr.outputs.Image(type='file', label=f'completion_{i}') for i in range(len(samples_img))],
|
| 59 |
+
title=title,
|
| 60 |
+
description=description,
|
| 61 |
+
article=article,
|
| 62 |
+
examples=examples)
|
| 63 |
+
iface.launch(debug=True)
|