from cProfile import label import glob import gradio as gr import tensorflow as tf from huggingface_hub import from_pretrained_keras import numpy as np pixel_cnn = from_pretrained_keras("keras-io/pixel-cnn-mnist") def infer(batch): pixels = np.zeros(shape=(batch,) + (pixel_cnn.input_shape)[1:]) batch, rows, cols, channels = pixels.shape # Iterate over the pixels because generation has to be done sequentially pixel by pixel. for row in range(rows): for col in range(cols): for channel in range(channels): # Feed the whole array and retrieving the pixel value probabilities for the next # pixel. probs = pixel_cnn.predict(pixels)[:, row, col, channel] # Use the probabilities to pick pixel values and append the values to the image # frame. pixels[:, row, col, channel] = tf.math.ceil( probs - tf.random.uniform(probs.shape) ) for i, pic in enumerate(pixels): tf.keras.preprocessing.image.save_img( "/tmp/generated_image_{}.png".format(i), deprocess_image(np.squeeze(pic, -1)) ) return glob.glob("/tmp/generated*") def deprocess_image(x): # Stack the single channeled black and white image to RGB values. x = np.stack((x, x, x), 2) # Undo preprocessing x *= 255.0 # Convert to uint8 and clip to the valid range [0, 255] x = np.clip(x, 0, 255).astype("uint8") return x article = """