import requests import shutil from PIL import Image from io import BytesIO import numpy as np import matplotlib.pyplot as plt import pandas as pd import random import gradio as gr design='india' def lexica(design,n): request=requests.get(f'https://lexica.art/api/v1/search?q={design}') request.json() data = request.json() data_items = list(data.items()) random.shuffle(data_items) data = dict(data_items) image_urls = [] image_prompts = [] image_gallery=[] for key, value in data.items(): for i in range(n): image_url = value[i]['src'] if isinstance(image_url, list): image_url = image_url[0] image_urls.append(image_url) image_prompts.append(value[i]['prompt']) image_gallery.append(value[i]['gallery']) images = [] # Loop through the image URLs for url in image_urls: # Download the image from the URL response = requests.get(url) # Load the image data into PIL format image = Image.open(BytesIO(response.content)) # Add the image to the list images.append(image) # df = pd.DataFrame(image_prompts, columns=["Lexica Prompt"], index=range(1, len(image_prompts)+1)) # df.index.name = "Sr. No." df = pd.DataFrame({ 'image_gallery': image_gallery,'image_prompts': image_prompts}) def make_clickable(val): return '{}'.format(val, val) # df.style.format({'image_gallery': make_clickable}) df.style.format({'image_prompts': make_clickable}).set_properties(subset=['image_prompts'], width=30) for image in images: array = np.array(image) return images , df design='india' # lexica(design) inputs =[ gr.Textbox(label = 'Enter prompt to search Lexica.art'), gr.Slider(label='Number of images ', minimum = 4, maximum = 20, step = 1, value = 4)] outputs= [gr.Gallery(lable='Output gallery').style(grid=4,height=100,container=True), gr.Dataframe(label='links and prompts for corresponding images')] # Create and launch the interface interface = gr.Interface(lexica, inputs=inputs, outputs=outputs, examples =[ ['trending digital art', 5], ['beautiful home', 5], ['interior design of living room', 5]] , title = "" +' šŸ” šŸ–ŒļøšŸŽØ Lexica Art - A Search Engine for Generative Art Prompts and Works '+ "", description="šŸ”šŸ–Œļø šŸŽØ lexica huggingface space , Find inspiration and discover new generative artworks with Lexica Art, a search engine built by by @[Sharif shameem](https://twitter.com/sharifshameem) . Explore a vast collection of prompts and corresponding artworks, and let your imagination take over as you create your own masterpieces. \n\n Visit @[baith_al_suroor](https://huggingface.co/spaces/Xhaheen/Baith-al-suroor) to redesign your home interiors for FREE \n\nšŸ’”šŸ–Œļø spaces built with ā¤ļø @[Xhaheen](https://www.linkedin.com/in/sallu-mandya)") interface.launch(debug=True)