lhoestq HF staff commited on
Commit
8a9db0e
β€’
1 Parent(s): aa12bbc

add dataset selection

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from huggingface_hub import HfApi
4
+
5
+ session = requests.Session()
6
+
7
+ css = """
8
+ @media (prefers-color-scheme: dark) {
9
+ .transparent-dropdown, .transparent-dropdown .container .wrap {
10
+ background: var(--bg-dark);
11
+ }
12
+ }
13
+ @media (prefers-color-scheme: light) {
14
+ .transparent-dropdown, .transparent-dropdown .container .wrap {
15
+ background: var(--bg);
16
+ }
17
+ }
18
+ """
19
+
20
+ with gr.Blocks(css=css) as demo:
21
+ with gr.Row():
22
+ with gr.Column(scale=4):
23
+ with gr.Group():
24
+ dataset_dropdown = gr.Dropdown(label="Dataset", allow_custom_value=True, interactive=True)
25
+ with gr.Row():
26
+ subset_dropdown = gr.Dropdown(info="Subset", show_label=False, visible=False, elem_classes="transparent-dropdown")
27
+ split_dropdown = gr.Dropdown(info="Split", show_label=False, visible=False, elem_classes="transparent-dropdown")
28
+ gr.LoginButton()
29
+ loading_codes_json = gr.JSON(visible=False)
30
+ dataset_subset_split_textbox = gr.Textbox(visible=False)
31
+ dataframe = gr.DataFrame()
32
+
33
+ @demo.load(outputs=dataset_dropdown)
34
+ def fetch_datasets(request: gr.Request, oauth_token: gr.OAuthToken | None):
35
+ api = HfApi(token=oauth_token.token if oauth_token else None)
36
+ datasets = list(api.list_datasets(limit=3, sort="trendingScore", direction=-1, filter=["format:parquet"]))
37
+ if oauth_token and (user := api.whoami().get("user")):
38
+ datasets += list(api.list_datasets(limit=3, sort="trendingScore", direction=-1, filter=["format:parquet"], author=user))
39
+ dataset = request.query_params.get("dataset") or datasets[0].id
40
+ return {dataset_dropdown: gr.Dropdown(choices=[dataset.id for dataset in datasets], value=dataset)}
41
+
42
+ @dataset_dropdown.change(inputs=dataset_dropdown, outputs=loading_codes_json)
43
+ def fetch_read_parquet_loading(dataset: str):
44
+ if "/" not in dataset.strip().strip("/"):
45
+ return []
46
+ resp = session.get(f"https://datasets-server.huggingface.co/compatible-libraries?dataset={dataset}", timeout=3).json()
47
+ return ([lib["loading_codes"] for lib in resp.get("libraries", []) if lib["function"] == "dd.read_parquet"] or [[]])[0] or []
48
+
49
+ @loading_codes_json.change(inputs=loading_codes_json, outputs=[subset_dropdown, split_dropdown])
50
+ def show_subset_dropdown(loading_codes: list[dict]):
51
+ subsets = [loading_code["config_name"] for loading_code in loading_codes]
52
+ subset = (subsets or [""])[0]
53
+ splits = ([list(loading_code["arguments"]["splits"]) for loading_code in loading_codes if loading_code["config_name"] == subset] or [[]])[0]
54
+ split = (splits or [""])[0]
55
+ return gr.Dropdown(subsets, value=subset, visible=len(subsets) > 1), gr.Dropdown(splits, value=split, visible=len(splits) > 1)
56
+
57
+ @subset_dropdown.change(inputs=[loading_codes_json, subset_dropdown], outputs=split_dropdown)
58
+ def show_split_dropdown(loading_codes: list[dict], subset: str):
59
+ splits = ([list(loading_code["arguments"]["splits"]) for loading_code in loading_codes if loading_code["config_name"] == subset] or [[]])[0]
60
+ split = (splits or [""])[0]
61
+ return gr.Dropdown(splits, value=split, visible=len(splits) > 1)
62
+
63
+
64
+
65
+ if __name__ == "__main__":
66
+ demo.launch()