Spaces:
Sleeping
Sleeping
Create app.py (#1)
Browse files- Create app.py (50e0f31ac38ab1b8b72979d316bfe47a870dfdf8)
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def load_images(files):
|
| 4 |
+
if not files:
|
| 5 |
+
return [], gr.update(visible=False), 0, ""
|
| 6 |
+
|
| 7 |
+
# Sort alphabetically by file name (handles imageA, imageB, image1, image2)
|
| 8 |
+
sorted_files = sorted(files, key=lambda x: x.name)
|
| 9 |
+
|
| 10 |
+
# Return the sorted list, make the carousel visible,
|
| 11 |
+
# set index to 0, and update the display
|
| 12 |
+
return sorted_files, gr.update(visible=True), 0, sorted_files[0].name
|
| 13 |
+
|
| 14 |
+
def navigate(direction, current_index, file_list):
|
| 15 |
+
if not file_list:
|
| 16 |
+
return 0, None, ""
|
| 17 |
+
|
| 18 |
+
# Calculate new index with wrap-around logic
|
| 19 |
+
new_index = (current_index + direction) % len(file_list)
|
| 20 |
+
active_file = file_list[new_index]
|
| 21 |
+
|
| 22 |
+
return new_index, active_file.name, active_file.name
|
| 23 |
+
|
| 24 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 25 |
+
gr.Markdown("# 🖼️ Sequential Image Carousel")
|
| 26 |
+
|
| 27 |
+
# State to keep track of the uploaded files and current position
|
| 28 |
+
file_state = gr.State([])
|
| 29 |
+
index_state = gr.State(0)
|
| 30 |
+
|
| 31 |
+
# 1. Upload Section
|
| 32 |
+
with gr.Row():
|
| 33 |
+
upload_btn = gr.File(
|
| 34 |
+
label="Upload Images",
|
| 35 |
+
file_count="multiple",
|
| 36 |
+
file_types=["image"]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# 2. Carousel Section (Hidden until images are uploaded)
|
| 40 |
+
with gr.Column(visible=False) as carousel_container:
|
| 41 |
+
with gr.Row():
|
| 42 |
+
prev_btn = gr.Button("⬅️ Previous", scale=1)
|
| 43 |
+
# Display area for the single image
|
| 44 |
+
image_display = gr.Image(label="Viewing Image", interactive=False, scale=4)
|
| 45 |
+
next_btn = gr.Button("Next ➡️", scale=1)
|
| 46 |
+
|
| 47 |
+
image_name_label = gr.Markdown(elem_id="img-label")
|
| 48 |
+
|
| 49 |
+
# --- Event Listeners ---
|
| 50 |
+
|
| 51 |
+
# When files are uploaded: Sort them and show the first one
|
| 52 |
+
upload_btn.upload(
|
| 53 |
+
fn=load_images,
|
| 54 |
+
inputs=upload_btn,
|
| 55 |
+
outputs=[file_state, carousel_container, index_state, image_display]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Navigation Buttons
|
| 59 |
+
prev_btn.click(
|
| 60 |
+
fn=lambda idx, files: navigate(-1, idx, files),
|
| 61 |
+
inputs=[index_state, file_state],
|
| 62 |
+
outputs=[index_state, image_display, image_name_label]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
next_btn.click(
|
| 66 |
+
fn=lambda idx, files: navigate(1, idx, files),
|
| 67 |
+
inputs=[index_state, file_state],
|
| 68 |
+
outputs=[index_state, image_display, image_name_label]
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
demo.launch()
|