File size: 1,425 Bytes
940863d
01cde28
 
940863d
01cde28
940863d
01cde28
 
940863d
01cde28
 
940863d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()