import io import gradio as gr import requests import base64 from PIL import Image # define the function that will be called when the user inputs text def get_images(text): headers = {'Content-Type': 'application/json'} params = { 'return-images': 'true', 'number-results': '4', } response = requests.post( "https://wjdr33c1id.execute-api.eu-west-1.amazonaws.com/dev/prediction", params=params, headers=headers, json={"data": text} ) # get the list of image data from the response image_data = response.json()["image"] # decode the base64-encoded image data and convert it to PIL images images = [ Image.open(io.BytesIO(base64.b64decode(data))) for data in image_data ] # first comes on the top images.reverse() # return the list of images return images # create the gradio app, passing the function as the input and output app = gr.Interface(get_images, gr.components.Textbox(label="Description"), # gr.components.Image(label="Images", type="pil") gr.Gallery(label="Images") ) # start the app app.launch()