Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
|
| 5 |
+
dataset = load_dataset("agentsea/wave-ui-25k", split="train")
|
| 6 |
+
|
| 7 |
+
current_index = 0
|
| 8 |
+
|
| 9 |
+
def draw_bounding_box(image, bbox):
|
| 10 |
+
draw = ImageDraw.Draw(image)
|
| 11 |
+
draw.rectangle(bbox, outline="red", width=2)
|
| 12 |
+
return image
|
| 13 |
+
|
| 14 |
+
def show_image(index):
|
| 15 |
+
global current_index
|
| 16 |
+
current_index = index
|
| 17 |
+
data = dataset[current_index]
|
| 18 |
+
image = data['image']
|
| 19 |
+
bbox = data['bbox']
|
| 20 |
+
|
| 21 |
+
image_with_bbox = draw_bounding_box(image, bbox)
|
| 22 |
+
|
| 23 |
+
image_info = f"""
|
| 24 |
+
\n**Image {current_index + 1} of {len(dataset)}**
|
| 25 |
+
\n**Source**: {data['source']}
|
| 26 |
+
\n**Platform**: {data['platform']}
|
| 27 |
+
\n**Name**: {data['name']}
|
| 28 |
+
\n**Description**: {data['description']}
|
| 29 |
+
\n**Type**: {data['type']}
|
| 30 |
+
\n**OCR**: {data['OCR']}
|
| 31 |
+
\n**Language**: {data['language']}
|
| 32 |
+
\n**Purpose**: {data['purpose']}
|
| 33 |
+
\n**Expectation**: {data['expectation']}
|
| 34 |
+
"""
|
| 35 |
+
return image_with_bbox, image_info
|
| 36 |
+
|
| 37 |
+
def next_image():
|
| 38 |
+
global current_index
|
| 39 |
+
current_index = (current_index + 1) % len(dataset)
|
| 40 |
+
return show_image(current_index)
|
| 41 |
+
|
| 42 |
+
def previous_image():
|
| 43 |
+
global current_index
|
| 44 |
+
current_index = (current_index - 1) % len(dataset)
|
| 45 |
+
return show_image(current_index)
|
| 46 |
+
|
| 47 |
+
shortcut_js = """
|
| 48 |
+
<script>
|
| 49 |
+
function shortcuts(e) {
|
| 50 |
+
var event = document.all ? window.event : e;
|
| 51 |
+
switch (e.target.tagName.toLowerCase()) {
|
| 52 |
+
case "input":
|
| 53 |
+
case "textarea":
|
| 54 |
+
case "select":
|
| 55 |
+
case "button":
|
| 56 |
+
break;
|
| 57 |
+
default:
|
| 58 |
+
if (e.key === "ArrowRight") {
|
| 59 |
+
document.getElementById("next_btn").click();
|
| 60 |
+
} else if (e.key === "ArrowLeft") {
|
| 61 |
+
document.getElementById("prev_btn").click();
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
document.addEventListener('keydown', shortcuts, false);
|
| 66 |
+
</script>
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(head=shortcut_js) as app:
|
| 70 |
+
gr.Markdown("# Wave UI 25k Dataset Explorer")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
image_output = gr.Image(type="pil")
|
| 74 |
+
label_output = gr.Markdown()
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
prev_button = gr.Button("Previous", elem_id="prev_btn")
|
| 78 |
+
next_button = gr.Button("Next", elem_id="next_btn")
|
| 79 |
+
|
| 80 |
+
prev_button.click(previous_image, outputs=[image_output, label_output])
|
| 81 |
+
next_button.click(next_image, outputs=[image_output, label_output])
|
| 82 |
+
|
| 83 |
+
app.load(show_image, inputs=[gr.Number(value=0, visible=False)], outputs=[image_output, label_output])
|
| 84 |
+
|
| 85 |
+
app.launch()
|