Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(wrap=True)
|
| 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(debug=True)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# original: https://huggingface.co/spaces/freddyaboulton/dataset-viewer -- Freddy thanks! Your examples are the best.
|
| 69 |
+
# playlist on Gradio and Mermaid: https://www.youtube.com/watch?v=o7kCD4aWMR4&list=PLHgX2IExbFosW7hWNryq8hs2bt2aj91R-
|
| 70 |
+
# Link to Mermaid model and code: [](https://mermaid.live/edit#pako:eNp1U8mO2zAM_RXCZ-eQpZccCmSZTIpOMQESIAdnDrRMx0JkydXSNDOYfy_lpUgD1AfBfnx8fCTlj0SYgpJ5UipzFRVaD4flSQM_YjwafcVJ9-FCfrbYVGA0ZQeLUkt9futiOM72pEh4QFijR9iTf2tzsx3Z0ti6hxslvb_Lm0TSNPvBDhQsg1TFXXAag7NBef_9hdDqFA6knbEbdgvGwu7mjRXVkDOLOV-yNXmytdQEsoROvTfi4EhK9XTSxUNz_mo4uVHm1lPyce-uR1k_n2RHymHRNPAvNXaTT7NVZYwjeDECVbS4UiYUAyc2lc-yFoPXxkujHaAl2G54PCjIpfBssZAGtsZ5KlLYkjWXkMLiuOfjPVhiymr3_x4qS7wicneTFuMW6Gdxlb6Cb7oJvt1LbEpMso08sza8MnqskA9jL27Ij72Jafb0G-tGkQNTdgKOy_XcFP5GDxFbWsJLV3FQid2LWfZsfpHVqAXBCBYa1e2dAHUBu5Ar6dgby0ghPWxQWk2Oh_L0M0h_S2Ep0YHUrXFHXD_msefo5XEkfFWBK8atdkA7mgfoalpATJI0qfnWoCz4b_iI0VPiK6rplMz5taASg_Kn5KQ_mYrBm_1Ni2TubaA0CU2BntYSeQl1Mi9ROfr8A8FBGds)
|