Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
URL = "https://source.unsplash.com/random/500x500/?nature,fruit"
|
| 7 |
+
|
| 8 |
+
js_pos_select = """
|
| 9 |
+
async (images_selected_state) => {
|
| 10 |
+
const gallery = document.querySelector("#gallery_id")
|
| 11 |
+
const buttons_thumbnails = gallery.querySelectorAll(".thumbnails > button");
|
| 12 |
+
const buttons_large = gallery.querySelectorAll(".grid-container > button");
|
| 13 |
+
buttons_thumbnails.forEach((btn, idx) => {
|
| 14 |
+
if(images_selected_state.includes(idx)){
|
| 15 |
+
btn.classList.add('selected-custom');
|
| 16 |
+
}else{
|
| 17 |
+
btn.classList.remove('selected-custom');
|
| 18 |
+
}
|
| 19 |
+
})
|
| 20 |
+
buttons_large.forEach((btn, idx) => {
|
| 21 |
+
if(images_selected_state.includes(idx)){
|
| 22 |
+
btn.classList.add('selected-custom');
|
| 23 |
+
}else{
|
| 24 |
+
btn.classList.remove('selected-custom');
|
| 25 |
+
}
|
| 26 |
+
})
|
| 27 |
+
return images_selected_state
|
| 28 |
+
}
|
| 29 |
+
"""
|
| 30 |
+
css = """
|
| 31 |
+
.selected-custom {
|
| 32 |
+
--ring-color: red !important;
|
| 33 |
+
transform: scale(0.9) !important;
|
| 34 |
+
border-color: red !important;
|
| 35 |
+
}
|
| 36 |
+
"""
|
| 37 |
+
def get_random_images(n=8):
|
| 38 |
+
images = []
|
| 39 |
+
for i in range(n):
|
| 40 |
+
images.append(Image.open(requests.get(URL + f"?{time.time()}", stream=True).raw))
|
| 41 |
+
return images
|
| 42 |
+
|
| 43 |
+
def get_select_index(imgs_set, evt: gr.SelectData):
|
| 44 |
+
print(evt.index)
|
| 45 |
+
if(evt.index in imgs_set):
|
| 46 |
+
imgs_set.pop(imgs_set.index(evt.index))
|
| 47 |
+
else:
|
| 48 |
+
imgs_set.append(evt.index)
|
| 49 |
+
print(imgs_set)
|
| 50 |
+
return imgs_set
|
| 51 |
+
|
| 52 |
+
with gr.Blocks(css=css) as demo:
|
| 53 |
+
images_selected_state = gr.JSON([], visible=False)
|
| 54 |
+
|
| 55 |
+
with gr.Column(variant="panel"):
|
| 56 |
+
with gr.Row(variant="compact"):
|
| 57 |
+
btn = gr.Button("Generate image").style(full_width=False)
|
| 58 |
+
|
| 59 |
+
gallery_photos = gr.Gallery(
|
| 60 |
+
label="Generated images", show_label=False, elem_id="gallery_id"
|
| 61 |
+
).style(columns=[4], rows=[2], object_fit="contain", height="auto")
|
| 62 |
+
|
| 63 |
+
gallery_photos.select(
|
| 64 |
+
get_select_index, images_selected_state, images_selected_state).then(
|
| 65 |
+
None, images_selected_state, None, _js=js_pos_select)
|
| 66 |
+
demo.load(get_random_images, None, gallery_photos)
|
| 67 |
+
|
| 68 |
+
demo.launch(debug=True)
|