jasvir-singh1021's picture
Create app.py
409e2ec verified
# app.py
import gradio as gr
from datasets import load_dataset
# Load an image dataset (e.g., cifar10)
dataset = load_dataset("cifar10", split="test", trust_remote_code=True)
def get_image(index):
index = int(index)
# Get the image and its label
sample = dataset[index]
image = sample["img"] # Note: Some datasets use "image", "img", or "pixel_values"
label = sample["label"]
# CIFAR10 has a list of label names
label_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
return image, label_names[label]
with gr.Blocks() as demo:
gr.Markdown("# 🖼️ Image Dataset Viewer")
index_slider = gr.Slider(0, len(dataset)-1, value=0, step=1, label="Sample Index")
image_output = gr.Image(label="Image", type="pil") # Use 'pil' for PIL Images from datasets
label_output = gr.Label(label="Class")
index_slider.change(fn=get_image,
inputs=index_slider,
outputs=[image_output, label_output])
demo.launch()