freddyaboulton HF staff commited on
Commit
f996d26
1 Parent(s): 0df5d92
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Dict
2
+ import httpx
3
+ import gradio as gr
4
+ import pandas as pd
5
+
6
+
7
+ async def get_splits(dataset_name: str) -> Dict[str, List[Dict]]:
8
+ URL = f"https://datasets-server.huggingface.co/splits?dataset={dataset_name}"
9
+ async with httpx.AsyncClient() as session:
10
+ response = await session.get(URL)
11
+ return response.json()
12
+
13
+
14
+ async def get_valid_datasets() -> Dict[str, List[str]]:
15
+ URL = f"https://datasets-server.huggingface.co/valid"
16
+ async with httpx.AsyncClient() as session:
17
+ response = await session.get(URL)
18
+ datasets = response.json()["valid"]
19
+ return gr.Dropdown.update(choices=datasets, value="glue")
20
+
21
+
22
+ async def get_first_rows(dataset: str, config: str, split: str) -> Dict[str, Dict[str, List[Dict]]]:
23
+ URL = f"https://datasets-server.huggingface.co/first-rows?dataset={dataset}&config={config}&split={split}"
24
+ async with httpx.AsyncClient() as session:
25
+ response = await session.get(URL)
26
+ return response.json()
27
+
28
+ def get_df_from_rows(api_output):
29
+ return pd.DataFrame([row["row"] for row in api_output["rows"]])
30
+
31
+
32
+ async def update_configs(dataset_name: str):
33
+ splits = await get_splits(dataset_name)
34
+ all_configs = sorted(set([s["config"] for s in splits["splits"]]))
35
+ return (gr.Dropdown.update(choices=all_configs, value=all_configs[0]),
36
+ splits)
37
+
38
+
39
+ async def update_splits(config_name: str, state: gr.State):
40
+ splits_for_config = sorted(set([s["split"] for s in state["splits"] if s["config"] == config_name]))
41
+ dataset_name = state["splits"][0]["dataset"]
42
+ dataset = await update_dataset(splits_for_config[0], config_name, dataset_name)
43
+ return (gr.Dropdown.update(choices=splits_for_config, value=splits_for_config[0]), dataset)
44
+
45
+
46
+ async def update_dataset(split_name: str, config_name: str, dataset_name: str):
47
+ rows = await get_first_rows(dataset_name, config_name, split_name)
48
+ df = get_df_from_rows(rows)
49
+ return df
50
+
51
+
52
+ with gr.Blocks() as demo:
53
+ splits_data = gr.State()
54
+ with gr.Row():
55
+ dataset_name = gr.Dropdown(label="Dataset")
56
+ config = gr.Dropdown(label="Subset")
57
+ split = gr.Dropdown(label="Split")
58
+ with gr.Row():
59
+ dataset = gr.DataFrame()
60
+ demo.load(get_valid_datasets, inputs=None, outputs=[dataset_name])
61
+ dataset_name.change(update_configs, inputs=[dataset_name], outputs=[config, splits_data])
62
+ config.change(update_splits, inputs=[config, splits_data], outputs=[split, dataset])
63
+ split.change(update_dataset, inputs=[split, config, dataset_name], outputs=[dataset])
64
+
65
+ demo.launch()
66
+
67
+
68
+