File size: 5,553 Bytes
8748f4d | 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import gradio as gr
import pandas as pd
import requests
API_BASE = "https://datasets-server.huggingface.co"
def fetch(endpoint, params):
try:
response = requests.get(f"{API_BASE}/{endpoint}", params=params, timeout=30)
response.raise_for_status()
return response.json()
except Exception as exc:
return {"error": str(exc)}
def get_splits(dataset):
dataset = (dataset or "").strip()
if not dataset:
return (
pd.DataFrame(),
gr.update(choices=[], value=None),
"β οΈ Please enter a dataset name.",
)
data = fetch("splits", {"dataset": dataset})
if "error" in data:
return (
pd.DataFrame(),
gr.update(choices=[], value=None),
f"β {data['error']}",
)
splits = data.get("splits", [])
if not splits:
return (
pd.DataFrame(),
gr.update(choices=[], value=None),
"No splits found.",
)
df = pd.DataFrame(splits)
columns = [c for c in ["dataset", "config", "split"] if c in df.columns]
if columns:
df = df[columns]
choices = [f"{s.get('config', '')} :: {s.get('split', '')}" for s in splits]
return (
df,
gr.update(choices=choices, value=choices[0] if choices else None),
f"β
Found {len(splits)} splits.",
)
def get_rows(dataset, config_split, num_rows):
dataset = (dataset or "").strip()
if not dataset:
return pd.DataFrame(), pd.DataFrame(), "β οΈ Please enter a dataset name."
config = None
split = None
if config_split:
if "::" in config_split:
config, split = [x.strip() for x in config_split.split("::", 1)]
else:
split = config_split.strip()
# Auto-fill missing config/split info using the splits endpoint
if not config or not split:
data = fetch("splits", {"dataset": dataset})
if "error" in data:
return pd.DataFrame(), pd.DataFrame(), f"β {data['error']}"
splits = data.get("splits", [])
if not splits:
return pd.DataFrame(), pd.DataFrame(), "No splits found."
if not split:
if config:
matching = [s for s in splits if s.get("config") == config]
if not matching:
return pd.DataFrame(), pd.DataFrame(), f"β Config '{config}' not found."
split = matching[0].get("split", "")
else:
config = splits[0].get("config", "")
split = splits[0].get("split", "")
else:
for s in splits:
if s.get("split") == split and (not config or s.get("config") == config):
config = s.get("config", "")
break
else:
return pd.DataFrame(), pd.DataFrame(), f"β Split '{split}' not found."
data = fetch(
"rows",
{
"dataset": dataset,
"config": config,
"split": split,
"offset": 0,
"length": int(num_rows),
},
)
if "error" in data:
return pd.DataFrame(), pd.DataFrame(), f"β {data['error']}"
# Features / schema
features = data.get("features", [])
features_df = pd.DataFrame(features)
if not features_df.empty and "type" in features_df.columns:
features_df["type"] = features_df["type"].astype(str)
# Rows
rows = data.get("rows", [])
if not rows:
return features_df, pd.DataFrame(), "No rows returned."
records = [r.get("row", {}) for r in rows]
rows_df = pd.DataFrame(records)
if rows and "row_idx" in rows[0]:
rows_df.index = [r.get("row_idx", i) for i, r in enumerate(rows)]
rows_df.index.name = "row_idx"
return (
features_df,
rows_df,
f"β
Loaded {len(rows)} rows from `{config}` / `{split}`.",
)
with gr.Blocks(title="HF Dataset Viewer") as demo:
gr.Markdown(
"""
# π§ Hugging Face Dataset Viewer
Browse any public Hugging Face dataset through the
[datasets-server API](https://datasets-server.huggingface.co).
No dataset download needed.
"""
)
with gr.Row():
dataset_text = gr.Textbox(
label="Dataset name",
value="imdb",
placeholder="e.g. emotions, glue, squad",
)
splits_button = gr.Button("Get Splits")
splits_table = gr.Dataframe(label="Splits", interactive=False)
splits_status = gr.Markdown()
with gr.Row():
config_split_dropdown = gr.Dropdown(
label="Config / Split",
choices=[],
interactive=True,
)
num_rows_slider = gr.Slider(
minimum=1,
maximum=100,
value=10,
step=1,
label="Number of rows",
)
rows_button = gr.Button("Load Rows")
with gr.Row():
features_table = gr.Dataframe(label="Features", interactive=False)
rows_table = gr.Dataframe(label="Rows", interactive=False)
rows_status = gr.Markdown()
splits_button.click(
get_splits,
inputs=dataset_text,
outputs=[splits_table, config_split_dropdown, splits_status],
)
rows_button.click(
get_rows,
inputs=[dataset_text, config_split_dropdown, num_rows_slider],
outputs=[features_table, rows_table, rows_status],
)
if __name__ == "__main__":
demo.launch() |