Spaces:
Running
Running
import gradio as gr | |
from datasets import load_dataset | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
# Load a dataset (you can change this to any HF dataset) | |
dataset = load_dataset("ag_news", split="train[:1000]") | |
# Convert to DataFrame | |
df = pd.DataFrame(dataset) | |
# Label map for better readability | |
label_map = { | |
0: "World", | |
1: "Sports", | |
2: "Business", | |
3: "Sci/Tech" | |
} | |
df["label_name"] = df["label"].map(label_map) | |
def preview_data(n_rows): | |
return df.head(n_rows) | |
def plot_distribution(): | |
counts = df["label_name"].value_counts() | |
fig, ax = plt.subplots() | |
counts.plot(kind="bar", ax=ax, color="skyblue") | |
ax.set_title("Label Distribution") | |
ax.set_ylabel("Count") | |
ax.set_xlabel("Category") | |
return fig | |
with gr.Blocks() as demo: | |
gr.Markdown("# π§ AG News Dataset Explorer") | |
gr.Markdown("Explore the AG News dataset from Hugging Face. Useful for data engineers and NLP practitioners.") | |
with gr.Row(): | |
num_slider = gr.Slider(1, 20, value=5, label="Number of Rows") | |
data_output = gr.Dataframe() | |
show_data_btn = gr.Button("Show Data") | |
show_data_btn.click(preview_data, inputs=[num_slider], outputs=[data_output]) | |
gr.Markdown("## π Class Distribution") | |
dist_btn = gr.Button("Show Distribution Chart") | |
chart_output = gr.Plot() | |
dist_btn.click(plot_distribution, outputs=[chart_output]) | |
# Launch app | |
demo.launch() | |